@zimic/http 0.4.0-canary.0 → 0.4.0-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/README.md +1 -1
- package/dist/{chunk-IJVT4RDC.mjs → chunk-KVTV4E5K.mjs} +26 -100
- package/dist/chunk-KVTV4E5K.mjs.map +1 -0
- package/dist/{chunk-DIZB36SQ.js → chunk-LOHINQWU.js} +26 -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 +140 -195
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -3
- 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 +44 -54
- package/src/formData/types.ts +36 -42
- package/src/headers/HttpHeaders.ts +38 -35
- package/src/headers/types.ts +2 -2
- package/src/pathParams/types.ts +29 -40
- package/src/searchParams/HttpSearchParams.ts +57 -49
- package/src/searchParams/types.ts +39 -45
- 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 +60 -19
- package/src/types/schema.ts +50 -134
- package/dist/chunk-DIZB36SQ.js.map +0 -1
- package/dist/chunk-IJVT4RDC.mjs.map +0 -1
package/src/formData/types.ts
CHANGED
|
@@ -50,61 +50,55 @@ export namespace HttpFormDataSchemaName {
|
|
|
50
50
|
*/
|
|
51
51
|
export type HttpFormDataSchemaName<Schema extends HttpFormDataSchema> = IfNever<Schema, never, keyof Schema & string>;
|
|
52
52
|
|
|
53
|
-
type PrimitiveHttpFormDataSerialized<Type> = Type extends
|
|
54
|
-
?
|
|
55
|
-
: Type extends
|
|
56
|
-
?
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
type PrimitiveHttpFormDataSerialized<Type> = [Type] extends [never]
|
|
54
|
+
? never
|
|
55
|
+
: Type extends number
|
|
56
|
+
? `${number}`
|
|
57
|
+
: Type extends boolean
|
|
58
|
+
? `${boolean}`
|
|
59
|
+
: Type extends null
|
|
60
|
+
? 'null'
|
|
61
|
+
: Type extends symbol
|
|
62
|
+
? never
|
|
63
|
+
: Type extends HttpFormDataSchema[string]
|
|
64
|
+
? Type
|
|
65
|
+
: Type extends (infer ArrayItem)[]
|
|
66
|
+
? ArrayItem extends (infer _InternalArrayItem)[]
|
|
67
|
+
? never
|
|
68
|
+
: PrimitiveHttpFormDataSerialized<ArrayItem>[]
|
|
69
|
+
: string;
|
|
64
70
|
|
|
65
71
|
/**
|
|
66
72
|
* Recursively converts a schema to its {@link https://developer.mozilla.org/docs/Web/API/FormData FormData}-serialized
|
|
67
|
-
* version. Numbers and
|
|
68
|
-
*
|
|
73
|
+
* version. Numbers, booleans, and null are converted to `${number}`, `${boolean}`, and 'null' respectively, and other
|
|
74
|
+
* values become strings.
|
|
69
75
|
*
|
|
70
76
|
* @example
|
|
71
77
|
* import { type HttpFormDataSerialized } from '@zimic/http';
|
|
72
78
|
*
|
|
73
79
|
* type Schema = HttpFormDataSerialized<{
|
|
74
|
-
* contentTitle: string
|
|
75
|
-
* contentSize: number;
|
|
80
|
+
* contentTitle: string;
|
|
81
|
+
* contentSize: number | null;
|
|
76
82
|
* content: Blob;
|
|
77
83
|
* full?: boolean;
|
|
78
|
-
* date: Date;
|
|
79
|
-
* method: () => void;
|
|
80
84
|
* }>;
|
|
81
85
|
* // {
|
|
82
|
-
* // contentTitle: string
|
|
83
|
-
* // contentSize? `${number}
|
|
86
|
+
* // contentTitle: string;
|
|
87
|
+
* // contentSize? `${number}` | 'null';
|
|
84
88
|
* // content: Blob;
|
|
85
89
|
* // full?: "false" | "true";
|
|
86
90
|
* // }
|
|
87
91
|
*/
|
|
88
|
-
export type HttpFormDataSerialized<Type> = Type extends
|
|
89
|
-
?
|
|
90
|
-
: Type extends
|
|
91
|
-
?
|
|
92
|
-
: Type extends
|
|
93
|
-
?
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
: Type
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
? never
|
|
102
|
-
: Type extends object
|
|
103
|
-
? {
|
|
104
|
-
[Key in keyof Type as IfNever<
|
|
105
|
-
PrimitiveHttpFormDataSerialized<Type[Key]>,
|
|
106
|
-
never,
|
|
107
|
-
Key
|
|
108
|
-
>]: PrimitiveHttpFormDataSerialized<Type[Key]>;
|
|
109
|
-
}
|
|
110
|
-
: never;
|
|
92
|
+
export type HttpFormDataSerialized<Type> = [Type] extends [never]
|
|
93
|
+
? never
|
|
94
|
+
: Type extends HttpFormDataSchema
|
|
95
|
+
? Type
|
|
96
|
+
: Type extends object
|
|
97
|
+
? {
|
|
98
|
+
[Key in keyof Type as IfNever<
|
|
99
|
+
PrimitiveHttpFormDataSerialized<Type[Key]>,
|
|
100
|
+
never,
|
|
101
|
+
Key
|
|
102
|
+
>]: PrimitiveHttpFormDataSerialized<Type[Key]>;
|
|
103
|
+
}
|
|
104
|
+
: never;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Default, ReplaceBy } from '@zimic/utils/types';
|
|
2
2
|
|
|
3
|
-
import { HttpHeadersSchema, HttpHeadersInit, HttpHeadersSchemaName } from './types';
|
|
3
|
+
import { HttpHeadersSchema, HttpHeadersInit, HttpHeadersSchemaName, HttpHeadersSerialized } from './types';
|
|
4
4
|
|
|
5
|
-
function pickPrimitiveProperties<
|
|
5
|
+
function pickPrimitiveProperties<LooseSchema extends HttpHeadersSchema.Loose>(schema: LooseSchema) {
|
|
6
6
|
return Object.entries(schema).reduce<Record<string, string>>((accumulated, [key, value]) => {
|
|
7
7
|
if (value !== undefined) {
|
|
8
|
-
accumulated[key] = value;
|
|
8
|
+
accumulated[key] = String(value);
|
|
9
9
|
}
|
|
10
10
|
return accumulated;
|
|
11
11
|
}, {});
|
|
@@ -15,9 +15,6 @@ function pickPrimitiveProperties<Schema extends HttpHeadersSchema>(schema: Schem
|
|
|
15
15
|
* An extended HTTP headers object with a strictly-typed schema. Fully compatible with the built-in
|
|
16
16
|
* {@link https://developer.mozilla.org/docs/Web/API/Headers `Headers`} class.
|
|
17
17
|
*
|
|
18
|
-
* **IMPORTANT**: the input of `HttpHeaders` and all of its internal types must be declared inline or as a type aliases
|
|
19
|
-
* (`type`). They cannot be interfaces.
|
|
20
|
-
*
|
|
21
18
|
* @example
|
|
22
19
|
* import { HttpHeaders } from '@zimic/http';
|
|
23
20
|
*
|
|
@@ -34,8 +31,10 @@ function pickPrimitiveProperties<Schema extends HttpHeadersSchema>(schema: Schem
|
|
|
34
31
|
*
|
|
35
32
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders `HttpHeaders` API reference}
|
|
36
33
|
*/
|
|
37
|
-
class HttpHeaders<
|
|
38
|
-
|
|
34
|
+
class HttpHeaders<LooseSchema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose> extends Headers {
|
|
35
|
+
readonly _schema!: HttpHeadersSerialized<LooseSchema>;
|
|
36
|
+
|
|
37
|
+
constructor(init?: HttpHeadersInit<LooseSchema>) {
|
|
39
38
|
if (init instanceof Headers || Array.isArray(init) || !init) {
|
|
40
39
|
super(init);
|
|
41
40
|
} else {
|
|
@@ -44,38 +43,40 @@ class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends
|
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/set MDN Reference} */
|
|
47
|
-
set<Name extends HttpHeadersSchemaName<
|
|
46
|
+
set<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name, value: NonNullable<LooseSchema[Name]>): void {
|
|
48
47
|
super.set(name, value);
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/append MDN Reference} */
|
|
52
|
-
append<Name extends HttpHeadersSchemaName<
|
|
51
|
+
append<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name, value: NonNullable<LooseSchema[Name]>): void {
|
|
53
52
|
super.append(name, value);
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/get MDN Reference} */
|
|
57
|
-
get<Name extends HttpHeadersSchemaName<
|
|
58
|
-
|
|
56
|
+
get<Name extends HttpHeadersSchemaName<this['_schema']>>(
|
|
57
|
+
name: Name,
|
|
58
|
+
): ReplaceBy<this['_schema'][Name], undefined, null> {
|
|
59
|
+
return super.get(name) as never;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
62
|
-
getSetCookie(): NonNullable<Default<
|
|
63
|
-
return super.getSetCookie() as
|
|
63
|
+
getSetCookie(): NonNullable<Default<this['_schema']['Set-Cookie'], string>>[] {
|
|
64
|
+
return super.getSetCookie() as never;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
67
|
-
has<Name extends HttpHeadersSchemaName<
|
|
68
|
+
has<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name): boolean {
|
|
68
69
|
return super.has(name);
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/delete MDN Reference} */
|
|
72
|
-
delete<Name extends HttpHeadersSchemaName<
|
|
73
|
+
delete<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name): void {
|
|
73
74
|
super.delete(name);
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
forEach<This extends HttpHeaders<
|
|
77
|
-
callback: <Key extends HttpHeadersSchemaName<
|
|
78
|
-
value: NonNullable<
|
|
77
|
+
forEach<This extends HttpHeaders<this['_schema']>>(
|
|
78
|
+
callback: <Key extends HttpHeadersSchemaName<this['_schema']>>(
|
|
79
|
+
value: NonNullable<this['_schema'][Key]> & string,
|
|
79
80
|
key: Key,
|
|
80
81
|
parent: Headers,
|
|
81
82
|
) => void,
|
|
@@ -85,30 +86,32 @@ class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends
|
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/keys MDN Reference} */
|
|
88
|
-
keys(): HeadersIterator<HttpHeadersSchemaName<
|
|
89
|
-
return super.keys() as
|
|
89
|
+
keys(): HeadersIterator<HttpHeadersSchemaName<this['_schema']>> {
|
|
90
|
+
return super.keys() as never;
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/values MDN Reference} */
|
|
93
|
-
values(): HeadersIterator<NonNullable<
|
|
94
|
-
return super.values() as
|
|
94
|
+
values(): HeadersIterator<NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string> {
|
|
95
|
+
return super.values() as never;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/entries MDN Reference} */
|
|
98
99
|
entries(): HeadersIterator<
|
|
99
|
-
[
|
|
100
|
+
[
|
|
101
|
+
HttpHeadersSchemaName<this['_schema']>,
|
|
102
|
+
NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string,
|
|
103
|
+
]
|
|
100
104
|
> {
|
|
101
|
-
return super.entries() as
|
|
102
|
-
[HttpHeadersSchemaName<Schema>, NonNullable<Schema[HttpHeadersSchemaName<Schema>]> & string]
|
|
103
|
-
>;
|
|
105
|
+
return super.entries() as never;
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
[Symbol.iterator](): HeadersIterator<
|
|
107
|
-
[
|
|
109
|
+
[
|
|
110
|
+
HttpHeadersSchemaName<this['_schema']>,
|
|
111
|
+
NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string,
|
|
112
|
+
]
|
|
108
113
|
> {
|
|
109
|
-
return super[Symbol.iterator]() as
|
|
110
|
-
[HttpHeadersSchemaName<Schema>, NonNullable<Schema[HttpHeadersSchemaName<Schema>]> & string]
|
|
111
|
-
>;
|
|
114
|
+
return super[Symbol.iterator]() as never;
|
|
112
115
|
}
|
|
113
116
|
|
|
114
117
|
/**
|
|
@@ -118,7 +121,7 @@ class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends
|
|
|
118
121
|
* @param otherHeaders The other headers object to compare against.
|
|
119
122
|
* @returns `true` if the headers are equal, `false` otherwise.
|
|
120
123
|
*/
|
|
121
|
-
equals<OtherSchema extends
|
|
124
|
+
equals<OtherSchema extends LooseSchema>(otherHeaders: HttpHeaders<OtherSchema>): boolean {
|
|
122
125
|
if (!this.contains(otherHeaders)) {
|
|
123
126
|
return false;
|
|
124
127
|
}
|
|
@@ -141,7 +144,7 @@ class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends
|
|
|
141
144
|
* @param otherHeaders The other headers object to compare against.
|
|
142
145
|
* @returns `true` if these headers contain the other headers, `false` otherwise.
|
|
143
146
|
*/
|
|
144
|
-
contains<OtherSchema extends
|
|
147
|
+
contains<OtherSchema extends LooseSchema>(otherHeaders: HttpHeaders<OtherSchema>): boolean {
|
|
145
148
|
for (const [key, otherValue] of otherHeaders.entries()) {
|
|
146
149
|
const value = super.get.call(this, key);
|
|
147
150
|
|
|
@@ -180,8 +183,8 @@ class HttpHeaders<Schema extends HttpHeadersSchema = HttpHeadersSchema> extends
|
|
|
180
183
|
*
|
|
181
184
|
* @returns A plain object representation of these headers.
|
|
182
185
|
*/
|
|
183
|
-
toObject():
|
|
184
|
-
const object = {} as
|
|
186
|
+
toObject(): this['_schema'] {
|
|
187
|
+
const object = {} as this['_schema'];
|
|
185
188
|
|
|
186
189
|
for (const [key, value] of this.entries()) {
|
|
187
190
|
object[key] = value;
|
package/src/headers/types.ts
CHANGED
|
@@ -15,12 +15,12 @@ export namespace HttpHeadersSchema {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/** A strict tuple representation of a {@link HttpHeadersSchema}. */
|
|
18
|
-
export type HttpHeadersSchemaTuple<Schema extends HttpHeadersSchema = HttpHeadersSchema> = {
|
|
18
|
+
export type HttpHeadersSchemaTuple<Schema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose> = {
|
|
19
19
|
[Key in keyof Schema & string]: [Key, NonNullable<Schema[Key]>];
|
|
20
20
|
}[keyof Schema & string];
|
|
21
21
|
|
|
22
22
|
/** An initialization value for {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpheaders `HttpHeaders`}. */
|
|
23
|
-
export type HttpHeadersInit<Schema extends HttpHeadersSchema = HttpHeadersSchema> =
|
|
23
|
+
export type HttpHeadersInit<Schema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose> =
|
|
24
24
|
| Headers
|
|
25
25
|
| Schema
|
|
26
26
|
| HttpHeaders<Schema>
|
package/src/pathParams/types.ts
CHANGED
|
@@ -10,21 +10,22 @@ export namespace HttpPathParamsSchema {
|
|
|
10
10
|
export type Loose = Record<string, any>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
type PrimitiveHttpPathParamsSerialized<Type> = Type extends
|
|
14
|
-
?
|
|
15
|
-
: Type extends
|
|
16
|
-
?
|
|
17
|
-
: Type extends
|
|
18
|
-
? `${
|
|
19
|
-
: Type extends
|
|
20
|
-
?
|
|
21
|
-
: Type extends
|
|
22
|
-
?
|
|
23
|
-
:
|
|
13
|
+
type PrimitiveHttpPathParamsSerialized<Type> = [Type] extends [never]
|
|
14
|
+
? never
|
|
15
|
+
: Type extends number
|
|
16
|
+
? `${number}`
|
|
17
|
+
: Type extends boolean
|
|
18
|
+
? `${boolean}`
|
|
19
|
+
: Type extends null
|
|
20
|
+
? 'null'
|
|
21
|
+
: Type extends symbol
|
|
22
|
+
? never
|
|
23
|
+
: Type extends HttpPathParamsSchema[string]
|
|
24
|
+
? Type
|
|
25
|
+
: string;
|
|
24
26
|
/**
|
|
25
|
-
* Recursively converts a schema to its path parameters-serialized version. Numbers and
|
|
26
|
-
* `${number}
|
|
27
|
-
* functions and dates.
|
|
27
|
+
* Recursively converts a schema to its path parameters-serialized version. Numbers, booleans, and null are converted to
|
|
28
|
+
* `${number}`, `${boolean}`, and 'null' respectively, and other values become strings.
|
|
28
29
|
*
|
|
29
30
|
* @example
|
|
30
31
|
* import { type HttpPathParamsSerialized } from '@zimic/http';
|
|
@@ -33,35 +34,23 @@ type PrimitiveHttpPathParamsSerialized<Type> = Type extends HttpPathParamsSchema
|
|
|
33
34
|
* userId: string;
|
|
34
35
|
* notificationId: number | null;
|
|
35
36
|
* full?: boolean;
|
|
36
|
-
* from?: Date;
|
|
37
|
-
* method: () => void;
|
|
38
37
|
* }>;
|
|
39
38
|
* // {
|
|
40
39
|
* // userId: string;
|
|
41
|
-
* // notificationId: `${number}` |
|
|
40
|
+
* // notificationId: `${number}` | 'null';
|
|
42
41
|
* // full?: "false" | "true";
|
|
43
42
|
* // }
|
|
44
43
|
*/
|
|
45
|
-
export type HttpPathParamsSerialized<Type> = Type extends
|
|
46
|
-
?
|
|
47
|
-
: Type extends
|
|
48
|
-
?
|
|
49
|
-
: Type extends
|
|
50
|
-
?
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
: Type
|
|
56
|
-
|
|
57
|
-
|
|
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;
|
|
44
|
+
export type HttpPathParamsSerialized<Type> = [Type] extends [never]
|
|
45
|
+
? never
|
|
46
|
+
: Type extends HttpPathParamsSchema
|
|
47
|
+
? Type
|
|
48
|
+
: Type extends object
|
|
49
|
+
? {
|
|
50
|
+
[Key in keyof Type as IfNever<
|
|
51
|
+
PrimitiveHttpPathParamsSerialized<Type[Key]>,
|
|
52
|
+
never,
|
|
53
|
+
Key
|
|
54
|
+
>]: PrimitiveHttpPathParamsSerialized<Type[Key]>;
|
|
55
|
+
}
|
|
56
|
+
: never;
|
|
@@ -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
|
},
|
|
@@ -19,9 +24,6 @@ function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema>(schema:
|
|
|
19
24
|
* An extended HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
20
25
|
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
21
26
|
*
|
|
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
27
|
* @example
|
|
26
28
|
* import { HttpSearchParams } from '@zimic/http';
|
|
27
29
|
*
|
|
@@ -41,8 +43,12 @@ function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema>(schema:
|
|
|
41
43
|
*
|
|
42
44
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐http#httpsearchparams `HttpSearchParams` API reference}
|
|
43
45
|
*/
|
|
44
|
-
class HttpSearchParams<
|
|
45
|
-
|
|
46
|
+
class HttpSearchParams<
|
|
47
|
+
LooseSchema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose,
|
|
48
|
+
> extends URLSearchParams {
|
|
49
|
+
readonly _schema!: HttpSearchParamsSerialized<LooseSchema>;
|
|
50
|
+
|
|
51
|
+
constructor(init?: HttpSearchParamsInit<LooseSchema>) {
|
|
46
52
|
if (init instanceof URLSearchParams || Array.isArray(init) || typeof init === 'string' || !init) {
|
|
47
53
|
super(init);
|
|
48
54
|
} else {
|
|
@@ -51,28 +57,28 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
|
|
54
|
-
private populateInitArrayProperties(init:
|
|
60
|
+
private populateInitArrayProperties(init: LooseSchema) {
|
|
55
61
|
for (const [key, value] of Object.entries(init)) {
|
|
56
62
|
if (Array.isArray(value)) {
|
|
57
|
-
for (const item of value) {
|
|
58
|
-
super.append(key, item);
|
|
63
|
+
for (const item of value as unknown[]) {
|
|
64
|
+
super.append(key, String(item));
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/set MDN Reference} */
|
|
65
|
-
set<Name extends HttpSearchParamsSchemaName<
|
|
71
|
+
set<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
66
72
|
name: Name,
|
|
67
|
-
value: ArrayItemIfArray<NonNullable<
|
|
73
|
+
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
68
74
|
): void {
|
|
69
75
|
super.set(name, value);
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/append MDN Reference} */
|
|
73
|
-
append<Name extends HttpSearchParamsSchemaName<
|
|
79
|
+
append<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
74
80
|
name: Name,
|
|
75
|
-
value: ArrayItemIfArray<NonNullable<
|
|
81
|
+
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
76
82
|
): void {
|
|
77
83
|
super.append(name, value);
|
|
78
84
|
}
|
|
@@ -86,10 +92,10 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
86
92
|
* @returns The value associated with the key name, or `null` if the key does not exist.
|
|
87
93
|
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/get MDN Reference}
|
|
88
94
|
*/
|
|
89
|
-
get<Name extends HttpSearchParamsSchemaName.NonArray<
|
|
95
|
+
get<Name extends HttpSearchParamsSchemaName.NonArray<this['_schema']>>(
|
|
90
96
|
name: Name,
|
|
91
|
-
): ReplaceBy<ArrayItemIfArray<
|
|
92
|
-
return super.get(name) as
|
|
97
|
+
): ReplaceBy<ArrayItemIfArray<this['_schema'][Name]>, undefined, null> {
|
|
98
|
+
return super.get(name) as never;
|
|
93
99
|
}
|
|
94
100
|
|
|
95
101
|
/**
|
|
@@ -101,33 +107,33 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
101
107
|
* @returns An array of values associated with the key name, or an empty array if the key does not exist.
|
|
102
108
|
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll MDN Reference}
|
|
103
109
|
*/
|
|
104
|
-
getAll<Name extends HttpSearchParamsSchemaName.Array<
|
|
110
|
+
getAll<Name extends HttpSearchParamsSchemaName.Array<this['_schema']>>(
|
|
105
111
|
name: Name,
|
|
106
|
-
): ArrayItemIfArray<NonNullable<
|
|
107
|
-
return super.getAll(name) as
|
|
112
|
+
): ArrayItemIfArray<NonNullable<this['_schema'][Name]>>[] {
|
|
113
|
+
return super.getAll(name) as never;
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/has MDN Reference} */
|
|
111
|
-
has<Name extends HttpSearchParamsSchemaName<
|
|
117
|
+
has<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
112
118
|
name: Name,
|
|
113
|
-
value?: ArrayItemIfArray<NonNullable<
|
|
119
|
+
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
114
120
|
): boolean {
|
|
115
121
|
return super.has(name, value);
|
|
116
122
|
}
|
|
117
123
|
|
|
118
124
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete MDN Reference} */
|
|
119
|
-
delete<Name extends HttpSearchParamsSchemaName<
|
|
125
|
+
delete<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
120
126
|
name: Name,
|
|
121
|
-
value?: ArrayItemIfArray<NonNullable<
|
|
127
|
+
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
122
128
|
): void {
|
|
123
129
|
super.delete(name, value);
|
|
124
130
|
}
|
|
125
131
|
|
|
126
|
-
forEach<This extends HttpSearchParams<
|
|
127
|
-
callback: <Key extends HttpSearchParamsSchemaName<
|
|
128
|
-
value: ArrayItemIfArray<NonNullable<
|
|
132
|
+
forEach<This extends HttpSearchParams<this['_schema']>>(
|
|
133
|
+
callback: <Key extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
134
|
+
value: ArrayItemIfArray<NonNullable<this['_schema'][Key]>>,
|
|
129
135
|
key: Key,
|
|
130
|
-
parent: HttpSearchParams<
|
|
136
|
+
parent: HttpSearchParams<this['_schema']>,
|
|
131
137
|
) => void,
|
|
132
138
|
thisArg?: This,
|
|
133
139
|
): void {
|
|
@@ -135,32 +141,34 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
135
141
|
}
|
|
136
142
|
|
|
137
143
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys MDN Reference} */
|
|
138
|
-
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<
|
|
139
|
-
return super.keys() as
|
|
144
|
+
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<this['_schema']>> {
|
|
145
|
+
return super.keys() as never;
|
|
140
146
|
}
|
|
141
147
|
|
|
142
148
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/values MDN Reference} */
|
|
143
|
-
values(): URLSearchParamsIterator<
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
149
|
+
values(): URLSearchParamsIterator<
|
|
150
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>
|
|
151
|
+
> {
|
|
152
|
+
return super.values() as never;
|
|
147
153
|
}
|
|
148
154
|
|
|
149
155
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries MDN Reference} */
|
|
150
156
|
entries(): URLSearchParamsIterator<
|
|
151
|
-
[
|
|
157
|
+
[
|
|
158
|
+
HttpSearchParamsSchemaName<this['_schema']>,
|
|
159
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>,
|
|
160
|
+
]
|
|
152
161
|
> {
|
|
153
|
-
return super.entries() as
|
|
154
|
-
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
155
|
-
>;
|
|
162
|
+
return super.entries() as never;
|
|
156
163
|
}
|
|
157
164
|
|
|
158
165
|
[Symbol.iterator](): URLSearchParamsIterator<
|
|
159
|
-
[
|
|
166
|
+
[
|
|
167
|
+
HttpSearchParamsSchemaName<this['_schema']>,
|
|
168
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>,
|
|
169
|
+
]
|
|
160
170
|
> {
|
|
161
|
-
return super[Symbol.iterator]() as
|
|
162
|
-
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
163
|
-
>;
|
|
171
|
+
return super[Symbol.iterator]() as never;
|
|
164
172
|
}
|
|
165
173
|
|
|
166
174
|
/**
|
|
@@ -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
|
|
|
@@ -219,13 +227,13 @@ class HttpSearchParams<Schema extends HttpSearchParamsSchema = HttpSearchParamsS
|
|
|
219
227
|
* @returns A plain object representation of these search params.
|
|
220
228
|
*/
|
|
221
229
|
toObject() {
|
|
222
|
-
const object = {} as
|
|
230
|
+
const object = {} as this['_schema'];
|
|
223
231
|
|
|
224
|
-
type SchemaValue =
|
|
232
|
+
type SchemaValue = this['_schema'][HttpSearchParamsSchemaName<this['_schema']>];
|
|
225
233
|
|
|
226
234
|
for (const [key, value] of this.entries()) {
|
|
227
235
|
if (key in object) {
|
|
228
|
-
const existingValue = object[key];
|
|
236
|
+
const existingValue = object[key] as SchemaValue[];
|
|
229
237
|
|
|
230
238
|
if (Array.isArray<SchemaValue>(existingValue)) {
|
|
231
239
|
existingValue.push(value as SchemaValue);
|