@zimic/fetch 0.4.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/README.md +24 -146
- package/dist/index.d.ts +27 -798
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- 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
|
@@ -4,83 +4,14 @@ import { PossiblePromise, RequiredByKey } from '@zimic/utils/types';
|
|
|
4
4
|
import FetchResponseError from '../errors/FetchResponseError';
|
|
5
5
|
import { FetchRequest, FetchRequestConstructor, FetchRequestInit, FetchResponse } from './requests';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* The input to fetch a resource, either a path, a URL, or a {@link FetchRequest request}.
|
|
9
|
-
*
|
|
10
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
11
|
-
*/
|
|
7
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
12
8
|
export type FetchInput<
|
|
13
9
|
Schema extends HttpSchema,
|
|
14
10
|
Method extends HttpSchemaMethod<Schema>,
|
|
15
11
|
Path extends HttpSchemaPath.NonLiteral<Schema, Method>,
|
|
16
12
|
> = Path | URL | FetchRequest<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>>;
|
|
17
13
|
|
|
18
|
-
/**
|
|
19
|
-
* A fetch instance typed with an HTTP schema, closely compatible with the
|
|
20
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Fetch_API native Fetch API}. All requests and responses are typed by
|
|
21
|
-
* default with the schema, including methods, paths, status codes, parameters, and bodies.
|
|
22
|
-
*
|
|
23
|
-
* Requests sent by the fetch instance have their URL automatically prefixed with the base URL of the instance. Default
|
|
24
|
-
* options are also applied to the requests, if present in the instance.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
28
|
-
* import { createFetch } from '@zimic/fetch';
|
|
29
|
-
*
|
|
30
|
-
* interface User {
|
|
31
|
-
* id: string;
|
|
32
|
-
* username: string;
|
|
33
|
-
* }
|
|
34
|
-
*
|
|
35
|
-
* type Schema = HttpSchema<{
|
|
36
|
-
* '/users': {
|
|
37
|
-
* GET: {
|
|
38
|
-
* request: {
|
|
39
|
-
* searchParams: { query?: string };
|
|
40
|
-
* };
|
|
41
|
-
* response: {
|
|
42
|
-
* 200: { body: User[] };
|
|
43
|
-
* 404: { body: { message: string } };
|
|
44
|
-
* 500: { body: { message: string } };
|
|
45
|
-
* };
|
|
46
|
-
* };
|
|
47
|
-
* };
|
|
48
|
-
* }>;
|
|
49
|
-
*
|
|
50
|
-
* const fetch = createFetch<Schema>({
|
|
51
|
-
* baseURL: 'http://localhost:3000',
|
|
52
|
-
* headers: { 'accept-language': 'en' },
|
|
53
|
-
* });
|
|
54
|
-
*
|
|
55
|
-
* const response = await fetch('/users', {
|
|
56
|
-
* method: 'GET',
|
|
57
|
-
* searchParams: { query: 'u' },
|
|
58
|
-
* });
|
|
59
|
-
*
|
|
60
|
-
* if (response.status === 404) {
|
|
61
|
-
* return null; // User not found
|
|
62
|
-
* }
|
|
63
|
-
*
|
|
64
|
-
* if (!response.ok) {
|
|
65
|
-
* throw response.error;
|
|
66
|
-
* }
|
|
67
|
-
*
|
|
68
|
-
* const users = await response.json();
|
|
69
|
-
* return users; // User[]
|
|
70
|
-
*
|
|
71
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided, it
|
|
72
|
-
* is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a request
|
|
73
|
-
* is provided, it is used as is.
|
|
74
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required and
|
|
75
|
-
* should contain at least the method of the request. If the first argument is a {@link FetchRequest request}, this
|
|
76
|
-
* argument is optional.
|
|
77
|
-
* @returns A promise that resolves to the response to the request.
|
|
78
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
79
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Fetch_API}
|
|
80
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
81
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit}
|
|
82
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
83
|
-
*/
|
|
14
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
84
15
|
export interface Fetch<Schema extends HttpSchema> extends Pick<FetchOptions<Schema>, 'onRequest' | 'onResponse'> {
|
|
85
16
|
<
|
|
86
17
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -100,373 +31,30 @@ export interface Fetch<Schema extends HttpSchema> extends Pick<FetchOptions<Sche
|
|
|
100
31
|
init?: FetchRequestInit<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, Redirect>,
|
|
101
32
|
): Promise<FetchResponse<Schema, Method, LiteralHttpSchemaPathFromNonLiteral<Schema, Method, Path>, false, Redirect>>;
|
|
102
33
|
|
|
103
|
-
/**
|
|
104
|
-
* The default options for each request sent by the fetch instance. The available options are the same as the
|
|
105
|
-
* {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`} options, plus `baseURL`.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
109
|
-
* import { createFetch } from '@zimic/fetch';
|
|
110
|
-
*
|
|
111
|
-
* interface Post {
|
|
112
|
-
* id: string;
|
|
113
|
-
* title: string;
|
|
114
|
-
* }
|
|
115
|
-
*
|
|
116
|
-
* type Schema = HttpSchema<{
|
|
117
|
-
* '/posts': {
|
|
118
|
-
* POST: {
|
|
119
|
-
* request: {
|
|
120
|
-
* headers: { 'content-type': 'application/json' };
|
|
121
|
-
* body: { title: string };
|
|
122
|
-
* };
|
|
123
|
-
* response: {
|
|
124
|
-
* 201: { body: Post };
|
|
125
|
-
* };
|
|
126
|
-
* };
|
|
127
|
-
* };
|
|
128
|
-
* }>;
|
|
129
|
-
*
|
|
130
|
-
* const fetch = createFetch<Schema>({
|
|
131
|
-
* baseURL: 'http://localhost:3000',
|
|
132
|
-
* headers: { 'accept-language': 'en' },
|
|
133
|
-
* });
|
|
134
|
-
*
|
|
135
|
-
* // Set the authorization header for all requests
|
|
136
|
-
* const { accessToken } = await authenticate();
|
|
137
|
-
*
|
|
138
|
-
* fetch.defaults.headers.authorization = `Bearer ${accessToken}`;
|
|
139
|
-
* console.log(fetch.defaults.headers);
|
|
140
|
-
*
|
|
141
|
-
* const response = await fetch('/posts', {
|
|
142
|
-
* method: 'POST',
|
|
143
|
-
* headers: { 'content-type': 'application/json' },
|
|
144
|
-
* body: JSON.stringify({ title: 'My post' }),
|
|
145
|
-
* });
|
|
146
|
-
*
|
|
147
|
-
* const post = await response.json(); // Post
|
|
148
|
-
*/
|
|
34
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchdefaults `fetch.defaults`} */
|
|
149
35
|
defaults: FetchDefaults;
|
|
150
36
|
|
|
151
|
-
/**
|
|
152
|
-
* A loosely-typed version of {@link Fetch `fetch`}. This can be useful to make requests with fewer type constraints,
|
|
153
|
-
* such as in {@link onRequest `onRequest`} and {@link onResponse `onResponse`} listeners.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
157
|
-
* import { createFetch } from '@zimic/fetch';
|
|
158
|
-
*
|
|
159
|
-
* interface User {
|
|
160
|
-
* id: string;
|
|
161
|
-
* username: string;
|
|
162
|
-
* }
|
|
163
|
-
*
|
|
164
|
-
* type Schema = HttpSchema<{
|
|
165
|
-
* '/auth/login': {
|
|
166
|
-
* POST: {
|
|
167
|
-
* request: {
|
|
168
|
-
* headers: { 'content-type': 'application/json' };
|
|
169
|
-
* body: { username: string; password: string };
|
|
170
|
-
* };
|
|
171
|
-
* response: {
|
|
172
|
-
* 201: { body: { accessToken: string } };
|
|
173
|
-
* };
|
|
174
|
-
* };
|
|
175
|
-
* };
|
|
176
|
-
*
|
|
177
|
-
* '/auth/refresh': {
|
|
178
|
-
* POST: {
|
|
179
|
-
* response: {
|
|
180
|
-
* 201: { body: { accessToken: string } };
|
|
181
|
-
* };
|
|
182
|
-
* };
|
|
183
|
-
* };
|
|
184
|
-
*
|
|
185
|
-
* '/users': {
|
|
186
|
-
* GET: {
|
|
187
|
-
* request: {
|
|
188
|
-
* headers: { authorization: string };
|
|
189
|
-
* };
|
|
190
|
-
* response: {
|
|
191
|
-
* 200: { body: User[] };
|
|
192
|
-
* 401: { body: { message: string } };
|
|
193
|
-
* 403: { body: { message: string } };
|
|
194
|
-
* };
|
|
195
|
-
* };
|
|
196
|
-
* };
|
|
197
|
-
* }>;
|
|
198
|
-
*
|
|
199
|
-
* const fetch = createFetch<Schema>({
|
|
200
|
-
* baseURL,
|
|
201
|
-
*
|
|
202
|
-
* async onResponse(response) {
|
|
203
|
-
* if (response.status === 401) {
|
|
204
|
-
* const body = await response.clone().json();
|
|
205
|
-
*
|
|
206
|
-
* if (body.message === 'Access token expired') {
|
|
207
|
-
* // Refresh the access token
|
|
208
|
-
* const refreshResponse = await this('/auth/refresh', { method: 'POST' });
|
|
209
|
-
* const { accessToken } = await refreshResponse.json();
|
|
210
|
-
*
|
|
211
|
-
* // Clone the original request and update its headers
|
|
212
|
-
* const updatedRequest = response.request.clone();
|
|
213
|
-
* updatedRequest.headers.set('authorization', `Bearer ${accessToken}`);
|
|
214
|
-
*
|
|
215
|
-
* // Retry the original request with the updated headers
|
|
216
|
-
* return this.loose(updatedRequest);
|
|
217
|
-
* }
|
|
218
|
-
* }
|
|
219
|
-
*
|
|
220
|
-
* return response;
|
|
221
|
-
* },
|
|
222
|
-
* });
|
|
223
|
-
*
|
|
224
|
-
* // Authenticate to your service before requests
|
|
225
|
-
* const loginRequest = await fetch('/auth/login', {
|
|
226
|
-
* method: 'POST',
|
|
227
|
-
* headers: { 'content-type': 'application/json' },
|
|
228
|
-
* body: JSON.stringify({ username: 'me', password: 'password' }),
|
|
229
|
-
* });
|
|
230
|
-
* const { accessToken } = await loginRequest.json();
|
|
231
|
-
*
|
|
232
|
-
* // Set the authorization header for all requests
|
|
233
|
-
* fetch.defaults.headers.authorization = `Bearer ${accessToken}`;
|
|
234
|
-
*
|
|
235
|
-
* const request = await fetch('/users', {
|
|
236
|
-
* method: 'GET',
|
|
237
|
-
* searchParams: { query: 'u' },
|
|
238
|
-
* });
|
|
239
|
-
*
|
|
240
|
-
* const users = await request.json(); // User[]
|
|
241
|
-
*
|
|
242
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided,
|
|
243
|
-
* it is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a
|
|
244
|
-
* request is provided, it is used as is.
|
|
245
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required
|
|
246
|
-
* and should contain at least the method of the request. If the first argument is a {@link FetchRequest request},
|
|
247
|
-
* this argument is optional.
|
|
248
|
-
* @returns A promise that resolves to the response to the request.
|
|
249
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchloose `fetch.loose` API reference}
|
|
250
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Fetch_API}
|
|
251
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
252
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit}
|
|
253
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
254
|
-
*/
|
|
37
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchloose `fetch.loose`} */
|
|
255
38
|
loose: Fetch.Loose;
|
|
256
39
|
|
|
257
|
-
/**
|
|
258
|
-
* A constructor for creating
|
|
259
|
-
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest-1 `FetchRequest`}, closely compatible with
|
|
260
|
-
* the native {@link https://developer.mozilla.org/docs/Web/API/Request Request} constructor.
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
264
|
-
* import { createFetch } from '@zimic/fetch';
|
|
265
|
-
*
|
|
266
|
-
* interface User {
|
|
267
|
-
* id: string;
|
|
268
|
-
* username: string;
|
|
269
|
-
* }
|
|
270
|
-
*
|
|
271
|
-
* type Schema = HttpSchema<{
|
|
272
|
-
* '/users': {
|
|
273
|
-
* POST: {
|
|
274
|
-
* request: {
|
|
275
|
-
* headers: { 'content-type': 'application/json' };
|
|
276
|
-
* body: { username: string };
|
|
277
|
-
* };
|
|
278
|
-
* response: {
|
|
279
|
-
* 201: { body: User };
|
|
280
|
-
* };
|
|
281
|
-
* };
|
|
282
|
-
* };
|
|
283
|
-
* }>;
|
|
284
|
-
*
|
|
285
|
-
* const fetch = createFetch<Schema>({
|
|
286
|
-
* baseURL: 'http://localhost:3000',
|
|
287
|
-
* });
|
|
288
|
-
*
|
|
289
|
-
* const request = new fetch.Request('/users', {
|
|
290
|
-
* method: 'POST',
|
|
291
|
-
* headers: { 'content-type': 'application/json' },
|
|
292
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
293
|
-
* });
|
|
294
|
-
*
|
|
295
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
296
|
-
* console.log(request.path); // '/users'
|
|
297
|
-
*
|
|
298
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest-1 `FetchRequest`}
|
|
299
|
-
*/
|
|
40
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchrequest `fetch.Request`} */
|
|
300
41
|
Request: FetchRequestConstructor<Schema>;
|
|
301
42
|
|
|
302
|
-
/**
|
|
303
|
-
* A type guard that checks if a request is a {@link FetchRequest}, was created by the fetch instance, and has a
|
|
304
|
-
* specific method and path. This is useful to narrow down the type of a request before using it.
|
|
305
|
-
*
|
|
306
|
-
* @example
|
|
307
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
308
|
-
* import { createFetch } from '@zimic/fetch';
|
|
309
|
-
*
|
|
310
|
-
* interface User {
|
|
311
|
-
* id: string;
|
|
312
|
-
* username: string;
|
|
313
|
-
* }
|
|
314
|
-
*
|
|
315
|
-
* type Schema = HttpSchema<{
|
|
316
|
-
* '/users': {
|
|
317
|
-
* POST: {
|
|
318
|
-
* request: {
|
|
319
|
-
* headers: { 'content-type': 'application/json' };
|
|
320
|
-
* body: { username: string };
|
|
321
|
-
* };
|
|
322
|
-
* response: {
|
|
323
|
-
* 201: { body: User };
|
|
324
|
-
* };
|
|
325
|
-
* };
|
|
326
|
-
* };
|
|
327
|
-
* }>;
|
|
328
|
-
*
|
|
329
|
-
* const fetch = createFetch<Schema>({
|
|
330
|
-
* baseURL: 'http://localhost:3000',
|
|
331
|
-
* });
|
|
332
|
-
*
|
|
333
|
-
* const request = new fetch.Request('/users', {
|
|
334
|
-
* method: 'POST',
|
|
335
|
-
* headers: { 'content-type': 'application/json' },
|
|
336
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
337
|
-
* });
|
|
338
|
-
*
|
|
339
|
-
* if (fetch.isRequest(request, 'POST', '/users')) {
|
|
340
|
-
* // request is a FetchRequest<Schema, 'POST', '/users'>
|
|
341
|
-
*
|
|
342
|
-
* const contentType = request.headers.get('content-type'); // 'application/json'
|
|
343
|
-
* const body = await request.json(); // { username: string }
|
|
344
|
-
* }
|
|
345
|
-
*
|
|
346
|
-
* @param request The request to check.
|
|
347
|
-
* @param method The method to check.
|
|
348
|
-
* @param path The path to check.
|
|
349
|
-
* @returns `true` if the request was created by the fetch instance and has the specified method and path; `false`
|
|
350
|
-
* otherwise.
|
|
351
|
-
*/
|
|
43
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isrequest `fetch.isRequest`} */
|
|
352
44
|
isRequest: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(
|
|
353
45
|
request: unknown,
|
|
354
46
|
method: Method,
|
|
355
47
|
path: Path,
|
|
356
48
|
) => request is FetchRequest<Schema, Method, Path>;
|
|
357
49
|
|
|
358
|
-
/**
|
|
359
|
-
* A type guard that checks if a response is a {@link FetchResponse}, was received by the fetch instance, and has a
|
|
360
|
-
* specific method and path. This is useful to narrow down the type of a response before using it.
|
|
361
|
-
*
|
|
362
|
-
* @example
|
|
363
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
364
|
-
* import { createFetch } from '@zimic/fetch';
|
|
365
|
-
*
|
|
366
|
-
* interface User {
|
|
367
|
-
* id: string;
|
|
368
|
-
* username: string;
|
|
369
|
-
* }
|
|
370
|
-
*
|
|
371
|
-
* type Schema = HttpSchema<{
|
|
372
|
-
* '/users': {
|
|
373
|
-
* GET: {
|
|
374
|
-
* request: {
|
|
375
|
-
* searchParams: { query?: string };
|
|
376
|
-
* };
|
|
377
|
-
* response: {
|
|
378
|
-
* 200: { body: User[] };
|
|
379
|
-
* };
|
|
380
|
-
* };
|
|
381
|
-
* };
|
|
382
|
-
* }>;
|
|
383
|
-
*
|
|
384
|
-
* const fetch = createFetch<Schema>({
|
|
385
|
-
* baseURL: 'http://localhost:3000',
|
|
386
|
-
* });
|
|
387
|
-
*
|
|
388
|
-
* const response = await fetch('/users', {
|
|
389
|
-
* method: 'GET',
|
|
390
|
-
* searchParams: { query: 'u' },
|
|
391
|
-
* });
|
|
392
|
-
*
|
|
393
|
-
* if (fetch.isResponse(response, 'GET', '/users')) {
|
|
394
|
-
* // response is a FetchResponse<Schema, 'GET', '/users'>
|
|
395
|
-
*
|
|
396
|
-
* const users = await response.json(); // User[]
|
|
397
|
-
* }
|
|
398
|
-
*
|
|
399
|
-
* @param response The response to check.
|
|
400
|
-
* @param method The method to check.
|
|
401
|
-
* @param path The path to check.
|
|
402
|
-
* @returns `true` if the response was received by the fetch instance and has the specified method and path; `false`
|
|
403
|
-
* otherwise.
|
|
404
|
-
*/
|
|
50
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isresponseerror `fetch.isResponse`} */
|
|
405
51
|
isResponse: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(
|
|
406
52
|
response: unknown,
|
|
407
53
|
method: Method,
|
|
408
54
|
path: Path,
|
|
409
55
|
) => response is FetchResponse<Schema, Method, Path>;
|
|
410
56
|
|
|
411
|
-
/**
|
|
412
|
-
* A type guard that checks if an error is a {@link FetchResponseError} related to a {@link FetchResponse response}
|
|
413
|
-
* received by the fetch instance with a specific method and path. This is useful to narrow down the type of an error
|
|
414
|
-
* before handling it.
|
|
415
|
-
*
|
|
416
|
-
* @example
|
|
417
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
418
|
-
* import { createFetch } from '@zimic/fetch';
|
|
419
|
-
*
|
|
420
|
-
* interface User {
|
|
421
|
-
* id: string;
|
|
422
|
-
* username: string;
|
|
423
|
-
* }
|
|
424
|
-
*
|
|
425
|
-
* type Schema = HttpSchema<{
|
|
426
|
-
* '/users': {
|
|
427
|
-
* GET: {
|
|
428
|
-
* request: {
|
|
429
|
-
* searchParams: { query?: string };
|
|
430
|
-
* };
|
|
431
|
-
* response: {
|
|
432
|
-
* 200: { body: User[] };
|
|
433
|
-
* 400: { body: { message: string } };
|
|
434
|
-
* 500: { body: { message: string } };
|
|
435
|
-
* };
|
|
436
|
-
* };
|
|
437
|
-
* };
|
|
438
|
-
* }>;
|
|
439
|
-
*
|
|
440
|
-
* const fetch = createFetch<Schema>({
|
|
441
|
-
* baseURL: 'http://localhost:3000',
|
|
442
|
-
* });
|
|
443
|
-
*
|
|
444
|
-
* try {
|
|
445
|
-
* const response = await fetch('/users', {
|
|
446
|
-
* method: 'GET',
|
|
447
|
-
* searchParams: { query: 'u' },
|
|
448
|
-
* });
|
|
449
|
-
*
|
|
450
|
-
* if (!response.ok) {
|
|
451
|
-
* throw response.error; // FetchResponseError<Schema, 'GET', '/users'>
|
|
452
|
-
* }
|
|
453
|
-
* } catch (error) {
|
|
454
|
-
* if (fetch.isResponseError(error, 'GET', '/users')) {
|
|
455
|
-
* // error is a FetchResponseError<Schema, 'GET', '/users'>
|
|
456
|
-
*
|
|
457
|
-
* const status = error.response.status; // 400 | 500
|
|
458
|
-
* const { message } = await error.response.json(); // { message: string }
|
|
459
|
-
*
|
|
460
|
-
* console.error('Could not fetch users:', { status, message });
|
|
461
|
-
* }
|
|
462
|
-
* }
|
|
463
|
-
*
|
|
464
|
-
* @param error The error to check.
|
|
465
|
-
* @param method The method to check.
|
|
466
|
-
* @param path The path to check.
|
|
467
|
-
* @returns `true` if the error is a response error received by the fetch instance and has the specified method and
|
|
468
|
-
* path; `false` otherwise.
|
|
469
|
-
*/
|
|
57
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#isresponseerror `fetch.isResponseError`} */
|
|
470
58
|
isResponseError: <Method extends HttpSchemaMethod<Schema>, Path extends HttpSchemaPath.Literal<Schema, Method>>(
|
|
471
59
|
error: unknown,
|
|
472
60
|
method: Method,
|
|
@@ -482,112 +70,24 @@ export namespace Fetch {
|
|
|
482
70
|
) => Promise<FetchResponse.Loose>;
|
|
483
71
|
}
|
|
484
72
|
|
|
485
|
-
/**
|
|
486
|
-
* The options to create a {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch fetch instance}.
|
|
487
|
-
*
|
|
488
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#createfetch `createFetch(options)` API reference}
|
|
489
|
-
*/
|
|
73
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch `createFetch` API reference} */
|
|
490
74
|
export interface FetchOptions<Schema extends HttpSchema> extends Omit<FetchRequestInit.Defaults, 'method'> {
|
|
491
|
-
/**
|
|
492
|
-
* A listener function that is called for each request. It can modify the requests before they are sent.
|
|
493
|
-
*
|
|
494
|
-
* @example
|
|
495
|
-
* import { createFetch } from '@zimic/fetch';
|
|
496
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
497
|
-
*
|
|
498
|
-
* interface User {
|
|
499
|
-
* id: string;
|
|
500
|
-
* username: string;
|
|
501
|
-
* }
|
|
502
|
-
*
|
|
503
|
-
* type Schema = HttpSchema<{
|
|
504
|
-
* '/users': {
|
|
505
|
-
* GET: {
|
|
506
|
-
* request: {
|
|
507
|
-
* searchParams: { page?: number; limit?: number };
|
|
508
|
-
* };
|
|
509
|
-
* response: {
|
|
510
|
-
* 200: { body: User[] };
|
|
511
|
-
* };
|
|
512
|
-
* };
|
|
513
|
-
* };
|
|
514
|
-
* }>;
|
|
515
|
-
*
|
|
516
|
-
* const fetch = createFetch<Schema>({
|
|
517
|
-
* baseURL: 'http://localhost:80',
|
|
518
|
-
*
|
|
519
|
-
* onRequest(request) {
|
|
520
|
-
* if (this.isRequest(request, 'GET', '/users')) {
|
|
521
|
-
* const url = new URL(request.url);
|
|
522
|
-
* url.searchParams.append('limit', '10');
|
|
523
|
-
*
|
|
524
|
-
* const updatedRequest = new Request(url, request);
|
|
525
|
-
* return updatedRequest;
|
|
526
|
-
* }
|
|
527
|
-
*
|
|
528
|
-
* return request;
|
|
529
|
-
* },
|
|
530
|
-
* });
|
|
531
|
-
*
|
|
532
|
-
* @param request The original request.
|
|
533
|
-
* @returns The request to be sent. It can be the original request or a modified version of it.
|
|
534
|
-
* @this {Fetch<Schema>} The fetch instance that is sending the request.
|
|
535
|
-
*/
|
|
75
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch#onrequest `createFetch.onRequest`} API reference */
|
|
536
76
|
onRequest?: (this: Fetch<Schema>, request: FetchRequest.Loose) => PossiblePromise<Request>;
|
|
537
77
|
|
|
538
|
-
/**
|
|
539
|
-
* A listener function that is called after each response is received. It can modify the responses before they are
|
|
540
|
-
* returned to the fetch caller.
|
|
541
|
-
*
|
|
542
|
-
* @example
|
|
543
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
544
|
-
* import { createFetch } from '@zimic/fetch';
|
|
545
|
-
*
|
|
546
|
-
* interface User {
|
|
547
|
-
* id: string;
|
|
548
|
-
* username: string;
|
|
549
|
-
* }
|
|
550
|
-
*
|
|
551
|
-
* type Schema = HttpSchema<{
|
|
552
|
-
* '/users': {
|
|
553
|
-
* GET: {
|
|
554
|
-
* response: {
|
|
555
|
-
* 200: {
|
|
556
|
-
* headers: { 'content-encoding'?: string };
|
|
557
|
-
* body: User[];
|
|
558
|
-
* };
|
|
559
|
-
* };
|
|
560
|
-
* };
|
|
561
|
-
* };
|
|
562
|
-
* }>;
|
|
563
|
-
*
|
|
564
|
-
* const fetch = createFetch<Schema>({
|
|
565
|
-
* baseURL: 'http://localhost:80',
|
|
566
|
-
*
|
|
567
|
-
* onResponse(response) {
|
|
568
|
-
* if (this.isResponse(response, 'GET', '/users')) {
|
|
569
|
-
* console.log(response.headers.get('content-encoding'));
|
|
570
|
-
* }
|
|
571
|
-
* return response;
|
|
572
|
-
* },
|
|
573
|
-
* });
|
|
574
|
-
*
|
|
575
|
-
* @param response The original response.
|
|
576
|
-
* @returns The response to be returned.
|
|
577
|
-
* @this {Fetch<Schema>} The fetch instance that received the response.
|
|
578
|
-
*/
|
|
78
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/create-fetch#onresponse `createFetch.onResponse`} API reference */
|
|
579
79
|
onResponse?: (this: Fetch<Schema>, response: FetchResponse.Loose) => PossiblePromise<Response>;
|
|
580
80
|
}
|
|
581
81
|
|
|
582
82
|
/**
|
|
583
83
|
* The default options for each request sent by the fetch instance.
|
|
584
84
|
*
|
|
585
|
-
* @see {@link https://
|
|
85
|
+
* @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchdefaults `fetch.defaults` API reference}
|
|
586
86
|
*/
|
|
587
87
|
export type FetchDefaults = RequiredByKey<FetchRequestInit.Defaults, 'headers' | 'searchParams'>;
|
|
588
88
|
|
|
589
89
|
/**
|
|
590
|
-
* Infers the schema of a {@link https://
|
|
90
|
+
* Infers the schema of a {@link https://zimic.dev/docs/fetch/api/fetch fetch instance}.
|
|
591
91
|
*
|
|
592
92
|
* @example
|
|
593
93
|
* import { type HttpSchema } from '@zimic/http';
|
|
@@ -611,6 +111,6 @@ export type FetchDefaults = RequiredByKey<FetchRequestInit.Defaults, 'headers' |
|
|
|
611
111
|
* // };
|
|
612
112
|
* // };
|
|
613
113
|
*
|
|
614
|
-
* @see {@link https://
|
|
114
|
+
* @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference}
|
|
615
115
|
*/
|
|
616
116
|
export type InferFetchSchema<FetchInstance> = FetchInstance extends Fetch<infer Schema> ? Schema : never;
|