Module fetch
The promise side of the Fetch API, on the same records as the callback API of module net (a Response is a Fetch: Fetch_status, Fetch_headers, Fetch_release... apply to it).
- Fetch_promise settles with the Response when the headers arrive; it is rejected on a network error (Network) or an abort (the signal's reason).
- Response_text / Response_arrayBuffer settle when the whole body is there; a body is consumed once (bodyUsed).
- Response_body is a minimal ReadableStream: Stream_read settles with a buffer of what arrived since the last read, or with 0 (text "done") at the end; Stream_cancel stops the download.
- An abort signal cancels the request and fails it with error kind Aborted.
Exposed to: Window. Source: api/fetch.widl.
Odin: import "wren:fetch".
Types
handle ReadableStream : Object
The body of a response being read in chunks.
Checked by the browser on every call (lookup_ReadableStream).
Functions
Fetch_promise
Promise<Response> Fetch_promise(string url, string method = "GET", string headers = "", optional bytes body, optional Buffer body_buffer, optional AbortSignal signal, FetchOptions options = none, string integrity = "") raisesfetch(url, init): the body is body, or the bytes of body_buffer when given. Type when the signal is not an
AbortSignal or the buffer not a buffer. With integrity metadata (SRI: sha256-, sha384-, sha512- hashes), the
promise settles once the whole body is there: with the Response when it matches, rejected (Network) when not.
Odin: fetch.promise.
Raises: returns an Error (see web).
Host: Fetch_promise :: proc(c: ^Call, url: string, method: string, headers: string, body: Maybe([]u8), body_buffer: Api_Buffer, signal: Api_AbortSignal, options: Api_FetchOptions, integrity: string) -> (u64, Api_Error)
Fetch_startWithSignal
Fetch Fetch_startWithSignal(string url, FetchCallback callback, i32 user = 0, string method = "GET", string headers = "", optional bytes body, optional Buffer body_buffer, optional AbortSignal signal, FetchOptions options = none) raisesThe callback API (Fetch_start) with an abort signal and an optional buffer body.
Odin: fetch.startWithSignal.
Raises: returns an Error (see web).
Host: Fetch_startWithSignal :: proc(c: ^Call, url: string, callback: u32, user: i32, method: string, headers: string, body: Maybe([]u8), body_buffer: Api_Buffer, signal: Api_AbortSignal, options: Api_FetchOptions) -> (u64, Api_Error)
Fetch_sync
Fetch Fetch_sync(string url, string method = "GET", string headers = "", optional bytes body, FetchOptions options = none, u32 timeout_ms = 0) raisesA synchronous XMLHttpRequest of a worker: the request Fetch_startWithSignal would make, waited for on the worker's
thread; the finished record (read it with Fetch_status, Fetch_headers, Fetch_readBody..., then release it).
Not_Supported in a document (its thread is the page's), Timeout past timeout_ms (0: none), Network on a failure.
Odin: fetch.sync.
Raises: returns an Error (see web).
Host: Fetch_sync :: proc(c: ^Call, url: string, method: string, headers: string, body: Maybe([]u8), options: Api_FetchOptions, timeout_ms: u32) -> (u64, Api_Error)
Response_text
Promise<string> Response_text(Response response)response.text(): settles with the text (decoded as UTF-8) in the promise's text, its length as value. A callback fetch can be read once it is done (Invalid_State before).
Host: Response_text :: proc(c: ^Call, response: Api_Fetch) -> u64
Response_arrayBuffer
Promise<Buffer> Response_arrayBuffer(Response response)response.arrayBuffer(): settles with a buffer.
Host: Response_arrayBuffer :: proc(c: ^Call, response: Api_Fetch) -> u64
Response_bytes
Promise<Buffer> Response_bytes(Response response)response.bytes(): the same as arrayBuffer on this platform.
Host: Response_bytes :: proc(c: ^Call, response: Api_Fetch) -> u64
Response_bodyUsed
bool Response_bodyUsed(Response response)response.bodyUsed
Host: Response_bodyUsed :: proc(c: ^Call, response: Api_Fetch) -> bool
Response_body
ReadableStream Response_body(Response response) raisesresponse.body: the body as a stream. Type when the body was used already.
Raises: returns an Error (see web).
Host: Response_body :: proc(c: ^Call, response: Api_Fetch) -> (u64, Api_Error)
Stream_read
Promise<Buffer> Stream_read(ReadableStream stream)reader.read(): settles with a buffer, or with 0 at the end.
Host: Stream_read :: proc(c: ^Call, stream: Api_ReadableStream) -> u64
Stream_cancel
void Stream_cancel(ReadableStream stream)reader.cancel(): the download stops, waiting reads settle with "done".
Host: Stream_cancel :: proc(c: ^Call, stream: Api_ReadableStream)
Fetch_startFormData
Fetch Fetch_startFormData(string url, FormData data, FetchCallback callback, i32 user = 0, string method = "POST", string headers = "", FormEnctype enctype = Multipart, FetchOptions options = none)fetch with a FormData body: the Content-Type (with its boundary) is set unless headers name one. Same callback
and options as Fetch_start.
Odin: fetch.startFormData.
Host: Fetch_startFormData :: proc(c: ^Call, url: string, data: Api_FormData, callback: u32, user: i32, method: string, headers: string, enctype: Api_FormEnctype, options: Api_FetchOptions) -> u64