@zimic/fetch 0.4.1-canary.0 → 0.4.1-canary.1
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/dist/index.d.ts +27 -798
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
- package/src/client/errors/FetchResponseError.ts +6 -87
- package/src/client/factory.ts +1 -53
- package/src/client/types/json.ts +1 -1
- package/src/client/types/public.ts +14 -514
- package/src/client/types/requests.ts +5 -144
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ declare const value: unique symbol;
|
|
|
6
6
|
*
|
|
7
7
|
* This type is used to validate that the expected stringified body is passed to `fetch`.
|
|
8
8
|
*
|
|
9
|
-
* @see {@link https://
|
|
9
|
+
* @see {@link https://zimic.dev/docs/fetch/guides/bodies#json-request-body Using a JSON request body}
|
|
10
10
|
*/
|
|
11
11
|
type JSONStringified<Value> = string & {
|
|
12
12
|
[value]: Value;
|
|
@@ -46,13 +46,7 @@ type FetchRequestInitWithBody<BodySchema extends HttpBody> = [BodySchema] extend
|
|
|
46
46
|
body: BodySchema;
|
|
47
47
|
};
|
|
48
48
|
type FetchRequestInitPerPath<MethodSchema extends HttpMethodSchema> = FetchRequestInitWithHeaders<HttpRequestHeadersSchema<MethodSchema>> & FetchRequestInitWithSearchParams<HttpRequestSearchParamsSchema<MethodSchema>> & FetchRequestInitWithBody<FetchRequestBodySchema<Default<MethodSchema['request']>>>;
|
|
49
|
-
/**
|
|
50
|
-
* The options to create a {@link FetchRequest} instance, compatible with
|
|
51
|
-
* {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}.
|
|
52
|
-
*
|
|
53
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
54
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}
|
|
55
|
-
*/
|
|
49
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
56
50
|
type FetchRequestInit<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath<Schema, Method>, Redirect extends RequestRedirect = 'follow'> = Omit<RequestInit, 'method' | 'headers' | 'body'> & {
|
|
57
51
|
/** The HTTP method of the request. */
|
|
58
52
|
method: Method;
|
|
@@ -78,55 +72,7 @@ type AllFetchResponseStatusCode<MethodSchema extends HttpMethodSchema> = HttpRes
|
|
|
78
72
|
type FilterFetchResponseStatusCodeByError<StatusCode extends HttpStatusCode, ErrorOnly extends boolean> = ErrorOnly extends true ? Extract<StatusCode, HttpStatusCode.ClientError | HttpStatusCode.ServerError> : StatusCode;
|
|
79
73
|
type FilterFetchResponseStatusCodeByRedirect<StatusCode extends HttpStatusCode, Redirect extends RequestRedirect> = Redirect extends 'error' ? FilterFetchResponseStatusCodeByRedirect<StatusCode, 'follow'> : Redirect extends 'follow' ? Exclude<StatusCode, Exclude<HttpStatusCode.Redirection, 304>> : StatusCode;
|
|
80
74
|
type FetchResponseStatusCode<MethodSchema extends HttpMethodSchema, ErrorOnly extends boolean, Redirect extends RequestRedirect> = FilterFetchResponseStatusCodeByRedirect<FilterFetchResponseStatusCodeByError<AllFetchResponseStatusCode<MethodSchema>, ErrorOnly>, Redirect>;
|
|
81
|
-
/**
|
|
82
|
-
* A request instance typed with an HTTP schema, closely compatible with the
|
|
83
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Request native Request class}.
|
|
84
|
-
*
|
|
85
|
-
* On top of the properties available in native {@link https://developer.mozilla.org/docs/Web/API/Request `Request`}
|
|
86
|
-
* instances, fetch requests have their URL automatically prefixed with the base URL of their fetch instance. Default
|
|
87
|
-
* options are also applied, if present in the fetch instance.
|
|
88
|
-
*
|
|
89
|
-
* The path of the request is extracted from the URL, excluding the base URL, and is available in the `path` property.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
93
|
-
* import { createFetch } from '@zimic/fetch';
|
|
94
|
-
*
|
|
95
|
-
* interface User {
|
|
96
|
-
* id: string;
|
|
97
|
-
* username: string;
|
|
98
|
-
* }
|
|
99
|
-
*
|
|
100
|
-
* type Schema = HttpSchema<{
|
|
101
|
-
* '/users': {
|
|
102
|
-
* POST: {
|
|
103
|
-
* request: {
|
|
104
|
-
* headers: { 'content-type': 'application/json' };
|
|
105
|
-
* body: { username: string };
|
|
106
|
-
* };
|
|
107
|
-
* response: {
|
|
108
|
-
* 201: { body: User };
|
|
109
|
-
* };
|
|
110
|
-
* };
|
|
111
|
-
* };
|
|
112
|
-
* }>;
|
|
113
|
-
*
|
|
114
|
-
* const fetch = createFetch<Schema>({
|
|
115
|
-
* baseURL: 'http://localhost:3000',
|
|
116
|
-
* });
|
|
117
|
-
*
|
|
118
|
-
* const request = new fetch.Request('/users', {
|
|
119
|
-
* method: 'POST',
|
|
120
|
-
* headers: { 'content-type': 'application/json' },
|
|
121
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
122
|
-
* });
|
|
123
|
-
*
|
|
124
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
125
|
-
* console.log(request.path); // '/users'
|
|
126
|
-
*
|
|
127
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest `FetchRequest` API reference}
|
|
128
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
129
|
-
*/
|
|
75
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-request `FetchRequest` API reference} */
|
|
130
76
|
interface FetchRequest<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>> extends HttpRequest<HttpRequestBodySchema<Default<Schema[Path][Method]>>, Default<HttpRequestHeadersSchema<Default<Schema[Path][Method]>>>> {
|
|
131
77
|
/** The path of the request, excluding the base URL. */
|
|
132
78
|
path: AllowAnyStringInPathParams<Path>;
|
|
@@ -159,12 +105,7 @@ type FetchRequestObject = Pick<FetchRequest.Loose, 'url' | 'path' | 'method' | '
|
|
|
159
105
|
*/
|
|
160
106
|
body?: string | null;
|
|
161
107
|
};
|
|
162
|
-
/**
|
|
163
|
-
* A {@link FetchResponse `FetchResponse`} instance with a specific status code.
|
|
164
|
-
*
|
|
165
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
166
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
167
|
-
*/
|
|
108
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response `FetchResponse` API reference} */
|
|
168
109
|
interface FetchResponsePerStatusCode<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>, StatusCode extends HttpStatusCode = HttpStatusCode> extends HttpResponse<HttpResponseBodySchema<Default<Schema[Path][Method]>, StatusCode>, Default<HttpResponseHeadersSchema<Default<Schema[Path][Method]>, StatusCode>>, StatusCode> {
|
|
169
110
|
/** The request that originated the response. */
|
|
170
111
|
request: FetchRequest<Schema, Method, Path>;
|
|
@@ -176,57 +117,7 @@ interface FetchResponsePerStatusCode<Schema extends HttpSchema, Method extends H
|
|
|
176
117
|
*/
|
|
177
118
|
error: StatusCode extends HttpStatusCode.ClientError | HttpStatusCode.ServerError ? FetchResponseError<Schema, Method, Path> : null;
|
|
178
119
|
}
|
|
179
|
-
/**
|
|
180
|
-
* A response instance typed with an HTTP schema, closely compatible with the
|
|
181
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Response native Response class}.
|
|
182
|
-
*
|
|
183
|
-
* On top of the properties available in native Response instances, fetch responses have a reference to the request that
|
|
184
|
-
* originated them, available in the `request` property.
|
|
185
|
-
*
|
|
186
|
-
* If the response has a failure status code (4XX or 5XX), an error is available in the `error` property.
|
|
187
|
-
*
|
|
188
|
-
* @example
|
|
189
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
190
|
-
* import { createFetch } from '@zimic/fetch';
|
|
191
|
-
*
|
|
192
|
-
* interface User {
|
|
193
|
-
* id: string;
|
|
194
|
-
* username: string;
|
|
195
|
-
* }
|
|
196
|
-
*
|
|
197
|
-
* type Schema = HttpSchema<{
|
|
198
|
-
* '/users/:userId': {
|
|
199
|
-
* GET: {
|
|
200
|
-
* response: {
|
|
201
|
-
* 200: { body: User };
|
|
202
|
-
* 404: { body: { message: string } };
|
|
203
|
-
* };
|
|
204
|
-
* };
|
|
205
|
-
* };
|
|
206
|
-
* }>;
|
|
207
|
-
*
|
|
208
|
-
* const fetch = createFetch<Schema>({
|
|
209
|
-
* baseURL: 'http://localhost:3000',
|
|
210
|
-
* });
|
|
211
|
-
*
|
|
212
|
-
* const response = await fetch(`/users/${userId}`, {
|
|
213
|
-
* method: 'GET',
|
|
214
|
-
* });
|
|
215
|
-
*
|
|
216
|
-
* console.log(response); // FetchResponse<Schema, 'GET', '/users'>
|
|
217
|
-
*
|
|
218
|
-
* if (response.status === 404) {
|
|
219
|
-
* const errorBody = await response.json(); // { message: string }
|
|
220
|
-
* console.error(errorBody.message);
|
|
221
|
-
* return null;
|
|
222
|
-
* } else {
|
|
223
|
-
* const user = await response.json(); // User
|
|
224
|
-
* return user;
|
|
225
|
-
* }
|
|
226
|
-
*
|
|
227
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
228
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
229
|
-
*/
|
|
120
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response `FetchResponse` API reference} */
|
|
230
121
|
type FetchResponse<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>, ErrorOnly extends boolean = false, Redirect extends RequestRedirect = 'follow', StatusCode extends FetchResponseStatusCode<Default<Schema[Path][Method]>, ErrorOnly, Redirect> = FetchResponseStatusCode<Default<Schema[Path][Method]>, ErrorOnly, Redirect>> = StatusCode extends StatusCode ? FetchResponsePerStatusCode<Schema, Method, Path, StatusCode> : never;
|
|
231
122
|
declare namespace FetchResponse {
|
|
232
123
|
/** A loosely typed version of a {@link FetchResponse}. */
|
|
@@ -259,56 +150,14 @@ type FetchResponseObject = Pick<FetchResponse.Loose, 'url' | 'type' | 'status' |
|
|
|
259
150
|
*/
|
|
260
151
|
body?: string | null;
|
|
261
152
|
};
|
|
262
|
-
/**
|
|
263
|
-
* A constructor for {@link FetchRequest} instances, typed with an HTTP schema and compatible with the
|
|
264
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Request Request class constructor}.
|
|
265
|
-
*
|
|
266
|
-
* @example
|
|
267
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
268
|
-
* import { createFetch } from '@zimic/fetch';
|
|
269
|
-
*
|
|
270
|
-
* type Schema = HttpSchema<{
|
|
271
|
-
* // ...
|
|
272
|
-
* }>;
|
|
273
|
-
*
|
|
274
|
-
* const fetch = createFetch<Schema>({
|
|
275
|
-
* baseURL: 'http://localhost:3000',
|
|
276
|
-
* });
|
|
277
|
-
*
|
|
278
|
-
* const request = new fetch.Request('POST', '/users', {
|
|
279
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
280
|
-
* });
|
|
281
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
282
|
-
*
|
|
283
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided, it
|
|
284
|
-
* is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a request
|
|
285
|
-
* is provided, it is used as is.
|
|
286
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required and
|
|
287
|
-
* should contain at least the method of the request. If the first argument is a {@link FetchRequest request}, this
|
|
288
|
-
* argument is optional.
|
|
289
|
-
* @returns A promise that resolves to the response to the request.
|
|
290
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
291
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
292
|
-
*/
|
|
153
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchrequest `fetch.Request` API reference} */
|
|
293
154
|
type FetchRequestConstructor<Schema extends HttpSchema> = new <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.NonLiteral<Schema, Method>>(input: FetchInput<Schema, Method, Path>, init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>) => FetchRequest<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>;
|
|
294
155
|
|
|
295
|
-
/**
|
|
296
|
-
* Options for converting a {@link FetchResponseError `FetchResponseError`} into a plain object.
|
|
297
|
-
*
|
|
298
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponseerrortoobject `FetchResponseError#toObject` API reference}
|
|
299
|
-
*/
|
|
156
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error#errortoobject `fetchResponseError.toObject()` API reference} */
|
|
300
157
|
interface FetchResponseErrorObjectOptions {
|
|
301
|
-
/**
|
|
302
|
-
* Whether to include the body of the request in the plain object.
|
|
303
|
-
*
|
|
304
|
-
* @default false
|
|
305
|
-
*/
|
|
158
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error#errortoobject `fetchResponseError.toObject()` API reference} */
|
|
306
159
|
includeRequestBody?: boolean;
|
|
307
|
-
/**
|
|
308
|
-
* Whether to include the body of the response in the plain object.
|
|
309
|
-
*
|
|
310
|
-
* @default false
|
|
311
|
-
*/
|
|
160
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error#errortoobject `fetchResponseError.toObject()` API reference} */
|
|
312
161
|
includeResponseBody?: boolean;
|
|
313
162
|
}
|
|
314
163
|
declare namespace FetchResponseErrorObjectOptions {
|
|
@@ -335,7 +184,7 @@ declare namespace FetchResponseErrorObjectOptions {
|
|
|
335
184
|
* A plain object representation of a {@link FetchResponseError `FetchResponseError`}, compatible with JSON. It is useful
|
|
336
185
|
* for serialization, debugging, and logging purposes.
|
|
337
186
|
*
|
|
338
|
-
* @see {@link https://
|
|
187
|
+
* @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error#errortoobject `fetchResponseError.toObject()` API reference}
|
|
339
188
|
*/
|
|
340
189
|
interface FetchResponseErrorObject {
|
|
341
190
|
name: string;
|
|
@@ -343,80 +192,12 @@ interface FetchResponseErrorObject {
|
|
|
343
192
|
request: FetchRequestObject;
|
|
344
193
|
response: FetchResponseObject;
|
|
345
194
|
}
|
|
346
|
-
/**
|
|
347
|
-
* An error representing a response with a failure status code (4XX or 5XX).
|
|
348
|
-
*
|
|
349
|
-
* @example
|
|
350
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
351
|
-
* import { createFetch } from '@zimic/fetch';
|
|
352
|
-
*
|
|
353
|
-
* interface User {
|
|
354
|
-
* id: string;
|
|
355
|
-
* username: string;
|
|
356
|
-
* }
|
|
357
|
-
*
|
|
358
|
-
* type Schema = HttpSchema<{
|
|
359
|
-
* '/users/:userId': {
|
|
360
|
-
* GET: {
|
|
361
|
-
* response: {
|
|
362
|
-
* 200: { body: User };
|
|
363
|
-
* 404: { body: { message: string } };
|
|
364
|
-
* };
|
|
365
|
-
* };
|
|
366
|
-
* };
|
|
367
|
-
* }>;
|
|
368
|
-
*
|
|
369
|
-
* const fetch = createFetch<Schema>({
|
|
370
|
-
* baseURL: 'http://localhost:3000',
|
|
371
|
-
* });
|
|
372
|
-
*
|
|
373
|
-
* const response = await fetch(`/users/${userId}`, {
|
|
374
|
-
* method: 'GET',
|
|
375
|
-
* });
|
|
376
|
-
*
|
|
377
|
-
* if (!response.ok) {
|
|
378
|
-
* console.log(response.status); // 404
|
|
379
|
-
*
|
|
380
|
-
* console.log(response.error); // FetchResponseError<Schema, 'GET', '/users'>
|
|
381
|
-
* console.log(response.error.request); // FetchRequest<Schema, 'GET', '/users'>
|
|
382
|
-
* console.log(response.error.response); // FetchResponse<Schema, 'GET', '/users'>
|
|
383
|
-
*
|
|
384
|
-
* const plainError = response.error.toObject();
|
|
385
|
-
* console.log(JSON.stringify(plainError));
|
|
386
|
-
* // {"name":"FetchResponseError","message":"...","request":{...},"response":{...}}
|
|
387
|
-
* }
|
|
388
|
-
*
|
|
389
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponseerror `FetchResponseError` API reference}
|
|
390
|
-
*/
|
|
195
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error `FetchResponseError` API reference} */
|
|
391
196
|
declare class FetchResponseError<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>> extends Error {
|
|
392
197
|
request: FetchRequest<Schema, Method, Path>;
|
|
393
198
|
response: FetchResponse<Schema, Method, Path, true, 'manual'>;
|
|
394
199
|
constructor(request: FetchRequest<Schema, Method, Path>, response: FetchResponse<Schema, Method, Path, true, 'manual'>);
|
|
395
|
-
/**
|
|
396
|
-
* Converts this error into a plain object. This method is useful for serialization, debugging, and logging purposes.
|
|
397
|
-
*
|
|
398
|
-
* @example
|
|
399
|
-
* const fetch = createFetch<Schema>({
|
|
400
|
-
* baseURL: 'http://localhost:3000',
|
|
401
|
-
* });
|
|
402
|
-
*
|
|
403
|
-
* const response = await fetch(`/users/${userId}`, {
|
|
404
|
-
* method: 'GET',
|
|
405
|
-
* });
|
|
406
|
-
*
|
|
407
|
-
* if (!response.ok) {
|
|
408
|
-
* const plainError = response.error.toObject();
|
|
409
|
-
* console.log(JSON.stringify(plainError));
|
|
410
|
-
* // {"name":"FetchResponseError","message":"...","request":{...},"response":{...}}
|
|
411
|
-
* }
|
|
412
|
-
*
|
|
413
|
-
* @param options Options for converting this error into a plain object. By default, the body of the request and
|
|
414
|
-
* response will not be included.
|
|
415
|
-
* @returns A plain object representing this error. If `options.includeRequestBody` or `options.includeResponseBody`
|
|
416
|
-
* is `true`, the body of the request and response will be included, respectively, and the return is a `Promise`.
|
|
417
|
-
* Otherwise, the return is the plain object itself without the bodies.
|
|
418
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponseerrortoobject `FetchResponseError#toObject` API reference}
|
|
419
|
-
*/
|
|
200
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response-error#errortoobject `fetchResponseError.toObject()` API reference} */
|
|
420
201
|
toObject(options: FetchResponseErrorObjectOptions.WithBody): Promise<FetchResponseErrorObject>;
|
|
421
202
|
toObject(options: FetchResponseErrorObjectOptions.WithoutBody): FetchResponseErrorObject;
|
|
422
203
|
toObject(options?: FetchResponseErrorObjectOptions): Promise<FetchResponseErrorObject> | FetchResponseErrorObject;
|
|
@@ -425,544 +206,44 @@ declare class FetchResponseError<Schema extends HttpSchema, Method extends HttpS
|
|
|
425
206
|
}
|
|
426
207
|
type AnyFetchRequestError = FetchResponseError<any, any, any>;
|
|
427
208
|
|
|
428
|
-
/**
|
|
429
|
-
* The input to fetch a resource, either a path, a URL, or a {@link FetchRequest request}.
|
|
430
|
-
*
|
|
431
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
432
|
-
*/
|
|
209
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
433
210
|
type FetchInput<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.NonLiteral<Schema, Method>> = Path | URL | FetchRequest<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>;
|
|
434
|
-
/**
|
|
435
|
-
* A fetch instance typed with an HTTP schema, closely compatible with the
|
|
436
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Fetch_API native Fetch API}. All requests and responses are typed by
|
|
437
|
-
* default with the schema, including methods, paths, status codes, parameters, and bodies.
|
|
438
|
-
*
|
|
439
|
-
* Requests sent by the fetch instance have their URL automatically prefixed with the base URL of the instance. Default
|
|
440
|
-
* options are also applied to the requests, if present in the instance.
|
|
441
|
-
*
|
|
442
|
-
* @example
|
|
443
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
444
|
-
* import { createFetch } from '@zimic/fetch';
|
|
445
|
-
*
|
|
446
|
-
* interface User {
|
|
447
|
-
* id: string;
|
|
448
|
-
* username: string;
|
|
449
|
-
* }
|
|
450
|
-
*
|
|
451
|
-
* type Schema = HttpSchema<{
|
|
452
|
-
* '/users': {
|
|
453
|
-
* GET: {
|
|
454
|
-
* request: {
|
|
455
|
-
* searchParams: { query?: string };
|
|
456
|
-
* };
|
|
457
|
-
* response: {
|
|
458
|
-
* 200: { body: User[] };
|
|
459
|
-
* 404: { body: { message: string } };
|
|
460
|
-
* 500: { body: { message: string } };
|
|
461
|
-
* };
|
|
462
|
-
* };
|
|
463
|
-
* };
|
|
464
|
-
* }>;
|
|
465
|
-
*
|
|
466
|
-
* const fetch = createFetch<Schema>({
|
|
467
|
-
* baseURL: 'http://localhost:3000',
|
|
468
|
-
* headers: { 'accept-language': 'en' },
|
|
469
|
-
* });
|
|
470
|
-
*
|
|
471
|
-
* const response = await fetch('/users', {
|
|
472
|
-
* method: 'GET',
|
|
473
|
-
* searchParams: { query: 'u' },
|
|
474
|
-
* });
|
|
475
|
-
*
|
|
476
|
-
* if (response.status === 404) {
|
|
477
|
-
* return null; // User not found
|
|
478
|
-
* }
|
|
479
|
-
*
|
|
480
|
-
* if (!response.ok) {
|
|
481
|
-
* throw response.error;
|
|
482
|
-
* }
|
|
483
|
-
*
|
|
484
|
-
* const users = await response.json();
|
|
485
|
-
* return users; // User[]
|
|
486
|
-
*
|
|
487
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided, it
|
|
488
|
-
* is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a request
|
|
489
|
-
* is provided, it is used as is.
|
|
490
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required and
|
|
491
|
-
* should contain at least the method of the request. If the first argument is a {@link FetchRequest request}, this
|
|
492
|
-
* argument is optional.
|
|
493
|
-
* @returns A promise that resolves to the response to the request.
|
|
494
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
495
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Fetch_API}
|
|
496
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
497
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit}
|
|
498
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
499
|
-
*/
|
|
211
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
500
212
|
interface Fetch<Schema extends HttpSchema> extends Pick<FetchOptions<Schema>, 'onRequest' | 'onResponse'> {
|
|
501
213
|
<Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.NonLiteral<Schema, Method>, Redirect extends RequestRedirect = 'follow'>(input: Path | URL, init: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Redirect>): Promise<FetchResponse<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, false, Redirect>>;
|
|
502
214
|
<Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.NonLiteral<Schema, Method>, Redirect extends RequestRedirect = 'follow'>(input: FetchRequest<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>, init?: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Redirect>): Promise<FetchResponse<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, false, Redirect>>;
|
|
503
|
-
/**
|
|
504
|
-
* The default options for each request sent by the fetch instance. The available options are the same as the
|
|
505
|
-
* {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`} options, plus `baseURL`.
|
|
506
|
-
*
|
|
507
|
-
* @example
|
|
508
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
509
|
-
* import { createFetch } from '@zimic/fetch';
|
|
510
|
-
*
|
|
511
|
-
* interface Post {
|
|
512
|
-
* id: string;
|
|
513
|
-
* title: string;
|
|
514
|
-
* }
|
|
515
|
-
*
|
|
516
|
-
* type Schema = HttpSchema<{
|
|
517
|
-
* '/posts': {
|
|
518
|
-
* POST: {
|
|
519
|
-
* request: {
|
|
520
|
-
* headers: { 'content-type': 'application/json' };
|
|
521
|
-
* body: { title: string };
|
|
522
|
-
* };
|
|
523
|
-
* response: {
|
|
524
|
-
* 201: { body: Post };
|
|
525
|
-
* };
|
|
526
|
-
* };
|
|
527
|
-
* };
|
|
528
|
-
* }>;
|
|
529
|
-
*
|
|
530
|
-
* const fetch = createFetch<Schema>({
|
|
531
|
-
* baseURL: 'http://localhost:3000',
|
|
532
|
-
* headers: { 'accept-language': 'en' },
|
|
533
|
-
* });
|
|
534
|
-
*
|
|
535
|
-
* // Set the authorization header for all requests
|
|
536
|
-
* const { accessToken } = await authenticate();
|
|
537
|
-
*
|
|
538
|
-
* fetch.defaults.headers.authorization = `Bearer ${accessToken}`;
|
|
539
|
-
* console.log(fetch.defaults.headers);
|
|
540
|
-
*
|
|
541
|
-
* const response = await fetch('/posts', {
|
|
542
|
-
* method: 'POST',
|
|
543
|
-
* headers: { 'content-type': 'application/json' },
|
|
544
|
-
* body: JSON.stringify({ title: 'My post' }),
|
|
545
|
-
* });
|
|
546
|
-
*
|
|
547
|
-
* const post = await response.json(); // Post
|
|
548
|
-
*/
|
|
215
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchdefaults `fetch.defaults`} */
|
|
549
216
|
defaults: FetchDefaults;
|
|
550
|
-
/**
|
|
551
|
-
* A loosely-typed version of {@link Fetch `fetch`}. This can be useful to make requests with fewer type constraints,
|
|
552
|
-
* such as in {@link onRequest `onRequest`} and {@link onResponse `onResponse`} listeners.
|
|
553
|
-
*
|
|
554
|
-
* @example
|
|
555
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
556
|
-
* import { createFetch } from '@zimic/fetch';
|
|
557
|
-
*
|
|
558
|
-
* interface User {
|
|
559
|
-
* id: string;
|
|
560
|
-
* username: string;
|
|
561
|
-
* }
|
|
562
|
-
*
|
|
563
|
-
* type Schema = HttpSchema<{
|
|
564
|
-
* '/auth/login': {
|
|
565
|
-
* POST: {
|
|
566
|
-
* request: {
|
|
567
|
-
* headers: { 'content-type': 'application/json' };
|
|
568
|
-
* body: { username: string; password: string };
|
|
569
|
-
* };
|
|
570
|
-
* response: {
|
|
571
|
-
* 201: { body: { accessToken: string } };
|
|
572
|
-
* };
|
|
573
|
-
* };
|
|
574
|
-
* };
|
|
575
|
-
*
|
|
576
|
-
* '/auth/refresh': {
|
|
577
|
-
* POST: {
|
|
578
|
-
* response: {
|
|
579
|
-
* 201: { body: { accessToken: string } };
|
|
580
|
-
* };
|
|
581
|
-
* };
|
|
582
|
-
* };
|
|
583
|
-
*
|
|
584
|
-
* '/users': {
|
|
585
|
-
* GET: {
|
|
586
|
-
* request: {
|
|
587
|
-
* headers: { authorization: string };
|
|
588
|
-
* };
|
|
589
|
-
* response: {
|
|
590
|
-
* 200: { body: User[] };
|
|
591
|
-
* 401: { body: { message: string } };
|
|
592
|
-
* 403: { body: { message: string } };
|
|
593
|
-
* };
|
|
594
|
-
* };
|
|
595
|
-
* };
|
|
596
|
-
* }>;
|
|
597
|
-
*
|
|
598
|
-
* const fetch = createFetch<Schema>({
|
|
599
|
-
* baseURL,
|
|
600
|
-
*
|
|
601
|
-
* async onResponse(response) {
|
|
602
|
-
* if (response.status === 401) {
|
|
603
|
-
* const body = await response.clone().json();
|
|
604
|
-
*
|
|
605
|
-
* if (body.message === 'Access token expired') {
|
|
606
|
-
* // Refresh the access token
|
|
607
|
-
* const refreshResponse = await this('/auth/refresh', { method: 'POST' });
|
|
608
|
-
* const { accessToken } = await refreshResponse.json();
|
|
609
|
-
*
|
|
610
|
-
* // Clone the original request and update its headers
|
|
611
|
-
* const updatedRequest = response.request.clone();
|
|
612
|
-
* updatedRequest.headers.set('authorization', `Bearer ${accessToken}`);
|
|
613
|
-
*
|
|
614
|
-
* // Retry the original request with the updated headers
|
|
615
|
-
* return this.loose(updatedRequest);
|
|
616
|
-
* }
|
|
617
|
-
* }
|
|
618
|
-
*
|
|
619
|
-
* return response;
|
|
620
|
-
* },
|
|
621
|
-
* });
|
|
622
|
-
*
|
|
623
|
-
* // Authenticate to your service before requests
|
|
624
|
-
* const loginRequest = await fetch('/auth/login', {
|
|
625
|
-
* method: 'POST',
|
|
626
|
-
* headers: { 'content-type': 'application/json' },
|
|
627
|
-
* body: JSON.stringify({ username: 'me', password: 'password' }),
|
|
628
|
-
* });
|
|
629
|
-
* const { accessToken } = await loginRequest.json();
|
|
630
|
-
*
|
|
631
|
-
* // Set the authorization header for all requests
|
|
632
|
-
* fetch.defaults.headers.authorization = `Bearer ${accessToken}`;
|
|
633
|
-
*
|
|
634
|
-
* const request = await fetch('/users', {
|
|
635
|
-
* method: 'GET',
|
|
636
|
-
* searchParams: { query: 'u' },
|
|
637
|
-
* });
|
|
638
|
-
*
|
|
639
|
-
* const users = await request.json(); // User[]
|
|
640
|
-
*
|
|
641
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided,
|
|
642
|
-
* it is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a
|
|
643
|
-
* request is provided, it is used as is.
|
|
644
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required
|
|
645
|
-
* and should contain at least the method of the request. If the first argument is a {@link FetchRequest request},
|
|
646
|
-
* this argument is optional.
|
|
647
|
-
* @returns A promise that resolves to the response to the request.
|
|
648
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchloose `fetch.loose` API reference}
|
|
649
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Fetch_API}
|
|
650
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
651
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit}
|
|
652
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
653
|
-
*/
|
|
217
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchloose `fetch.loose`} */
|
|
654
218
|
loose: Fetch.Loose;
|
|
655
|
-
/**
|
|
656
|
-
* A constructor for creating
|
|
657
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest-1 `FetchRequest`}, closely compatible with
|
|
658
|
-
* the native {@link https://developer.mozilla.org/docs/Web/API/Request Request} constructor.
|
|
659
|
-
*
|
|
660
|
-
* @example
|
|
661
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
662
|
-
* import { createFetch } from '@zimic/fetch';
|
|
663
|
-
*
|
|
664
|
-
* interface User {
|
|
665
|
-
* id: string;
|
|
666
|
-
* username: string;
|
|
667
|
-
* }
|
|
668
|
-
*
|
|
669
|
-
* type Schema = HttpSchema<{
|
|
670
|
-
* '/users': {
|
|
671
|
-
* POST: {
|
|
672
|
-
* request: {
|
|
673
|
-
* headers: { 'content-type': 'application/json' };
|
|
674
|
-
* body: { username: string };
|
|
675
|
-
* };
|
|
676
|
-
* response: {
|
|
677
|
-
* 201: { body: User };
|
|
678
|
-
* };
|
|
679
|
-
* };
|
|
680
|
-
* };
|
|
681
|
-
* }>;
|
|
682
|
-
*
|
|
683
|
-
* const fetch = createFetch<Schema>({
|
|
684
|
-
* baseURL: 'http://localhost:3000',
|
|
685
|
-
* });
|
|
686
|
-
*
|
|
687
|
-
* const request = new fetch.Request('/users', {
|
|
688
|
-
* method: 'POST',
|
|
689
|
-
* headers: { 'content-type': 'application/json' },
|
|
690
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
691
|
-
* });
|
|
692
|
-
*
|
|
693
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
694
|
-
* console.log(request.path); // '/users'
|
|
695
|
-
*
|
|
696
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest-1 `FetchRequest`}
|
|
697
|
-
*/
|
|
219
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchrequest `fetch.Request`} */
|
|
698
220
|
Request: FetchRequestConstructor<Schema>;
|
|
699
|
-
/**
|
|
700
|
-
* A type guard that checks if a request is a {@link FetchRequest}, was created by the fetch instance, and has a
|
|
701
|
-
* specific method and path. This is useful to narrow down the type of a request before using it.
|
|
702
|
-
*
|
|
703
|
-
* @example
|
|
704
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
705
|
-
* import { createFetch } from '@zimic/fetch';
|
|
706
|
-
*
|
|
707
|
-
* interface User {
|
|
708
|
-
* id: string;
|
|
709
|
-
* username: string;
|
|
710
|
-
* }
|
|
711
|
-
*
|
|
712
|
-
* type Schema = HttpSchema<{
|
|
713
|
-
* '/users': {
|
|
714
|
-
* POST: {
|
|
715
|
-
* request: {
|
|
716
|
-
* headers: { 'content-type': 'application/json' };
|
|
717
|
-
* body: { username: string };
|
|
718
|
-
* };
|
|
719
|
-
* response: {
|
|
720
|
-
* 201: { body: User };
|
|
721
|
-
* };
|
|
722
|
-
* };
|
|
723
|
-
* };
|
|
724
|
-
* }>;
|
|
725
|
-
*
|
|
726
|
-
* const fetch = createFetch<Schema>({
|
|
727
|
-
* baseURL: 'http://localhost:3000',
|
|
728
|
-
* });
|
|
729
|
-
*
|
|
730
|
-
* const request = new fetch.Request('/users', {
|
|
731
|
-
* method: 'POST',
|
|
732
|
-
* headers: { 'content-type': 'application/json' },
|
|
733
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
734
|
-
* });
|
|
735
|
-
*
|
|
736
|
-
* if (fetch.isRequest(request, 'POST', '/users')) {
|
|
737
|
-
* // request is a FetchRequest<Schema, 'POST', '/users'>
|
|
738
|
-
*
|
|
739
|
-
* const contentType = request.headers.get('content-type'); // 'application/json'
|
|
740
|
-
* const body = await request.json(); // { username: string }
|
|
741
|
-
* }
|
|
742
|
-
*
|
|
743
|
-
* @param request The request to check.
|
|
744
|
-
* @param method The method to check.
|
|
745
|
-
* @param path The path to check.
|
|
746
|
-
* @returns `true` if the request was created by the fetch instance and has the specified method and path; `false`
|
|
747
|
-
* otherwise.
|
|
748
|
-
*/
|
|
221
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isrequest `fetch.isRequest`} */
|
|
749
222
|
isRequest: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(request: unknown, method: Method, path: Path) => request is FetchRequest<Schema, Method, Path>;
|
|
750
|
-
/**
|
|
751
|
-
* A type guard that checks if a response is a {@link FetchResponse}, was received by the fetch instance, and has a
|
|
752
|
-
* specific method and path. This is useful to narrow down the type of a response before using it.
|
|
753
|
-
*
|
|
754
|
-
* @example
|
|
755
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
756
|
-
* import { createFetch } from '@zimic/fetch';
|
|
757
|
-
*
|
|
758
|
-
* interface User {
|
|
759
|
-
* id: string;
|
|
760
|
-
* username: string;
|
|
761
|
-
* }
|
|
762
|
-
*
|
|
763
|
-
* type Schema = HttpSchema<{
|
|
764
|
-
* '/users': {
|
|
765
|
-
* GET: {
|
|
766
|
-
* request: {
|
|
767
|
-
* searchParams: { query?: string };
|
|
768
|
-
* };
|
|
769
|
-
* response: {
|
|
770
|
-
* 200: { body: User[] };
|
|
771
|
-
* };
|
|
772
|
-
* };
|
|
773
|
-
* };
|
|
774
|
-
* }>;
|
|
775
|
-
*
|
|
776
|
-
* const fetch = createFetch<Schema>({
|
|
777
|
-
* baseURL: 'http://localhost:3000',
|
|
778
|
-
* });
|
|
779
|
-
*
|
|
780
|
-
* const response = await fetch('/users', {
|
|
781
|
-
* method: 'GET',
|
|
782
|
-
* searchParams: { query: 'u' },
|
|
783
|
-
* });
|
|
784
|
-
*
|
|
785
|
-
* if (fetch.isResponse(response, 'GET', '/users')) {
|
|
786
|
-
* // response is a FetchResponse<Schema, 'GET', '/users'>
|
|
787
|
-
*
|
|
788
|
-
* const users = await response.json(); // User[]
|
|
789
|
-
* }
|
|
790
|
-
*
|
|
791
|
-
* @param response The response to check.
|
|
792
|
-
* @param method The method to check.
|
|
793
|
-
* @param path The path to check.
|
|
794
|
-
* @returns `true` if the response was received by the fetch instance and has the specified method and path; `false`
|
|
795
|
-
* otherwise.
|
|
796
|
-
*/
|
|
223
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isresponseerror `fetch.isResponse`} */
|
|
797
224
|
isResponse: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(response: unknown, method: Method, path: Path) => response is FetchResponse<Schema, Method, Path>;
|
|
798
|
-
/**
|
|
799
|
-
* A type guard that checks if an error is a {@link FetchResponseError} related to a {@link FetchResponse response}
|
|
800
|
-
* received by the fetch instance with a specific method and path. This is useful to narrow down the type of an error
|
|
801
|
-
* before handling it.
|
|
802
|
-
*
|
|
803
|
-
* @example
|
|
804
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
805
|
-
* import { createFetch } from '@zimic/fetch';
|
|
806
|
-
*
|
|
807
|
-
* interface User {
|
|
808
|
-
* id: string;
|
|
809
|
-
* username: string;
|
|
810
|
-
* }
|
|
811
|
-
*
|
|
812
|
-
* type Schema = HttpSchema<{
|
|
813
|
-
* '/users': {
|
|
814
|
-
* GET: {
|
|
815
|
-
* request: {
|
|
816
|
-
* searchParams: { query?: string };
|
|
817
|
-
* };
|
|
818
|
-
* response: {
|
|
819
|
-
* 200: { body: User[] };
|
|
820
|
-
* 400: { body: { message: string } };
|
|
821
|
-
* 500: { body: { message: string } };
|
|
822
|
-
* };
|
|
823
|
-
* };
|
|
824
|
-
* };
|
|
825
|
-
* }>;
|
|
826
|
-
*
|
|
827
|
-
* const fetch = createFetch<Schema>({
|
|
828
|
-
* baseURL: 'http://localhost:3000',
|
|
829
|
-
* });
|
|
830
|
-
*
|
|
831
|
-
* try {
|
|
832
|
-
* const response = await fetch('/users', {
|
|
833
|
-
* method: 'GET',
|
|
834
|
-
* searchParams: { query: 'u' },
|
|
835
|
-
* });
|
|
836
|
-
*
|
|
837
|
-
* if (!response.ok) {
|
|
838
|
-
* throw response.error; // FetchResponseError<Schema, 'GET', '/users'>
|
|
839
|
-
* }
|
|
840
|
-
* } catch (error) {
|
|
841
|
-
* if (fetch.isResponseError(error, 'GET', '/users')) {
|
|
842
|
-
* // error is a FetchResponseError<Schema, 'GET', '/users'>
|
|
843
|
-
*
|
|
844
|
-
* const status = error.response.status; // 400 | 500
|
|
845
|
-
* const { message } = await error.response.json(); // { message: string }
|
|
846
|
-
*
|
|
847
|
-
* console.error('Could not fetch users:', { status, message });
|
|
848
|
-
* }
|
|
849
|
-
* }
|
|
850
|
-
*
|
|
851
|
-
* @param error The error to check.
|
|
852
|
-
* @param method The method to check.
|
|
853
|
-
* @param path The path to check.
|
|
854
|
-
* @returns `true` if the error is a response error received by the fetch instance and has the specified method and
|
|
855
|
-
* path; `false` otherwise.
|
|
856
|
-
*/
|
|
225
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isresponseerror `fetch.isResponseError`} */
|
|
857
226
|
isResponseError: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(error: unknown, method: Method, path: Path) => error is FetchResponseError<Schema, Method, Path>;
|
|
858
227
|
}
|
|
859
228
|
declare namespace Fetch {
|
|
860
229
|
/** A loosely-typed version of {@link Fetch `fetch`}. This can be useful to make requests with fewer type constraints, */
|
|
861
230
|
type Loose = (input: string | URL | FetchRequest.Loose, init?: FetchRequestInit.Loose) => Promise<FetchResponse.Loose>;
|
|
862
231
|
}
|
|
863
|
-
/**
|
|
864
|
-
* The options to create a {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch fetch instance}.
|
|
865
|
-
*
|
|
866
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch `createFetch(options)` API reference}
|
|
867
|
-
*/
|
|
232
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch `createFetch` API reference} */
|
|
868
233
|
interface FetchOptions<Schema extends HttpSchema> extends Omit<FetchRequestInit.Defaults, 'method'> {
|
|
869
|
-
/**
|
|
870
|
-
* A listener function that is called for each request. It can modify the requests before they are sent.
|
|
871
|
-
*
|
|
872
|
-
* @example
|
|
873
|
-
* import { createFetch } from '@zimic/fetch';
|
|
874
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
875
|
-
*
|
|
876
|
-
* interface User {
|
|
877
|
-
* id: string;
|
|
878
|
-
* username: string;
|
|
879
|
-
* }
|
|
880
|
-
*
|
|
881
|
-
* type Schema = HttpSchema<{
|
|
882
|
-
* '/users': {
|
|
883
|
-
* GET: {
|
|
884
|
-
* request: {
|
|
885
|
-
* searchParams: { page?: number; limit?: number };
|
|
886
|
-
* };
|
|
887
|
-
* response: {
|
|
888
|
-
* 200: { body: User[] };
|
|
889
|
-
* };
|
|
890
|
-
* };
|
|
891
|
-
* };
|
|
892
|
-
* }>;
|
|
893
|
-
*
|
|
894
|
-
* const fetch = createFetch<Schema>({
|
|
895
|
-
* baseURL: 'http://localhost:80',
|
|
896
|
-
*
|
|
897
|
-
* onRequest(request) {
|
|
898
|
-
* if (this.isRequest(request, 'GET', '/users')) {
|
|
899
|
-
* const url = new URL(request.url);
|
|
900
|
-
* url.searchParams.append('limit', '10');
|
|
901
|
-
*
|
|
902
|
-
* const updatedRequest = new Request(url, request);
|
|
903
|
-
* return updatedRequest;
|
|
904
|
-
* }
|
|
905
|
-
*
|
|
906
|
-
* return request;
|
|
907
|
-
* },
|
|
908
|
-
* });
|
|
909
|
-
*
|
|
910
|
-
* @param request The original request.
|
|
911
|
-
* @returns The request to be sent. It can be the original request or a modified version of it.
|
|
912
|
-
* @this {Fetch<Schema>} The fetch instance that is sending the request.
|
|
913
|
-
*/
|
|
234
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch#onrequest `createFetch.onRequest`} API reference */
|
|
914
235
|
onRequest?: (this: Fetch<Schema>, request: FetchRequest.Loose) => PossiblePromise<Request>;
|
|
915
|
-
/**
|
|
916
|
-
* A listener function that is called after each response is received. It can modify the responses before they are
|
|
917
|
-
* returned to the fetch caller.
|
|
918
|
-
*
|
|
919
|
-
* @example
|
|
920
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
921
|
-
* import { createFetch } from '@zimic/fetch';
|
|
922
|
-
*
|
|
923
|
-
* interface User {
|
|
924
|
-
* id: string;
|
|
925
|
-
* username: string;
|
|
926
|
-
* }
|
|
927
|
-
*
|
|
928
|
-
* type Schema = HttpSchema<{
|
|
929
|
-
* '/users': {
|
|
930
|
-
* GET: {
|
|
931
|
-
* response: {
|
|
932
|
-
* 200: {
|
|
933
|
-
* headers: { 'content-encoding'?: string };
|
|
934
|
-
* body: User[];
|
|
935
|
-
* };
|
|
936
|
-
* };
|
|
937
|
-
* };
|
|
938
|
-
* };
|
|
939
|
-
* }>;
|
|
940
|
-
*
|
|
941
|
-
* const fetch = createFetch<Schema>({
|
|
942
|
-
* baseURL: 'http://localhost:80',
|
|
943
|
-
*
|
|
944
|
-
* onResponse(response) {
|
|
945
|
-
* if (this.isResponse(response, 'GET', '/users')) {
|
|
946
|
-
* console.log(response.headers.get('content-encoding'));
|
|
947
|
-
* }
|
|
948
|
-
* return response;
|
|
949
|
-
* },
|
|
950
|
-
* });
|
|
951
|
-
*
|
|
952
|
-
* @param response The original response.
|
|
953
|
-
* @returns The response to be returned.
|
|
954
|
-
* @this {Fetch<Schema>} The fetch instance that received the response.
|
|
955
|
-
*/
|
|
236
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch#onresponse `createFetch.onResponse`} API reference */
|
|
956
237
|
onResponse?: (this: Fetch<Schema>, response: FetchResponse.Loose) => PossiblePromise<Response>;
|
|
957
238
|
}
|
|
958
239
|
/**
|
|
959
240
|
* The default options for each request sent by the fetch instance.
|
|
960
241
|
*
|
|
961
|
-
* @see {@link https://
|
|
242
|
+
* @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchdefaults `fetch.defaults` API reference}
|
|
962
243
|
*/
|
|
963
244
|
type FetchDefaults = RequiredByKey<FetchRequestInit.Defaults, 'headers' | 'searchParams'>;
|
|
964
245
|
/**
|
|
965
|
-
* Infers the schema of a {@link https://
|
|
246
|
+
* Infers the schema of a {@link https://zimic.dev/docs/fetch/api/fetch fetch instance}.
|
|
966
247
|
*
|
|
967
248
|
* @example
|
|
968
249
|
* import { type HttpSchema } from '@zimic/http';
|
|
@@ -986,63 +267,11 @@ type FetchDefaults = RequiredByKey<FetchRequestInit.Defaults, 'headers' | 'searc
|
|
|
986
267
|
* // };
|
|
987
268
|
* // };
|
|
988
269
|
*
|
|
989
|
-
* @see {@link https://
|
|
270
|
+
* @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference}
|
|
990
271
|
*/
|
|
991
272
|
type InferFetchSchema<FetchInstance> = FetchInstance extends Fetch<infer Schema> ? Schema : never;
|
|
992
273
|
|
|
993
|
-
/**
|
|
994
|
-
* Creates a {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch fetch instance} typed with an HTTP
|
|
995
|
-
* schema, closely compatible with the {@link https://developer.mozilla.org/docs/Web/API/Fetch_API native Fetch API}. All
|
|
996
|
-
* requests and responses are typed by default with the schema, including methods, paths, status codes, parameters, and
|
|
997
|
-
* bodies.
|
|
998
|
-
*
|
|
999
|
-
* Requests sent by the fetch instance have their URL automatically prefixed with the base URL of the instance.
|
|
1000
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch.defaults Default options} are also applied to the
|
|
1001
|
-
* requests, if provided.
|
|
1002
|
-
*
|
|
1003
|
-
* @example
|
|
1004
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
1005
|
-
* import { createFetch } from '@zimic/fetch';
|
|
1006
|
-
*
|
|
1007
|
-
* interface User {
|
|
1008
|
-
* id: string;
|
|
1009
|
-
* username: string;
|
|
1010
|
-
* }
|
|
1011
|
-
*
|
|
1012
|
-
* type Schema = HttpSchema<{
|
|
1013
|
-
* '/users': {
|
|
1014
|
-
* POST: {
|
|
1015
|
-
* request: {
|
|
1016
|
-
* headers: { 'content-type': 'application/json' };
|
|
1017
|
-
* body: { username: string };
|
|
1018
|
-
* };
|
|
1019
|
-
* response: {
|
|
1020
|
-
* 201: { body: User };
|
|
1021
|
-
* };
|
|
1022
|
-
* };
|
|
1023
|
-
*
|
|
1024
|
-
* GET: {
|
|
1025
|
-
* request: {
|
|
1026
|
-
* searchParams: {
|
|
1027
|
-
* query?: string;
|
|
1028
|
-
* page?: number;
|
|
1029
|
-
* limit?: number;
|
|
1030
|
-
* };
|
|
1031
|
-
* };
|
|
1032
|
-
* response: {
|
|
1033
|
-
* 200: { body: User[] };
|
|
1034
|
-
* };
|
|
1035
|
-
* };
|
|
1036
|
-
* };
|
|
1037
|
-
* }>;
|
|
1038
|
-
*
|
|
1039
|
-
* const fetch = createFetch<Schema>({
|
|
1040
|
-
* baseURL: 'http://localhost:3000',
|
|
1041
|
-
* });
|
|
1042
|
-
*
|
|
1043
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch `createFetch(options)` API reference}
|
|
1044
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
1045
|
-
*/
|
|
274
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch `createFetch` API reference} */
|
|
1046
275
|
declare function createFetch<Schema extends HttpSchema>(options: FetchOptions<Schema>): Fetch<Schema>;
|
|
1047
276
|
|
|
1048
277
|
export { Fetch, type FetchDefaults, type FetchInput, type FetchOptions, FetchRequest, type FetchRequestConstructor, FetchRequestInit, type FetchRequestObject, FetchResponse, FetchResponseError, type FetchResponseErrorObject, FetchResponseErrorObjectOptions, type FetchResponseObject, type FetchResponsePerStatusCode, type InferFetchSchema, type JSONStringified, createFetch };
|