@zimic/fetch 0.4.1-canary.0 → 0.4.1-canary.2
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 +6 -6
- 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
|
@@ -69,13 +69,7 @@ type FetchRequestInitPerPath<MethodSchema extends HttpMethodSchema> = FetchReque
|
|
|
69
69
|
FetchRequestInitWithSearchParams<HttpRequestSearchParamsSchema<MethodSchema>> &
|
|
70
70
|
FetchRequestInitWithBody<FetchRequestBodySchema<Default<MethodSchema['request']>>>;
|
|
71
71
|
|
|
72
|
-
/**
|
|
73
|
-
* The options to create a {@link FetchRequest} instance, compatible with
|
|
74
|
-
* {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}.
|
|
75
|
-
*
|
|
76
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
77
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}
|
|
78
|
-
*/
|
|
72
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch `fetch` API reference} */
|
|
79
73
|
export type FetchRequestInit<
|
|
80
74
|
Schema extends HttpSchema,
|
|
81
75
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -132,55 +126,7 @@ type FetchResponseStatusCode<
|
|
|
132
126
|
Redirect
|
|
133
127
|
>;
|
|
134
128
|
|
|
135
|
-
/**
|
|
136
|
-
* A request instance typed with an HTTP schema, closely compatible with the
|
|
137
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Request native Request class}.
|
|
138
|
-
*
|
|
139
|
-
* On top of the properties available in native {@link https://developer.mozilla.org/docs/Web/API/Request `Request`}
|
|
140
|
-
* instances, fetch requests have their URL automatically prefixed with the base URL of their fetch instance. Default
|
|
141
|
-
* options are also applied, if present in the fetch instance.
|
|
142
|
-
*
|
|
143
|
-
* The path of the request is extracted from the URL, excluding the base URL, and is available in the `path` property.
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
147
|
-
* import { createFetch } from '@zimic/fetch';
|
|
148
|
-
*
|
|
149
|
-
* interface User {
|
|
150
|
-
* id: string;
|
|
151
|
-
* username: string;
|
|
152
|
-
* }
|
|
153
|
-
*
|
|
154
|
-
* type Schema = HttpSchema<{
|
|
155
|
-
* '/users': {
|
|
156
|
-
* POST: {
|
|
157
|
-
* request: {
|
|
158
|
-
* headers: { 'content-type': 'application/json' };
|
|
159
|
-
* body: { username: string };
|
|
160
|
-
* };
|
|
161
|
-
* response: {
|
|
162
|
-
* 201: { body: User };
|
|
163
|
-
* };
|
|
164
|
-
* };
|
|
165
|
-
* };
|
|
166
|
-
* }>;
|
|
167
|
-
*
|
|
168
|
-
* const fetch = createFetch<Schema>({
|
|
169
|
-
* baseURL: 'http://localhost:3000',
|
|
170
|
-
* });
|
|
171
|
-
*
|
|
172
|
-
* const request = new fetch.Request('/users', {
|
|
173
|
-
* method: 'POST',
|
|
174
|
-
* headers: { 'content-type': 'application/json' },
|
|
175
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
176
|
-
* });
|
|
177
|
-
*
|
|
178
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
179
|
-
* console.log(request.path); // '/users'
|
|
180
|
-
*
|
|
181
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest `FetchRequest` API reference}
|
|
182
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
183
|
-
*/
|
|
129
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-request `FetchRequest` API reference} */
|
|
184
130
|
export interface FetchRequest<
|
|
185
131
|
Schema extends HttpSchema,
|
|
186
132
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -237,12 +183,7 @@ export type FetchRequestObject = Pick<
|
|
|
237
183
|
body?: string | null;
|
|
238
184
|
};
|
|
239
185
|
|
|
240
|
-
/**
|
|
241
|
-
* A {@link FetchResponse `FetchResponse`} instance with a specific status code.
|
|
242
|
-
*
|
|
243
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
244
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
245
|
-
*/
|
|
186
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response `FetchResponse` API reference} */
|
|
246
187
|
export interface FetchResponsePerStatusCode<
|
|
247
188
|
Schema extends HttpSchema,
|
|
248
189
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -267,57 +208,7 @@ export interface FetchResponsePerStatusCode<
|
|
|
267
208
|
: null;
|
|
268
209
|
}
|
|
269
210
|
|
|
270
|
-
/**
|
|
271
|
-
* A response instance typed with an HTTP schema, closely compatible with the
|
|
272
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Response native Response class}.
|
|
273
|
-
*
|
|
274
|
-
* On top of the properties available in native Response instances, fetch responses have a reference to the request that
|
|
275
|
-
* originated them, available in the `request` property.
|
|
276
|
-
*
|
|
277
|
-
* If the response has a failure status code (4XX or 5XX), an error is available in the `error` property.
|
|
278
|
-
*
|
|
279
|
-
* @example
|
|
280
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
281
|
-
* import { createFetch } from '@zimic/fetch';
|
|
282
|
-
*
|
|
283
|
-
* interface User {
|
|
284
|
-
* id: string;
|
|
285
|
-
* username: string;
|
|
286
|
-
* }
|
|
287
|
-
*
|
|
288
|
-
* type Schema = HttpSchema<{
|
|
289
|
-
* '/users/:userId': {
|
|
290
|
-
* GET: {
|
|
291
|
-
* response: {
|
|
292
|
-
* 200: { body: User };
|
|
293
|
-
* 404: { body: { message: string } };
|
|
294
|
-
* };
|
|
295
|
-
* };
|
|
296
|
-
* };
|
|
297
|
-
* }>;
|
|
298
|
-
*
|
|
299
|
-
* const fetch = createFetch<Schema>({
|
|
300
|
-
* baseURL: 'http://localhost:3000',
|
|
301
|
-
* });
|
|
302
|
-
*
|
|
303
|
-
* const response = await fetch(`/users/${userId}`, {
|
|
304
|
-
* method: 'GET',
|
|
305
|
-
* });
|
|
306
|
-
*
|
|
307
|
-
* console.log(response); // FetchResponse<Schema, 'GET', '/users'>
|
|
308
|
-
*
|
|
309
|
-
* if (response.status === 404) {
|
|
310
|
-
* const errorBody = await response.json(); // { message: string }
|
|
311
|
-
* console.error(errorBody.message);
|
|
312
|
-
* return null;
|
|
313
|
-
* } else {
|
|
314
|
-
* const user = await response.json(); // User
|
|
315
|
-
* return user;
|
|
316
|
-
* }
|
|
317
|
-
*
|
|
318
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
319
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
320
|
-
*/
|
|
211
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch-response `FetchResponse` API reference} */
|
|
321
212
|
export type FetchResponse<
|
|
322
213
|
Schema extends HttpSchema,
|
|
323
214
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -369,37 +260,7 @@ export type FetchResponseObject = Pick<
|
|
|
369
260
|
body?: string | null;
|
|
370
261
|
};
|
|
371
262
|
|
|
372
|
-
/**
|
|
373
|
-
* A constructor for {@link FetchRequest} instances, typed with an HTTP schema and compatible with the
|
|
374
|
-
* {@link https://developer.mozilla.org/docs/Web/API/Request Request class constructor}.
|
|
375
|
-
*
|
|
376
|
-
* @example
|
|
377
|
-
* import { type HttpSchema } from '@zimic/http';
|
|
378
|
-
* import { createFetch } from '@zimic/fetch';
|
|
379
|
-
*
|
|
380
|
-
* type Schema = HttpSchema<{
|
|
381
|
-
* // ...
|
|
382
|
-
* }>;
|
|
383
|
-
*
|
|
384
|
-
* const fetch = createFetch<Schema>({
|
|
385
|
-
* baseURL: 'http://localhost:3000',
|
|
386
|
-
* });
|
|
387
|
-
*
|
|
388
|
-
* const request = new fetch.Request('POST', '/users', {
|
|
389
|
-
* body: JSON.stringify({ username: 'me' }),
|
|
390
|
-
* });
|
|
391
|
-
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
392
|
-
*
|
|
393
|
-
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided, it
|
|
394
|
-
* is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a request
|
|
395
|
-
* is provided, it is used as is.
|
|
396
|
-
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required and
|
|
397
|
-
* should contain at least the method of the request. If the first argument is a {@link FetchRequest request}, this
|
|
398
|
-
* argument is optional.
|
|
399
|
-
* @returns A promise that resolves to the response to the request.
|
|
400
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
401
|
-
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
402
|
-
*/
|
|
263
|
+
/** @see {@link https://zimic.dev/docs/fetch/api/fetch#fetchrequest `fetch.Request` API reference} */
|
|
403
264
|
export type FetchRequestConstructor<Schema extends HttpSchema> = new <
|
|
404
265
|
Method extends HttpSchemaMethod<Schema>,
|
|
405
266
|
Path extends HttpSchemaPath.NonLiteral<Schema, Method>,
|