@zimic/http 0.4.0-canary.1 → 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/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.ts +85 -83
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/formData/HttpFormData.ts +34 -47
- package/src/formData/types.ts +36 -42
- package/src/headers/HttpHeaders.ts +32 -32
- package/src/pathParams/types.ts +29 -40
- package/src/searchParams/HttpSearchParams.ts +35 -35
- package/src/searchParams/types.ts +37 -43
- package/src/types/requests.ts +49 -5
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;
|
|
@@ -15,9 +15,6 @@ function pickPrimitiveProperties<LooseSchema extends HttpHeadersSchema.Loose>(sc
|
|
|
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,10 +31,9 @@ function pickPrimitiveProperties<LooseSchema extends HttpHeadersSchema.Loose>(sc
|
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
> extends Headers {
|
|
34
|
+
class HttpHeaders<LooseSchema extends HttpHeadersSchema.Loose = HttpHeadersSchema.Loose> extends Headers {
|
|
35
|
+
readonly _schema!: HttpHeadersSerialized<LooseSchema>;
|
|
36
|
+
|
|
41
37
|
constructor(init?: HttpHeadersInit<LooseSchema>) {
|
|
42
38
|
if (init instanceof Headers || Array.isArray(init) || !init) {
|
|
43
39
|
super(init);
|
|
@@ -47,38 +43,40 @@ class HttpHeaders<
|
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/set MDN Reference} */
|
|
50
|
-
set<Name extends HttpHeadersSchemaName<
|
|
46
|
+
set<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name, value: NonNullable<LooseSchema[Name]>): void {
|
|
51
47
|
super.set(name, value);
|
|
52
48
|
}
|
|
53
49
|
|
|
54
50
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/append MDN Reference} */
|
|
55
|
-
append<Name extends HttpHeadersSchemaName<
|
|
51
|
+
append<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name, value: NonNullable<LooseSchema[Name]>): void {
|
|
56
52
|
super.append(name, value);
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/get MDN Reference} */
|
|
60
|
-
get<Name extends HttpHeadersSchemaName<
|
|
61
|
-
|
|
56
|
+
get<Name extends HttpHeadersSchemaName<this['_schema']>>(
|
|
57
|
+
name: Name,
|
|
58
|
+
): ReplaceBy<this['_schema'][Name], undefined, null> {
|
|
59
|
+
return super.get(name) as never;
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
65
|
-
getSetCookie(): NonNullable<Default<
|
|
66
|
-
return super.getSetCookie() as
|
|
63
|
+
getSetCookie(): NonNullable<Default<this['_schema']['Set-Cookie'], string>>[] {
|
|
64
|
+
return super.getSetCookie() as never;
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/has MDN Reference} */
|
|
70
|
-
has<Name extends HttpHeadersSchemaName<
|
|
68
|
+
has<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name): boolean {
|
|
71
69
|
return super.has(name);
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/delete MDN Reference} */
|
|
75
|
-
delete<Name extends HttpHeadersSchemaName<
|
|
73
|
+
delete<Name extends HttpHeadersSchemaName<this['_schema']>>(name: Name): void {
|
|
76
74
|
super.delete(name);
|
|
77
75
|
}
|
|
78
76
|
|
|
79
|
-
forEach<This extends HttpHeaders<
|
|
80
|
-
callback: <Key extends HttpHeadersSchemaName<
|
|
81
|
-
value: NonNullable<
|
|
77
|
+
forEach<This extends HttpHeaders<this['_schema']>>(
|
|
78
|
+
callback: <Key extends HttpHeadersSchemaName<this['_schema']>>(
|
|
79
|
+
value: NonNullable<this['_schema'][Key]> & string,
|
|
82
80
|
key: Key,
|
|
83
81
|
parent: Headers,
|
|
84
82
|
) => void,
|
|
@@ -88,30 +86,32 @@ class HttpHeaders<
|
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/keys MDN Reference} */
|
|
91
|
-
keys(): HeadersIterator<HttpHeadersSchemaName<
|
|
92
|
-
return super.keys() as
|
|
89
|
+
keys(): HeadersIterator<HttpHeadersSchemaName<this['_schema']>> {
|
|
90
|
+
return super.keys() as never;
|
|
93
91
|
}
|
|
94
92
|
|
|
95
93
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/values MDN Reference} */
|
|
96
|
-
values(): HeadersIterator<NonNullable<
|
|
97
|
-
return super.values() as
|
|
94
|
+
values(): HeadersIterator<NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string> {
|
|
95
|
+
return super.values() as never;
|
|
98
96
|
}
|
|
99
97
|
|
|
100
98
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/Headers/entries MDN Reference} */
|
|
101
99
|
entries(): HeadersIterator<
|
|
102
|
-
[
|
|
100
|
+
[
|
|
101
|
+
HttpHeadersSchemaName<this['_schema']>,
|
|
102
|
+
NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string,
|
|
103
|
+
]
|
|
103
104
|
> {
|
|
104
|
-
return super.entries() as
|
|
105
|
-
[HttpHeadersSchemaName<Schema>, NonNullable<Schema[HttpHeadersSchemaName<Schema>]> & string]
|
|
106
|
-
>;
|
|
105
|
+
return super.entries() as never;
|
|
107
106
|
}
|
|
108
107
|
|
|
109
108
|
[Symbol.iterator](): HeadersIterator<
|
|
110
|
-
[
|
|
109
|
+
[
|
|
110
|
+
HttpHeadersSchemaName<this['_schema']>,
|
|
111
|
+
NonNullable<this['_schema'][HttpHeadersSchemaName<this['_schema']>]> & string,
|
|
112
|
+
]
|
|
111
113
|
> {
|
|
112
|
-
return super[Symbol.iterator]() as
|
|
113
|
-
[HttpHeadersSchemaName<Schema>, NonNullable<Schema[HttpHeadersSchemaName<Schema>]> & string]
|
|
114
|
-
>;
|
|
114
|
+
return super[Symbol.iterator]() as never;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
@@ -183,8 +183,8 @@ class HttpHeaders<
|
|
|
183
183
|
*
|
|
184
184
|
* @returns A plain object representation of these headers.
|
|
185
185
|
*/
|
|
186
|
-
toObject():
|
|
187
|
-
const object = {} as
|
|
186
|
+
toObject(): this['_schema'] {
|
|
187
|
+
const object = {} as this['_schema'];
|
|
188
188
|
|
|
189
189
|
for (const [key, value] of this.entries()) {
|
|
190
190
|
object[key] = value;
|
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;
|
|
@@ -24,9 +24,6 @@ function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema.Loose>(sc
|
|
|
24
24
|
* An extended HTTP search params object with a strictly-typed schema. Fully compatible with the built-in
|
|
25
25
|
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams `URLSearchParams`} class.
|
|
26
26
|
*
|
|
27
|
-
* **IMPORTANT**: the input of `HttpSearchParams` and all of its internal types must be declared inline or as a type
|
|
28
|
-
* aliases (`type`). They cannot be interfaces.
|
|
29
|
-
*
|
|
30
27
|
* @example
|
|
31
28
|
* import { HttpSearchParams } from '@zimic/http';
|
|
32
29
|
*
|
|
@@ -48,8 +45,9 @@ function pickPrimitiveProperties<Schema extends HttpSearchParamsSchema.Loose>(sc
|
|
|
48
45
|
*/
|
|
49
46
|
class HttpSearchParams<
|
|
50
47
|
LooseSchema extends HttpSearchParamsSchema.Loose = HttpSearchParamsSchema.Loose,
|
|
51
|
-
Schema extends HttpSearchParamsSchema = HttpSearchParamsSerialized<LooseSchema>,
|
|
52
48
|
> extends URLSearchParams {
|
|
49
|
+
readonly _schema!: HttpSearchParamsSerialized<LooseSchema>;
|
|
50
|
+
|
|
53
51
|
constructor(init?: HttpSearchParamsInit<LooseSchema>) {
|
|
54
52
|
if (init instanceof URLSearchParams || Array.isArray(init) || typeof init === 'string' || !init) {
|
|
55
53
|
super(init);
|
|
@@ -70,7 +68,7 @@ class HttpSearchParams<
|
|
|
70
68
|
}
|
|
71
69
|
|
|
72
70
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/set MDN Reference} */
|
|
73
|
-
set<Name extends HttpSearchParamsSchemaName<
|
|
71
|
+
set<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
74
72
|
name: Name,
|
|
75
73
|
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
76
74
|
): void {
|
|
@@ -78,7 +76,7 @@ class HttpSearchParams<
|
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/append MDN Reference} */
|
|
81
|
-
append<Name extends HttpSearchParamsSchemaName<
|
|
79
|
+
append<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
82
80
|
name: Name,
|
|
83
81
|
value: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
84
82
|
): void {
|
|
@@ -94,10 +92,10 @@ class HttpSearchParams<
|
|
|
94
92
|
* @returns The value associated with the key name, or `null` if the key does not exist.
|
|
95
93
|
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/get MDN Reference}
|
|
96
94
|
*/
|
|
97
|
-
get<Name extends HttpSearchParamsSchemaName.NonArray<
|
|
95
|
+
get<Name extends HttpSearchParamsSchemaName.NonArray<this['_schema']>>(
|
|
98
96
|
name: Name,
|
|
99
|
-
): ReplaceBy<ArrayItemIfArray<
|
|
100
|
-
return super.get(name) as
|
|
97
|
+
): ReplaceBy<ArrayItemIfArray<this['_schema'][Name]>, undefined, null> {
|
|
98
|
+
return super.get(name) as never;
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
/**
|
|
@@ -109,14 +107,14 @@ class HttpSearchParams<
|
|
|
109
107
|
* @returns An array of values associated with the key name, or an empty array if the key does not exist.
|
|
110
108
|
* @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll MDN Reference}
|
|
111
109
|
*/
|
|
112
|
-
getAll<Name extends HttpSearchParamsSchemaName.Array<
|
|
110
|
+
getAll<Name extends HttpSearchParamsSchemaName.Array<this['_schema']>>(
|
|
113
111
|
name: Name,
|
|
114
|
-
): ArrayItemIfArray<NonNullable<
|
|
115
|
-
return super.getAll(name) as
|
|
112
|
+
): ArrayItemIfArray<NonNullable<this['_schema'][Name]>>[] {
|
|
113
|
+
return super.getAll(name) as never;
|
|
116
114
|
}
|
|
117
115
|
|
|
118
116
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/has MDN Reference} */
|
|
119
|
-
has<Name extends HttpSearchParamsSchemaName<
|
|
117
|
+
has<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
120
118
|
name: Name,
|
|
121
119
|
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
122
120
|
): boolean {
|
|
@@ -124,18 +122,18 @@ class HttpSearchParams<
|
|
|
124
122
|
}
|
|
125
123
|
|
|
126
124
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete MDN Reference} */
|
|
127
|
-
delete<Name extends HttpSearchParamsSchemaName<
|
|
125
|
+
delete<Name extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
128
126
|
name: Name,
|
|
129
127
|
value?: ArrayItemIfArray<NonNullable<LooseSchema[Name]>>,
|
|
130
128
|
): void {
|
|
131
129
|
super.delete(name, value);
|
|
132
130
|
}
|
|
133
131
|
|
|
134
|
-
forEach<This extends HttpSearchParams<
|
|
135
|
-
callback: <Key extends HttpSearchParamsSchemaName<
|
|
136
|
-
value: ArrayItemIfArray<NonNullable<
|
|
132
|
+
forEach<This extends HttpSearchParams<this['_schema']>>(
|
|
133
|
+
callback: <Key extends HttpSearchParamsSchemaName<this['_schema']>>(
|
|
134
|
+
value: ArrayItemIfArray<NonNullable<this['_schema'][Key]>>,
|
|
137
135
|
key: Key,
|
|
138
|
-
parent: HttpSearchParams<
|
|
136
|
+
parent: HttpSearchParams<this['_schema']>,
|
|
139
137
|
) => void,
|
|
140
138
|
thisArg?: This,
|
|
141
139
|
): void {
|
|
@@ -143,32 +141,34 @@ class HttpSearchParams<
|
|
|
143
141
|
}
|
|
144
142
|
|
|
145
143
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys MDN Reference} */
|
|
146
|
-
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<
|
|
147
|
-
return super.keys() as
|
|
144
|
+
keys(): URLSearchParamsIterator<HttpSearchParamsSchemaName<this['_schema']>> {
|
|
145
|
+
return super.keys() as never;
|
|
148
146
|
}
|
|
149
147
|
|
|
150
148
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/values MDN Reference} */
|
|
151
|
-
values(): URLSearchParamsIterator<
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
149
|
+
values(): URLSearchParamsIterator<
|
|
150
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>
|
|
151
|
+
> {
|
|
152
|
+
return super.values() as never;
|
|
155
153
|
}
|
|
156
154
|
|
|
157
155
|
/** @see {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries MDN Reference} */
|
|
158
156
|
entries(): URLSearchParamsIterator<
|
|
159
|
-
[
|
|
157
|
+
[
|
|
158
|
+
HttpSearchParamsSchemaName<this['_schema']>,
|
|
159
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>,
|
|
160
|
+
]
|
|
160
161
|
> {
|
|
161
|
-
return super.entries() as
|
|
162
|
-
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
163
|
-
>;
|
|
162
|
+
return super.entries() as never;
|
|
164
163
|
}
|
|
165
164
|
|
|
166
165
|
[Symbol.iterator](): URLSearchParamsIterator<
|
|
167
|
-
[
|
|
166
|
+
[
|
|
167
|
+
HttpSearchParamsSchemaName<this['_schema']>,
|
|
168
|
+
ArrayItemIfArray<NonNullable<this['_schema'][HttpSearchParamsSchemaName<this['_schema']>]>>,
|
|
169
|
+
]
|
|
168
170
|
> {
|
|
169
|
-
return super[Symbol.iterator]() as
|
|
170
|
-
[HttpSearchParamsSchemaName<Schema>, ArrayItemIfArray<NonNullable<Schema[HttpSearchParamsSchemaName<Schema>]>>]
|
|
171
|
-
>;
|
|
171
|
+
return super[Symbol.iterator]() as never;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
/**
|
|
@@ -227,13 +227,13 @@ class HttpSearchParams<
|
|
|
227
227
|
* @returns A plain object representation of these search params.
|
|
228
228
|
*/
|
|
229
229
|
toObject() {
|
|
230
|
-
const object = {} as
|
|
230
|
+
const object = {} as this['_schema'];
|
|
231
231
|
|
|
232
|
-
type SchemaValue =
|
|
232
|
+
type SchemaValue = this['_schema'][HttpSearchParamsSchemaName<this['_schema']>];
|
|
233
233
|
|
|
234
234
|
for (const [key, value] of this.entries()) {
|
|
235
235
|
if (key in object) {
|
|
236
|
-
const existingValue = object[key];
|
|
236
|
+
const existingValue = object[key] as SchemaValue[];
|
|
237
237
|
|
|
238
238
|
if (Array.isArray<SchemaValue>(existingValue)) {
|
|
239
239
|
existingValue.push(value as SchemaValue);
|
|
@@ -72,62 +72,56 @@ export type HttpSearchParamsSchemaName<Schema extends HttpSearchParamsSchema> =
|
|
|
72
72
|
keyof Schema & string
|
|
73
73
|
>;
|
|
74
74
|
|
|
75
|
-
type PrimitiveHttpSearchParamsSerialized<Type> = Type extends
|
|
76
|
-
?
|
|
77
|
-
: Type extends
|
|
78
|
-
?
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
type PrimitiveHttpSearchParamsSerialized<Type> = [Type] extends [never]
|
|
76
|
+
? never
|
|
77
|
+
: Type extends number
|
|
78
|
+
? `${number}`
|
|
79
|
+
: Type extends boolean
|
|
80
|
+
? `${boolean}`
|
|
81
|
+
: Type extends null
|
|
82
|
+
? 'null'
|
|
83
|
+
: Type extends symbol
|
|
84
|
+
? never
|
|
85
|
+
: Type extends HttpSearchParamsSchema[string]
|
|
86
|
+
? Type
|
|
87
|
+
: Type extends (infer ArrayItem)[]
|
|
88
|
+
? ArrayItem extends (infer _InternalArrayItem)[]
|
|
89
|
+
? string
|
|
90
|
+
: PrimitiveHttpSearchParamsSerialized<ArrayItem>[]
|
|
91
|
+
: string;
|
|
88
92
|
|
|
89
93
|
/**
|
|
90
94
|
* Recursively converts a schema to its
|
|
91
|
-
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams URLSearchParams}-serialized version. Numbers
|
|
92
|
-
* booleans are converted to `${number}
|
|
93
|
-
*
|
|
95
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URLSearchParams URLSearchParams}-serialized version. Numbers,
|
|
96
|
+
* booleans, and null are converted to `${number}`, `${boolean}`, and 'null' respectively, and other values become
|
|
97
|
+
* strings.
|
|
94
98
|
*
|
|
95
99
|
* @example
|
|
96
100
|
* import { type HttpSearchParamsSerialized } from '@zimic/http';
|
|
97
101
|
*
|
|
98
102
|
* type Params = HttpSearchParamsSerialized<{
|
|
99
|
-
* query
|
|
103
|
+
* query?: string;
|
|
104
|
+
* order: 'asc' | 'desc' | null;
|
|
100
105
|
* page?: number;
|
|
101
106
|
* full?: boolean;
|
|
102
|
-
* date: Date;
|
|
103
|
-
* method: () => void;
|
|
104
107
|
* }>;
|
|
105
108
|
* // {
|
|
106
|
-
* // query
|
|
109
|
+
* // query?: string;
|
|
110
|
+
* // order: 'asc' | 'desc' | 'null';
|
|
107
111
|
* // page?: `${number}`;
|
|
108
112
|
* // full?: "false" | "true";
|
|
109
113
|
* // }
|
|
110
114
|
*/
|
|
111
|
-
export type HttpSearchParamsSerialized<Type> = Type extends
|
|
112
|
-
?
|
|
113
|
-
: Type extends
|
|
114
|
-
?
|
|
115
|
-
: Type extends
|
|
116
|
-
?
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
: Type
|
|
122
|
-
|
|
123
|
-
|
|
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;
|
|
115
|
+
export type HttpSearchParamsSerialized<Type> = [Type] extends [never]
|
|
116
|
+
? never
|
|
117
|
+
: Type extends HttpSearchParamsSchema
|
|
118
|
+
? Type
|
|
119
|
+
: Type extends object
|
|
120
|
+
? {
|
|
121
|
+
[Key in keyof Type as IfNever<
|
|
122
|
+
PrimitiveHttpSearchParamsSerialized<Type[Key]>,
|
|
123
|
+
never,
|
|
124
|
+
Key
|
|
125
|
+
>]: PrimitiveHttpSearchParamsSerialized<Type[Key]>;
|
|
126
|
+
}
|
|
127
|
+
: never;
|