@zimic/fetch 0.1.0-canary.20 → 0.1.0-canary.21
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/LICENSE.md +1 -1
- package/README.md +111 -177
- package/dist/index.d.ts +810 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/client/errors/FetchResponseError.ts +41 -0
- package/src/client/factory.ts +53 -0
- package/src/client/types/json.ts +7 -0
- package/src/client/types/public.ts +544 -0
- package/src/client/types/requests.ts +168 -0
|
@@ -66,13 +66,22 @@ type FetchRequestInitPerPath<RequestSchema extends HttpRequestSchema> = FetchReq
|
|
|
66
66
|
FetchRequestInitWithSearchParams<RequestSchema> &
|
|
67
67
|
FetchRequestInitWithBody<RequestSchema>;
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* The options to create a {@link FetchRequest} instance, compatible with
|
|
71
|
+
* {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}.
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetch `fetch` API reference}
|
|
74
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/RequestInit `RequestInit`}
|
|
75
|
+
*/
|
|
69
76
|
export type FetchRequestInit<
|
|
70
77
|
Schema extends HttpSchema,
|
|
71
78
|
Method extends HttpSchemaMethod<Schema>,
|
|
72
79
|
Path extends HttpSchemaPath<Schema, Method>,
|
|
73
80
|
Redirect extends RequestRedirect = 'follow',
|
|
74
81
|
> = Omit<RequestInit, 'method' | 'headers' | 'body'> & {
|
|
82
|
+
/** The HTTP method of the request. */
|
|
75
83
|
method: Method;
|
|
84
|
+
/** The base URL to prefix the path of the request. */
|
|
76
85
|
baseURL?: string;
|
|
77
86
|
redirect?: Redirect;
|
|
78
87
|
} & (Path extends Path ? FetchRequestInitPerPath<Default<Default<Schema[Path][Method]>['request']>> : never);
|
|
@@ -81,11 +90,15 @@ export namespace FetchRequestInit {
|
|
|
81
90
|
/** The default options for each request sent by a fetch instance. */
|
|
82
91
|
export interface Defaults extends Omit<RequestInit, 'headers'> {
|
|
83
92
|
baseURL: string;
|
|
93
|
+
/** The HTTP method of the request. */
|
|
84
94
|
method?: HttpMethod;
|
|
95
|
+
/** The headers of the request. */
|
|
85
96
|
headers?: HttpHeadersSchema;
|
|
97
|
+
/** The search parameters of the request. */
|
|
86
98
|
searchParams?: HttpSearchParamsSchema;
|
|
87
99
|
}
|
|
88
100
|
|
|
101
|
+
/** A loosely typed version of {@link FetchRequestInit `FetchRequestInit`}. */
|
|
89
102
|
export type Loose = Partial<Defaults>;
|
|
90
103
|
}
|
|
91
104
|
|
|
@@ -122,6 +135,55 @@ type HttpRequestBodySchema<MethodSchema extends HttpMethodSchema> = ReplaceBy<
|
|
|
122
135
|
Blob
|
|
123
136
|
>;
|
|
124
137
|
|
|
138
|
+
/**
|
|
139
|
+
* A request instance typed with an HTTP schema, closely compatible with the
|
|
140
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Request native Request class}.
|
|
141
|
+
*
|
|
142
|
+
* On top of the properties available in native {@link https://developer.mozilla.org/docs/Web/API/Request `Request`}
|
|
143
|
+
* instances, fetch requests have their URL automatically prefixed with the base URL of their fetch instance. Default
|
|
144
|
+
* options are also applied, if present in the fetch instance.
|
|
145
|
+
*
|
|
146
|
+
* The path of the request is extracted from the URL, excluding the base URL, and is available in the `path` property.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
150
|
+
* import { createFetch } from '@zimic/fetch';
|
|
151
|
+
*
|
|
152
|
+
* interface User {
|
|
153
|
+
* id: string;
|
|
154
|
+
* username: string;
|
|
155
|
+
* }
|
|
156
|
+
*
|
|
157
|
+
* type Schema = HttpSchema<{
|
|
158
|
+
* '/users': {
|
|
159
|
+
* POST: {
|
|
160
|
+
* request: {
|
|
161
|
+
* headers: { 'content-type': 'application/json' };
|
|
162
|
+
* body: { username: string };
|
|
163
|
+
* };
|
|
164
|
+
* response: {
|
|
165
|
+
* 201: { body: User };
|
|
166
|
+
* };
|
|
167
|
+
* };
|
|
168
|
+
* };
|
|
169
|
+
* }>;
|
|
170
|
+
*
|
|
171
|
+
* const fetch = createFetch<Schema>({
|
|
172
|
+
* baseURL: 'http://localhost:3000',
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* const request = new fetch.Request('/users', {
|
|
176
|
+
* method: 'POST',
|
|
177
|
+
* headers: { 'content-type': 'application/json' },
|
|
178
|
+
* body: JSON.stringify({ username: 'me' }),
|
|
179
|
+
* });
|
|
180
|
+
*
|
|
181
|
+
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
182
|
+
* console.log(request.path); // '/users'
|
|
183
|
+
*
|
|
184
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchrequest `FetchRequest` API reference}
|
|
185
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
186
|
+
*/
|
|
125
187
|
export interface FetchRequest<
|
|
126
188
|
Schema extends HttpSchema,
|
|
127
189
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -130,14 +192,20 @@ export interface FetchRequest<
|
|
|
130
192
|
HttpRequestBodySchema<Default<Schema[Path][Method]>>,
|
|
131
193
|
HttpRequestHeadersSchema<Default<Schema[Path][Method]>>
|
|
132
194
|
> {
|
|
195
|
+
/** The path of the request, excluding the base URL. */
|
|
133
196
|
path: AllowAnyStringInPathParams<Path>;
|
|
197
|
+
/** The HTTP method of the request. */
|
|
134
198
|
method: Method;
|
|
135
199
|
}
|
|
136
200
|
|
|
137
201
|
export namespace FetchRequest {
|
|
202
|
+
/** A loosely typed version of {@link FetchRequest `FetchRequest`}. */
|
|
138
203
|
export interface Loose extends Request {
|
|
204
|
+
/** The path of the request, excluding the base URL. */
|
|
139
205
|
path: string;
|
|
206
|
+
/** The HTTP method of the request. */
|
|
140
207
|
method: HttpMethod;
|
|
208
|
+
/** Clones the request instance, returning a new instance with the same properties. */
|
|
141
209
|
clone: () => Loose;
|
|
142
210
|
}
|
|
143
211
|
}
|
|
@@ -152,13 +220,71 @@ export interface FetchResponsePerStatusCode<
|
|
|
152
220
|
StatusCode,
|
|
153
221
|
HttpResponseHeadersSchema<Default<Schema[Path][Method]>, StatusCode>
|
|
154
222
|
> {
|
|
223
|
+
/** The request that originated the response. */
|
|
155
224
|
request: FetchRequest<Schema, Method, Path>;
|
|
156
225
|
|
|
226
|
+
/**
|
|
227
|
+
* An error representing a response with a failure status code (4XX or 5XX). It can be thrown to handle the error
|
|
228
|
+
* upper in the call stack.
|
|
229
|
+
*
|
|
230
|
+
* If the response has a success status code (1XX, 2XX or 3XX), this property will be null.
|
|
231
|
+
*/
|
|
157
232
|
error: StatusCode extends HttpStatusCode.ClientError | HttpStatusCode.ServerError
|
|
158
233
|
? FetchResponseError<Schema, Method, Path>
|
|
159
234
|
: null;
|
|
160
235
|
}
|
|
161
236
|
|
|
237
|
+
/**
|
|
238
|
+
* A response instance typed with an HTTP schema, closely compatible with the
|
|
239
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Response native Response class}.
|
|
240
|
+
*
|
|
241
|
+
* On top of the properties available in native Response instances, fetch responses have a reference to the request that
|
|
242
|
+
* originated them, available in the `request` property.
|
|
243
|
+
*
|
|
244
|
+
* If the response has a failure status code (4XX or 5XX), an error is available in the `error` property.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
248
|
+
* import { createFetch } from '@zimic/fetch';
|
|
249
|
+
*
|
|
250
|
+
* interface User {
|
|
251
|
+
* id: string;
|
|
252
|
+
* username: string;
|
|
253
|
+
* }
|
|
254
|
+
*
|
|
255
|
+
* type Schema = HttpSchema<{
|
|
256
|
+
* '/users/:userId': {
|
|
257
|
+
* GET: {
|
|
258
|
+
* response: {
|
|
259
|
+
* 200: { body: User };
|
|
260
|
+
* 404: { body: { message: string } };
|
|
261
|
+
* };
|
|
262
|
+
* };
|
|
263
|
+
* };
|
|
264
|
+
* }>;
|
|
265
|
+
*
|
|
266
|
+
* const fetch = createFetch<Schema>({
|
|
267
|
+
* baseURL: 'http://localhost:3000',
|
|
268
|
+
* });
|
|
269
|
+
*
|
|
270
|
+
* const response = await fetch(`/users/${userId}`, {
|
|
271
|
+
* method: 'GET',
|
|
272
|
+
* });
|
|
273
|
+
*
|
|
274
|
+
* console.log(response); // FetchResponse<Schema, 'GET', '/users'>
|
|
275
|
+
*
|
|
276
|
+
* if (response.status === 404) {
|
|
277
|
+
* const errorBody = await response.json(); // { message: string }
|
|
278
|
+
* console.error(errorBody.message);
|
|
279
|
+
* return null;
|
|
280
|
+
* } else {
|
|
281
|
+
* const user = await response.json(); // User
|
|
282
|
+
* return user;
|
|
283
|
+
* }
|
|
284
|
+
*
|
|
285
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
286
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
287
|
+
*/
|
|
162
288
|
export type FetchResponse<
|
|
163
289
|
Schema extends HttpSchema,
|
|
164
290
|
Method extends HttpSchemaMethod<Schema>,
|
|
@@ -173,13 +299,55 @@ export type FetchResponse<
|
|
|
173
299
|
> = StatusCode extends StatusCode ? FetchResponsePerStatusCode<Schema, Method, Path, StatusCode> : never;
|
|
174
300
|
|
|
175
301
|
export namespace FetchResponse {
|
|
302
|
+
/** A loosely typed version of {@link FetchResponse}. */
|
|
176
303
|
export interface Loose extends Response {
|
|
304
|
+
/** The request that originated the response. */
|
|
177
305
|
request: FetchRequest.Loose;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* An error representing a response with a failure status code (4XX or 5XX). It can be thrown to handle the error
|
|
309
|
+
* upper in the call stack.
|
|
310
|
+
*
|
|
311
|
+
* If the response has a success status code (1XX, 2XX or 3XX), this property will be null.
|
|
312
|
+
*/
|
|
178
313
|
error: AnyFetchRequestError | null;
|
|
314
|
+
|
|
315
|
+
/** Clones the request instance, returning a new instance with the same properties. */
|
|
179
316
|
clone: () => Loose;
|
|
180
317
|
}
|
|
181
318
|
}
|
|
182
319
|
|
|
320
|
+
/**
|
|
321
|
+
* A constructor for {@link FetchRequest} instances, typed with an HTTP schema and compatible with the
|
|
322
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Request Request class constructor}.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
326
|
+
* import { createFetch } from '@zimic/fetch';
|
|
327
|
+
*
|
|
328
|
+
* type Schema = HttpSchema<{
|
|
329
|
+
* // ...
|
|
330
|
+
* }>;
|
|
331
|
+
*
|
|
332
|
+
* const fetch = createFetch<Schema>({
|
|
333
|
+
* baseURL: 'http://localhost:3000',
|
|
334
|
+
* });
|
|
335
|
+
*
|
|
336
|
+
* const request = new fetch.Request('POST', '/users', {
|
|
337
|
+
* body: JSON.stringify({ username: 'me' }),
|
|
338
|
+
* });
|
|
339
|
+
* console.log(request); // FetchRequest<Schema, 'POST', '/users'>
|
|
340
|
+
*
|
|
341
|
+
* @param input The resource to fetch, either a path, a URL, or a {@link FetchRequest request}. If a path is provided, it
|
|
342
|
+
* is automatically prefixed with the base URL of the fetch instance when the request is sent. If a URL or a request
|
|
343
|
+
* is provided, it is used as is.
|
|
344
|
+
* @param init The request options. If a path or a URL is provided as the first argument, this argument is required and
|
|
345
|
+
* should contain at least the method of the request. If the first argument is a {@link FetchRequest request}, this
|
|
346
|
+
* argument is optional.
|
|
347
|
+
* @returns A promise that resolves to the response to the request.
|
|
348
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐fetch#fetchresponse `FetchResponse` API reference}
|
|
349
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Request}
|
|
350
|
+
*/
|
|
183
351
|
export type FetchRequestConstructor<Schema extends HttpSchema> = new <
|
|
184
352
|
Method extends HttpSchemaMethod<Schema>,
|
|
185
353
|
Path extends HttpSchemaPath.NonLiteral<Schema, Method>,
|