@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/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type { JSONValue, JSONSerialized } from './types/json';
|
|
2
|
+
|
|
3
|
+
export { default as HttpFormData } from './formData/HttpFormData';
|
|
4
|
+
export type { HttpFormDataSchema, HttpFormDataSchemaName, HttpFormDataSerialized } from './formData/types';
|
|
5
|
+
|
|
6
|
+
export type { HttpPathParamsSchema, HttpPathParamsSerialized } from './pathParams/types';
|
|
7
|
+
|
|
8
|
+
export { default as HttpHeaders } from './headers/HttpHeaders';
|
|
9
|
+
export type {
|
|
10
|
+
HttpHeadersInit,
|
|
11
|
+
HttpHeadersSchema,
|
|
12
|
+
HttpHeadersSchemaTuple,
|
|
13
|
+
HttpHeadersSchemaName,
|
|
14
|
+
HttpHeadersSerialized,
|
|
15
|
+
} from './headers/types';
|
|
16
|
+
|
|
17
|
+
export { default as HttpSearchParams } from './searchParams/HttpSearchParams';
|
|
18
|
+
export type {
|
|
19
|
+
HttpSearchParamsInit,
|
|
20
|
+
HttpSearchParamsSchema,
|
|
21
|
+
HttpSearchParamsSchemaTuple,
|
|
22
|
+
HttpSearchParamsSchemaName,
|
|
23
|
+
HttpSearchParamsSerialized,
|
|
24
|
+
} from './searchParams/types';
|
|
25
|
+
|
|
26
|
+
export type {
|
|
27
|
+
HttpBody,
|
|
28
|
+
HttpRequest,
|
|
29
|
+
HttpResponse,
|
|
30
|
+
StrictHeaders,
|
|
31
|
+
StrictURLSearchParams,
|
|
32
|
+
StrictFormData,
|
|
33
|
+
} from './types/requests';
|
|
34
|
+
|
|
35
|
+
export type {
|
|
36
|
+
HttpSchema,
|
|
37
|
+
HttpMethod,
|
|
38
|
+
HttpMethodWithRequestBody,
|
|
39
|
+
HttpMethodWithResponseBody,
|
|
40
|
+
HttpStatusCode,
|
|
41
|
+
HttpRequestSchema,
|
|
42
|
+
HttpResponseSchema,
|
|
43
|
+
HttpResponseSchemaByStatusCode,
|
|
44
|
+
HttpResponseSchemaStatusCode,
|
|
45
|
+
HttpMethodSchema,
|
|
46
|
+
HttpMethodsSchema,
|
|
47
|
+
HttpSchemaMethod,
|
|
48
|
+
AllowAnyStringInPathParams,
|
|
49
|
+
LiteralHttpSchemaPathFromNonLiteral,
|
|
50
|
+
HttpSchemaPath,
|
|
51
|
+
InferPathParams,
|
|
52
|
+
MergeHttpResponsesByStatusCode,
|
|
53
|
+
} from './types/schema';
|
|
54
|
+
|
|
55
|
+
export { HTTP_METHODS, HTTP_METHODS_WITH_REQUEST_BODY, HTTP_METHODS_WITH_RESPONSE_BODY } from './types/schema';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { IfNever } from '@/types/utils';
|
|
2
|
+
|
|
3
|
+
export interface HttpPathParamsSchema {
|
|
4
|
+
[paramName: string]: string | undefined;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export namespace HttpPathParamsSchema {
|
|
8
|
+
/** A schema for loose HTTP path parameters. Parameter values are not strictly typed. */
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export type Loose = Record<string, any>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type PrimitiveHttpPathParamsSerialized<Type> = Type extends HttpPathParamsSchema[string]
|
|
14
|
+
? Type
|
|
15
|
+
: Type extends (infer _ArrayItem)[]
|
|
16
|
+
? never
|
|
17
|
+
: Type extends number
|
|
18
|
+
? `${number}`
|
|
19
|
+
: Type extends boolean
|
|
20
|
+
? `${boolean}`
|
|
21
|
+
: Type extends null
|
|
22
|
+
? undefined
|
|
23
|
+
: never;
|
|
24
|
+
/**
|
|
25
|
+
* Recursively converts a schema to its path parameters-serialized version. Numbers and booleans are converted to
|
|
26
|
+
* `${number}` and `${boolean}` respectively, null becomes undefined and not serializable values are excluded, such as
|
|
27
|
+
* functions and dates.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* import { type HttpPathParamsSerialized } from '@zimic/http';
|
|
31
|
+
*
|
|
32
|
+
* type Params = HttpPathParamsSerialized<{
|
|
33
|
+
* userId: string;
|
|
34
|
+
* notificationId: number | null;
|
|
35
|
+
* full?: boolean;
|
|
36
|
+
* from?: Date;
|
|
37
|
+
* method: () => void;
|
|
38
|
+
* }>;
|
|
39
|
+
* // {
|
|
40
|
+
* // userId: string;
|
|
41
|
+
* // notificationId: `${number}` | undefined;
|
|
42
|
+
* // full?: "false" | "true";
|
|
43
|
+
* // }
|
|
44
|
+
*/
|
|
45
|
+
export type HttpPathParamsSerialized<Type> = Type extends HttpPathParamsSchema
|
|
46
|
+
? Type
|
|
47
|
+
: Type extends (infer _ArrayItem)[]
|
|
48
|
+
? never
|
|
49
|
+
: Type extends Date
|
|
50
|
+
? never
|
|
51
|
+
: Type extends (...parameters: never[]) => unknown
|
|
52
|
+
? never
|
|
53
|
+
: Type extends symbol
|
|
54
|
+
? never
|
|
55
|
+
: Type extends Map<infer _Key, infer _Value>
|
|
56
|
+
? never
|
|
57
|
+
: Type extends Set<infer _Value>
|
|
58
|
+
? never
|
|
59
|
+
: Type extends object
|
|
60
|
+
? {
|
|
61
|
+
[Key in keyof Type as IfNever<
|
|
62
|
+
PrimitiveHttpPathParamsSerialized<Type[Key]>,
|
|
63
|
+
never,
|
|
64
|
+
Key
|
|
65
|
+
>]: PrimitiveHttpPathParamsSerialized<Type[Key]>;
|
|
66
|
+
}
|
|
67
|
+
: never;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { ReplaceBy, ArrayItemIfArray } from '@/types/utils';
|
|
2
|
+
|
|
3
|
+
import { HttpSearchParamsSchema, HttpSearchParamsInit, HttpSearchParamsSchemaName } from './types';
|
|
4
|
+
|
|
5
|
+
function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema>(schema: Schema) {
|
|
6
|
+
const schemaWithPrimitiveProperties = Object.entries(schema).reduce<Record<string, string>>(
|
|
7
|
+
(accumulated, [key, value]) => {
|
|
8
|
+
if (value !== undefined && !Array.isArray(value)) {
|
|
9
|
+
accumulated[key] = value;
|
|
10
|
+
}
|
|
11
|
+
return accumulated;
|
|
12
|
+
},
|
|
13
|
+
{},
|
|
14
|
+
);
|
|
15
|
+
return schemaWithPrimitiveProperties;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An extended HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
20
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
21
|
+
*
|
|
22
|
+
* **IMPORTANT**: the input of `HttpSearchParams` and all of its internal types must be declared inline or as a type
|
|
23
|
+
* aliases (`type`). They cannot be interfaces.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* import { HttpSearchParams } from '@zimic/http';
|
|
27
|
+
*
|
|
28
|
+
* const searchParams = new HttpSearchParams<{
|
|
29
|
+
* names?: string[];
|
|
30
|
+
* page?: `${number}`;
|
|
31
|
+
* }>({
|
|
32
|
+
* names: ['user 1', 'user 2'],
|
|
33
|
+
* page: '1',
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const names = searchParams.getAll('names');
|
|
37
|
+
* console.log(names); // ['user 1', 'user 2']
|
|
38
|
+
*
|
|
39
|
+
* const page = searchParams.get('page');
|
|
40
|
+
* console.log(page); // '1'
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams` API reference}
|
|
43
|
+
*/
|
|
44
|
+
class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> extends URLSearchParams {
|
|
45
|
+
constructor(init?: HttpSearchParamsInit<Schema>) {
|
|
46
|
+
if (init instanceof URLSearchParams || Array.isArray(init) || typeof init === 'string' || !init) {
|
|
47
|
+
super(init);
|
|
48
|
+
} else {
|
|
49
|
+
super(pickPrimitiveProperties(init));
|
|
50
|
+
this.populateInitArrayProperties(init);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private populateInitArrayProperties(init: Schema) {
|
|
55
|
+
for (const [key, value] of Object.entries(init)) {
|
|
56
|
+
if (Array.isArray(value)) {
|
|
57
|
+
for (const item of value) {
|
|
58
|
+
super.append(key, item);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/set MDN Reference} */
|
|
65
|
+
set<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
66
|
+
name: Name,
|
|
67
|
+
value: ArrayItemIfArray<NonNullable<Schema[Name]>>,
|
|
68
|
+
): void {
|
|
69
|
+
super.set(name, value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/append MDN Reference} */
|
|
73
|
+
append<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
74
|
+
name: Name,
|
|
75
|
+
value: ArrayItemIfArray<NonNullable<Schema[Name]>>,
|
|
76
|
+
): void {
|
|
77
|
+
super.append(name, value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the value of the entry associated to a key name.
|
|
82
|
+
*
|
|
83
|
+
* If the key might have multiple values, use {@link HttpSearchParams#getAll} instead.
|
|
84
|
+
*
|
|
85
|
+
* @param name The name of the key to get the value of.
|
|
86
|
+
* @returns The value associated with the key name, or `null` if the key does not exist.
|
|
87
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/get MDN Reference}
|
|
88
|
+
*/
|
|
89
|
+
get<Name extends HttpSearchParamsSchemaName.NonArray<Schema>>(
|
|
90
|
+
name: Name,
|
|
91
|
+
): ReplaceBy<ArrayItemIfArray<Schema[Name]>, undefined, null> {
|
|
92
|
+
return super.get(name) as ReplaceBy<ArrayItemIfArray<Schema[Name]>, undefined, null>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get all the values of the entry associated with a key name.
|
|
97
|
+
*
|
|
98
|
+
* If the key has at most one value, use {@link HttpSearchParams#get} instead.
|
|
99
|
+
*
|
|
100
|
+
* @param name The name of the key to get the values of.
|
|
101
|
+
* @returns An array of values associated with the key name, or an empty array if the key does not exist.
|
|
102
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll MDN Reference}
|
|
103
|
+
*/
|
|
104
|
+
getAll<Name extends HttpSearchParamsSchemaName.Array<Schema>>(
|
|
105
|
+
name: Name,
|
|
106
|
+
): ArrayItemIfArray<NonNullable<Schema[Name]>>[] {
|
|
107
|
+
return super.getAll(name) as ArrayItemIfArray<NonNullable<Schema[Name]>>[];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/has MDN Reference} */
|
|
111
|
+
has<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
112
|
+
name: Name,
|
|
113
|
+
value?: ArrayItemIfArray<NonNullable<Schema[Name]>>,
|
|
114
|
+
): boolean {
|
|
115
|
+
return super.has(name, value);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete MDN Reference} */
|
|
119
|
+
delete<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
120
|
+
name: Name,
|
|
121
|
+
value?: ArrayItemIfArray<NonNullable<Schema[Name]>>,
|
|
122
|
+
): void {
|
|
123
|
+
super.delete(name, value);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
forEach<This extends HttpSearchParams<Schema>>(
|
|
127
|
+
callback: <Key extends HttpSearchParamsSchemaName<Schema>>(
|
|
128
|
+
value: ArrayItemIfArray<NonNullable<Schema[Key]>>,
|
|
129
|
+
key: Key,
|
|
130
|
+
parent: HttpSearchParams<Schema>,
|
|
131
|
+
) => void,
|
|
132
|
+
thisArg?: This,
|
|
133
|
+
): void {
|
|
134
|
+
super.forEach(callback as (value: string, key: string, parent: URLSearchParams) => void, thisArg);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys MDN Reference} */
|
|
138
|
+
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<Schema>> {
|
|
139
|
+
return super.keys() as URLSearchParamsIterator<HttpSearchParamsSchemaName<Schema>>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/values MDN Reference} */
|
|
143
|
+
values(): URLSearchParamsIterator<ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>> {
|
|
144
|
+
return super.values() as URLSearchParamsIterator<
|
|
145
|
+
ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>
|
|
146
|
+
>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries MDN Reference} */
|
|
150
|
+
entries(): URLSearchParamsIterator<
|
|
151
|
+
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
152
|
+
> {
|
|
153
|
+
return super.entries() as URLSearchParamsIterator<
|
|
154
|
+
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
155
|
+
>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
[Symbol.iterator](): URLSearchParamsIterator<
|
|
159
|
+
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
160
|
+
> {
|
|
161
|
+
return super[Symbol.iterator]() as URLSearchParamsIterator<
|
|
162
|
+
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
163
|
+
>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Checks if these search params are equal to another set of search parameters. Equality is defined as having the same
|
|
168
|
+
* keys and values, regardless of the order of the keys.
|
|
169
|
+
*
|
|
170
|
+
* @param otherParams The other search parameters to compare against.
|
|
171
|
+
* @returns `true` if the search parameters are equal, `false` otherwise.
|
|
172
|
+
*/
|
|
173
|
+
equals<OtherSchema extends Schema>(otherParams: HttpSearchParams<OtherSchema>): boolean {
|
|
174
|
+
for (const [key, otherValue] of otherParams.entries()) {
|
|
175
|
+
const values = super.getAll.call(this, key);
|
|
176
|
+
|
|
177
|
+
const haveSameNumberOfValues = values.length === super.getAll.call(otherParams, key).length;
|
|
178
|
+
if (!haveSameNumberOfValues) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const valueExists = values.includes(otherValue);
|
|
183
|
+
if (!valueExists) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return this.size === otherParams.size;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Checks if these search params contain another set of search parameters. This method is less strict than
|
|
193
|
+
* {@link HttpSearchParams#equals} and only requires that all keys and values in the other search parameters are
|
|
194
|
+
* present in these search parameters.
|
|
195
|
+
*
|
|
196
|
+
* @param otherParams The other search parameters to check for containment.
|
|
197
|
+
* @returns `true` if these search parameters contain the other search parameters, `false` otherwise.
|
|
198
|
+
*/
|
|
199
|
+
contains<OtherSchema extends Schema>(otherParams: HttpSearchParams<OtherSchema>): boolean {
|
|
200
|
+
for (const [key, otherValue] of otherParams.entries()) {
|
|
201
|
+
const values = super.getAll.call(this, key);
|
|
202
|
+
|
|
203
|
+
const haveCompatibleNumberOfValues = values.length >= super.getAll.call(otherParams, key).length;
|
|
204
|
+
if (!haveCompatibleNumberOfValues) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const valueExists = values.includes(otherValue);
|
|
209
|
+
if (!valueExists) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Converts these search params into a plain object. This method is useful for serialization and debugging purposes.
|
|
219
|
+
*
|
|
220
|
+
* **NOTE**: If a key has multiple values, the object will contain an array of values for that key. If the key has
|
|
221
|
+
* only one value, the object will contain its value directly, without an array, regardless of how the value was
|
|
222
|
+
* initialized when creating the search params object.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* const searchParams = new HttpSearchParams({
|
|
226
|
+
* names: ['user 1', 'user 2'],
|
|
227
|
+
* name: ['user 3'],
|
|
228
|
+
* page: '1',
|
|
229
|
+
* });
|
|
230
|
+
* const object = searchParams.toObject();
|
|
231
|
+
* console.log(object); // { names: ['user 1', 'user 2'], name: 'user 3', page: '1' }
|
|
232
|
+
*
|
|
233
|
+
* @returns A plain object representation of these search params.
|
|
234
|
+
*/
|
|
235
|
+
toObject() {
|
|
236
|
+
const object = {} as Schema;
|
|
237
|
+
|
|
238
|
+
type SchemaValue = Schema[HttpSearchParamsSchemaName<Schema>];
|
|
239
|
+
|
|
240
|
+
for (const [key, value] of this.entries()) {
|
|
241
|
+
if (key in object) {
|
|
242
|
+
const existingValue = object[key];
|
|
243
|
+
|
|
244
|
+
if (Array.isArray<SchemaValue>(existingValue)) {
|
|
245
|
+
existingValue.push(value as SchemaValue);
|
|
246
|
+
} else {
|
|
247
|
+
object[key] = [existingValue, value] as SchemaValue;
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
object[key] = value as SchemaValue;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return object;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export default HttpSearchParams;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { ArrayItemIfArray, IfNever, NonArrayKey, ArrayKey } from '@/types/utils';
|
|
2
|
+
|
|
3
|
+
import HttpSearchParams from './HttpSearchParams';
|
|
4
|
+
|
|
5
|
+
/** A schema for strict HTTP URL search parameters. */
|
|
6
|
+
export interface HttpSearchParamsSchema {
|
|
7
|
+
[paramName: string]: string | string[] | undefined;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export namespace HttpSearchParamsSchema {
|
|
11
|
+
/** A schema for loose HTTP URL search parameters. Parameter values are not strictly typed. */
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
export type Loose = Record<string, any>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A strict tuple representation of a {@link HttpSearchParamsSchema}. */
|
|
17
|
+
export type HttpSearchParamsSchemaTuple<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = {
|
|
18
|
+
[Key in keyof Schema & string]: [Key, ArrayItemIfArray<NonNullable<Schema[Key]>>];
|
|
19
|
+
}[keyof Schema & string];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* An initialization value for
|
|
23
|
+
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams`}.
|
|
24
|
+
*/
|
|
25
|
+
export type HttpSearchParamsInit<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> =
|
|
26
|
+
| string
|
|
27
|
+
| URLSearchParams
|
|
28
|
+
| Schema
|
|
29
|
+
| HttpSearchParams<Schema>
|
|
30
|
+
| HttpSearchParamsSchemaTuple<Schema>[];
|
|
31
|
+
|
|
32
|
+
export namespace HttpSearchParamsSchemaName {
|
|
33
|
+
/** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are arrays. */
|
|
34
|
+
export type Array<Schema extends HttpSearchParamsSchema> = IfNever<Schema, never, ArrayKey<Schema> & string>;
|
|
35
|
+
|
|
36
|
+
/** Extracts the names of the search params defined in a {@link HttpSearchParamsSchema} that are not arrays. */
|
|
37
|
+
export type NonArray<Schema extends HttpSearchParamsSchema> = IfNever<Schema, never, NonArrayKey<Schema> & string>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Extracts the names of the search params defined in a {@link HttpSearchParamsSchema}. Each key is considered a search
|
|
42
|
+
* param name. `HttpSearchParamsSchemaName.Array` can be used to extract the names of array search params, whereas
|
|
43
|
+
* `HttpSearchParamsSchemaName.NonArray` extracts the names of non-array search params.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* import { type HttpSearchParamsSchemaName } from '@zimic/http';
|
|
47
|
+
*
|
|
48
|
+
* type SearchParamsName = HttpSearchParamsSchemaName<{
|
|
49
|
+
* query?: string[];
|
|
50
|
+
* page?: `${number}`;
|
|
51
|
+
* perPage?: `${number}`;
|
|
52
|
+
* }>;
|
|
53
|
+
* // "query" | "page" | "perPage"
|
|
54
|
+
*
|
|
55
|
+
* type ArraySearchParamsName = HttpSearchParamsSchemaName.Array<{
|
|
56
|
+
* query?: string[];
|
|
57
|
+
* page?: `${number}`;
|
|
58
|
+
* perPage?: `${number}`;
|
|
59
|
+
* }>;
|
|
60
|
+
* // "query"
|
|
61
|
+
*
|
|
62
|
+
* type NonArraySearchParamsName = HttpSearchParamsSchemaName.NonArray<{
|
|
63
|
+
* query?: string[];
|
|
64
|
+
* page?: `${number}`;
|
|
65
|
+
* perPage?: `${number}`;
|
|
66
|
+
* }>;
|
|
67
|
+
* // "page" | "perPage"
|
|
68
|
+
*/
|
|
69
|
+
export type HttpSearchParamsSchemaName<Schema extends HttpSearchParamsSchema> = IfNever<
|
|
70
|
+
Schema,
|
|
71
|
+
never,
|
|
72
|
+
keyof Schema & string
|
|
73
|
+
>;
|
|
74
|
+
|
|
75
|
+
type PrimitiveHttpSearchParamsSerialized<Type> = Type extends HttpSearchParamsSchema[string]
|
|
76
|
+
? Type
|
|
77
|
+
: Type extends (infer ArrayItem)[]
|
|
78
|
+
? ArrayItem extends (infer _InternalArrayItem)[]
|
|
79
|
+
? never
|
|
80
|
+
: PrimitiveHttpSearchParamsSerialized<ArrayItem>[]
|
|
81
|
+
: Type extends number
|
|
82
|
+
? `${number}`
|
|
83
|
+
: Type extends boolean
|
|
84
|
+
? `${boolean}`
|
|
85
|
+
: Type extends null
|
|
86
|
+
? undefined
|
|
87
|
+
: never;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Recursively converts a schema to its
|
|
91
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams URLSearchParams}-serialized version. Numbers and
|
|
92
|
+
* booleans are converted to `${number}` and `${boolean}` respectively, null becomes undefined and not serializable
|
|
93
|
+
* values are excluded, such as functions and dates.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* import { type HttpSearchParamsSerialized } from '@zimic/http';
|
|
97
|
+
*
|
|
98
|
+
* type Params = HttpSearchParamsSerialized<{
|
|
99
|
+
* query: string | null;
|
|
100
|
+
* page?: number;
|
|
101
|
+
* full?: boolean;
|
|
102
|
+
* date: Date;
|
|
103
|
+
* method: () => void;
|
|
104
|
+
* }>;
|
|
105
|
+
* // {
|
|
106
|
+
* // query: string | undefined;
|
|
107
|
+
* // page?: `${number}`;
|
|
108
|
+
* // full?: "false" | "true";
|
|
109
|
+
* // }
|
|
110
|
+
*/
|
|
111
|
+
export type HttpSearchParamsSerialized<Type> = Type extends HttpSearchParamsSchema
|
|
112
|
+
? Type
|
|
113
|
+
: Type extends (infer _ArrayItem)[]
|
|
114
|
+
? never
|
|
115
|
+
: Type extends Date
|
|
116
|
+
? never
|
|
117
|
+
: Type extends (...parameters: never[]) => unknown
|
|
118
|
+
? never
|
|
119
|
+
: Type extends symbol
|
|
120
|
+
? never
|
|
121
|
+
: Type extends Map<infer _Key, infer _Value>
|
|
122
|
+
? never
|
|
123
|
+
: Type extends Set<infer _Value>
|
|
124
|
+
? never
|
|
125
|
+
: Type extends object
|
|
126
|
+
? {
|
|
127
|
+
[Key in keyof Type as IfNever<
|
|
128
|
+
PrimitiveHttpSearchParamsSerialized<Type[Key]>,
|
|
129
|
+
never,
|
|
130
|
+
Key
|
|
131
|
+
>]: PrimitiveHttpSearchParamsSerialized<Type[Key]>;
|
|
132
|
+
}
|
|
133
|
+
: never;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import TypegenNamespace from './namespace/TypegenNamespace';
|
|
2
|
+
|
|
3
|
+
export type { OpenAPITypegenOptions } from './openapi/generate';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A namespace of type generation resources.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐typegen `zimic typegen` API reference}
|
|
9
|
+
*/
|
|
10
|
+
export const typegen = Object.freeze(new TypegenNamespace());
|
|
11
|
+
|
|
12
|
+
export type { TypegenNamespace };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import generateTypesFromOpenAPI from '../openapi/generate';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A namespace of type generation resources.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐typegen `zimic/typegen` API reference}
|
|
7
|
+
*/
|
|
8
|
+
class TypegenNamespace {
|
|
9
|
+
/**
|
|
10
|
+
* Generates TypeScript types from an OpenAPI schema.
|
|
11
|
+
*
|
|
12
|
+
* @param options The options to use when generating the types.
|
|
13
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐typegen#typegengeneratefromopenapioptions `typegen.generateFromOpenAPI(options)` API reference}
|
|
14
|
+
*/
|
|
15
|
+
generateFromOpenAPI = generateTypesFromOpenAPI;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default TypegenNamespace;
|