@zimic/http 0.3.2 → 0.4.0-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-4OL77NXP.mjs → chunk-KVTV4E5K.mjs} +28 -100
- package/dist/chunk-KVTV4E5K.mjs.map +1 -0
- package/dist/{chunk-56PYXPSB.js → chunk-LOHINQWU.js} +28 -100
- package/dist/chunk-LOHINQWU.js.map +1 -0
- package/dist/cli.js +7 -7
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.ts +71 -132
- package/dist/index.js +3 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -13
- package/dist/index.mjs.map +1 -1
- package/dist/typegen.js +2 -2
- package/dist/typegen.mjs +1 -1
- package/package.json +2 -2
- package/src/formData/HttpFormData.ts +14 -11
- package/src/headers/HttpHeaders.ts +12 -9
- package/src/headers/types.ts +2 -2
- package/src/index.ts +1 -3
- package/src/searchParams/HttpSearchParams.ts +23 -15
- package/src/searchParams/types.ts +2 -2
- package/src/typegen/openapi/transform/components.ts +1 -11
- package/src/typegen/openapi/transform/context.ts +0 -3
- package/src/typegen/openapi/transform/methods.ts +23 -88
- package/src/typegen/openapi/transform/operations.ts +1 -11
- package/src/typegen/openapi/transform/paths.ts +1 -17
- package/src/types/requests.ts +15 -18
- package/src/types/schema.ts +50 -147
- package/dist/chunk-4OL77NXP.mjs.map +0 -1
- package/dist/chunk-56PYXPSB.js.map +0 -1
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { ReplaceBy, ArrayItemIfArray } from '@zimic/utils/types';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import {
|
|
4
|
+
HttpSearchParamsSchema,
|
|
5
|
+
HttpSearchParamsInit,
|
|
6
|
+
HttpSearchParamsSchemaName,
|
|
7
|
+
HttpSearchParamsSerialized,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
10
|
+
function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema.Loose>(schema: Schema) {
|
|
6
11
|
const schemaWithPrimitiveProperties = Object.entries(schema).reduce<Record<string, string>>(
|
|
7
12
|
(accumulated, [key, value]) => {
|
|
8
13
|
if (value !== undefined && !Array.isArray(value)) {
|
|
9
|
-
accumulated[key] = value;
|
|
14
|
+
accumulated[key] = String(value);
|
|
10
15
|
}
|
|
11
16
|
return accumulated;
|
|
12
17
|
},
|
|
@@ -41,8 +46,11 @@ function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema>(schema:
|
|
|
41
46
|
*
|
|
42
47
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams` API reference}
|
|
43
48
|
*/
|
|
44
|
-
class HttpSearchParams<
|
|
45
|
-
|
|
49
|
+
class HttpSearchParams<
|
|
50
|
+
LooseSchema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose,
|
|
51
|
+
Schema extends HttpSearchParamsSchema = HttpSearchParamsSerialized<LooseSchema>,
|
|
52
|
+
> extends URLSearchParams {
|
|
53
|
+
constructor(init?: HttpSearchParamsInit<LooseSchema>) {
|
|
46
54
|
if (init instanceof URLSearchParams || Array.isArray(init) || typeof init === 'string' || !init) {
|
|
47
55
|
super(init);
|
|
48
56
|
} else {
|
|
@@ -51,11 +59,11 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
51
59
|
}
|
|
52
60
|
}
|
|
53
61
|
|
|
54
|
-
private populateInitArrayProperties(init:
|
|
62
|
+
private populateInitArrayProperties(init: LooseSchema) {
|
|
55
63
|
for (const [key, value] of Object.entries(init)) {
|
|
56
64
|
if (Array.isArray(value)) {
|
|
57
|
-
for (const item of value) {
|
|
58
|
-
super.append(key, item);
|
|
65
|
+
for (const item of value as unknown[]) {
|
|
66
|
+
super.append(key, String(item));
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
}
|
|
@@ -64,7 +72,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
64
72
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/set MDN Reference} */
|
|
65
73
|
set<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
66
74
|
name: Name,
|
|
67
|
-
value: ArrayItemIfArray<NonNullable<
|
|
75
|
+
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
68
76
|
): void {
|
|
69
77
|
super.set(name, value);
|
|
70
78
|
}
|
|
@@ -72,7 +80,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
72
80
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/append MDN Reference} */
|
|
73
81
|
append<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
74
82
|
name: Name,
|
|
75
|
-
value: ArrayItemIfArray<NonNullable<
|
|
83
|
+
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
76
84
|
): void {
|
|
77
85
|
super.append(name, value);
|
|
78
86
|
}
|
|
@@ -110,7 +118,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
110
118
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/has MDN Reference} */
|
|
111
119
|
has<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
112
120
|
name: Name,
|
|
113
|
-
value?: ArrayItemIfArray<NonNullable<
|
|
121
|
+
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
114
122
|
): boolean {
|
|
115
123
|
return super.has(name, value);
|
|
116
124
|
}
|
|
@@ -118,7 +126,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
118
126
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete MDN Reference} */
|
|
119
127
|
delete<Name extends HttpSearchParamsSchemaName<Schema>>(
|
|
120
128
|
name: Name,
|
|
121
|
-
value?: ArrayItemIfArray<NonNullable<
|
|
129
|
+
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
122
130
|
): void {
|
|
123
131
|
super.delete(name, value);
|
|
124
132
|
}
|
|
@@ -170,7 +178,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
170
178
|
* @param otherParams The other search parameters to compare against.
|
|
171
179
|
* @returns `true` if the search parameters are equal, `false` otherwise.
|
|
172
180
|
*/
|
|
173
|
-
equals<OtherSchema extends
|
|
181
|
+
equals<OtherSchema extends LooseSchema>(otherParams: HttpSearchParams<OtherSchema>): boolean {
|
|
174
182
|
return this.contains(otherParams) && this.size === otherParams.size;
|
|
175
183
|
}
|
|
176
184
|
|
|
@@ -182,7 +190,7 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
182
190
|
* @param otherParams The other search parameters to check for containment.
|
|
183
191
|
* @returns `true` if these search parameters contain the other search parameters, `false` otherwise.
|
|
184
192
|
*/
|
|
185
|
-
contains<OtherSchema extends
|
|
193
|
+
contains<OtherSchema extends LooseSchema>(otherParams: HttpSearchParams<OtherSchema>): boolean {
|
|
186
194
|
for (const [key, otherValue] of otherParams.entries()) {
|
|
187
195
|
const values = super.getAll.call(this, key);
|
|
188
196
|
|
|
@@ -14,7 +14,7 @@ export namespace HttpSearchParamsSchema {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/** A strict tuple representation of a {@link HttpSearchParamsSchema}. */
|
|
17
|
-
export type HttpSearchParamsSchemaTuple<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = {
|
|
17
|
+
export type HttpSearchParamsSchemaTuple<Schema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose> = {
|
|
18
18
|
[Key in keyof Schema & string]: [Key, ArrayItemIfArray<NonNullable<Schema[Key]>>];
|
|
19
19
|
}[keyof Schema & string];
|
|
20
20
|
|
|
@@ -22,7 +22,7 @@ export type HttpSearchParamsSchemaTuple<Schema extends HttpSearchParamsSchema =
|
|
|
22
22
|
* An initialization value for
|
|
23
23
|
* {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams`}.
|
|
24
24
|
*/
|
|
25
|
-
export type HttpSearchParamsInit<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> =
|
|
25
|
+
export type HttpSearchParamsInit<Schema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose> =
|
|
26
26
|
| string
|
|
27
27
|
| URLSearchParams
|
|
28
28
|
| Schema
|
|
@@ -264,16 +264,6 @@ function processPendingRequestComponentActions(component: RequestComponent, cont
|
|
|
264
264
|
return { bodyQuestionToken };
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
-
function wrapRequestComponentType(type: ts.TypeNode, context: TypeTransformContext) {
|
|
268
|
-
context.typeImports.http.add('HttpSchema');
|
|
269
|
-
|
|
270
|
-
const httpSchemaRequestWrapper = ts.factory.createQualifiedName(
|
|
271
|
-
ts.factory.createIdentifier('HttpSchema'),
|
|
272
|
-
ts.factory.createIdentifier('Request'),
|
|
273
|
-
);
|
|
274
|
-
return ts.factory.createTypeReferenceNode(httpSchemaRequestWrapper, [type]);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
267
|
function normalizeRequestComponent(component: Component, context: TypeTransformContext) {
|
|
278
268
|
/* istanbul ignore if -- @preserve
|
|
279
269
|
* Component group members in `requests` are always expected the be request components. */
|
|
@@ -289,7 +279,7 @@ function normalizeRequestComponent(component: Component, context: TypeTransformC
|
|
|
289
279
|
component.modifiers,
|
|
290
280
|
component.name,
|
|
291
281
|
component.questionToken,
|
|
292
|
-
|
|
282
|
+
newType,
|
|
293
283
|
);
|
|
294
284
|
}
|
|
295
285
|
|
|
@@ -10,10 +10,7 @@ export interface TypePathFilters {
|
|
|
10
10
|
type HttpTypeImportName =
|
|
11
11
|
| 'HttpSchema'
|
|
12
12
|
| 'HttpFormData'
|
|
13
|
-
| 'HttpFormDataSerialized'
|
|
14
13
|
| 'HttpSearchParams'
|
|
15
|
-
| 'HttpSearchParamsSerialized'
|
|
16
|
-
| 'HttpHeadersSerialized'
|
|
17
14
|
| 'HttpStatusCode'
|
|
18
15
|
| 'MergeHttpResponsesByStatusCode';
|
|
19
16
|
|
|
@@ -65,22 +65,10 @@ function isRequestHeaders(node: ts.Node): node is RequestHeaders {
|
|
|
65
65
|
return isRequestMember(node) && node.name.text === 'headers' && ts.isTypeLiteralNode(node.type);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
type NormalizedRequestHeaders = Override<
|
|
69
|
-
RequestHeaders,
|
|
70
|
-
{
|
|
71
|
-
type: Override<ts.TypeReferenceNode, { typeArguments: ts.NodeArray<ts.TypeLiteralNode> }>;
|
|
72
|
-
}
|
|
73
|
-
>;
|
|
68
|
+
type NormalizedRequestHeaders = Override<RequestHeaders, { type: ts.TypeLiteralNode }>;
|
|
74
69
|
|
|
75
70
|
function isNormalizedRequestHeaders(node: ts.Node): node is NormalizedRequestHeaders {
|
|
76
|
-
return (
|
|
77
|
-
isRequestMember(node) &&
|
|
78
|
-
node.name.text === 'headers' &&
|
|
79
|
-
ts.isTypeReferenceNode(node.type) &&
|
|
80
|
-
node.type.typeArguments !== undefined &&
|
|
81
|
-
node.type.typeArguments.length === 1 &&
|
|
82
|
-
ts.isTypeLiteralNode(node.type.typeArguments[0])
|
|
83
|
-
);
|
|
71
|
+
return isRequestMember(node) && node.name.text === 'headers' && ts.isTypeLiteralNode(node.type);
|
|
84
72
|
}
|
|
85
73
|
|
|
86
74
|
type RequestParameters = Override<RequestMember, { type: ts.TypeLiteralNode }>;
|
|
@@ -170,23 +158,17 @@ function removeRedundantNullUnionIfNecessary(type: ts.TypeNode) {
|
|
|
170
158
|
|
|
171
159
|
function wrapFormDataContentType(type: ts.TypeNode, context: TypeTransformContext) {
|
|
172
160
|
context.typeImports.http.add('HttpFormData');
|
|
173
|
-
context.typeImports.http.add('HttpFormDataSerialized');
|
|
174
161
|
|
|
175
162
|
return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('HttpFormData'), [
|
|
176
|
-
|
|
177
|
-
renameComponentReferences(type, context),
|
|
178
|
-
]),
|
|
163
|
+
renameComponentReferences(type, context),
|
|
179
164
|
]);
|
|
180
165
|
}
|
|
181
166
|
|
|
182
167
|
function wrapURLEncodedContentType(type: ts.TypeNode, context: TypeTransformContext) {
|
|
183
168
|
context.typeImports.http.add('HttpSearchParams');
|
|
184
|
-
context.typeImports.http.add('HttpSearchParamsSerialized');
|
|
185
169
|
|
|
186
170
|
return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('HttpSearchParams'), [
|
|
187
|
-
|
|
188
|
-
renameComponentReferences(type, context),
|
|
189
|
-
]),
|
|
171
|
+
renameComponentReferences(type, context),
|
|
190
172
|
]);
|
|
191
173
|
}
|
|
192
174
|
|
|
@@ -224,18 +206,7 @@ function normalizeRequestBodyMember(
|
|
|
224
206
|
};
|
|
225
207
|
}
|
|
226
208
|
|
|
227
|
-
function
|
|
228
|
-
context.typeImports.http.add('HttpHeadersSerialized');
|
|
229
|
-
|
|
230
|
-
const serializedWrapper = ts.factory.createIdentifier('HttpHeadersSerialized');
|
|
231
|
-
|
|
232
|
-
return ts.factory.createTypeReferenceNode(
|
|
233
|
-
serializedWrapper,
|
|
234
|
-
ts.factory.createNodeArray([type]) satisfies NormalizedRequestHeaders['type']['typeArguments'],
|
|
235
|
-
) as NormalizedRequestHeaders['type'];
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function normalizeHeaders(headers: ts.TypeLiteralNode, context: TypeTransformContext) {
|
|
209
|
+
function normalizeHeaders(headers: ts.TypeLiteralNode) {
|
|
239
210
|
const newHeaderMembers = headers.members.filter((header) => {
|
|
240
211
|
if (ts.isIndexSignatureDeclaration(header)) {
|
|
241
212
|
return false;
|
|
@@ -253,19 +224,15 @@ function normalizeHeaders(headers: ts.TypeLiteralNode, context: TypeTransformCon
|
|
|
253
224
|
return undefined;
|
|
254
225
|
}
|
|
255
226
|
|
|
256
|
-
|
|
257
|
-
return wrapHeadersType(newHeaders, context);
|
|
227
|
+
return ts.factory.updateTypeLiteralNode(headers, ts.factory.createNodeArray(newHeaderMembers));
|
|
258
228
|
}
|
|
259
229
|
|
|
260
|
-
function normalizeRequestHeaders(
|
|
261
|
-
requestHeader: ts.TypeElement,
|
|
262
|
-
context: TypeTransformContext,
|
|
263
|
-
): NormalizedRequestHeaders | undefined {
|
|
230
|
+
function normalizeRequestHeaders(requestHeader: ts.TypeElement): NormalizedRequestHeaders | undefined {
|
|
264
231
|
if (!isRequestHeaders(requestHeader)) {
|
|
265
232
|
return undefined;
|
|
266
233
|
}
|
|
267
234
|
|
|
268
|
-
const newType = normalizeHeaders(requestHeader.type
|
|
235
|
+
const newType = normalizeHeaders(requestHeader.type);
|
|
269
236
|
|
|
270
237
|
if (!newType) {
|
|
271
238
|
return undefined;
|
|
@@ -283,20 +250,16 @@ function normalizeRequestHeaders(
|
|
|
283
250
|
function createHeaderForUnionByContentType(
|
|
284
251
|
existingHeader: NormalizedRequestHeaders | undefined,
|
|
285
252
|
contentTypeName: string,
|
|
286
|
-
context: TypeTransformContext,
|
|
287
253
|
) {
|
|
288
|
-
const existingHeaderMembers = existingHeader
|
|
254
|
+
const existingHeaderMembers = existingHeader?.type.members ?? [];
|
|
289
255
|
|
|
290
256
|
const contentTypeIdentifier = ts.factory.createIdentifier('"content-type"');
|
|
291
257
|
const contentTypeValue = ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(contentTypeName));
|
|
292
258
|
|
|
293
|
-
const newHeaderType =
|
|
294
|
-
ts.factory.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
]),
|
|
298
|
-
context,
|
|
299
|
-
);
|
|
259
|
+
const newHeaderType = ts.factory.createTypeLiteralNode([
|
|
260
|
+
ts.factory.createPropertySignature(undefined, contentTypeIdentifier, undefined, contentTypeValue),
|
|
261
|
+
...existingHeaderMembers,
|
|
262
|
+
]);
|
|
300
263
|
|
|
301
264
|
return ts.factory.createPropertySignature(
|
|
302
265
|
existingHeader?.modifiers,
|
|
@@ -321,7 +284,7 @@ export function normalizeContentType(
|
|
|
321
284
|
return contentType;
|
|
322
285
|
}
|
|
323
286
|
|
|
324
|
-
const newHeader = contentType.members.map(
|
|
287
|
+
const newHeader = contentType.members.map(normalizeRequestHeaders).find(isDefined);
|
|
325
288
|
|
|
326
289
|
const newBodyMembers = contentType.members.flatMap((body) => {
|
|
327
290
|
if (isContentPropertySignature(body)) {
|
|
@@ -338,7 +301,7 @@ export function normalizeContentType(
|
|
|
338
301
|
return ts.factory.updateTypeLiteralNode(contentType, ts.factory.createNodeArray(newMembers));
|
|
339
302
|
} else {
|
|
340
303
|
const bodyMemberUnionTypes = newBodyMembers.map((bodyMember) => {
|
|
341
|
-
const headerMember = createHeaderForUnionByContentType(newHeader, bodyMember.contentTypeName
|
|
304
|
+
const headerMember = createHeaderForUnionByContentType(newHeader, bodyMember.contentTypeName);
|
|
342
305
|
return ts.factory.createTypeLiteralNode([headerMember, bodyMember.propertySignature]);
|
|
343
306
|
});
|
|
344
307
|
|
|
@@ -366,29 +329,18 @@ function normalizeRequest(request: MethodMember, context: TypeTransformContext)
|
|
|
366
329
|
return ts.factory.updatePropertySignature(request, request.modifiers, newIdentifier, undefined, newType);
|
|
367
330
|
}
|
|
368
331
|
|
|
369
|
-
function wrapResponseType(type: ts.TypeNode, context: TypeTransformContext) {
|
|
370
|
-
context.typeImports.http.add('HttpSchema');
|
|
371
|
-
|
|
372
|
-
const httpSchemaResponseWrapper = ts.factory.createQualifiedName(
|
|
373
|
-
ts.factory.createIdentifier('HttpSchema'),
|
|
374
|
-
ts.factory.createIdentifier('Response'),
|
|
375
|
-
);
|
|
376
|
-
return ts.factory.createTypeReferenceNode(httpSchemaResponseWrapper, [type]);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
332
|
function normalizeResponseType(
|
|
380
333
|
responseType: ts.TypeNode,
|
|
381
334
|
context: TypeTransformContext,
|
|
382
|
-
options: {
|
|
335
|
+
options: { questionToken?: ts.QuestionToken },
|
|
383
336
|
) {
|
|
384
|
-
const {
|
|
337
|
+
const { questionToken } = options;
|
|
385
338
|
|
|
386
339
|
if (!ts.isTypeLiteralNode(responseType)) {
|
|
387
340
|
return responseType;
|
|
388
341
|
}
|
|
389
342
|
|
|
390
|
-
|
|
391
|
-
return isComponent ? wrapResponseType(newType, context) : newType;
|
|
343
|
+
return normalizeContentType(responseType, context, { bodyQuestionToken: questionToken });
|
|
392
344
|
}
|
|
393
345
|
|
|
394
346
|
const NON_NUMERIC_RESPONSE_STATUS_TO_MAPPED_TYPE: Record<string, string | undefined> = {
|
|
@@ -414,7 +366,6 @@ export function normalizeResponse(
|
|
|
414
366
|
}
|
|
415
367
|
|
|
416
368
|
const newType = normalizeResponseType(response.type, context, {
|
|
417
|
-
isComponent,
|
|
418
369
|
questionToken: response.questionToken,
|
|
419
370
|
});
|
|
420
371
|
|
|
@@ -552,17 +503,12 @@ function normalizeRequestQueryWithParameters(requestMember: RequestMember, conte
|
|
|
552
503
|
|
|
553
504
|
const newType = renameComponentReferences(requestMember.type, context);
|
|
554
505
|
|
|
555
|
-
context.typeImports.http.add('HttpSearchParamsSerialized');
|
|
556
|
-
|
|
557
|
-
const serializedWrapper = ts.factory.createIdentifier('HttpSearchParamsSerialized');
|
|
558
|
-
const wrappedNewType = ts.factory.createTypeReferenceNode(serializedWrapper, [newType]);
|
|
559
|
-
|
|
560
506
|
return ts.factory.updatePropertySignature(
|
|
561
507
|
requestMember,
|
|
562
508
|
requestMember.modifiers,
|
|
563
509
|
newIdentifier,
|
|
564
510
|
newQuestionToken,
|
|
565
|
-
|
|
511
|
+
newType,
|
|
566
512
|
);
|
|
567
513
|
}
|
|
568
514
|
|
|
@@ -572,17 +518,12 @@ function normalizeRequestHeadersWithParameters(requestMember: RequestMember, con
|
|
|
572
518
|
|
|
573
519
|
const newType = renameComponentReferences(requestMember.type, context);
|
|
574
520
|
|
|
575
|
-
context.typeImports.http.add('HttpHeadersSerialized');
|
|
576
|
-
|
|
577
|
-
const serializedWrapper = ts.factory.createIdentifier('HttpHeadersSerialized');
|
|
578
|
-
const wrappedNewType = ts.factory.createTypeReferenceNode(serializedWrapper, [newType]);
|
|
579
|
-
|
|
580
521
|
return ts.factory.updatePropertySignature(
|
|
581
522
|
requestMember,
|
|
582
523
|
requestMember.modifiers,
|
|
583
524
|
newIdentifier,
|
|
584
525
|
newQuestionToken,
|
|
585
|
-
|
|
526
|
+
newType,
|
|
586
527
|
);
|
|
587
528
|
}
|
|
588
529
|
|
|
@@ -602,16 +543,10 @@ function normalizeRequestMemberWithParameters(requestMember: ts.TypeElement, con
|
|
|
602
543
|
}
|
|
603
544
|
|
|
604
545
|
function mergeRequestHeadersMember(headers: NormalizedRequestHeaders, otherHeaders: NormalizedRequestHeaders) {
|
|
605
|
-
const
|
|
606
|
-
const otherHeadersTypeLiteral = otherHeaders.type.typeArguments[0];
|
|
607
|
-
|
|
608
|
-
const newType = ts.factory.updateTypeReferenceNode(
|
|
546
|
+
const newType = ts.factory.updateTypeLiteralNode(
|
|
609
547
|
headers.type,
|
|
610
|
-
headers.type.
|
|
611
|
-
|
|
612
|
-
ts.factory.createTypeLiteralNode([...otherHeadersTypeLiteral.members, ...headersTypeLiteral.members]),
|
|
613
|
-
]) satisfies NormalizedRequestHeaders['type']['typeArguments'],
|
|
614
|
-
) as NormalizedRequestHeaders['type'];
|
|
548
|
+
ts.factory.createNodeArray([...otherHeaders.type.members, ...headers.type.members]),
|
|
549
|
+
);
|
|
615
550
|
|
|
616
551
|
return ts.factory.updatePropertySignature(
|
|
617
552
|
headers,
|
|
@@ -36,16 +36,6 @@ function isOperation(node: ts.Node): node is Operation {
|
|
|
36
36
|
);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
function wrapOperationType(type: ts.TypeLiteralNode, context: TypeTransformContext) {
|
|
40
|
-
context.typeImports.http.add('HttpSchema');
|
|
41
|
-
|
|
42
|
-
const httpSchemaMethodWrapper = ts.factory.createQualifiedName(
|
|
43
|
-
ts.factory.createIdentifier('HttpSchema'),
|
|
44
|
-
ts.factory.createIdentifier('Method'),
|
|
45
|
-
);
|
|
46
|
-
return ts.factory.createTypeReferenceNode(httpSchemaMethodWrapper, [type]);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
39
|
function normalizeOperation(operation: ts.TypeElement, context: TypeTransformContext) {
|
|
50
40
|
/* istanbul ignore if -- @preserve
|
|
51
41
|
* Operation members are always expected to be an operation. */
|
|
@@ -60,7 +50,7 @@ function normalizeOperation(operation: ts.TypeElement, context: TypeTransformCon
|
|
|
60
50
|
operation.modifiers,
|
|
61
51
|
operation.name,
|
|
62
52
|
operation.questionToken,
|
|
63
|
-
|
|
53
|
+
newType,
|
|
64
54
|
);
|
|
65
55
|
}
|
|
66
56
|
|
|
@@ -41,16 +41,6 @@ function normalizePathNameWithParameters(pathName: string) {
|
|
|
41
41
|
return pathName.replace(/{([^}]+)}/g, ':$1');
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function wrapComponentPathType(type: ts.TypeNode, context: TypeTransformContext) {
|
|
45
|
-
context.typeImports.http.add('HttpSchema');
|
|
46
|
-
|
|
47
|
-
const httpSchemaMethodsWrapper = ts.factory.createQualifiedName(
|
|
48
|
-
ts.factory.createIdentifier('HttpSchema'),
|
|
49
|
-
ts.factory.createIdentifier('Methods'),
|
|
50
|
-
);
|
|
51
|
-
return ts.factory.createTypeReferenceNode(httpSchemaMethodsWrapper, [type]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
44
|
export function normalizePath(
|
|
55
45
|
path: ts.TypeElement,
|
|
56
46
|
context: TypeTransformContext,
|
|
@@ -83,13 +73,7 @@ export function normalizePath(
|
|
|
83
73
|
newType = renameComponentReferences(path.type, context);
|
|
84
74
|
}
|
|
85
75
|
|
|
86
|
-
return ts.factory.updatePropertySignature(
|
|
87
|
-
path,
|
|
88
|
-
path.modifiers,
|
|
89
|
-
newIdentifier,
|
|
90
|
-
path.questionToken,
|
|
91
|
-
isComponent ? wrapComponentPathType(newType, context) : newType,
|
|
92
|
-
);
|
|
76
|
+
return ts.factory.updatePropertySignature(path, path.modifiers, newIdentifier, path.questionToken, newType);
|
|
93
77
|
}
|
|
94
78
|
|
|
95
79
|
function wrapPathsType(type: ts.TypeLiteralNode, context: TypeTransformContext) {
|
package/src/types/requests.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Default, DefaultNoExclude, IfNever, ReplaceBy } from '@zimic/utils/types';
|
|
2
|
-
import {
|
|
2
|
+
import { JSONValue } from '@zimic/utils/types/json';
|
|
3
3
|
|
|
4
4
|
import { HttpMethodSchema, HttpStatusCode } from '@/types/schema';
|
|
5
5
|
|
|
@@ -17,16 +17,13 @@ export type HttpBody = JSONValue | HttpFormData<any> | HttpSearchParams<any> | B
|
|
|
17
17
|
export namespace HttpBody {
|
|
18
18
|
/** A loose version of the HTTP body type. JSON values are not strictly typed. */
|
|
19
19
|
export type Loose = ReplaceBy<HttpBody, JSONValue, JSONValue.Loose>;
|
|
20
|
-
|
|
21
|
-
/** Convert an HTTP body to be strictly typed. JSON values are serialized to their strict form. */
|
|
22
|
-
export type AsStrict<Type> = Type extends Exclude<HttpBody, JSONValue> ? Type : JSONSerialized<Type>;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
/**
|
|
26
23
|
* An HTTP headers object with a strictly-typed schema. Fully compatible with the built-in
|
|
27
24
|
* {@link https://developer.mozilla.org/docs/Web/API/Headers `Headers`} class.
|
|
28
25
|
*/
|
|
29
|
-
export type StrictHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> = Pick<
|
|
26
|
+
export type StrictHeaders<Schema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose> = Pick<
|
|
30
27
|
HttpHeaders<Schema>,
|
|
31
28
|
keyof Headers
|
|
32
29
|
>;
|
|
@@ -35,7 +32,7 @@ export type StrictHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema>
|
|
|
35
32
|
* An HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
36
33
|
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
37
34
|
*/
|
|
38
|
-
export type StrictURLSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsSchema> = Pick<
|
|
35
|
+
export type StrictURLSearchParams<Schema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose> = Pick<
|
|
39
36
|
HttpSearchParams<Schema>,
|
|
40
37
|
keyof URLSearchParams
|
|
41
38
|
>;
|
|
@@ -44,7 +41,7 @@ export type StrictURLSearchParams<Schema extends HttpSearchParamsSchema = HttpSe
|
|
|
44
41
|
* An HTTP form data object with a strictly-typed schema. Fully compatible with the built-in
|
|
45
42
|
* {@link https://developer.mozilla.org/docs/Web/API/FormData `FormData`} class.
|
|
46
43
|
*/
|
|
47
|
-
export type StrictFormData<Schema extends HttpFormDataSchema = HttpFormDataSchema> = Pick<
|
|
44
|
+
export type StrictFormData<Schema extends HttpFormDataSchema.Loose = HttpFormDataSchema.Loose> = Pick<
|
|
48
45
|
HttpFormData<Schema>,
|
|
49
46
|
keyof FormData
|
|
50
47
|
>;
|
|
@@ -54,8 +51,8 @@ export type StrictFormData<Schema extends HttpFormDataSchema = HttpFormDataSchem
|
|
|
54
51
|
* {@link https://developer.mozilla.org/docs/Web/API/Request `Request`} class.
|
|
55
52
|
*/
|
|
56
53
|
export interface HttpRequest<
|
|
57
|
-
StrictBody extends HttpBody.Loose = HttpBody,
|
|
58
|
-
StrictHeadersSchema extends HttpHeadersSchema = HttpHeadersSchema,
|
|
54
|
+
StrictBody extends HttpBody.Loose = HttpBody.Loose,
|
|
55
|
+
StrictHeadersSchema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose,
|
|
59
56
|
> extends Request {
|
|
60
57
|
headers: StrictHeaders<StrictHeadersSchema>;
|
|
61
58
|
text: () => Promise<StrictBody extends string ? StrictBody : string>;
|
|
@@ -75,9 +72,9 @@ export interface HttpRequest<
|
|
|
75
72
|
* {@link https://developer.mozilla.org/docs/Web/API/Response `Response`} class.
|
|
76
73
|
*/
|
|
77
74
|
export interface HttpResponse<
|
|
78
|
-
StrictBody extends HttpBody.Loose = HttpBody,
|
|
75
|
+
StrictBody extends HttpBody.Loose = HttpBody.Loose,
|
|
79
76
|
StatusCode extends number = number,
|
|
80
|
-
StrictHeadersSchema extends HttpHeadersSchema = HttpHeadersSchema,
|
|
77
|
+
StrictHeadersSchema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose,
|
|
81
78
|
> extends Response {
|
|
82
79
|
ok: StatusCode extends HttpStatusCode.Information | HttpStatusCode.Success | HttpStatusCode.Redirection
|
|
83
80
|
? true
|
|
@@ -96,13 +93,11 @@ export interface HttpResponse<
|
|
|
96
93
|
clone: () => this;
|
|
97
94
|
}
|
|
98
95
|
|
|
99
|
-
export type HttpRequestHeadersSchema<MethodSchema extends HttpMethodSchema> =
|
|
100
|
-
Default<MethodSchema['request']>['headers']
|
|
101
|
-
>;
|
|
96
|
+
export type HttpRequestHeadersSchema<MethodSchema extends HttpMethodSchema> =
|
|
97
|
+
'headers' extends keyof MethodSchema['request'] ? Default<MethodSchema['request']>['headers'] : never;
|
|
102
98
|
|
|
103
|
-
export type HttpRequestSearchParamsSchema<MethodSchema extends HttpMethodSchema> =
|
|
104
|
-
Default<MethodSchema['request']>['searchParams']
|
|
105
|
-
>;
|
|
99
|
+
export type HttpRequestSearchParamsSchema<MethodSchema extends HttpMethodSchema> =
|
|
100
|
+
'searchParams' extends keyof MethodSchema['request'] ? Default<MethodSchema['request']>['searchParams'] : never;
|
|
106
101
|
|
|
107
102
|
export type HttpRequestBodySchema<MethodSchema extends HttpMethodSchema> = ReplaceBy<
|
|
108
103
|
ReplaceBy<IfNever<DefaultNoExclude<Default<MethodSchema['request']>['body']>, null>, undefined, null>,
|
|
@@ -113,7 +108,9 @@ export type HttpRequestBodySchema<MethodSchema extends HttpMethodSchema> = Repla
|
|
|
113
108
|
export type HttpResponseHeadersSchema<
|
|
114
109
|
MethodSchema extends HttpMethodSchema,
|
|
115
110
|
StatusCode extends HttpStatusCode,
|
|
116
|
-
> = Default<
|
|
111
|
+
> = 'headers' extends keyof Default<MethodSchema['response']>[StatusCode]
|
|
112
|
+
? Default<Default<MethodSchema['response']>[StatusCode]>['headers']
|
|
113
|
+
: never;
|
|
117
114
|
|
|
118
115
|
export type HttpResponseBodySchema<
|
|
119
116
|
MethodSchema extends HttpMethodSchema,
|