Sign in

    #mizchi/js_web/http

    Fetch API for HTTP requests and responses.

    #Installation

    Add to your moon.mod:

    import { "mizchi/js_web@0.13.0", }

    ...and to the moon.pkg of the package that uses it:

    import { "mizchi/js_web/http", }

    #Overview

    Provides bindings for the Fetch API: fetch, Request, RequestInit, Response, Headers and FormData.

    #Usage Example

    fetch takes its options as labelled arguments — method_~ is required (method is a reserved word in MoonBit):

    ///|
    pub async fn get_json() -> @core.Any {
    let res = @http.fetch("https://api.example.com/data", method_="GET")
    res.json()
    }

    ///|
    pub async fn post() -> String {
    let res = @http.fetch(
    "https://api.example.com/users",
    method_="POST",
    headers={ "content-type": "application/json" },
    body=@core.identity("{\"name\":\"value\"}"),
    )
    res.text()
    }

    To build a request up front, pass a RequestInit to the Request constructor and hand it to fetch_request. RequestInit is configured at construction — it exposes getters, not setters:

    ///|
    pub async fn post_request() -> String {
    let init = @http.RequestInit(
    http_method="POST",
    body=@core.identity("{\"name\":\"value\"}"),
    )
    let request = @http.Request("https://api.example.com/users", init~)
    (@http.fetch_request(request)).text()
    }

    Serving a response — a plain text body, or any BodyInit (bytes, Blob, ReadableStream, …) via from_body_init:

    ///|
    pub fn respond() -> @http.Response {
    @http.Response(
    body="hello",
    status=200,
    headers=@core.from_entries([
    ("content-type", @core.identity("text/plain")),
    ]),
    )
    }

    Note that Headers and FormData have no constructors of their own — Headers is a read side type reached through RequestInit::get_headers, and a FormData comes from Response::formData(). Outgoing headers are passed either as a Map[String, String] (to fetch) or as a @core.Any object built with @core.from_entries (to Response), as above.

    #Available Types

    • fetch() / fetch_request() - Make HTTP requests
    • Request / RequestInit - HTTP request and its options
    • Response - HTTP response object
    • Headers - HTTP headers, read side
    • FormData - Form data decoding

    #Reference

    FormData

    #external
    pub type FormData

    https://developer.mozilla.org/ja/docs/Web/API/FormData

    FormData::append

    fn FormData::append(self : FormData, name : String, value : String) -> Unit

    FormData::as_any

    FormData::delete

    fn FormData::delete(self : FormData, name : String) -> Unit

    FormData::get

    fn FormData::get(self : FormData, name : String) -> String

    Headers

    #external
    pub type Headers

    https://developer.mozilla.org/ja/docs/Web/API/Headers

    Headers::as_any

    fn Headers::as_any(self : Headers) ->
    Any

    Request

    pub(all) struct Request {
    bodyUsed : Bool
    url : String
    credentials : String
    }

    https://developer.mozilla.org/ja/docs/Web/API/Request

    Request::Request

    fn Request::Request(url : String, init? : RequestInit) -> Request

    Create a Request, optionally with RequestInit options. https://developer.mozilla.org/ja/docs/Web/API/Request/Request

    Request::arraybuffer

    Request::as_any

    fn Request::as_any(self : Request) ->
    Any

    Request::blob

    async fn Request::blob(self : Request) ->
    Blob

    Request::clone

    fn Request::clone(self : Request) -> Request

    Request::json

    async fn Request::json(self : Request) ->
    Any

    Request::method_

    fn Request::method_(self : Request) -> String

    Get the HTTP method (e.g., "GET", "POST") Note: This is a method because "method" is a reserved word in MoonBit

    Request::new

    #deprecated("use `Request(..)` instead")
    fn Request::new(url : String) -> Request

    Deprecated alias for the Request(..) constructor.

    Request::new_with_init

    #deprecated("use `Request(url, init~)` instead")
    fn Request::new_with_init(url : String, init : RequestInit) -> Request

    Deprecated alias for Request(url, init~).

    Request::text

    async fn Request::text(self : Request) -> String

    RequestInit

    pub struct RequestInit {
    // private fields
    }

    RequestInit - options for creating a Request or fetch call https://developer.mozilla.org/en-US/docs/Web/API/RequestInit

    RequestInit::RequestInit

    fn RequestInit::RequestInit(http_method? : String, headers? : Headers, body? :
    Any
    , mode? : String, credentials? : String, cache? : String, redirect? : String, referrer? : String, referrerPolicy? : String, integrity? : String, keepalive? : Bool, signal? :
    AbortSignal
    , priority? : String) -> RequestInit

    RequestInit::as_any

    RequestInit::get_body

    Get the body

    RequestInit::get_cache

    fn RequestInit::get_cache(self : RequestInit) -> String?

    Get the cache mode

    RequestInit::get_credentials

    fn RequestInit::get_credentials(self : RequestInit) -> String?

    Get the credentials

    RequestInit::get_headers

    fn RequestInit::get_headers(self : RequestInit) -> Headers?

    Get the headers

    RequestInit::get_integrity

    fn RequestInit::get_integrity(self : RequestInit) -> String?

    Get the integrity

    RequestInit::get_keepalive

    fn RequestInit::get_keepalive(self : RequestInit) -> Bool?

    Get the keepalive flag

    RequestInit::get_method

    fn RequestInit::get_method(self : RequestInit) -> String?

    Get the HTTP method

    RequestInit::get_mode

    fn RequestInit::get_mode(self : RequestInit) -> String?

    Get the mode

    RequestInit::get_priority

    fn RequestInit::get_priority(self : RequestInit) -> String?

    Get the priority

    RequestInit::get_redirect

    fn RequestInit::get_redirect(self : RequestInit) -> String?

    Get the redirect mode

    RequestInit::get_referrer

    fn RequestInit::get_referrer(self : RequestInit) -> String?

    Get the referrer

    RequestInit::get_referrerPolicy

    fn RequestInit::get_referrerPolicy(self : RequestInit) -> String?

    Get the referrer policy

    RequestInit::get_signal

    Get the abort signal

    RequestInit::new

    #deprecated("use `RequestInit(..)` instead")
    fn RequestInit::new(http_method? : String, headers? : Headers, body? :
    Any
    , mode? : String, credentials? : String, cache? : String, redirect? : String, referrer? : String, referrerPolicy? : String, integrity? : String, keepalive? : Bool, signal? :
    AbortSignal
    , priority? : String) -> RequestInit

    Deprecated alias for the RequestInit(..) constructor.

    Response

    pub(all) struct Response {
    ok : Bool
    status : Int
    statusText : String
    redirected : Bool
    }

    https://developer.mozilla.org/ja/docs/Web/API/Response

    Response::Response

    fn Response::Response(body? : String, status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Create a new Response https://developer.mozilla.org/ja/docs/Web/API/Response/Response

    Response::as_any

    Response::body

    Get the body as a ReadableStream

    Response::clone

    fn Response::clone(self : Response) -> Response

    Response::error

    fn Response::error() -> Response

    https://developer.mozilla.org/ja/docs/Web/API/Response/error_static

    Response::formData

    #alias(form_data)
    async fn Response::formData(self : Response) -> FormData

    Response::from_body_init

    fn Response::from_body_init(body? :
    Any
    , status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Create a new Response from an arbitrary BodyInit value — ArrayBuffer, TypedArray, Blob, ReadableStream, FormData, URLSearchParams, …. Response::new types the body as String for the common text case, which makes binary bodies unrepresentable (bytes cannot ride a UTF-16 String); this constructor mirrors RequestInit, whose body is already @core.Any. Typical use: serving image bytes from a Worker. https://developer.mozilla.org/ja/docs/Web/API/Response/Response

    Response::html

    fn Response::html(body : String, status : Int) -> Response

    Create an HTML response

    Response::json

    async fn Response::json(self : Response) ->
    Any

    https://developer.mozilla.org/ja/docs/Web/API/Response/json

    Response::json_

    https://developer.mozilla.org/ja/docs/Web/API/Response/json_static

    Response::new

    #deprecated("use `Response(..)` instead")
    fn Response::new(body? : String, status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Deprecated alias for the Response(..) constructor.

    Response::new_with_body

    #deprecated("use `Response::from_body_init(..)` instead")
    fn Response::new_with_body(body? :
    Any
    , status? : Int, statusText? : String, headers? :
    Any
    ) -> Response

    Deprecated alias for Response::from_body_init(..).

    Response::redirect

    fn Response::redirect(url : String, status? : Int) -> Response

    https://developer.mozilla.org/ja/docs/Web/API/Response/redirect_static

    Response::text

    async fn Response::text(self : Response) -> String

    https://developer.mozilla.org/ja/docs/Web/API/Response/text

    Response::text_

    fn Response::text_(body : String, status : Int) -> Response

    Create a text response

    Response::type_

    fn Response::type_(self : Response) -> String

    Get the response type Note: This is a method because "type" is a reserved word in MoonBit

    fetch

    async fn fetch(url : String, method_~ : String, headers? : Map[String, String], cache? : String, mode? : String, body? :
    Any
    , credentials? : String, integrity? : String, keepalive? : Bool, priority? : String, redirect? : String, referrer? : String, referrerPolicy? : String, signal? :
    AbortSignal
    ) -> Response

    https://developer.mozilla.org/ja/docs/Web/API/fetch

    fetch_request

    async fn fetch_request(request : Request) -> Response