@zimic/http 0.0.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/LICENSE.md +16 -0
- package/README.md +230 -0
- package/dist/chunk-VHQRAQPQ.mjs +1371 -0
- package/dist/chunk-VHQRAQPQ.mjs.map +1 -0
- package/dist/chunk-VUDGONB5.js +1382 -0
- package/dist/chunk-VUDGONB5.js.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +109 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.ts +1306 -0
- package/dist/index.js +544 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +537 -0
- package/dist/index.mjs.map +1 -0
- package/dist/typegen.d.ts +86 -0
- package/dist/typegen.js +12 -0
- package/dist/typegen.js.map +1 -0
- package/dist/typegen.mjs +3 -0
- package/dist/typegen.mjs.map +1 -0
- package/index.d.ts +1 -0
- package/package.json +110 -0
- package/src/cli/cli.ts +92 -0
- package/src/cli/index.ts +4 -0
- package/src/cli/typegen/openapi.ts +24 -0
- package/src/formData/HttpFormData.ts +300 -0
- package/src/formData/types.ts +110 -0
- package/src/headers/HttpHeaders.ts +217 -0
- package/src/headers/types.ts +65 -0
- package/src/index.ts +55 -0
- package/src/pathParams/types.ts +67 -0
- package/src/searchParams/HttpSearchParams.ts +258 -0
- package/src/searchParams/types.ts +133 -0
- package/src/typegen/index.ts +12 -0
- package/src/typegen/namespace/TypegenNamespace.ts +18 -0
- package/src/typegen/openapi/generate.ts +168 -0
- package/src/typegen/openapi/transform/components.ts +481 -0
- package/src/typegen/openapi/transform/context.ts +67 -0
- package/src/typegen/openapi/transform/filters.ts +71 -0
- package/src/typegen/openapi/transform/imports.ts +15 -0
- package/src/typegen/openapi/transform/io.ts +86 -0
- package/src/typegen/openapi/transform/methods.ts +803 -0
- package/src/typegen/openapi/transform/operations.ts +120 -0
- package/src/typegen/openapi/transform/paths.ts +119 -0
- package/src/typegen/openapi/utils/types.ts +45 -0
- package/src/types/arrays.d.ts +4 -0
- package/src/types/json.ts +89 -0
- package/src/types/objects.d.ts +14 -0
- package/src/types/requests.ts +96 -0
- package/src/types/schema.ts +834 -0
- package/src/types/strings.d.ts +9 -0
- package/src/types/utils.ts +64 -0
- package/src/utils/console.ts +7 -0
- package/src/utils/data.ts +13 -0
- package/src/utils/files.ts +28 -0
- package/src/utils/imports.ts +12 -0
- package/src/utils/prettier.ts +13 -0
- package/src/utils/strings.ts +3 -0
- package/src/utils/time.ts +25 -0
- package/src/utils/urls.ts +52 -0
- package/typegen.d.ts +1 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1306 @@
|
|
|
1
|
+
type JSON = {
|
|
2
|
+
[key: string]: JSON;
|
|
3
|
+
} | JSON[] | string | number | boolean | null | undefined;
|
|
4
|
+
declare namespace JSON {
|
|
5
|
+
type Loose = Record<string, any> | Loose[] | string | number | boolean | null | undefined;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Represents or validates a type that is compatible with JSON.
|
|
9
|
+
*
|
|
10
|
+
* **IMPORTANT**: the input of `JSONValue` and all of its internal types must be declared inline or as a type aliases
|
|
11
|
+
* (`type`). They cannot be interfaces.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { type JSONValue } from '@zimic/http';
|
|
15
|
+
*
|
|
16
|
+
* // Can be used as a standalone type:
|
|
17
|
+
* const value: JSONValue = {
|
|
18
|
+
* name: 'example',
|
|
19
|
+
* tags: ['one', 'two'],
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* import { type JSONValue } from '@zimic/http';
|
|
24
|
+
*
|
|
25
|
+
* // Can be used with a type argument to validate a JSON value:
|
|
26
|
+
* type ValidJSON = JSONValue<{
|
|
27
|
+
* id: string;
|
|
28
|
+
* email: string;
|
|
29
|
+
* createdAt: string;
|
|
30
|
+
* }>;
|
|
31
|
+
*
|
|
32
|
+
* // This results in a type error:
|
|
33
|
+
* type InvalidJSON = JSONValue<{
|
|
34
|
+
* id: string;
|
|
35
|
+
* email: string;
|
|
36
|
+
* createdAt: Date; // `Date` is not a valid JSON value.
|
|
37
|
+
* save: () => Promise<void>; // Functions are not valid JSON values.
|
|
38
|
+
* }>;
|
|
39
|
+
*/
|
|
40
|
+
type JSONValue<Type extends JSON = JSON> = Type;
|
|
41
|
+
declare namespace JSONValue {
|
|
42
|
+
/** A loose version of the JSON value type. JSON objects are not strictly typed. */
|
|
43
|
+
type Loose<Type extends JSON.Loose = JSON.Loose> = Type;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Recursively converts a type to its JSON-serialized version. Dates are converted to strings and keys with non-JSON
|
|
47
|
+
* values are excluded.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* import { type JSONSerialized } from '@zimic/http';
|
|
51
|
+
*
|
|
52
|
+
* type SerializedUser = JSONSerialized<{
|
|
53
|
+
* id: string;
|
|
54
|
+
* email: string;
|
|
55
|
+
* createdAt: Date;
|
|
56
|
+
* save: () => Promise<void>;
|
|
57
|
+
* }>;
|
|
58
|
+
* // {
|
|
59
|
+
* // id: string;
|
|
60
|
+
* // email: string;
|
|
61
|
+
* // createdAt: string;
|
|
62
|
+
* // }
|
|
63
|
+
*/
|
|
64
|
+
type JSONSerialized<Type> = Type extends JSONValue ? Type : Type extends string | number | boolean | null | undefined ? Type : Type extends Date ? string : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map<infer _Key, infer _Value> ? Record<string, never> : Type extends Set<infer _Value> ? Record<string, never> : Type extends (infer ArrayItem)[] ? JSONSerialized<ArrayItem>[] : Type extends object ? {
|
|
65
|
+
[Key in keyof Type as [JSONSerialized<Type[Key]>] extends [never] ? never : Key]: JSONSerialized<Type[Key]>;
|
|
66
|
+
} : never;
|
|
67
|
+
|
|
68
|
+
type Default<Type, IfEmpty = never> = [undefined | void] extends [Type] ? IfEmpty : Exclude<Type, undefined | void>;
|
|
69
|
+
type IfAny<Type, Yes, No = Type> = 0 extends 1 & Type ? Yes : No;
|
|
70
|
+
type IfNever<Type, Yes, No = Type> = [Type] extends [never] ? Yes : No;
|
|
71
|
+
type UnionToIntersection<Union> = (Union extends unknown ? (union: Union) => void : never) extends (intersectedUnion: infer IntersectedUnion) => void ? IntersectedUnion : never;
|
|
72
|
+
type UnionHasMoreThanOneType<Union> = [UnionToIntersection<Union>] extends [never] ? true : false;
|
|
73
|
+
type Prettify<Type> = {
|
|
74
|
+
[Key in keyof Type]: Type[Key];
|
|
75
|
+
};
|
|
76
|
+
type ArrayItemIfArray<Type> = Type extends (infer Item)[] ? Item : Type;
|
|
77
|
+
type PickArrayProperties<Type> = {
|
|
78
|
+
[Key in keyof Type as never[] extends Type[Key] ? Key : never]: Type[Key];
|
|
79
|
+
};
|
|
80
|
+
type ArrayKey<Type> = keyof PickArrayProperties<Type>;
|
|
81
|
+
type NonArrayKey<Type> = string | number extends ArrayKey<Type> ? keyof Type : Exclude<keyof Type, ArrayKey<Type>>;
|
|
82
|
+
type NonEmptyArray<Type> = [Type, ...Type[]];
|
|
83
|
+
type ReplaceBy<Type, Source, Target> = Type extends Source ? Target : Type;
|
|
84
|
+
|
|
85
|
+
/** A schema for strict HTTP form data. */
|
|
86
|
+
interface HttpFormDataSchema {
|
|
87
|
+
[fieldName: string]: string | string[] | Blob | Blob[] | null | undefined;
|
|
88
|
+
}
|
|
89
|
+
declare namespace HttpFormDataSchema {
|
|
90
|
+
/** A schema for loose HTTP form data. Field values are not strictly typed. */
|
|
91
|
+
type Loose = Record<string, any>;
|
|
92
|
+
}
|
|
93
|
+
declare namespace HttpFormDataSchemaName {
|
|
94
|
+
/** Extracts the names of the form data fields defined in a {@link HttpFormDataSchema} that are arrays. */
|
|
95
|
+
type Array<Schema extends HttpFormDataSchema> = IfNever<Schema, never, ArrayKey<Schema> & string>;
|
|
96
|
+
/** Extracts the names of the form data fields defined in a {@link HttpFormDataSchema} that are not arrays. */
|
|
97
|
+
type NonArray<Schema extends HttpFormDataSchema> = IfNever<Schema, never, NonArrayKey<Schema> & string>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Extracts the names of the form data fields defined in a {@link HttpFormDataSchema}. Each key is considered a field
|
|
101
|
+
* name. `HttpFormDataSchemaName.Array` can be used to extract the names of array form data fields, whereas
|
|
102
|
+
* `HttpFormDataSchemaName.NonArray` extracts the names of non-array form data fields.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* import { type HttpFormDataSchemaName } from '@zimic/http';
|
|
106
|
+
*
|
|
107
|
+
* type FormDataName = HttpFormDataSchemaName<{
|
|
108
|
+
* title: string;
|
|
109
|
+
* descriptions: string[];
|
|
110
|
+
* content: Blob;
|
|
111
|
+
* }>;
|
|
112
|
+
* // "title" | "descriptions" | "content"
|
|
113
|
+
*
|
|
114
|
+
* type ArrayFormDataName = HttpFormDataSchemaName.Array<{
|
|
115
|
+
* title: string;
|
|
116
|
+
* descriptions: string[];
|
|
117
|
+
* content: Blob;
|
|
118
|
+
* }>;
|
|
119
|
+
* // "descriptions"
|
|
120
|
+
*
|
|
121
|
+
* type NonArrayFormDataName = HttpFormDataSchemaName.NonArray<{
|
|
122
|
+
* title: string;
|
|
123
|
+
* descriptions: string[];
|
|
124
|
+
* content: Blob;
|
|
125
|
+
* }>;
|
|
126
|
+
* // "title" | "content"
|
|
127
|
+
*/
|
|
128
|
+
type HttpFormDataSchemaName<Schema extends HttpFormDataSchema> = IfNever<Schema, never, keyof Schema & string>;
|
|
129
|
+
type PrimitiveHttpFormDataSerialized<Type> = Type extends HttpFormDataSchema[string] ? Type : Type extends (infer ArrayItem)[] ? ArrayItem extends (infer _InternalArrayItem)[] ? never : PrimitiveHttpFormDataSerialized<ArrayItem>[] : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : never;
|
|
130
|
+
/**
|
|
131
|
+
* Recursively converts a schema to its {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}-serialized
|
|
132
|
+
* version. Numbers and booleans are converted to `${number}` and `${boolean}` respectively, and not serializable values
|
|
133
|
+
* are excluded, such as functions and dates.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* import { type HttpFormDataSerialized } from '@zimic/http';
|
|
137
|
+
*
|
|
138
|
+
* type Schema = HttpFormDataSerialized<{
|
|
139
|
+
* contentTitle: string | null;
|
|
140
|
+
* contentSize: number;
|
|
141
|
+
* content: Blob;
|
|
142
|
+
* full?: boolean;
|
|
143
|
+
* date: Date;
|
|
144
|
+
* method: () => void;
|
|
145
|
+
* }>;
|
|
146
|
+
* // {
|
|
147
|
+
* // contentTitle: string | null;
|
|
148
|
+
* // contentSize? `${number}`;
|
|
149
|
+
* // content: Blob;
|
|
150
|
+
* // full?: "false" | "true";
|
|
151
|
+
* // }
|
|
152
|
+
*/
|
|
153
|
+
type HttpFormDataSerialized<Type> = Type extends HttpFormDataSchema ? Type : Type extends (infer _ArrayItem)[] ? never : Type extends Date ? never : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map<infer _Key, infer _Value> ? never : Type extends Set<infer _Value> ? never : Type extends object ? {
|
|
154
|
+
[Key in keyof Type as IfNever<PrimitiveHttpFormDataSerialized<Type[Key]>, never, Key>]: PrimitiveHttpFormDataSerialized<Type[Key]>;
|
|
155
|
+
} : never;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* An extended HTTP form data object with a strictly-typed schema. Fully compatible with the built-in
|
|
159
|
+
* {@link https://developer.mozilla.org/docs/Web/API/FormData `FormData`} class.
|
|
160
|
+
*
|
|
161
|
+
* **IMPORTANT**: the input of `HttpFormData` and all of its internal types must be declared inline or as a type aliases
|
|
162
|
+
* (`type`). They cannot be interfaces.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* import { HttpFormData } from '@zimic/http';
|
|
166
|
+
*
|
|
167
|
+
* const formData = new HttpFormData<{
|
|
168
|
+
* files: File[];
|
|
169
|
+
* description?: string;
|
|
170
|
+
* }>();
|
|
171
|
+
*
|
|
172
|
+
* formData.append('file', new File(['content'], 'file.txt', { type: 'text/plain' }));
|
|
173
|
+
* formData.append('description', 'My file');
|
|
174
|
+
*
|
|
175
|
+
* const files = formData.getAll('file');
|
|
176
|
+
* console.log(files); // [File { name: 'file.txt', type: 'text/plain' }]
|
|
177
|
+
*
|
|
178
|
+
* const description = formData.get('description');
|
|
179
|
+
* console.log(description); // 'My file'
|
|
180
|
+
*
|
|
181
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpformdata `HttpFormData` API reference}
|
|
182
|
+
*/
|
|
183
|
+
declare class HttpFormData<Schema extends HttpFormDataSchema = HttpFormDataSchema> extends FormData {
|
|
184
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/set MDN Reference} */
|
|
185
|
+
set<Name extends HttpFormDataSchemaName<Schema>>(name: Name, value: Exclude<ArrayItemIfArray<NonNullable<Schema[Name]>>, Blob>): void;
|
|
186
|
+
set<Name extends HttpFormDataSchemaName<Schema>>(name: Name, blob: Exclude<ArrayItemIfArray<NonNullable<Schema[Name]>>, string>, fileName?: string): void;
|
|
187
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/append MDN Reference} */
|
|
188
|
+
append<Name extends HttpFormDataSchemaName<Schema>>(name: Name, value: Exclude<ArrayItemIfArray<NonNullable<Schema[Name]>>, Blob>): void;
|
|
189
|
+
append<Name extends HttpFormDataSchemaName<Schema>>(name: Name, blob: Exclude<ArrayItemIfArray<NonNullable<Schema[Name]>>, string>, fileName?: string): void;
|
|
190
|
+
/**
|
|
191
|
+
* Get the value of the entry associated to a key name.
|
|
192
|
+
*
|
|
193
|
+
* If the key might have multiple values, use {@link HttpFormData#getAll} instead.
|
|
194
|
+
*
|
|
195
|
+
* @param name The name of the key to get the value of.
|
|
196
|
+
* @returns The value associated with the key name, or `null` if the key does not exist.
|
|
197
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/FormData/get MDN Reference}
|
|
198
|
+
*/
|
|
199
|
+
get<Name extends HttpFormDataSchemaName.NonArray<Schema>>(name: Name): ReplaceBy<ReplaceBy<ArrayItemIfArray<Schema[Name]>, undefined, null>, Blob, File>;
|
|
200
|
+
/**
|
|
201
|
+
* Get all the values of the entry associated with a key name.
|
|
202
|
+
*
|
|
203
|
+
* If the key has at most a single value, use {@link HttpFormData#get} instead.
|
|
204
|
+
*
|
|
205
|
+
* @param name The name of the key to get the values of.
|
|
206
|
+
* @returns An array of values associated with the key name, or an empty array if the key does not exist.
|
|
207
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/FormData/getAll MDN Reference}
|
|
208
|
+
*/
|
|
209
|
+
getAll<Name extends HttpFormDataSchemaName.Array<Schema>>(name: Name): ReplaceBy<ArrayItemIfArray<NonNullable<Schema[Name]>>, Blob, File>[];
|
|
210
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/has MDN Reference} */
|
|
211
|
+
has<Name extends HttpFormDataSchemaName<Schema>>(name: Name): boolean;
|
|
212
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/delete MDN Reference} */
|
|
213
|
+
delete<Name extends HttpFormDataSchemaName<Schema>>(name: Name): void;
|
|
214
|
+
forEach<This extends HttpFormData<Schema>>(callback: <Key extends HttpFormDataSchemaName<Schema>>(value: ReplaceBy<ArrayItemIfArray<NonNullable<Schema[Key]>>, Blob, File>, key: Key, parent: HttpFormData<Schema>) => void, thisArg?: This): void;
|
|
215
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/keys MDN Reference} */
|
|
216
|
+
keys(): FormDataIterator<HttpFormDataSchemaName<Schema>>;
|
|
217
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/values MDN Reference} */
|
|
218
|
+
values(): FormDataIterator<ReplaceBy<ArrayItemIfArray<NonNullable<Schema[HttpFormDataSchemaName<Schema>]>>, Blob, File>>;
|
|
219
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/FormData/entries MDN Reference} */
|
|
220
|
+
entries(): FormDataIterator<[
|
|
221
|
+
HttpFormDataSchemaName<Schema>,
|
|
222
|
+
ReplaceBy<ArrayItemIfArray<NonNullable<Schema[HttpFormDataSchemaName<Schema>]>>, Blob, File>
|
|
223
|
+
]>;
|
|
224
|
+
[Symbol.iterator](): FormDataIterator<[
|
|
225
|
+
HttpFormDataSchemaName<Schema>,
|
|
226
|
+
ReplaceBy<ArrayItemIfArray<NonNullable<Schema[HttpFormDataSchemaName<Schema>]>>, Blob, File>
|
|
227
|
+
]>;
|
|
228
|
+
/**
|
|
229
|
+
* Checks if the data is equal to the other data. Equality is defined as having the same keys and values, regardless
|
|
230
|
+
* of the order of keys.
|
|
231
|
+
*
|
|
232
|
+
* @param otherData The other data to compare.
|
|
233
|
+
* @returns A promise that resolves with `true` if the data is equal to the other data, or `false` otherwise.
|
|
234
|
+
* Important: both form data might be read while comparing.
|
|
235
|
+
*/
|
|
236
|
+
equals<OtherSchema extends Schema>(otherData: HttpFormData<OtherSchema>): Promise<boolean>;
|
|
237
|
+
/**
|
|
238
|
+
* Checks if the data contains the other data. This method is less strict than {@link HttpFormData#equals} and only
|
|
239
|
+
* requires that all keys and values in the other data are present in this data.
|
|
240
|
+
*
|
|
241
|
+
* @param otherData The other data to compare.
|
|
242
|
+
* @returns A promise that resolves with `true` if this data contains the other data, or `false` otherwise. Important:
|
|
243
|
+
* both form data might be read while comparing.
|
|
244
|
+
*/
|
|
245
|
+
contains<OtherSchema extends Schema>(otherData: HttpFormData<OtherSchema>): Promise<boolean>;
|
|
246
|
+
/**
|
|
247
|
+
* Converts this form data into a plain object. This method is useful for serialization and debugging purposes.
|
|
248
|
+
*
|
|
249
|
+
* **NOTE**: If a key has multiple values, the object will contain an array of values for that key. If the key has
|
|
250
|
+
* only one value, the object will contain its value directly, without an array, regardless of how the value was
|
|
251
|
+
* initialized when creating the form data.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* const formData = new HttpFormData<{
|
|
255
|
+
* title: string;
|
|
256
|
+
* descriptions: string[];
|
|
257
|
+
* content: Blob;
|
|
258
|
+
* }>();
|
|
259
|
+
*
|
|
260
|
+
* formData.set('title', 'My title');
|
|
261
|
+
* formData.append('descriptions', 'Description 1');
|
|
262
|
+
* formData.append('descriptions', 'Description 2');
|
|
263
|
+
* formData.set('content', new Blob(['content'], { type: 'text/plain' }));
|
|
264
|
+
*
|
|
265
|
+
* const object = formData.toObject();
|
|
266
|
+
* console.log(object); // { title: 'My title', descriptions: ['Description 1', 'Description 2'], content: Blob { type: 'text/plain' } }
|
|
267
|
+
*
|
|
268
|
+
* @returns A plain object representation of this form data.
|
|
269
|
+
*/
|
|
270
|
+
toObject(): Schema;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
interface HttpPathParamsSchema {
|
|
274
|
+
[paramName: string]: string | undefined;
|
|
275
|
+
}
|
|
276
|
+
declare namespace HttpPathParamsSchema {
|
|
277
|
+
/** A schema for loose HTTP path parameters. Parameter values are not strictly typed. */
|
|
278
|
+
type Loose = Record<string, any>;
|
|
279
|
+
}
|
|
280
|
+
type PrimitiveHttpPathParamsSerialized<Type> = Type extends HttpPathParamsSchema[string] ? Type : Type extends (infer _ArrayItem)[] ? never : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : Type extends null ? undefined : never;
|
|
281
|
+
/**
|
|
282
|
+
* Recursively converts a schema to its path parameters-serialized version. Numbers and booleans are converted to
|
|
283
|
+
* `${number}` and `${boolean}` respectively, null becomes undefined and not serializable values are excluded, such as
|
|
284
|
+
* functions and dates.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* import { type HttpPathParamsSerialized } from '@zimic/http';
|
|
288
|
+
*
|
|
289
|
+
* type Params = HttpPathParamsSerialized<{
|
|
290
|
+
* userId: string;
|
|
291
|
+
* notificationId: number | null;
|
|
292
|
+
* full?: boolean;
|
|
293
|
+
* from?: Date;
|
|
294
|
+
* method: () => void;
|
|
295
|
+
* }>;
|
|
296
|
+
* // {
|
|
297
|
+
* // userId: string;
|
|
298
|
+
* // notificationId: `${number}` | undefined;
|
|
299
|
+
* // full?: "false" | "true";
|
|
300
|
+
* // }
|
|
301
|
+
*/
|
|
302
|
+
type HttpPathParamsSerialized<Type> = Type extends HttpPathParamsSchema ? Type : Type extends (infer _ArrayItem)[] ? never : Type extends Date ? never : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map<infer _Key, infer _Value> ? never : Type extends Set<infer _Value> ? never : Type extends object ? {
|
|
303
|
+
[Key in keyof Type as IfNever<PrimitiveHttpPathParamsSerialized<Type[Key]>, never, Key>]: PrimitiveHttpPathParamsSerialized<Type[Key]>;
|
|
304
|
+
} : never;
|
|
305
|
+
|
|
306
|
+
/** A schema for strict HTTP headers. */
|
|
307
|
+
interface HttpHeadersSchema {
|
|
308
|
+
[headerName: string]: string | undefined;
|
|
309
|
+
}
|
|
310
|
+
declare namespace HttpHeadersSchema {
|
|
311
|
+
/** A schema for loose HTTP headers. Header values are not strictly typed. */
|
|
312
|
+
type Loose = Record<string, any>;
|
|
313
|
+
}
|
|
314
|
+
/** A strict tuple representation of a {@link HttpHeadersSchema}. */
|
|
315
|
+
type HttpHeadersSchemaTuple<Schema extends HttpHeadersSchema = HttpHeadersSchema> = {
|
|
316
|
+
[Key in keyof Schema & string]: [Key, NonNullable<Schema[Key]>];
|
|
317
|
+
}[keyof Schema & string];
|
|
318
|
+
/** An initialization value for {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders `HttpHeaders`}. */
|
|
319
|
+
type HttpHeadersInit<Schema extends HttpHeadersSchema = HttpHeadersSchema> = Headers | Schema | HttpHeaders<Schema> | HttpHeadersSchemaTuple<Schema>[];
|
|
320
|
+
/**
|
|
321
|
+
* Extracts the names of the headers defined in a {@link HttpHeadersSchema}. Each key is considered a header name.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* import { type HttpHeadersSchemaName } from '@zimic/http';
|
|
325
|
+
*
|
|
326
|
+
* type HeaderName = HttpHeadersSchemaName<{
|
|
327
|
+
* 'content-type': string;
|
|
328
|
+
* 'content-length'?: string;
|
|
329
|
+
* }>;
|
|
330
|
+
* // "content-type" | "content-length"
|
|
331
|
+
*/
|
|
332
|
+
type HttpHeadersSchemaName<Schema extends HttpHeadersSchema> = IfNever<Schema, never, keyof Schema & string>;
|
|
333
|
+
/**
|
|
334
|
+
* Recursively converts a schema to its
|
|
335
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers HTTP headers}-serialized version. Numbers and booleans are
|
|
336
|
+
* converted to `${number}` and `${boolean}` respectively, null becomes undefined and not serializable values are
|
|
337
|
+
* excluded, such as functions and dates.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* import { type HttpHeadersSerialized } from '@zimic/http';
|
|
341
|
+
*
|
|
342
|
+
* type Params = HttpHeadersSerialized<{
|
|
343
|
+
* 'content-type': string;
|
|
344
|
+
* 'x-remaining-tries': number;
|
|
345
|
+
* 'x-full'?: boolean;
|
|
346
|
+
* 'x-date': Date;
|
|
347
|
+
* method: () => void;
|
|
348
|
+
* }>;
|
|
349
|
+
* // {
|
|
350
|
+
* // 'content-type': string;
|
|
351
|
+
* // 'x-remaining-tries': `${number}`;
|
|
352
|
+
* // 'x-full'?: "false" | "true";
|
|
353
|
+
* // }
|
|
354
|
+
*/
|
|
355
|
+
type HttpHeadersSerialized<Type> = HttpPathParamsSerialized<Type>;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* An extended HTTP headers object with a strictly-typed schema. Fully compatible with the built-in
|
|
359
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers `Headers`} class.
|
|
360
|
+
*
|
|
361
|
+
* **IMPORTANT**: the input of `HttpHeaders` and all of its internal types must be declared inline or as a type aliases
|
|
362
|
+
* (`type`). They cannot be interfaces.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* import { HttpHeaders } from '@zimic/http';
|
|
366
|
+
*
|
|
367
|
+
* const headers = new HttpHeaders<{
|
|
368
|
+
* accept?: string;
|
|
369
|
+
* 'content-type'?: string;
|
|
370
|
+
* }>({
|
|
371
|
+
* accept: '*',
|
|
372
|
+
* 'content-type': 'application/json',
|
|
373
|
+
* });
|
|
374
|
+
*
|
|
375
|
+
* const contentType = headers.get('content-type');
|
|
376
|
+
* console.log(contentType); // 'application/json'
|
|
377
|
+
*
|
|
378
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders `HttpHeaders` API reference}
|
|
379
|
+
*/
|
|
380
|
+
declare class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends Headers {
|
|
381
|
+
constructor(init?: HttpHeadersInit<Schema>);
|
|
382
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/set MDN Reference} */
|
|
383
|
+
set<Name extends HttpHeadersSchemaName<Schema>>(name: Name, value: NonNullable<Schema[Name]>): void;
|
|
384
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/append MDN Reference} */
|
|
385
|
+
append<Name extends HttpHeadersSchemaName<Schema>>(name: Name, value: NonNullable<Schema[Name]>): void;
|
|
386
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/get MDN Reference} */
|
|
387
|
+
get<Name extends HttpHeadersSchemaName<Schema>>(name: Name): ReplaceBy<Schema[Name], undefined, null>;
|
|
388
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
389
|
+
getSetCookie(): NonNullable<Default<Schema['Set-Cookie'], string>>[];
|
|
390
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
391
|
+
has<Name extends HttpHeadersSchemaName<Schema>>(name: Name): boolean;
|
|
392
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/delete MDN Reference} */
|
|
393
|
+
delete<Name extends HttpHeadersSchemaName<Schema>>(name: Name): void;
|
|
394
|
+
forEach<This extends HttpHeaders<Schema>>(callback: <Key extends HttpHeadersSchemaName<Schema>>(value: NonNullable<Schema[Key]>, key: Key, parent: Headers) => void, thisArg?: This): void;
|
|
395
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/keys MDN Reference} */
|
|
396
|
+
keys(): HeadersIterator<HttpHeadersSchemaName<Schema>>;
|
|
397
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/values MDN Reference} */
|
|
398
|
+
values(): HeadersIterator<NonNullable<Schema[HttpHeadersSchemaName<Schema>]>>;
|
|
399
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/entries MDN Reference} */
|
|
400
|
+
entries(): HeadersIterator<[HttpHeadersSchemaName<Schema>, NonNullable<Schema[HttpHeadersSchemaName<Schema>]>]>;
|
|
401
|
+
[Symbol.iterator](): HeadersIterator<[
|
|
402
|
+
HttpHeadersSchemaName<Schema>,
|
|
403
|
+
NonNullable<Schema[HttpHeadersSchemaName<Schema>]>
|
|
404
|
+
]>;
|
|
405
|
+
/**
|
|
406
|
+
* Checks if this headers object is equal to another set of headers. Equality is defined as having the same keys and
|
|
407
|
+
* values, regardless of the order of keys.
|
|
408
|
+
*
|
|
409
|
+
* @param otherHeaders The other headers object to compare against.
|
|
410
|
+
* @returns `true` if the headers are equal, `false` otherwise.
|
|
411
|
+
*/
|
|
412
|
+
equals<OtherSchema extends Schema>(otherHeaders: HttpHeaders<OtherSchema>): boolean;
|
|
413
|
+
/**
|
|
414
|
+
* Checks if this headers object contains another set of headers. This method is less strict than
|
|
415
|
+
* {@link HttpHeaders#equals} and only requires that all keys and values in the other headers are present in these
|
|
416
|
+
* headers.
|
|
417
|
+
*
|
|
418
|
+
* @param otherHeaders The other headers object to compare against.
|
|
419
|
+
* @returns `true` if these headers contain the other headers, `false` otherwise.
|
|
420
|
+
*/
|
|
421
|
+
contains<OtherSchema extends Schema>(otherHeaders: HttpHeaders<OtherSchema>): boolean;
|
|
422
|
+
/**
|
|
423
|
+
* Converts these headers into a plain object. This method is useful for serialization and debugging purposes.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* const headers = new HttpHeaders({
|
|
427
|
+
* accept: 'application/json',
|
|
428
|
+
* 'content-type': 'application/json',
|
|
429
|
+
* });
|
|
430
|
+
* const object = headers.toObject();
|
|
431
|
+
* console.log(object); // { accept: 'application/json', 'content-type': 'application/json' }
|
|
432
|
+
*
|
|
433
|
+
* @returns A plain object representation of these headers.
|
|
434
|
+
*/
|
|
435
|
+
toObject(): Schema;
|
|
436
|
+
private splitHeaderValues;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** A schema for strict HTTP URL search parameters. */
|
|
440
|
+
interface HttpSearchParamsSchema {
|
|
441
|
+
[paramName: string]: string | string[] | undefined;
|
|
442
|
+
}
|
|
443
|
+
declare namespace HttpSearchParamsSchema {
|
|
444
|
+
/** A schema for loose HTTP URL search parameters. Parameter values are not strictly typed. */
|
|
445
|
+
type Loose = Record<string, any>;
|
|
446
|
+
}
|
|
447
|
+
/** A strict tuple representation of a {@link HttpSearchParamsSchema}. */
|
|
448
|
+
type HttpSearchParamsSchemaTuple<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = {
|
|
449
|
+
[Key in keyof Schema & string]: [Key, ArrayItemIfArray<NonNullable<Schema[Key]>>];
|
|
450
|
+
}[keyof Schema & string];
|
|
451
|
+
/**
|
|
452
|
+
* An initialization value for
|
|
453
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams`}.
|
|
454
|
+
*/
|
|
455
|
+
type HttpSearchParamsInit<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = string | URLSearchParams | Schema | HttpSearchParams<Schema> | HttpSearchParamsSchemaTuple<Schema>[];
|
|
456
|
+
declare namespace HttpSearchParamsSchemaName {
|
|
457
|
+
/** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are arrays. */
|
|
458
|
+
type Array<Schema extends HttpSearchParamsSchema> = IfNever<Schema, never, ArrayKey<Schema> & string>;
|
|
459
|
+
/** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are not arrays. */
|
|
460
|
+
type NonArray<Schema extends HttpSearchParamsSchema> = IfNever<Schema, never, NonArrayKey<Schema> & string>;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Extracts the names of the search params defined in a {@link HttpSearchParamsSchema}. Each key is considered a search
|
|
464
|
+
* param name. `HttpSearchParamsSchemaName.Array` can be used to extract the names of array search params, whereas
|
|
465
|
+
* `HttpSearchParamsSchemaName.NonArray` extracts the names of non-array search params.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* import { type HttpSearchParamsSchemaName } from '@zimic/http';
|
|
469
|
+
*
|
|
470
|
+
* type SearchParamsName = HttpSearchParamsSchemaName<{
|
|
471
|
+
* query?: string[];
|
|
472
|
+
* page?: `${number}`;
|
|
473
|
+
* perPage?: `${number}`;
|
|
474
|
+
* }>;
|
|
475
|
+
* // "query" | "page" | "perPage"
|
|
476
|
+
*
|
|
477
|
+
* type ArraySearchParamsName = HttpSearchParamsSchemaName.Array<{
|
|
478
|
+
* query?: string[];
|
|
479
|
+
* page?: `${number}`;
|
|
480
|
+
* perPage?: `${number}`;
|
|
481
|
+
* }>;
|
|
482
|
+
* // "query"
|
|
483
|
+
*
|
|
484
|
+
* type NonArraySearchParamsName = HttpSearchParamsSchemaName.NonArray<{
|
|
485
|
+
* query?: string[];
|
|
486
|
+
* page?: `${number}`;
|
|
487
|
+
* perPage?: `${number}`;
|
|
488
|
+
* }>;
|
|
489
|
+
* // "page" | "perPage"
|
|
490
|
+
*/
|
|
491
|
+
type HttpSearchParamsSchemaName<Schema extends HttpSearchParamsSchema> = IfNever<Schema, never, keyof Schema & string>;
|
|
492
|
+
type PrimitiveHttpSearchParamsSerialized<Type> = Type extends HttpSearchParamsSchema[string] ? Type : Type extends (infer ArrayItem)[] ? ArrayItem extends (infer _InternalArrayItem)[] ? never : PrimitiveHttpSearchParamsSerialized<ArrayItem>[] : Type extends number ? `${number}` : Type extends boolean ? `${boolean}` : Type extends null ? undefined : never;
|
|
493
|
+
/**
|
|
494
|
+
* Recursively converts a schema to its
|
|
495
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams URLSearchParams}-serialized version. Numbers and
|
|
496
|
+
* booleans are converted to `${number}` and `${boolean}` respectively, null becomes undefined and not serializable
|
|
497
|
+
* values are excluded, such as functions and dates.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* import { type HttpSearchParamsSerialized } from '@zimic/http';
|
|
501
|
+
*
|
|
502
|
+
* type Params = HttpSearchParamsSerialized<{
|
|
503
|
+
* query: string | null;
|
|
504
|
+
* page?: number;
|
|
505
|
+
* full?: boolean;
|
|
506
|
+
* date: Date;
|
|
507
|
+
* method: () => void;
|
|
508
|
+
* }>;
|
|
509
|
+
* // {
|
|
510
|
+
* // query: string | undefined;
|
|
511
|
+
* // page?: `${number}`;
|
|
512
|
+
* // full?: "false" | "true";
|
|
513
|
+
* // }
|
|
514
|
+
*/
|
|
515
|
+
type HttpSearchParamsSerialized<Type> = Type extends HttpSearchParamsSchema ? Type : Type extends (infer _ArrayItem)[] ? never : Type extends Date ? never : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map<infer _Key, infer _Value> ? never : Type extends Set<infer _Value> ? never : Type extends object ? {
|
|
516
|
+
[Key in keyof Type as IfNever<PrimitiveHttpSearchParamsSerialized<Type[Key]>, never, Key>]: PrimitiveHttpSearchParamsSerialized<Type[Key]>;
|
|
517
|
+
} : never;
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* An extended HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
521
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
522
|
+
*
|
|
523
|
+
* **IMPORTANT**: the input of `HttpSearchParams` and all of its internal types must be declared inline or as a type
|
|
524
|
+
* aliases (`type`). They cannot be interfaces.
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* import { HttpSearchParams } from '@zimic/http';
|
|
528
|
+
*
|
|
529
|
+
* const searchParams = new HttpSearchParams<{
|
|
530
|
+
* names?: string[];
|
|
531
|
+
* page?: `${number}`;
|
|
532
|
+
* }>({
|
|
533
|
+
* names: ['user 1', 'user 2'],
|
|
534
|
+
* page: '1',
|
|
535
|
+
* });
|
|
536
|
+
*
|
|
537
|
+
* const names = searchParams.getAll('names');
|
|
538
|
+
* console.log(names); // ['user 1', 'user 2']
|
|
539
|
+
*
|
|
540
|
+
* const page = searchParams.get('page');
|
|
541
|
+
* console.log(page); // '1'
|
|
542
|
+
*
|
|
543
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams` API reference}
|
|
544
|
+
*/
|
|
545
|
+
declare class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> extends URLSearchParams {
|
|
546
|
+
constructor(init?: HttpSearchParamsInit<Schema>);
|
|
547
|
+
private populateInitArrayProperties;
|
|
548
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/set MDN Reference} */
|
|
549
|
+
set<Name extends HttpSearchParamsSchemaName<Schema>>(name: Name, value: ArrayItemIfArray<NonNullable<Schema[Name]>>): void;
|
|
550
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/append MDN Reference} */
|
|
551
|
+
append<Name extends HttpSearchParamsSchemaName<Schema>>(name: Name, value: ArrayItemIfArray<NonNullable<Schema[Name]>>): void;
|
|
552
|
+
/**
|
|
553
|
+
* Get the value of the entry associated to a key name.
|
|
554
|
+
*
|
|
555
|
+
* If the key might have multiple values, use {@link HttpSearchParams#getAll} instead.
|
|
556
|
+
*
|
|
557
|
+
* @param name The name of the key to get the value of.
|
|
558
|
+
* @returns The value associated with the key name, or `null` if the key does not exist.
|
|
559
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/get MDN Reference}
|
|
560
|
+
*/
|
|
561
|
+
get<Name extends HttpSearchParamsSchemaName.NonArray<Schema>>(name: Name): ReplaceBy<ArrayItemIfArray<Schema[Name]>, undefined, null>;
|
|
562
|
+
/**
|
|
563
|
+
* Get all the values of the entry associated with a key name.
|
|
564
|
+
*
|
|
565
|
+
* If the key has at most one value, use {@link HttpSearchParams#get} instead.
|
|
566
|
+
*
|
|
567
|
+
* @param name The name of the key to get the values of.
|
|
568
|
+
* @returns An array of values associated with the key name, or an empty array if the key does not exist.
|
|
569
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll MDN Reference}
|
|
570
|
+
*/
|
|
571
|
+
getAll<Name extends HttpSearchParamsSchemaName.Array<Schema>>(name: Name): ArrayItemIfArray<NonNullable<Schema[Name]>>[];
|
|
572
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/has MDN Reference} */
|
|
573
|
+
has<Name extends HttpSearchParamsSchemaName<Schema>>(name: Name, value?: ArrayItemIfArray<NonNullable<Schema[Name]>>): boolean;
|
|
574
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete MDN Reference} */
|
|
575
|
+
delete<Name extends HttpSearchParamsSchemaName<Schema>>(name: Name, value?: ArrayItemIfArray<NonNullable<Schema[Name]>>): void;
|
|
576
|
+
forEach<This extends HttpSearchParams<Schema>>(callback: <Key extends HttpSearchParamsSchemaName<Schema>>(value: ArrayItemIfArray<NonNullable<Schema[Key]>>, key: Key, parent: HttpSearchParams<Schema>) => void, thisArg?: This): void;
|
|
577
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys MDN Reference} */
|
|
578
|
+
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<Schema>>;
|
|
579
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/values MDN Reference} */
|
|
580
|
+
values(): URLSearchParamsIterator<ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>>;
|
|
581
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries MDN Reference} */
|
|
582
|
+
entries(): URLSearchParamsIterator<[
|
|
583
|
+
HttpSearchParamsSchemaName<Schema>,
|
|
584
|
+
ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>
|
|
585
|
+
]>;
|
|
586
|
+
[Symbol.iterator](): URLSearchParamsIterator<[
|
|
587
|
+
HttpSearchParamsSchemaName<Schema>,
|
|
588
|
+
ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>
|
|
589
|
+
]>;
|
|
590
|
+
/**
|
|
591
|
+
* Checks if these search params are equal to another set of search parameters. Equality is defined as having the same
|
|
592
|
+
* keys and values, regardless of the order of the keys.
|
|
593
|
+
*
|
|
594
|
+
* @param otherParams The other search parameters to compare against.
|
|
595
|
+
* @returns `true` if the search parameters are equal, `false` otherwise.
|
|
596
|
+
*/
|
|
597
|
+
equals<OtherSchema extends Schema>(otherParams: HttpSearchParams<OtherSchema>): boolean;
|
|
598
|
+
/**
|
|
599
|
+
* Checks if these search params contain another set of search parameters. This method is less strict than
|
|
600
|
+
* {@link HttpSearchParams#equals} and only requires that all keys and values in the other search parameters are
|
|
601
|
+
* present in these search parameters.
|
|
602
|
+
*
|
|
603
|
+
* @param otherParams The other search parameters to check for containment.
|
|
604
|
+
* @returns `true` if these search parameters contain the other search parameters, `false` otherwise.
|
|
605
|
+
*/
|
|
606
|
+
contains<OtherSchema extends Schema>(otherParams: HttpSearchParams<OtherSchema>): boolean;
|
|
607
|
+
/**
|
|
608
|
+
* Converts these search params into a plain object. This method is useful for serialization and debugging purposes.
|
|
609
|
+
*
|
|
610
|
+
* **NOTE**: If a key has multiple values, the object will contain an array of values for that key. If the key has
|
|
611
|
+
* only one value, the object will contain its value directly, without an array, regardless of how the value was
|
|
612
|
+
* initialized when creating the search params object.
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* const searchParams = new HttpSearchParams({
|
|
616
|
+
* names: ['user 1', 'user 2'],
|
|
617
|
+
* name: ['user 3'],
|
|
618
|
+
* page: '1',
|
|
619
|
+
* });
|
|
620
|
+
* const object = searchParams.toObject();
|
|
621
|
+
* console.log(object); // { names: ['user 1', 'user 2'], name: 'user 3', page: '1' }
|
|
622
|
+
*
|
|
623
|
+
* @returns A plain object representation of these search params.
|
|
624
|
+
*/
|
|
625
|
+
toObject(): Schema;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
declare const HTTP_METHODS_WITH_REQUEST_BODY: readonly ["POST", "PUT", "PATCH", "DELETE"];
|
|
629
|
+
type HttpMethodWithRequestBody = (typeof HTTP_METHODS_WITH_REQUEST_BODY)[number];
|
|
630
|
+
declare const HTTP_METHODS_WITH_RESPONSE_BODY: readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
631
|
+
type HttpMethodWithResponseBody = (typeof HTTP_METHODS_WITH_RESPONSE_BODY)[number];
|
|
632
|
+
declare const HTTP_METHODS: readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
|
|
633
|
+
/**
|
|
634
|
+
* A type representing the currently supported
|
|
635
|
+
* {@link https://developer.mozilla.org/docs/Web/HTTP/Methods `HTTP methods`}.
|
|
636
|
+
*/
|
|
637
|
+
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
638
|
+
/**
|
|
639
|
+
* A schema representing the structure of an HTTP request.
|
|
640
|
+
*
|
|
641
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
642
|
+
*/
|
|
643
|
+
interface HttpRequestSchema {
|
|
644
|
+
headers?: HttpHeadersSchema.Loose;
|
|
645
|
+
searchParams?: HttpSearchParamsSchema.Loose;
|
|
646
|
+
body?: HttpBody.Loose;
|
|
647
|
+
}
|
|
648
|
+
type ConvertToStrictHttpRequestSchema<Schema> = {
|
|
649
|
+
[Key in keyof Schema]: Key extends 'headers' ? HttpHeadersSerialized<Schema[Key]> : Key extends 'searchParams' ? HttpSearchParamsSerialized<Schema[Key]> : Key extends 'body' ? HttpBody.ConvertToStrict<Schema[Key]> : Schema[Key];
|
|
650
|
+
};
|
|
651
|
+
/**
|
|
652
|
+
* A schema representing the structure of an HTTP response.
|
|
653
|
+
*
|
|
654
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
655
|
+
*/
|
|
656
|
+
interface HttpResponseSchema {
|
|
657
|
+
headers?: HttpHeadersSchema.Loose;
|
|
658
|
+
body?: HttpBody.Loose;
|
|
659
|
+
}
|
|
660
|
+
declare namespace HttpResponseSchema {
|
|
661
|
+
/** A schema representing the structure of an HTTP response with no body. */
|
|
662
|
+
interface NoBody extends Omit<HttpResponseSchema, 'body'> {
|
|
663
|
+
body?: null;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
type ConvertToStrictHttpResponseSchema<Schema> = ConvertToStrictHttpRequestSchema<Schema>;
|
|
667
|
+
/**
|
|
668
|
+
* The status codes used in HTTP responses, as defined by
|
|
669
|
+
* {@link https://httpwg.org/specs/rfc9110.html#overview.of.status.codes RFC-9110}.
|
|
670
|
+
*
|
|
671
|
+
* - `HttpStatusCode.Information`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#information_responses `1XX`}
|
|
672
|
+
* - `HttpStatusCode.Success`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#successful_responses `2XX`}
|
|
673
|
+
* - `HttpStatusCode.Redirection`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#redirection_messages `3XX`}
|
|
674
|
+
* - `HttpStatusCode.ClientError`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#client_error_responses `4XX`}
|
|
675
|
+
* - `HttpStatusCode.ServerError`: {@link https://developer.mozilla.org/docs/Web/HTTP/Status#server_error_responses `5XX`}
|
|
676
|
+
*/
|
|
677
|
+
declare namespace HttpStatusCode {
|
|
678
|
+
/**
|
|
679
|
+
* An HTTP status code in the `1XX` range, representing an informational response.
|
|
680
|
+
*
|
|
681
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#information_responses `1XX`}
|
|
682
|
+
*/
|
|
683
|
+
type Information = 100 | 101 | 102 | 103;
|
|
684
|
+
/**
|
|
685
|
+
* An HTTP status code in the `2XX` range, representing a successful response.
|
|
686
|
+
*
|
|
687
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#successful_responses `2XX`}
|
|
688
|
+
*/
|
|
689
|
+
type Success = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226;
|
|
690
|
+
/**
|
|
691
|
+
* An HTTP status code in the `3XX` range, representing a redirection response.
|
|
692
|
+
*
|
|
693
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#redirection_messages `3XX`}
|
|
694
|
+
*/
|
|
695
|
+
type Redirection = 300 | 301 | 302 | 303 | 304 | 307 | 308;
|
|
696
|
+
/**
|
|
697
|
+
* An HTTP status code in the `4XX` range, representing a client error response.
|
|
698
|
+
*
|
|
699
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#client_error_responses `4XX`}
|
|
700
|
+
*/
|
|
701
|
+
type ClientError = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451;
|
|
702
|
+
/**
|
|
703
|
+
* An HTTP status code in the `5XX` range, representing a server error response.
|
|
704
|
+
*
|
|
705
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Status#server_error_responses `5XX`}
|
|
706
|
+
*/
|
|
707
|
+
type ServerError = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
|
|
708
|
+
}
|
|
709
|
+
type HttpStatusCode = HttpStatusCode.Information | HttpStatusCode.Success | HttpStatusCode.Redirection | HttpStatusCode.ClientError | HttpStatusCode.ServerError;
|
|
710
|
+
declare namespace HttpResponseSchemaByStatusCode {
|
|
711
|
+
/** A loose version of HTTP responses by status code. JSON values are not strictly typed. */
|
|
712
|
+
type Loose = {
|
|
713
|
+
[StatusCode in HttpStatusCode]?: HttpResponseSchema;
|
|
714
|
+
};
|
|
715
|
+
/**
|
|
716
|
+
* Converts a possibly loose HTTP response schema by status code to be strictly typed. JSON values are serialized to
|
|
717
|
+
* their strict form.
|
|
718
|
+
*/
|
|
719
|
+
type ConvertToStrict<Schema extends Loose> = {
|
|
720
|
+
[StatusCode in keyof Schema]: StatusCode extends 204 ? HttpResponseSchema.NoBody : Schema[StatusCode];
|
|
721
|
+
};
|
|
722
|
+
/** A schema representing the structure of HTTP responses by status code with no body. */
|
|
723
|
+
type NoBody = {
|
|
724
|
+
[StatusCode in HttpStatusCode]?: HttpResponseSchema.NoBody;
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* A schema representing the structure of HTTP responses by status code.
|
|
729
|
+
*
|
|
730
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
731
|
+
*/
|
|
732
|
+
type HttpResponseSchemaByStatusCode = HttpResponseSchemaByStatusCode.ConvertToStrict<HttpResponseSchemaByStatusCode.Loose>;
|
|
733
|
+
type ConvertToStrictHttpResponseSchemaByStatusCode<Schema> = {
|
|
734
|
+
[Key in keyof Schema]: ConvertToStrictHttpResponseSchema<Schema[Key]>;
|
|
735
|
+
};
|
|
736
|
+
/**
|
|
737
|
+
* Extracts the status codes used in a response schema by status code.
|
|
738
|
+
*
|
|
739
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
740
|
+
*/
|
|
741
|
+
type HttpResponseSchemaStatusCode<ResponseSchemaByStatusCode extends HttpResponseSchemaByStatusCode> = keyof ResponseSchemaByStatusCode & HttpStatusCode;
|
|
742
|
+
/**
|
|
743
|
+
* A schema representing the structure of an HTTP request and response for a given method.
|
|
744
|
+
*
|
|
745
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
746
|
+
*/
|
|
747
|
+
interface HttpMethodSchema {
|
|
748
|
+
request?: HttpRequestSchema;
|
|
749
|
+
response?: HttpResponseSchemaByStatusCode;
|
|
750
|
+
}
|
|
751
|
+
declare namespace HttpMethodSchema {
|
|
752
|
+
/** A schema representing the structure of an HTTP request and response for a given method, having no request body. */
|
|
753
|
+
interface NoRequestBody extends Omit<HttpMethodSchema, 'request'> {
|
|
754
|
+
request?: Omit<HttpRequestSchema, 'body'> & {
|
|
755
|
+
body?: null;
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* A schema representing the structure of an HTTP request and response for a given method, having no request and
|
|
760
|
+
* response bodies.
|
|
761
|
+
*/
|
|
762
|
+
interface NoBody extends Omit<NoRequestBody, 'response'> {
|
|
763
|
+
response?: HttpResponseSchemaByStatusCode.NoBody;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
type ConvertToStrictMethod<Schema> = {
|
|
767
|
+
[Key in keyof Schema]: Key extends 'request' ? ConvertToStrictHttpRequestSchema<Schema[Key]> : Key extends 'response' ? ConvertToStrictHttpResponseSchemaByStatusCode<Schema[Key]> : Schema[Key];
|
|
768
|
+
};
|
|
769
|
+
/**
|
|
770
|
+
* A schema representing the structure of HTTP request and response by method.
|
|
771
|
+
*
|
|
772
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
773
|
+
*/
|
|
774
|
+
interface HttpMethodsSchema {
|
|
775
|
+
GET?: HttpMethodSchema.NoRequestBody;
|
|
776
|
+
POST?: HttpMethodSchema;
|
|
777
|
+
PUT?: HttpMethodSchema;
|
|
778
|
+
PATCH?: HttpMethodSchema;
|
|
779
|
+
DELETE?: HttpMethodSchema;
|
|
780
|
+
HEAD?: HttpMethodSchema.NoBody;
|
|
781
|
+
OPTIONS?: HttpMethodSchema.NoRequestBody;
|
|
782
|
+
}
|
|
783
|
+
type ConvertToStrictHttpMethodsSchema<Schema> = {
|
|
784
|
+
[Method in keyof Schema]: ConvertToStrictMethod<Schema[Method]>;
|
|
785
|
+
};
|
|
786
|
+
interface BaseHttpSchema {
|
|
787
|
+
[path: string]: HttpMethodsSchema;
|
|
788
|
+
}
|
|
789
|
+
type ConvertToStrictHttpSchema<Schema extends HttpSchema> = {
|
|
790
|
+
[Path in keyof Schema]: ConvertToStrictHttpMethodsSchema<Schema[Path]>;
|
|
791
|
+
};
|
|
792
|
+
/**
|
|
793
|
+
* Declares an HTTP service schema.
|
|
794
|
+
*
|
|
795
|
+
* **IMPORTANT**: the input of `HttpSchema` and all of its internal types, except bodies, must be declared inline or as
|
|
796
|
+
* a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
800
|
+
*
|
|
801
|
+
* interface UserListHeaders {
|
|
802
|
+
* accept: string;
|
|
803
|
+
* }
|
|
804
|
+
*
|
|
805
|
+
* interface UserListSearchParams {
|
|
806
|
+
* name?: string;
|
|
807
|
+
* limit?: `${number}`;
|
|
808
|
+
* }
|
|
809
|
+
*
|
|
810
|
+
* type Schema = HttpSchema<{
|
|
811
|
+
* '/users': {
|
|
812
|
+
* GET: {
|
|
813
|
+
* request: {
|
|
814
|
+
* headers: UserListHeaders;
|
|
815
|
+
* searchParams: UserListSearchParams;
|
|
816
|
+
* };
|
|
817
|
+
* response: {
|
|
818
|
+
* 200: {
|
|
819
|
+
* // Inline headers declaration
|
|
820
|
+
* headers: { 'content-type': string };
|
|
821
|
+
* body: User[];
|
|
822
|
+
* };
|
|
823
|
+
* };
|
|
824
|
+
* };
|
|
825
|
+
* };
|
|
826
|
+
* }>;
|
|
827
|
+
*
|
|
828
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
829
|
+
*/
|
|
830
|
+
type HttpSchema<Schema extends BaseHttpSchema = BaseHttpSchema> = ConvertToStrictHttpSchema<Schema>;
|
|
831
|
+
declare namespace HttpSchema {
|
|
832
|
+
/**
|
|
833
|
+
* Declares an HTTP service methods schema.
|
|
834
|
+
*
|
|
835
|
+
* **IMPORTANT**: the input of `HttpSchema.Methods` and all of its internal types, except bodies, must be declared
|
|
836
|
+
* inline or as a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
840
|
+
*
|
|
841
|
+
* type UserMethods = HttpSchema.Methods<{
|
|
842
|
+
* GET: {
|
|
843
|
+
* response: {
|
|
844
|
+
* 200: { body: User[] };
|
|
845
|
+
* };
|
|
846
|
+
* };
|
|
847
|
+
* }>;
|
|
848
|
+
*
|
|
849
|
+
* type Schema = HttpSchema<{
|
|
850
|
+
* '/users': UserMethods;
|
|
851
|
+
* }>;
|
|
852
|
+
*/
|
|
853
|
+
type Methods<Schema extends HttpMethodsSchema> = ConvertToStrictHttpMethodsSchema<Schema>;
|
|
854
|
+
/**
|
|
855
|
+
* Declares an HTTP service method schema.
|
|
856
|
+
*
|
|
857
|
+
* **IMPORTANT**: the input of `HttpSchema.Method` and all of its internal types, except bodies, must be declared
|
|
858
|
+
* inline or as a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
859
|
+
*
|
|
860
|
+
* @example
|
|
861
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
862
|
+
*
|
|
863
|
+
* type UserListMethod = HttpSchema.Method<{
|
|
864
|
+
* response: {
|
|
865
|
+
* 200: { body: User[] };
|
|
866
|
+
* };
|
|
867
|
+
* }>;
|
|
868
|
+
*
|
|
869
|
+
* type Schema = HttpSchema<{
|
|
870
|
+
* '/users': {
|
|
871
|
+
* GET: UserListMethod;
|
|
872
|
+
* };
|
|
873
|
+
* }>;
|
|
874
|
+
*/
|
|
875
|
+
type Method<Schema extends HttpMethodSchema> = ConvertToStrictMethod<Schema>;
|
|
876
|
+
/**
|
|
877
|
+
* Declares an HTTP service request schema.
|
|
878
|
+
*
|
|
879
|
+
* **IMPORTANT**: the input of `HttpSchema.Request` and all of its internal types, except bodies, must be declared
|
|
880
|
+
* inline or as a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
881
|
+
*
|
|
882
|
+
* @example
|
|
883
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
884
|
+
*
|
|
885
|
+
* type UserCreationRequest = HttpSchema.Request<{
|
|
886
|
+
* headers: { 'content-type': 'application/json' };
|
|
887
|
+
* body: User;
|
|
888
|
+
* }>;
|
|
889
|
+
*
|
|
890
|
+
* type Schema = HttpSchema<{
|
|
891
|
+
* '/users': {
|
|
892
|
+
* POST: {
|
|
893
|
+
* request: UserCreationRequest;
|
|
894
|
+
* response: {
|
|
895
|
+
* 201: { body: User };
|
|
896
|
+
* };
|
|
897
|
+
* };
|
|
898
|
+
* };
|
|
899
|
+
* }>;
|
|
900
|
+
*/
|
|
901
|
+
type Request<Schema extends HttpRequestSchema> = ConvertToStrictHttpRequestSchema<Schema>;
|
|
902
|
+
/**
|
|
903
|
+
* Declares an HTTP service response schema by status code.
|
|
904
|
+
*
|
|
905
|
+
* **IMPORTANT**: the input of `HttpSchema.ResponseByStatusCode` and all of its internal types, except bodies, must be
|
|
906
|
+
* declared inline or as a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
907
|
+
*
|
|
908
|
+
* @example
|
|
909
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
910
|
+
*
|
|
911
|
+
* type UserListResponseByStatusCode = HttpSchema.ResponseByStatusCode<{
|
|
912
|
+
* 200: { body: User[] };
|
|
913
|
+
* 400: { body: { message: string } };
|
|
914
|
+
* }>;
|
|
915
|
+
*
|
|
916
|
+
* type Schema = HttpSchema<{
|
|
917
|
+
* '/users': {
|
|
918
|
+
* GET: {
|
|
919
|
+
* response: UserListResponseByStatusCode;
|
|
920
|
+
* };
|
|
921
|
+
* };
|
|
922
|
+
* }>;
|
|
923
|
+
*/
|
|
924
|
+
type ResponseByStatusCode<Schema extends HttpResponseSchemaByStatusCode> = ConvertToStrictHttpResponseSchemaByStatusCode<Schema>;
|
|
925
|
+
/**
|
|
926
|
+
* Declares an HTTP service response schema.
|
|
927
|
+
*
|
|
928
|
+
* **IMPORTANT**: the input of `HttpSchema.Response` and all of its internal types, except bodies, must be declared
|
|
929
|
+
* inline or as a type aliases (`type`). Types other than bodies cannot be interfaces.
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
933
|
+
*
|
|
934
|
+
* type UserListSuccessResponse = HttpSchema.Response<{
|
|
935
|
+
* body: User[];
|
|
936
|
+
* }>;
|
|
937
|
+
*
|
|
938
|
+
* type Schema = HttpSchema<{
|
|
939
|
+
* '/users': {
|
|
940
|
+
* GET: {
|
|
941
|
+
* response: {
|
|
942
|
+
* 200: UserListSuccessResponse;
|
|
943
|
+
* };
|
|
944
|
+
* };
|
|
945
|
+
* };
|
|
946
|
+
* }>;
|
|
947
|
+
*/
|
|
948
|
+
type Response<Schema extends HttpResponseSchema> = ConvertToStrictHttpResponseSchema<Schema>;
|
|
949
|
+
/**
|
|
950
|
+
* Declares an HTTP body schema. JSON values are serialized to their strict form using
|
|
951
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic#jsonserialized `JSONSerialized`} if necessary.
|
|
952
|
+
*
|
|
953
|
+
* @example
|
|
954
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
955
|
+
*
|
|
956
|
+
* type UserListSuccessResponseBody = HttpSchema.Body<User[]>;
|
|
957
|
+
*
|
|
958
|
+
* type Schema = HttpSchema<{
|
|
959
|
+
* '/users': {
|
|
960
|
+
* GET: {
|
|
961
|
+
* response: {
|
|
962
|
+
* 200: { body: UserListSuccessResponseBody };
|
|
963
|
+
* };
|
|
964
|
+
* };
|
|
965
|
+
* };
|
|
966
|
+
* }>;
|
|
967
|
+
*/
|
|
968
|
+
type Body<Schema extends HttpBody.Loose> = HttpBody.ConvertToStrict<Schema>;
|
|
969
|
+
/**
|
|
970
|
+
* Declares an HTTP headers schema. Headers are serialized to their strict form using
|
|
971
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheadersserialized `HttpHeadersSerialized`} if
|
|
972
|
+
* necessary.
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
976
|
+
*
|
|
977
|
+
* type UserListHeaders = HttpSchema.Headers<{
|
|
978
|
+
* accept: 'application/json';
|
|
979
|
+
* }>;
|
|
980
|
+
*
|
|
981
|
+
* type Schema = HttpSchema<{
|
|
982
|
+
* '/users': {
|
|
983
|
+
* GET: {
|
|
984
|
+
* request: {
|
|
985
|
+
* headers: UserListHeaders;
|
|
986
|
+
* };
|
|
987
|
+
* response: {
|
|
988
|
+
* 200: { body: User[] };
|
|
989
|
+
* };
|
|
990
|
+
* };
|
|
991
|
+
* };
|
|
992
|
+
* }>;
|
|
993
|
+
*/
|
|
994
|
+
type Headers<Schema extends HttpHeadersSchema.Loose> = HttpHeadersSerialized<Schema>;
|
|
995
|
+
/**
|
|
996
|
+
* Declares an HTTP search params schema. Search params are serialized to their strict form using
|
|
997
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparamsserialized `HttpSearchParamsSerialized`}
|
|
998
|
+
* if necessary.
|
|
999
|
+
*
|
|
1000
|
+
* @example
|
|
1001
|
+
* import { type HttpSchema } from '@zimic/http';
|
|
1002
|
+
*
|
|
1003
|
+
* type UserListSearchParams = HttpSchema.SearchParams<{
|
|
1004
|
+
* limit: `${number}`;
|
|
1005
|
+
* offset: `${number}`;
|
|
1006
|
+
* }>;
|
|
1007
|
+
*
|
|
1008
|
+
* type Schema = HttpSchema<{
|
|
1009
|
+
* '/users': {
|
|
1010
|
+
* GET: {
|
|
1011
|
+
* request: {
|
|
1012
|
+
* searchParams: UserListSearchParams;
|
|
1013
|
+
* };
|
|
1014
|
+
* response: {
|
|
1015
|
+
* 200: { body: User[] };
|
|
1016
|
+
* };
|
|
1017
|
+
* };
|
|
1018
|
+
* };
|
|
1019
|
+
* }>;
|
|
1020
|
+
*/
|
|
1021
|
+
type SearchParams<Schema extends HttpSearchParamsSchema.Loose> = HttpSearchParamsSerialized<Schema>;
|
|
1022
|
+
/**
|
|
1023
|
+
* Declares an HTTP path params schema. Path params are serialized to their strict form using
|
|
1024
|
+
* {@link HttpPathParamsSerialized} if necessary.
|
|
1025
|
+
*
|
|
1026
|
+
* @example
|
|
1027
|
+
* import { type HttpSchema, InferPathParams } from '@zimic/http';
|
|
1028
|
+
*
|
|
1029
|
+
* type Schema = HttpSchema<{
|
|
1030
|
+
* '/users/:userId': {
|
|
1031
|
+
* GET: {
|
|
1032
|
+
* response: {
|
|
1033
|
+
* 200: { body: User };
|
|
1034
|
+
* };
|
|
1035
|
+
* };
|
|
1036
|
+
* };
|
|
1037
|
+
* }>;
|
|
1038
|
+
*
|
|
1039
|
+
* type UserByIdPathParams = HttpSchema.PathParams<{
|
|
1040
|
+
* userId: string;
|
|
1041
|
+
* }>;
|
|
1042
|
+
*
|
|
1043
|
+
* // Or infer from the path string
|
|
1044
|
+
* type UserByIdPathParams = InferPathParams<Schema, '/users/:userId'>;
|
|
1045
|
+
*/
|
|
1046
|
+
type PathParams<Schema extends HttpPathParamsSchema.Loose> = HttpPathParamsSerialized<Schema>;
|
|
1047
|
+
/**
|
|
1048
|
+
* Declares an HTTP form data schema. Form data is serialized to their strict form using
|
|
1049
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpformdataserialized `HttpFormDataSerialized`} if
|
|
1050
|
+
* necessary.
|
|
1051
|
+
*
|
|
1052
|
+
* @example
|
|
1053
|
+
* import { HttpFormData, type HttpSchema } from '@zimic/http';
|
|
1054
|
+
*
|
|
1055
|
+
* type UserCreationFormData = HttpFormData<
|
|
1056
|
+
* HttpSchema.FormData<{
|
|
1057
|
+
* name: string;
|
|
1058
|
+
* email: string;
|
|
1059
|
+
* }>
|
|
1060
|
+
* >;
|
|
1061
|
+
*
|
|
1062
|
+
* type Schema = HttpSchema<{
|
|
1063
|
+
* '/users': {
|
|
1064
|
+
* POST: {
|
|
1065
|
+
* request: {
|
|
1066
|
+
* body: UserCreationFormData;
|
|
1067
|
+
* };
|
|
1068
|
+
* response: {
|
|
1069
|
+
* 201: { body: User };
|
|
1070
|
+
* };
|
|
1071
|
+
* };
|
|
1072
|
+
* };
|
|
1073
|
+
* }>;
|
|
1074
|
+
*/
|
|
1075
|
+
type FormData<Schema extends HttpFormDataSchema.Loose> = HttpFormDataSerialized<Schema>;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Extracts the methods from an HTTP service schema.
|
|
1079
|
+
*
|
|
1080
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
1081
|
+
*/
|
|
1082
|
+
type HttpSchemaMethod<Schema extends HttpSchema> = IfAny<Schema, any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1083
|
+
// eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1084
|
+
keyof UnionToIntersection<Schema[keyof Schema]> & HttpMethod>;
|
|
1085
|
+
type AllowAnyStringInPathParams<Path extends string> = Path extends `${infer Prefix}:${string}/${infer Suffix}` ? `${Prefix}${string}/${AllowAnyStringInPathParams<Suffix>}` : Path extends `${infer Prefix}:${string}` ? `${Prefix}${string}` : Path;
|
|
1086
|
+
/**
|
|
1087
|
+
* Extracts the paths from an HTTP service schema. Optionally receives a second argument with one or more methods to
|
|
1088
|
+
* filter the paths with. Only the methods defined in the schema are allowed.
|
|
1089
|
+
*
|
|
1090
|
+
* @example
|
|
1091
|
+
* import { type HttpSchema, type HttpSchemaPath } from '@zimic/http';
|
|
1092
|
+
*
|
|
1093
|
+
* type Schema = HttpSchema<{
|
|
1094
|
+
* '/users': {
|
|
1095
|
+
* GET: {
|
|
1096
|
+
* response: { 200: { body: User[] } };
|
|
1097
|
+
* };
|
|
1098
|
+
* };
|
|
1099
|
+
* '/users/:userId': {
|
|
1100
|
+
* DELETE: {
|
|
1101
|
+
* response: { 200: { body: User } };
|
|
1102
|
+
* };
|
|
1103
|
+
* };
|
|
1104
|
+
* }>;
|
|
1105
|
+
*
|
|
1106
|
+
* type Path = HttpSchemaPath<Schema>;
|
|
1107
|
+
* // "/users" | "/users/:userId" | "/users/${string}"
|
|
1108
|
+
*
|
|
1109
|
+
* type GetPath = HttpSchemaPath<Schema, 'GET'>;
|
|
1110
|
+
* // "/users"
|
|
1111
|
+
*
|
|
1112
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
1113
|
+
*/
|
|
1114
|
+
declare namespace HttpSchemaPath {
|
|
1115
|
+
type LooseLiteral<Schema extends HttpSchema, Method extends HttpMethod = HttpMethod> = {
|
|
1116
|
+
[Path in keyof Schema & string]: Method extends keyof Schema[Path] ? Path : never;
|
|
1117
|
+
}[keyof Schema & string];
|
|
1118
|
+
/**
|
|
1119
|
+
* Extracts the literal paths from an HTTP service schema. Optionally receives a second argument with one or more
|
|
1120
|
+
* methods to filter the paths with. Only the methods defined in the schema are allowed.
|
|
1121
|
+
*
|
|
1122
|
+
* @example
|
|
1123
|
+
* import { type HttpSchema, type HttpSchemaPath } from '@zimic/http';
|
|
1124
|
+
*
|
|
1125
|
+
* type Schema = HttpSchema<{
|
|
1126
|
+
* '/users': {
|
|
1127
|
+
* GET: {
|
|
1128
|
+
* response: { 200: { body: User[] } };
|
|
1129
|
+
* };
|
|
1130
|
+
* };
|
|
1131
|
+
* '/users/:userId': {
|
|
1132
|
+
* DELETE: {
|
|
1133
|
+
* response: { 200: { body: User } };
|
|
1134
|
+
* };
|
|
1135
|
+
* };
|
|
1136
|
+
* }>;
|
|
1137
|
+
*
|
|
1138
|
+
* type LiteralPath = HttpSchemaPath.Literal<Schema>;
|
|
1139
|
+
* // "/users" | "/users/:userId"
|
|
1140
|
+
*
|
|
1141
|
+
* type LiteralGetPath = HttpSchemaPath.Literal<Schema, 'GET'>;
|
|
1142
|
+
* // "/users"
|
|
1143
|
+
*
|
|
1144
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
1145
|
+
*/
|
|
1146
|
+
export type Literal<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema> = HttpSchemaMethod<Schema>> = LooseLiteral<Schema, Method>;
|
|
1147
|
+
/**
|
|
1148
|
+
* Extracts the non-literal paths from an HTTP service schema. Optionally receives a second argument with one or more
|
|
1149
|
+
* methods to filter the paths with. Only the methods defined in the schema are allowed.
|
|
1150
|
+
*
|
|
1151
|
+
* @example
|
|
1152
|
+
* import { type HttpSchema, type HttpSchemaPath } from '@zimic/http';
|
|
1153
|
+
*
|
|
1154
|
+
* type Schema = HttpSchema<{
|
|
1155
|
+
* '/users': {
|
|
1156
|
+
* GET: {
|
|
1157
|
+
* response: { 200: { body: User[] } };
|
|
1158
|
+
* };
|
|
1159
|
+
* };
|
|
1160
|
+
* '/users/:userId': {
|
|
1161
|
+
* DELETE: {
|
|
1162
|
+
* response: { 200: { body: User } };
|
|
1163
|
+
* };
|
|
1164
|
+
* };
|
|
1165
|
+
* }>;
|
|
1166
|
+
*
|
|
1167
|
+
* type NonLiteralPath = HttpSchemaPath.NonLiteral<Schema>;
|
|
1168
|
+
* // "/users" | "/users/${string}"
|
|
1169
|
+
*
|
|
1170
|
+
* type NonLiteralGetPath = HttpSchemaPath.NonLiteral<Schema, 'GET'>;
|
|
1171
|
+
* // "/users"
|
|
1172
|
+
*
|
|
1173
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http‐schemas Declaring HTTP interceptor schemas}
|
|
1174
|
+
*/
|
|
1175
|
+
export type NonLiteral<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema> = HttpSchemaMethod<Schema>> = AllowAnyStringInPathParams<Literal<Schema, Method>>;
|
|
1176
|
+
export { };
|
|
1177
|
+
}
|
|
1178
|
+
type HttpSchemaPath<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema> = HttpSchemaMethod<Schema>> = HttpSchemaPath.Literal<Schema, Method> | HttpSchemaPath.NonLiteral<Schema, Method>;
|
|
1179
|
+
type LargestPathPrefix<Path extends string> = Path extends `${infer Prefix}/${infer Suffix}` ? `${Prefix}/${Suffix extends `${string}/${string}` ? LargestPathPrefix<Suffix> : ''}` : Path;
|
|
1180
|
+
type ExcludeNonLiteralPathsSupersededByLiteralPath<Path extends string> = Path extends `${LargestPathPrefix<Path>}:${string}` ? never : Path;
|
|
1181
|
+
type PreferMostStaticLiteralPath<Path extends string> = UnionHasMoreThanOneType<Path> extends true ? ExcludeNonLiteralPathsSupersededByLiteralPath<Path> : Path;
|
|
1182
|
+
type RecursiveInferHttpSchemaPath<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, NonLiteralPath extends string, LiteralPath extends HttpSchemaPath.Literal<Schema, Method>> = NonLiteralPath extends AllowAnyStringInPathParams<LiteralPath> ? NonLiteralPath extends `${AllowAnyStringInPathParams<LiteralPath>}/${string}` ? never : LiteralPath : never;
|
|
1183
|
+
type LiteralHttpSchemaPathFromNonLiteral<Schema extends HttpSchema, Method extends HttpSchemaMethod<Schema>, NonLiteralPath extends string, LiteralPath extends HttpSchemaPath.Literal<Schema, Method> = HttpSchemaPath.Literal<Schema, Method>> = PreferMostStaticLiteralPath<LiteralPath extends LiteralPath ? RecursiveInferHttpSchemaPath<Schema, Method, NonLiteralPath, LiteralPath> : never>;
|
|
1184
|
+
type RecursiveInferPathParams<Path extends string> = Path extends `${infer _Prefix}:${infer ParamName}/${infer Suffix}` ? {
|
|
1185
|
+
[Name in ParamName]: string;
|
|
1186
|
+
} & RecursiveInferPathParams<Suffix> : Path extends `${infer _Prefix}:${infer ParamName}` ? {
|
|
1187
|
+
[Name in ParamName]: string;
|
|
1188
|
+
} : {};
|
|
1189
|
+
/**
|
|
1190
|
+
* Infers the path parameters schema from a path string, optionally validating it against an {@link HttpSchema}.
|
|
1191
|
+
*
|
|
1192
|
+
* If the first argument is a {@link HttpSchema} (recommended), the second argument is checked to be a valid path in that
|
|
1193
|
+
* schema.
|
|
1194
|
+
*
|
|
1195
|
+
* @example
|
|
1196
|
+
* import { HttpSchema, InferPathParams } from '@zimic/http';
|
|
1197
|
+
*
|
|
1198
|
+
* type MySchema = HttpSchema<{
|
|
1199
|
+
* '/users/:userId': {
|
|
1200
|
+
* GET: {
|
|
1201
|
+
* response: { 200: { body: User } };
|
|
1202
|
+
* };
|
|
1203
|
+
* };
|
|
1204
|
+
* }>;
|
|
1205
|
+
*
|
|
1206
|
+
* // Using a schema to validate the path (recommended):
|
|
1207
|
+
* type PathParams = InferPathParams<MySchema, '/users/:userId'>;
|
|
1208
|
+
* // { userId: string }
|
|
1209
|
+
*
|
|
1210
|
+
* @example
|
|
1211
|
+
* import { InferPathParams } from '@zimic/http';
|
|
1212
|
+
*
|
|
1213
|
+
* // Without using a schema to validate the path (works as `PathParamsSchemaFromPath`):
|
|
1214
|
+
* type PathParams = InferPathParams<'/users/:userId'>;
|
|
1215
|
+
* // { userId: string }
|
|
1216
|
+
*/
|
|
1217
|
+
type InferPathParams<PathOrSchema extends string | HttpSchema, OptionalPath extends PathOrSchema extends HttpSchema ? HttpSchemaPath.Literal<PathOrSchema> : never = never> = Prettify<RecursiveInferPathParams<PathOrSchema extends HttpSchema ? OptionalPath : PathOrSchema extends string ? PathOrSchema : never>>;
|
|
1218
|
+
type OmitPastHttpStatusCodes<Schema extends HttpResponseSchemaByStatusCode.Loose, PastSchemas extends HttpResponseSchemaByStatusCode.Loose[]> = PastSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode.Loose> ? Omit<Schema, keyof UnionToIntersection<PastSchemas[number]>> : Schema;
|
|
1219
|
+
type RecursiveMergeHttpResponsesByStatusCode<Schemas extends HttpResponseSchemaByStatusCode.Loose[], PastSchemas extends HttpResponseSchemaByStatusCode.Loose[] = []> = Schemas extends [
|
|
1220
|
+
infer FirstSchema extends HttpResponseSchemaByStatusCode.Loose,
|
|
1221
|
+
...infer RestSchemas extends HttpResponseSchemaByStatusCode.Loose[]
|
|
1222
|
+
] ? RestSchemas extends NonEmptyArray<HttpResponseSchemaByStatusCode.Loose> ? OmitPastHttpStatusCodes<FirstSchema, PastSchemas> & RecursiveMergeHttpResponsesByStatusCode<RestSchemas, [...PastSchemas, FirstSchema]> : OmitPastHttpStatusCodes<FirstSchema, PastSchemas> : never;
|
|
1223
|
+
/**
|
|
1224
|
+
* Merges multiple HTTP response schemas by status code into a single schema. When there are duplicate status codes, the
|
|
1225
|
+
* first declaration takes precedence.
|
|
1226
|
+
*
|
|
1227
|
+
* @example
|
|
1228
|
+
* import { type HttpSchema, type HttpStatusCode, MergeHttpResponsesByStatusCode } from '@zimic/http';
|
|
1229
|
+
*
|
|
1230
|
+
* // Overriding the 400 status code with a more specific schema
|
|
1231
|
+
* // and using a generic schema for all other client errors.
|
|
1232
|
+
* type MergedResponseByStatusCode = MergeHttpResponsesByStatusCode<
|
|
1233
|
+
* [
|
|
1234
|
+
* {
|
|
1235
|
+
* 400: { body: { message: string; issues: string[] } };
|
|
1236
|
+
* },
|
|
1237
|
+
* {
|
|
1238
|
+
* [StatusCode in HttpStatusCode.ClientError]: { body: { message: string } };
|
|
1239
|
+
* },
|
|
1240
|
+
* ]
|
|
1241
|
+
* >;
|
|
1242
|
+
* // {
|
|
1243
|
+
* // 400: { body: { message: string; issues: string[] } };
|
|
1244
|
+
* // 401: { body: { message: string}; };
|
|
1245
|
+
* // 402: { body: { message: string}; };
|
|
1246
|
+
* // 403: { body: { message: string}; };
|
|
1247
|
+
* // ...
|
|
1248
|
+
* // }
|
|
1249
|
+
*
|
|
1250
|
+
* type Schema = HttpSchema<{
|
|
1251
|
+
* '/users': {
|
|
1252
|
+
* GET: { response: MergedResponseByStatusCode };
|
|
1253
|
+
* };
|
|
1254
|
+
* }>;
|
|
1255
|
+
*/
|
|
1256
|
+
type MergeHttpResponsesByStatusCode<Schemas extends HttpResponseSchemaByStatusCode.Loose[], PastSchemas extends HttpResponseSchemaByStatusCode.Loose[] = []> = HttpResponseSchemaByStatusCode.ConvertToStrict<RecursiveMergeHttpResponsesByStatusCode<Schemas, PastSchemas>>;
|
|
1257
|
+
|
|
1258
|
+
/** The body type for HTTP requests and responses. */
|
|
1259
|
+
type HttpBody = JSONValue | HttpFormData<any> | HttpSearchParams<any> | Blob | ArrayBuffer;
|
|
1260
|
+
declare namespace HttpBody {
|
|
1261
|
+
/** A loose version of the HTTP body type. JSON values are not strictly typed. */
|
|
1262
|
+
type Loose = ReplaceBy<HttpBody, JSONValue, JSONValue.Loose>;
|
|
1263
|
+
/** Convert a possibly loose HTTP body to be strictly typed. JSON values are serialized to their strict form. */
|
|
1264
|
+
type ConvertToStrict<Type> = Type extends Exclude<HttpBody, JSONValue> ? Type : JSONSerialized<Type>;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* An HTTP headers object with a strictly-typed schema. Fully compatible with the built-in
|
|
1268
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers `Headers`} class.
|
|
1269
|
+
*/
|
|
1270
|
+
type StrictHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> = Pick<HttpHeaders<Schema>, keyof Headers>;
|
|
1271
|
+
/**
|
|
1272
|
+
* An HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
1273
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
1274
|
+
*/
|
|
1275
|
+
type StrictURLSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = Pick<HttpSearchParams<Schema>, keyof URLSearchParams>;
|
|
1276
|
+
/**
|
|
1277
|
+
* An HTTP form data object with a strictly-typed schema. Fully compatible with the built-in
|
|
1278
|
+
* {@link https://developer.mozilla.org/docs/Web/API/FormData `FormData`} class.
|
|
1279
|
+
*/
|
|
1280
|
+
type StrictFormData<Schema extends HttpFormDataSchema = HttpFormDataSchema> = Pick<HttpFormData<Schema>, keyof FormData>;
|
|
1281
|
+
/**
|
|
1282
|
+
* An HTTP request with a strictly-typed JSON body. Fully compatible with the built-in
|
|
1283
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Request `Request`} class.
|
|
1284
|
+
*/
|
|
1285
|
+
interface HttpRequest<StrictBody extends HttpBody.Loose = HttpBody, StrictHeadersSchema extends HttpHeadersSchema = HttpHeadersSchema> extends Request {
|
|
1286
|
+
headers: StrictHeaders<StrictHeadersSchema>;
|
|
1287
|
+
text: () => Promise<StrictBody extends string ? StrictBody : string>;
|
|
1288
|
+
json: () => Promise<StrictBody extends string | Exclude<HttpBody, JSONValue> ? never : StrictBody>;
|
|
1289
|
+
formData: () => Promise<StrictBody extends HttpFormData<infer HttpFormDataSchema> ? StrictFormData<HttpFormDataSchema> : StrictBody extends HttpSearchParams<infer HttpSearchParamsSchema> ? StrictFormData<HttpSearchParamsSchema> : FormData>;
|
|
1290
|
+
clone: () => this;
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* An HTTP response with a strictly-typed JSON body and status code. Fully compatible with the built-in
|
|
1294
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Response `Response`} class.
|
|
1295
|
+
*/
|
|
1296
|
+
interface HttpResponse<StrictBody extends HttpBody.Loose = HttpBody, StatusCode extends number = number, StrictHeadersSchema extends HttpHeadersSchema = HttpHeadersSchema> extends Response {
|
|
1297
|
+
ok: StatusCode extends HttpStatusCode.Information | HttpStatusCode.Success | HttpStatusCode.Redirection ? true : false;
|
|
1298
|
+
status: StatusCode;
|
|
1299
|
+
headers: StrictHeaders<StrictHeadersSchema>;
|
|
1300
|
+
text: () => Promise<StrictBody extends string ? StrictBody : string>;
|
|
1301
|
+
json: () => Promise<StrictBody extends string | Exclude<HttpBody, JSONValue> ? never : StrictBody>;
|
|
1302
|
+
formData: () => Promise<StrictBody extends HttpFormData<infer HttpFormDataSchema> ? StrictFormData<HttpFormDataSchema> : StrictBody extends HttpSearchParams<infer HttpSearchParamsSchema> ? StrictFormData<HttpSearchParamsSchema> : FormData>;
|
|
1303
|
+
clone: () => this;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
export { type AllowAnyStringInPathParams, HTTP_METHODS, HTTP_METHODS_WITH_REQUEST_BODY, HTTP_METHODS_WITH_RESPONSE_BODY, HttpBody, HttpFormData, HttpFormDataSchema, HttpFormDataSchemaName, type HttpFormDataSerialized, HttpHeaders, type HttpHeadersInit, HttpHeadersSchema, type HttpHeadersSchemaName, type HttpHeadersSchemaTuple, type HttpHeadersSerialized, type HttpMethod, HttpMethodSchema, type HttpMethodWithRequestBody, type HttpMethodWithResponseBody, type HttpMethodsSchema, HttpPathParamsSchema, type HttpPathParamsSerialized, type HttpRequest, type HttpRequestSchema, type HttpResponse, HttpResponseSchema, HttpResponseSchemaByStatusCode, type HttpResponseSchemaStatusCode, HttpSchema, type HttpSchemaMethod, HttpSchemaPath, HttpSearchParams, type HttpSearchParamsInit, HttpSearchParamsSchema, HttpSearchParamsSchemaName, type HttpSearchParamsSchemaTuple, type HttpSearchParamsSerialized, HttpStatusCode, type InferPathParams, type JSONSerialized, JSONValue, type LiteralHttpSchemaPathFromNonLiteral, type MergeHttpResponsesByStatusCode, type StrictFormData, type StrictHeaders, type StrictURLSearchParams };
|