rust-fetch 1.1.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rust_fetch.d.ts CHANGED
@@ -1,131 +1,384 @@
1
+ /// <reference lib="esnext.disposable" />
1
2
  /* tslint:disable */
2
3
  /* eslint-disable */
3
4
  /**
4
- * Simple fetch function for JSON data
5
+ * The `ReadableStreamType` enum.
6
+ *
7
+ * *This API requires the following crate features to be activated: `ReadableStreamType`*
5
8
  */
6
- export function fetch_json(url: string): Promise<any>;
9
+
10
+ export type ReadableStreamType = "bytes";
11
+
12
+ /** Headers as a plain object, a `Headers` instance, a `Map` or an array of `[name, value]` pairs. */
13
+ export type HeadersLike =
14
+ | Record<string, string | number | boolean | null | undefined>
15
+ | Headers
16
+ | Map<string, string>
17
+ | Array<[string, string]>;
18
+
19
+ /** Query parameters; arrays produce repeated keys, `null`/`undefined` values are skipped. */
20
+ export type QueryParams =
21
+ | Record<string, string | number | boolean | null | undefined | Array<string | number | boolean>>
22
+ | URLSearchParams;
23
+
7
24
  /**
8
- * Fetch JSON data and return as a Promise
25
+ * A request body:
26
+ * - `string` is sent as-is (`text/plain` unless you set `Content-Type`)
27
+ * - `Uint8Array` / `ArrayBuffer` / any `ArrayBufferView` is sent as bytes (`application/octet-stream`)
28
+ * - `Blob` / `File` is sent as bytes with the blob's type
29
+ * - `FormData` is sent as `multipart/form-data`, including files
30
+ * - `URLSearchParams` is sent form-encoded (`application/x-www-form-urlencoded`)
31
+ * - anything else is sent as JSON (`application/json`), like `JSON.stringify`
9
32
  */
10
- export function fetch_json_promise(url: string): Promise<any>;
33
+ export type RequestBody = string | Uint8Array | ArrayBuffer | ArrayBufferView | Blob | FormData | URLSearchParams | object | number | boolean | null;
34
+
11
35
  /**
12
- * Simple fetch function for text/HTML data
36
+ * How to decode the response body:
37
+ * - `auto` (default): JSON for `application/json` and `*+json`, text for `text/*` and XML, otherwise `Uint8Array`
38
+ * - `json`: always `JSON.parse` (fails with a `ParseError` if the body isn't JSON)
39
+ * - `text`: always a string
40
+ * - `binary`: always a `Uint8Array`
41
+ * - `stream`: a `ReadableStream<Uint8Array>`, returned as soon as the headers arrive
42
+ *
43
+ * Empty bodies are `null` for `auto`/`json`, `""` for `text` and an empty `Uint8Array` for `binary`.
13
44
  */
14
- export function fetch_text(url: string): Promise<string>;
45
+ export type ResponseBodyType = 'auto' | 'json' | 'text' | 'binary' | 'stream';
46
+
47
+ /** Download progress; `total` comes from `Content-Length` and is `null` when unknown. */
48
+ export interface DownloadProgress {
49
+ loaded: number;
50
+ total: number | null;
51
+ }
52
+
53
+ export interface RetryOptions {
54
+ /** Maximum number of retries after the first attempt. Default: 3 */
55
+ maxRetries?: number;
56
+ /** Delay before the first retry, in ms. Default: 100 */
57
+ initialDelayMs?: number;
58
+ /** Upper bound for any retry delay (including `Retry-After`), in ms. Default: 10000 */
59
+ maxDelayMs?: number;
60
+ /** Exponential backoff multiplier. Default: 2 */
61
+ multiplier?: number;
62
+ /** HTTP status codes to retry. Default: [408, 429, 500, 502, 503, 504] */
63
+ retryOn?: number[];
64
+ /** Retry on network errors. Default: true */
65
+ retryOnNetworkError?: boolean;
66
+ /** Retry on timeouts. Default: true */
67
+ retryOnTimeout?: boolean;
68
+ /** Also retry POST and PATCH requests, which may not be safe to repeat. Default: false */
69
+ retryNonIdempotent?: boolean;
70
+ }
71
+
72
+ export interface ClientOptions {
73
+ /** Base URL that relative request URLs are appended to, e.g. `https://api.example.com/v1` */
74
+ baseUrl?: string;
75
+ /** Headers sent with every request; per-request headers with the same name replace them */
76
+ headers?: HeadersLike;
77
+ /** Timeout for each attempt (sending + reading the body), in ms. `0` disables it. Default: 30000 */
78
+ timeoutMs?: number;
79
+ /** Retry failed requests: a maximum retry count, or detailed options. Default: no retries */
80
+ retries?: number | RetryOptions;
81
+ }
82
+
83
+ export interface RequestOptions {
84
+ headers?: HeadersLike;
85
+ params?: QueryParams;
86
+ /** Overrides the client timeout for this request, in ms. `0` disables it. */
87
+ timeoutMs?: number;
88
+ responseType?: ResponseBodyType;
89
+ /**
90
+ * Cancels the request (including pending retries) when aborted; rejects with a `CancelledError`.
91
+ * For `responseType: 'stream'` it also errors the body stream.
92
+ */
93
+ signal?: AbortSignal;
94
+ /** Called after each received chunk of a successful response */
95
+ onDownloadProgress?: (progress: DownloadProgress) => void;
96
+ }
97
+
98
+ export interface FetchInit extends RequestOptions {
99
+ /** HTTP method. Default: `GET` */
100
+ method?: string;
101
+ body?: RequestBody;
102
+ }
103
+
104
+ /** What request interceptors see and may change (headers include per-request headers, not client defaults). */
105
+ export interface InterceptedRequest {
106
+ method: string;
107
+ url: string;
108
+ headers: Record<string, string>;
109
+ body?: RequestBody;
110
+ }
111
+
112
+ /** Return a (possibly new) request, or nothing to keep the mutated one. */
113
+ export type RequestInterceptor = (request: InterceptedRequest) => InterceptedRequest | void | Promise<InterceptedRequest | void>;
114
+ /** Return a response (possibly replaced), or throw to reject. */
115
+ export type ResponseFulfilledInterceptor = (response: HttpResponse) => HttpResponse | Promise<HttpResponse>;
116
+ /** Return a response to recover, or throw (e.g. rethrow) to keep rejecting. */
117
+ export type ResponseRejectedInterceptor = (error: RustFetchError) => HttpResponse | Promise<HttpResponse>;
118
+
119
+ export interface HttpResponse<T = any> {
120
+ status: number;
121
+ statusText: string;
122
+ /** Always `true`: non-2xx responses reject with an `HttpError` */
123
+ ok: boolean;
124
+ /** Final URL after redirects */
125
+ url: string;
126
+ /** Lower-cased header names; repeated headers are joined with `, ` */
127
+ headers: Record<string, string>;
128
+ body: T;
129
+ }
130
+
131
+ /** Every rejection is an `Error` with `name` and `kind` set to one of these. */
132
+ export type RustFetchErrorKind =
133
+ | 'NetworkError'
134
+ | 'HttpError'
135
+ | 'ParseError'
136
+ | 'TimeoutError'
137
+ | 'InvalidInputError'
138
+ | 'JsInteropError'
139
+ | 'CancelledError';
140
+
141
+ export interface RustFetchError extends Error {
142
+ name: RustFetchErrorKind;
143
+ kind: RustFetchErrorKind;
144
+ /** HttpError: response status */
145
+ status?: number;
146
+ /** HttpError: response status text */
147
+ statusText?: string;
148
+ /** HttpError: response body as text */
149
+ body?: string;
150
+ /** TimeoutError: the timeout that was exceeded */
151
+ timeoutMs?: number;
152
+ /** InvalidInputError: the offending parameter */
153
+ parameter?: string;
154
+ }
155
+
156
+
157
+
158
+ export class IntoUnderlyingByteSource {
159
+ private constructor();
160
+ free(): void;
161
+ [Symbol.dispose](): void;
162
+ cancel(): void;
163
+ pull(controller: ReadableByteStreamController): Promise<any>;
164
+ start(controller: ReadableByteStreamController): void;
165
+ readonly autoAllocateChunkSize: number;
166
+ readonly type: ReadableStreamType;
167
+ }
168
+
169
+ export class IntoUnderlyingSink {
170
+ private constructor();
171
+ free(): void;
172
+ [Symbol.dispose](): void;
173
+ abort(reason: any): Promise<any>;
174
+ close(): Promise<any>;
175
+ write(chunk: any): Promise<any>;
176
+ }
177
+
178
+ export class IntoUnderlyingSource {
179
+ private constructor();
180
+ free(): void;
181
+ [Symbol.dispose](): void;
182
+ cancel(): void;
183
+ pull(controller: ReadableStreamDefaultController): Promise<any>;
184
+ }
185
+
15
186
  /**
16
- * Advanced fetch function with full options
187
+ * HTTP client for JavaScript
17
188
  */
18
- export function fetch_with_options(url: string, method: string, headers: any, body?: string): Promise<any>;
189
+ export class WasmClient {
190
+ free(): void;
191
+ [Symbol.dispose](): void;
192
+ /**
193
+ * Run `interceptor` on every request before it is sent; returns an id for `ejectInterceptor`
194
+ */
195
+ addRequestInterceptor(interceptor: RequestInterceptor): number;
196
+ /**
197
+ * Run handlers on every settled request, like `promise.then(onFulfilled, onRejected)`;
198
+ * returns an id for `ejectInterceptor`
199
+ */
200
+ addResponseInterceptor(on_fulfilled?: ResponseFulfilledInterceptor | null, on_rejected?: ResponseRejectedInterceptor | null): number;
201
+ /**
202
+ * Remove all interceptors
203
+ */
204
+ clearInterceptors(): void;
205
+ /**
206
+ * Make a DELETE request
207
+ */
208
+ delete(url: string, options?: RequestOptions | null): Promise<HttpResponse>;
209
+ /**
210
+ * Remove an interceptor; returns whether it was registered
211
+ */
212
+ ejectInterceptor(id: number): boolean;
213
+ /**
214
+ * Make a request described by a `fetch`-style init object
215
+ */
216
+ fetch(url: string, init?: FetchInit | null): Promise<HttpResponse>;
217
+ /**
218
+ * Make a GET request
219
+ */
220
+ get(url: string, options?: RequestOptions | null): Promise<HttpResponse>;
221
+ /**
222
+ * Make a HEAD request
223
+ */
224
+ head(url: string, options?: RequestOptions | null): Promise<HttpResponse>;
225
+ /**
226
+ * Create a new client
227
+ */
228
+ constructor(options?: ClientOptions | null);
229
+ /**
230
+ * Make a PATCH request; the body is taken from `init.body`
231
+ */
232
+ patch(url: string, init?: FetchInit | null): Promise<HttpResponse>;
233
+ /**
234
+ * Make a PATCH request with a JSON body
235
+ */
236
+ patch_json(url: string, body: any, options?: RequestOptions | null): Promise<HttpResponse>;
237
+ /**
238
+ * Make a POST request; the body is taken from `init.body`
239
+ */
240
+ post(url: string, init?: FetchInit | null): Promise<HttpResponse>;
241
+ /**
242
+ * Make a POST request with a JSON body
243
+ */
244
+ post_json(url: string, body: any, options?: RequestOptions | null): Promise<HttpResponse>;
245
+ /**
246
+ * Make a PUT request; the body is taken from `init.body`
247
+ */
248
+ put(url: string, init?: FetchInit | null): Promise<HttpResponse>;
249
+ /**
250
+ * Make a PUT request with a JSON body
251
+ */
252
+ put_json(url: string, body: any, options?: RequestOptions | null): Promise<HttpResponse>;
253
+ /**
254
+ * Make a request with any method, headers and body
255
+ */
256
+ request(method: string, url: string, headers?: HeadersLike | null, body?: RequestBody | null, options?: RequestOptions | null): Promise<HttpResponse>;
257
+ }
258
+
19
259
  /**
20
260
  * Create a configured HTTP client (exported for advanced use)
21
261
  */
22
262
  export function create_client(): WasmClient;
263
+
23
264
  /**
24
- * Fetch JSON data (deprecated, use fetch_json instead)
265
+ * Fetch a URL and parse the body as JSON (whatever its Content-Type).
266
+ * An empty body resolves to `null`.
25
267
  */
26
- export function fetch_wasm_json(url: string): Promise<any>;
268
+ export function fetch_json(url: string): Promise<any>;
269
+
27
270
  /**
28
- * Fetch HTML data (deprecated, use fetch_text instead)
271
+ * Fetch JSON data and return as a Promise
29
272
  */
30
- export function fetch_wasm_html(url: string): Promise<string>;
273
+ export function fetch_json_promise(url: string): Promise<any>;
274
+
31
275
  /**
32
- * Fetch data and return as map (deprecated, use fetch_json instead)
276
+ * Fetch a URL and return the body as text
33
277
  */
34
- export function fetch_wasm_map(url: string): Promise<any>;
278
+ export function fetch_text(url: string): Promise<string>;
279
+
35
280
  /**
36
281
  * Fetch API data (deprecated, use fetch_json instead)
37
282
  */
38
283
  export function fetch_wasm_api(url: string): Promise<any>;
284
+
39
285
  /**
40
- * WASM bindings for the client
286
+ * Fetch HTML data (deprecated, use fetch_text instead)
41
287
  */
42
- export class WasmClient {
43
- free(): void;
44
- /**
45
- * Create a new client
46
- */
47
- constructor();
48
- /**
49
- * Make a GET request
50
- */
51
- get(url: string): Promise<any>;
52
- /**
53
- * Make a POST request with JSON body
54
- */
55
- post_json(url: string, body: any): Promise<any>;
56
- /**
57
- * Make a PUT request with JSON body
58
- */
59
- put_json(url: string, body: any): Promise<any>;
60
- /**
61
- * Make a DELETE request
62
- */
63
- delete(url: string): Promise<any>;
64
- /**
65
- * Make a PATCH request with JSON body
66
- */
67
- patch_json(url: string, body: any): Promise<any>;
68
- /**
69
- * Make a HEAD request
70
- */
71
- head(url: string): Promise<any>;
72
- /**
73
- * Make a custom request with method, headers, and body
74
- */
75
- request(method: string, url: string, headers: any, body: any): Promise<any>;
76
- }
288
+ export function fetch_wasm_html(url: string): Promise<string>;
289
+
290
+ /**
291
+ * Fetch JSON data (deprecated, use fetch_json instead)
292
+ */
293
+ export function fetch_wasm_json(url: string): Promise<any>;
294
+
295
+ /**
296
+ * Fetch data and return as map (deprecated, use fetch_json instead)
297
+ */
298
+ export function fetch_wasm_map(url: string): Promise<any>;
299
+
300
+ /**
301
+ * Fetch with a method, headers and an optional string body.
302
+ *
303
+ * Resolves to `{ status, statusText, ok, url, headers, body }`.
304
+ */
305
+ export function fetch_with_options(url: string, method: string, headers: any, body?: string | null): Promise<any>;
77
306
 
78
307
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
79
308
 
80
309
  export interface InitOutput {
81
- readonly memory: WebAssembly.Memory;
82
- readonly __wbg_wasmclient_free: (a: number, b: number) => void;
83
- readonly wasmclient_new: () => [number, number, number];
84
- readonly wasmclient_get: (a: number, b: number, c: number) => any;
85
- readonly wasmclient_post_json: (a: number, b: number, c: number, d: any) => any;
86
- readonly wasmclient_put_json: (a: number, b: number, c: number, d: any) => any;
87
- readonly wasmclient_delete: (a: number, b: number, c: number) => any;
88
- readonly wasmclient_patch_json: (a: number, b: number, c: number, d: any) => any;
89
- readonly wasmclient_head: (a: number, b: number, c: number) => any;
90
- readonly wasmclient_request: (a: number, b: number, c: number, d: number, e: number, f: any, g: any) => any;
91
- readonly fetch_json: (a: number, b: number) => any;
92
- readonly fetch_json_promise: (a: number, b: number) => any;
93
- readonly fetch_text: (a: number, b: number) => any;
94
- readonly fetch_with_options: (a: number, b: number, c: number, d: number, e: any, f: number, g: number) => any;
95
- readonly create_client: () => [number, number, number];
96
- readonly fetch_wasm_json: (a: number, b: number) => any;
97
- readonly fetch_wasm_html: (a: number, b: number) => any;
98
- readonly fetch_wasm_map: (a: number, b: number) => any;
99
- readonly fetch_wasm_api: (a: number, b: number) => any;
100
- readonly __wbindgen_malloc: (a: number, b: number) => number;
101
- readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
102
- readonly __wbindgen_exn_store: (a: number) => void;
103
- readonly __externref_table_alloc: () => number;
104
- readonly __wbindgen_export_4: WebAssembly.Table;
105
- readonly __wbindgen_export_5: WebAssembly.Table;
106
- readonly __externref_table_dealloc: (a: number) => void;
107
- readonly closure260_externref_shim: (a: number, b: number, c: any) => void;
108
- readonly closure282_externref_shim: (a: number, b: number, c: any, d: any) => void;
109
- readonly __wbindgen_start: () => void;
310
+ readonly memory: WebAssembly.Memory;
311
+ readonly __wbg_intounderlyingbytesource_free: (a: number, b: number) => void;
312
+ readonly __wbg_intounderlyingsink_free: (a: number, b: number) => void;
313
+ readonly __wbg_intounderlyingsource_free: (a: number, b: number) => void;
314
+ readonly __wbg_wasmclient_free: (a: number, b: number) => void;
315
+ readonly create_client: () => [number, number, number];
316
+ readonly fetch_json: (a: number, b: number) => any;
317
+ readonly fetch_json_promise: (a: number, b: number) => any;
318
+ readonly fetch_text: (a: number, b: number) => any;
319
+ readonly fetch_wasm_api: (a: number, b: number) => any;
320
+ readonly fetch_wasm_html: (a: number, b: number) => any;
321
+ readonly fetch_wasm_json: (a: number, b: number) => any;
322
+ readonly fetch_wasm_map: (a: number, b: number) => any;
323
+ readonly fetch_with_options: (a: number, b: number, c: number, d: number, e: any, f: number, g: number) => any;
324
+ readonly intounderlyingbytesource_autoAllocateChunkSize: (a: number) => number;
325
+ readonly intounderlyingbytesource_cancel: (a: number) => void;
326
+ readonly intounderlyingbytesource_pull: (a: number, b: any) => any;
327
+ readonly intounderlyingbytesource_start: (a: number, b: any) => void;
328
+ readonly intounderlyingbytesource_type: (a: number) => number;
329
+ readonly intounderlyingsink_abort: (a: number, b: any) => any;
330
+ readonly intounderlyingsink_close: (a: number) => any;
331
+ readonly intounderlyingsink_write: (a: number, b: any) => any;
332
+ readonly intounderlyingsource_cancel: (a: number) => void;
333
+ readonly intounderlyingsource_pull: (a: number, b: any) => any;
334
+ readonly wasmclient_addRequestInterceptor: (a: number, b: any) => number;
335
+ readonly wasmclient_addResponseInterceptor: (a: number, b: number, c: number) => number;
336
+ readonly wasmclient_clearInterceptors: (a: number) => void;
337
+ readonly wasmclient_delete: (a: number, b: number, c: number, d: number) => any;
338
+ readonly wasmclient_ejectInterceptor: (a: number, b: number) => number;
339
+ readonly wasmclient_fetch: (a: number, b: number, c: number, d: number) => any;
340
+ readonly wasmclient_get: (a: number, b: number, c: number, d: number) => any;
341
+ readonly wasmclient_head: (a: number, b: number, c: number, d: number) => any;
342
+ readonly wasmclient_new: (a: number) => [number, number, number];
343
+ readonly wasmclient_patch: (a: number, b: number, c: number, d: number) => any;
344
+ readonly wasmclient_patch_json: (a: number, b: number, c: number, d: any, e: number) => any;
345
+ readonly wasmclient_post: (a: number, b: number, c: number, d: number) => any;
346
+ readonly wasmclient_post_json: (a: number, b: number, c: number, d: any, e: number) => any;
347
+ readonly wasmclient_put: (a: number, b: number, c: number, d: number) => any;
348
+ readonly wasmclient_put_json: (a: number, b: number, c: number, d: any, e: number) => any;
349
+ readonly wasmclient_request: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => any;
350
+ readonly wasm_bindgen_6b82e471f4a4091b___convert__closures_____invoke___js_sys_6c670598d03a71fc___Function_fn_wasm_bindgen_6b82e471f4a4091b___JsValue_____wasm_bindgen_6b82e471f4a4091b___sys__Undefined___js_sys_6c670598d03a71fc___Function_fn_wasm_bindgen_6b82e471f4a4091b___JsValue_____wasm_bindgen_6b82e471f4a4091b___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
351
+ readonly wasm_bindgen_6b82e471f4a4091b___convert__closures_____invoke___wasm_bindgen_6b82e471f4a4091b___JsValue__core_37ee0ce0c4cd57d5___result__Result_____wasm_bindgen_6b82e471f4a4091b___JsError___true_: (a: number, b: number, c: any) => [number, number];
352
+ readonly wasm_bindgen_6b82e471f4a4091b___convert__closures_____invoke___wasm_bindgen_6b82e471f4a4091b___JsValue______true_: (a: number, b: number, c: any) => void;
353
+ readonly wasm_bindgen_6b82e471f4a4091b___convert__closures_____invoke_______true_: (a: number, b: number) => void;
354
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
355
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
356
+ readonly __wbindgen_exn_store: (a: number) => void;
357
+ readonly __externref_table_alloc: () => number;
358
+ readonly __wbindgen_externrefs: WebAssembly.Table;
359
+ readonly __wbindgen_destroy_closure: (a: number, b: number) => void;
360
+ readonly __externref_table_dealloc: (a: number) => void;
361
+ readonly __wbindgen_start: () => void;
110
362
  }
111
363
 
112
364
  export type SyncInitInput = BufferSource | WebAssembly.Module;
365
+
113
366
  /**
114
- * Instantiates the given `module`, which can either be bytes or
115
- * a precompiled `WebAssembly.Module`.
116
- *
117
- * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
118
- *
119
- * @returns {InitOutput}
120
- */
367
+ * Instantiates the given `module`, which can either be bytes or
368
+ * a precompiled `WebAssembly.Module`.
369
+ *
370
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
371
+ *
372
+ * @returns {InitOutput}
373
+ */
121
374
  export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
122
375
 
123
376
  /**
124
- * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
125
- * for everything else, calls `WebAssembly.instantiate` directly.
126
- *
127
- * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
128
- *
129
- * @returns {Promise<InitOutput>}
130
- */
377
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
378
+ * for everything else, calls `WebAssembly.instantiate` directly.
379
+ *
380
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
381
+ *
382
+ * @returns {Promise<InitOutput>}
383
+ */
131
384
  export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;