@zimic/http 0.3.2 → 0.4.0-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.
@@ -1,24 +1,18 @@
1
- import { IfAny, UnionToIntersection, UnionHasMoreThanOneType, Prettify, NonEmptyArray } from '@zimic/utils/types';
2
-
3
- import { HttpFormDataSchema, HttpFormDataSerialized } from '../formData/types';
4
- import { HttpHeadersSchema, HttpHeadersSerialized } from '../headers/types';
5
- import { HttpPathParamsSchema, HttpPathParamsSerialized } from '../pathParams/types';
6
- import { HttpSearchParamsSchema, HttpSearchParamsSerialized } from '../searchParams/types';
1
+ import {
2
+ IfAny,
3
+ UnionToIntersection,
4
+ UnionHasMoreThanOneType,
5
+ Prettify,
6
+ NonEmptyArray,
7
+ Branded,
8
+ } from '@zimic/utils/types';
9
+
10
+ import { HttpFormDataSchema } from '../formData/types';
11
+ import { HttpHeadersSchema } from '../headers/types';
12
+ import { HttpPathParamsSchema } from '../pathParams/types';
13
+ import { HttpSearchParamsSchema } from '../searchParams/types';
7
14
  import { HttpBody } from './requests';
8
15
 
9
- export const HTTP_METHODS_WITH_REQUEST_BODY = Object.freeze(['POST', 'PUT', 'PATCH', 'DELETE'] as const);
10
- export type HttpMethodWithRequestBody = (typeof HTTP_METHODS_WITH_REQUEST_BODY)[number];
11
-
12
- export const HTTP_METHODS_WITH_RESPONSE_BODY = Object.freeze([
13
- 'GET',
14
- 'POST',
15
- 'PUT',
16
- 'PATCH',
17
- 'DELETE',
18
- 'OPTIONS',
19
- ] as const);
20
- export type HttpMethodWithResponseBody = (typeof HTTP_METHODS_WITH_RESPONSE_BODY)[number];
21
-
22
16
  export const HTTP_METHODS = Object.freeze(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] as const);
23
17
  /**
24
18
  * A type representing the currently supported
@@ -37,19 +31,6 @@ export interface HttpRequestSchema {
37
31
  body?: HttpBody.Loose;
38
32
  }
39
33
 
40
- export namespace HttpRequestSchema {
41
- /** Convert an HTTP request schema to be strictly typed. */
42
- export type AsStrict<Schema> = {
43
- [Key in keyof Schema]: Key extends 'headers'
44
- ? HttpHeadersSerialized<Schema[Key]>
45
- : Key extends 'searchParams'
46
- ? HttpSearchParamsSerialized<Schema[Key]>
47
- : Key extends 'body'
48
- ? HttpBody.AsStrict<Schema[Key]>
49
- : Schema[Key];
50
- };
51
- }
52
-
53
34
  /**
54
35
  * A schema representing the structure of an HTTP response.
55
36
  *
@@ -60,16 +41,6 @@ export interface HttpResponseSchema {
60
41
  body?: HttpBody.Loose;
61
42
  }
62
43
 
63
- export namespace HttpResponseSchema {
64
- /** A schema representing the structure of an HTTP response with no body. */
65
- export interface NoBody extends Omit<HttpResponseSchema, 'body'> {
66
- body?: null;
67
- }
68
-
69
- /** Convert an HTTP response schema to be strictly typed. */
70
- export type AsStrict<Schema> = HttpRequestSchema.AsStrict<Schema>;
71
- }
72
-
73
44
  /**
74
45
  * The status codes used in HTTP responses, as defined by
75
46
  * {@link https://httpwg.org/specs/rfc9110.html#overview.of.status.codes RFC-9110}.
@@ -190,27 +161,9 @@ export namespace HttpStatusCode {
190
161
  *
191
162
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas Declaring HTTP interceptor schemas}
192
163
  */
193
- export type HttpResponseSchemaByStatusCode =
194
- HttpResponseSchemaByStatusCode.AsStrict<HttpResponseSchemaByStatusCode.Loose>;
195
-
196
- export namespace HttpResponseSchemaByStatusCode {
197
- /** A loose version of HTTP responses by status code. JSON values are not strictly typed. */
198
- export type Loose = {
199
- [StatusCode in HttpStatusCode]?: HttpResponseSchema;
200
- };
201
-
202
- /** Converts an HTTP response schema by status code to be strictly typed. */
203
- export type AsStrict<Schema> = {
204
- [StatusCode in keyof Schema]: StatusCode extends 204
205
- ? HttpResponseSchema.NoBody
206
- : HttpResponseSchema.AsStrict<Schema[StatusCode]>;
207
- };
208
-
209
- /** A schema representing the structure of HTTP responses by status code with no body. */
210
- export type NoBody = {
211
- [StatusCode in HttpStatusCode]?: HttpResponseSchema.NoBody;
212
- };
213
- }
164
+ export type HttpResponseSchemaByStatusCode = {
165
+ [StatusCode in HttpStatusCode]?: HttpResponseSchema;
166
+ };
214
167
 
215
168
  /**
216
169
  * Extracts the status codes used in a response schema by status code.
@@ -230,50 +183,19 @@ export interface HttpMethodSchema {
230
183
  response?: HttpResponseSchemaByStatusCode;
231
184
  }
232
185
 
233
- export namespace HttpMethodSchema {
234
- /** A schema representing the structure of an HTTP request and response for a given method, having no request body. */
235
- export interface NoRequestBody extends Omit<HttpMethodSchema, 'request'> {
236
- request?: Omit<HttpRequestSchema, 'body'> & { body?: null };
237
- }
238
-
239
- /**
240
- * A schema representing the structure of an HTTP request and response for a given method, having no request and
241
- * response bodies.
242
- */
243
- export interface NoBody extends Omit<NoRequestBody, 'response'> {
244
- response?: HttpResponseSchemaByStatusCode.NoBody;
245
- }
246
-
247
- /** Converts an HTTP method schema to be strictly typed. */
248
- export type AsStrict<Schema> = {
249
- [Key in keyof Schema]: Key extends 'request'
250
- ? HttpRequestSchema.AsStrict<Schema[Key]>
251
- : Key extends 'response'
252
- ? HttpResponseSchemaByStatusCode.AsStrict<Schema[Key]>
253
- : Schema[Key];
254
- };
255
- }
256
-
257
186
  /**
258
187
  * A schema representing the structure of HTTP request and response by method.
259
188
  *
260
189
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas Declaring HTTP interceptor schemas}
261
190
  */
262
191
  export interface HttpMethodsSchema {
263
- GET?: HttpMethodSchema.NoRequestBody;
192
+ GET?: HttpMethodSchema;
264
193
  POST?: HttpMethodSchema;
265
194
  PUT?: HttpMethodSchema;
266
195
  PATCH?: HttpMethodSchema;
267
196
  DELETE?: HttpMethodSchema;
268
- HEAD?: HttpMethodSchema.NoBody;
269
- OPTIONS?: HttpMethodSchema.NoRequestBody;
270
- }
271
-
272
- export namespace HttpMethodsSchema {
273
- /** Converts an HTTP methods schema to be strictly typed. */
274
- export type AsStrict<Schema> = {
275
- [Method in keyof Schema]: HttpMethodSchema.AsStrict<Schema[Method]>;
276
- };
197
+ HEAD?: HttpMethodSchema;
198
+ OPTIONS?: HttpMethodSchema;
277
199
  }
278
200
 
279
201
  interface BaseHttpSchema {
@@ -292,7 +214,7 @@ interface BaseHttpSchema {
292
214
  *
293
215
  * interface UserListSearchParams {
294
216
  * name?: string;
295
- * limit?: `${number}`;
217
+ * limit?: number;
296
218
  * }
297
219
  *
298
220
  * type Schema = HttpSchema<{
@@ -315,14 +237,9 @@ interface BaseHttpSchema {
315
237
  *
316
238
  * @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http‐schemas Declaring HTTP interceptor schemas}
317
239
  */
318
- export type HttpSchema<Schema extends BaseHttpSchema = BaseHttpSchema> = HttpSchema.AsStrict<Schema>;
240
+ export type HttpSchema<Schema extends BaseHttpSchema = BaseHttpSchema> = Branded<Schema, 'HttpSchema'>;
319
241
 
320
242
  export namespace HttpSchema {
321
- /** Converts an HTTP service schema to be strictly typed. */
322
- export type AsStrict<Schema extends BaseHttpSchema> = {
323
- [Path in keyof Schema]: HttpMethodsSchema.AsStrict<Schema[Path]>;
324
- };
325
-
326
243
  /**
327
244
  * Declares an HTTP service methods schema.
328
245
  *
@@ -341,7 +258,7 @@ export namespace HttpSchema {
341
258
  * '/users': UserMethods;
342
259
  * }>;
343
260
  */
344
- export type Methods<Schema extends HttpMethodsSchema> = HttpMethodsSchema.AsStrict<Schema>;
261
+ export type Methods<Schema extends HttpMethodsSchema> = Schema;
345
262
 
346
263
  /**
347
264
  * Declares an HTTP service method schema.
@@ -361,7 +278,7 @@ export namespace HttpSchema {
361
278
  * };
362
279
  * }>;
363
280
  */
364
- export type Method<Schema extends HttpMethodSchema> = HttpMethodSchema.AsStrict<Schema>;
281
+ export type Method<Schema extends HttpMethodSchema> = Schema;
365
282
 
366
283
  /**
367
284
  * Declares an HTTP service request schema.
@@ -385,7 +302,7 @@ export namespace HttpSchema {
385
302
  * };
386
303
  * }>;
387
304
  */
388
- export type Request<Schema extends HttpRequestSchema> = HttpRequestSchema.AsStrict<Schema>;
305
+ export type Request<Schema extends HttpRequestSchema> = Schema;
389
306
 
390
307
  /**
391
308
  * Declares an HTTP service response schema by status code.
@@ -406,8 +323,7 @@ export namespace HttpSchema {
406
323
  * };
407
324
  * }>;
408
325
  */
409
- export type ResponseByStatusCode<Schema extends HttpResponseSchemaByStatusCode> =
410
- HttpResponseSchemaByStatusCode.AsStrict<Schema>;
326
+ export type ResponseByStatusCode<Schema extends HttpResponseSchemaByStatusCode> = Schema;
411
327
 
412
328
  /**
413
329
  * Declares an HTTP service response schema.
@@ -429,11 +345,10 @@ export namespace HttpSchema {
429
345
  * };
430
346
  * }>;
431
347
  */
432
- export type Response<Schema extends HttpResponseSchema> = HttpResponseSchema.AsStrict<Schema>;
348
+ export type Response<Schema extends HttpResponseSchema> = Schema;
433
349
 
434
350
  /**
435
- * Declares an HTTP body schema. JSON values are serialized to their strict form using
436
- * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic#jsonserialized `JSONSerialized`} if necessary.
351
+ * Declares an HTTP body schema.
437
352
  *
438
353
  * @example
439
354
  * import { type HttpSchema } from '@zimic/http';
@@ -450,12 +365,10 @@ export namespace HttpSchema {
450
365
  * };
451
366
  * }>;
452
367
  */
453
- export type Body<Schema extends HttpBody.Loose> = HttpBody.AsStrict<Schema>;
368
+ export type Body<Schema extends HttpBody.Loose> = Schema;
454
369
 
455
370
  /**
456
- * Declares an HTTP headers schema. Headers are serialized to their strict form using
457
- * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheadersserialized `HttpHeadersSerialized`} if
458
- * necessary.
371
+ * Declares an HTTP headers schema.
459
372
  *
460
373
  * @example
461
374
  * import { type HttpSchema } from '@zimic/http';
@@ -477,12 +390,10 @@ export namespace HttpSchema {
477
390
  * };
478
391
  * }>;
479
392
  */
480
- export type Headers<Schema extends HttpHeadersSchema.Loose> = HttpHeadersSerialized<Schema>;
393
+ export type Headers<Schema extends HttpHeadersSchema.Loose> = Schema;
481
394
 
482
395
  /**
483
- * Declares an HTTP search params schema. Search params are serialized to their strict form using
484
- * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparamsserialized `HttpSearchParamsSerialized`}
485
- * if necessary.
396
+ * Declares an HTTP search params schema.
486
397
  *
487
398
  * @example
488
399
  * import { type HttpSchema } from '@zimic/http';
@@ -505,11 +416,10 @@ export namespace HttpSchema {
505
416
  * };
506
417
  * }>;
507
418
  */
508
- export type SearchParams<Schema extends HttpSearchParamsSchema.Loose> = HttpSearchParamsSerialized<Schema>;
419
+ export type SearchParams<Schema extends HttpSearchParamsSchema.Loose> = Schema;
509
420
 
510
421
  /**
511
- * Declares an HTTP path params schema. Path params are serialized to their strict form using
512
- * {@link HttpPathParamsSerialized} if necessary.
422
+ * Declares an HTTP path params schema.
513
423
  *
514
424
  * @example
515
425
  * import { type HttpSchema, InferPathParams } from '@zimic/http';
@@ -531,12 +441,10 @@ export namespace HttpSchema {
531
441
  * // Or infer from the path string
532
442
  * type UserByIdPathParams = InferPathParams<Schema, '/users/:userId'>;
533
443
  */
534
- export type PathParams<Schema extends HttpPathParamsSchema.Loose> = HttpPathParamsSerialized<Schema>;
444
+ export type PathParams<Schema extends HttpPathParamsSchema.Loose> = Schema;
535
445
 
536
446
  /**
537
- * Declares an HTTP form data schema. Form data is serialized to their strict form using
538
- * {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpformdataserialized `HttpFormDataSerialized`} if
539
- * necessary.
447
+ * Declares an HTTP form data schema.
540
448
  *
541
449
  * @example
542
450
  * import { HttpFormData, type HttpSchema } from '@zimic/http';
@@ -561,7 +469,7 @@ export namespace HttpSchema {
561
469
  * };
562
470
  * }>;
563
471
  */
564
- export type FormData<Schema extends HttpFormDataSchema.Loose> = HttpFormDataSerialized<Schema>;
472
+ export type FormData<Schema extends HttpFormDataSchema.Loose> = Schema;
565
473
  }
566
474
 
567
475
  /**
@@ -761,26 +669,13 @@ export type InferPathParams<
761
669
  >;
762
670
 
763
671
  type OmitPastHttpStatusCodes<
764
- Schema extends HttpResponseSchemaByStatusCode.Loose,
765
- PastSchemas extends HttpResponseSchemaByStatusCode.Loose[],
672
+ Schema extends HttpResponseSchemaByStatusCode,
673
+ PastSchemas extends HttpResponseSchemaByStatusCode[],
766
674
  > =
767
- PastSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode.Loose>
675
+ PastSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode>
768
676
  ? Omit<Schema, keyof UnionToIntersection<PastSchemas[number]>>
769
677
  : Schema;
770
678
 
771
- type RecursiveMergeHttpResponsesByStatusCode<
772
- Schemas extends HttpResponseSchemaByStatusCode.Loose[],
773
- PastSchemas extends HttpResponseSchemaByStatusCode.Loose[] = [],
774
- > = Schemas extends [
775
- infer FirstSchema extends HttpResponseSchemaByStatusCode.Loose,
776
- ...infer RestSchemas extends HttpResponseSchemaByStatusCode.Loose[],
777
- ]
778
- ? RestSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode.Loose>
779
- ? OmitPastHttpStatusCodes<FirstSchema, PastSchemas> &
780
- RecursiveMergeHttpResponsesByStatusCode<RestSchemas, [...PastSchemas, FirstSchema]>
781
- : OmitPastHttpStatusCodes<FirstSchema, PastSchemas>
782
- : never;
783
-
784
679
  /**
785
680
  * Merges multiple HTTP response schemas by status code into a single schema. When there are duplicate status codes, the
786
681
  * first declaration takes precedence.
@@ -815,6 +710,14 @@ type RecursiveMergeHttpResponsesByStatusCode<
815
710
  * }>;
816
711
  */
817
712
  export type MergeHttpResponsesByStatusCode<
818
- Schemas extends HttpResponseSchemaByStatusCode.Loose[],
819
- PastSchemas extends HttpResponseSchemaByStatusCode.Loose[] = [],
820
- > = HttpResponseSchemaByStatusCode.AsStrict<RecursiveMergeHttpResponsesByStatusCode<Schemas, PastSchemas>>;
713
+ Schemas extends HttpResponseSchemaByStatusCode[],
714
+ PastSchemas extends HttpResponseSchemaByStatusCode[] = [],
715
+ > = Schemas extends [
716
+ infer FirstSchema extends HttpResponseSchemaByStatusCode,
717
+ ...infer RestSchemas extends HttpResponseSchemaByStatusCode[],
718
+ ]
719
+ ? RestSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode>
720
+ ? OmitPastHttpStatusCodes<FirstSchema, PastSchemas> &
721
+ MergeHttpResponsesByStatusCode<RestSchemas, [...PastSchemas, FirstSchema]>
722
+ : OmitPastHttpStatusCodes<FirstSchema, PastSchemas>
723
+ : never;