-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtyped-fetch.ts
More file actions
430 lines (401 loc) · 14.1 KB
/
Copy pathtyped-fetch.ts
File metadata and controls
430 lines (401 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { StatusCodeMap } from './types';
export type OkStatusCode = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226;
type RedirectStatusCode = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
type ClientErrorStatusCode =
| 400
| 401
| 402
| 403
| 404
| 405
| 406
| 407
| 408
| 409
| 410
| 411
| 412
| 413
| 414
| 415
| 416
| 417
| 418
| 421
| 422
| 423
| 424
| 425
| 426
| 428
| 429
| 431
| 451;
type ServerErrorStatusCode = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511;
export type StatusCode =
| OkStatusCode
| RedirectStatusCode
| ClientErrorStatusCode
| ServerErrorStatusCode;
export type NotOkStatusCode = Exclude<StatusCode, OkStatusCode>;
export type TypedBody<
TJSON,
TFormData extends Record<string, FormDataEntryValue | undefined>,
THeaders extends Record<string, string>,
> = Omit<Body, 'json' | 'formData' | 'headers'> & {
/**
* The `json()` method takes the stream and reads it to completion.
* It returns a promise which resolves with the result of parsing the body text as JSON.
*
* Note that despite the method being named `json()`, the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/json
*/
json(): Promise<TJSON>;
/**
* The formData() method takes the stream and reads it to completion. It returns a promise that resolves with a `FormData` object.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/formData
*/
formData(): Promise<TypedFormData<TFormData>>;
/**
* The headers read-only property of the Response interface contains the Headers object associated with the response.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/headers
*/
headers: TypedHeaders<THeaders>;
};
type DefaultHTTPHeaders =
| 'accept'
| 'accept-language'
| 'content-language'
| 'content-type'
| 'content-length';
type Maybe = undefined | null;
type UndefinedToNull<T> = T extends undefined ? Exclude<T, undefined> | null : T;
export type TypedHeaders<TMap extends Record<string, string>> = {
append<TName extends DefaultHTTPHeaders | keyof TMap>(
name: TName,
value: TName extends keyof TMap ? TMap[TName] : string,
): void;
delete<TName extends DefaultHTTPHeaders | keyof TMap>(name: TName): void;
get<TName extends DefaultHTTPHeaders | keyof TMap>(
name: TName,
): TName extends keyof TMap
? UndefinedToNull<TMap[TName]>
: TName extends DefaultHTTPHeaders
? string | null
: never;
has<TName extends DefaultHTTPHeaders | keyof TMap>(
name: TName,
): TName extends DefaultHTTPHeaders
? boolean
: TName extends keyof TMap
? TMap[TName] extends Maybe
? boolean
: true
: never;
set<TName extends DefaultHTTPHeaders | keyof TMap>(
name: TName,
value: TName extends keyof TMap ? TMap[TName] : string,
): void;
forEach(
callbackfn: <TName extends keyof TMap>(
value: TMap[TName],
key: TName,
parent: TypedHeaders<TMap>,
) => void,
thisArg?: any,
): void;
entries(): IterableIterator<[keyof TMap, TMap[keyof TMap]]>;
keys(): IterableIterator<keyof TMap>;
values(): IterableIterator<TMap[keyof TMap]>;
[Symbol.iterator](): IterableIterator<[keyof TMap, TMap[keyof TMap]]>;
};
export type TypedHeadersCtor = new <TMap extends Record<string, string>>(
init?: TMap,
) => TypedHeaders<TMap>;
export type TypedResponseInit<TStatusCode extends StatusCode = 200> = Omit<
ResponseInit,
'status' | 'statusText'
> & {
/**
* This is the status code that will be set in the response.
* For example, 200 for success, 404 if the resource could not be found.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/status
*/
status: TStatusCode;
/**
* This is the status message that will be set in the response.
* You don't need to set this value if it's a standard message.
* For example, this would be OK for a status code 200, Continue for 100, Not Found for 404.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText
*/
statusText?: TStatusCode extends keyof StatusTextMap ? StatusTextMap[TStatusCode] : string;
};
export type StatusTextMap = {
100: 'Continue';
101: 'Switching Protocols';
102: 'Processing';
103: 'Early Hints';
200: 'OK';
201: 'Created';
202: 'Accepted';
203: 'Non-Authoritative Information';
204: 'No Content';
205: 'Reset Content';
206: 'Partial Content';
207: 'Multi-Status';
208: 'Already Reported';
226: 'IM Used';
300: 'Multiple Choices';
301: 'Moved Permanently';
302: 'Found';
303: 'See Other';
304: 'Not Modified';
305: 'Use Proxy';
307: 'Temporary Redirect';
308: 'Permanent Redirect';
400: 'Bad Request';
401: 'Unauthorized';
402: 'Payment Required';
403: 'Forbidden';
404: 'Not Found';
405: 'Method Not Allowed';
406: 'Not Acceptable';
407: 'Proxy Authentication Required';
408: 'Request Timeout';
409: 'Conflict';
410: 'Gone';
411: 'Length Required';
412: 'Precondition Failed';
413: 'Payload Too Large';
414: 'URI Too Long';
415: 'Unsupported Media Type';
416: 'Range Not Satisfiable';
417: 'Expectation Failed';
418: "I'm a Teapot";
421: 'Misdirected Request';
422: 'Unprocessable Entity';
423: 'Locked';
424: 'Failed Dependency';
425: 'Too Early';
426: 'Upgrade Required';
428: 'Precondition Required';
429: 'Too Many Requests';
431: 'Request Header Fields Too Large';
451: 'Unavailable For Legal Reasons';
500: 'Internal Server Error';
501: 'Not Implemented';
502: 'Bad Gateway';
503: 'Service Unavailable';
504: 'Gateway Timeout';
505: 'HTTP Version Not Supported';
506: 'Variant Also Negotiates';
507: 'Insufficient Storage';
508: 'Loop Detected';
509: 'Bandwidth Limit Exceeded';
510: 'Not Extended';
511: 'Network Authentication Required';
};
export type TypedResponse<
TJSON = unknown,
THeaders extends Record<string, string> = Record<string, string>,
TStatusCode extends StatusCode = StatusCode,
> = Omit<Response, 'json' | 'status' | 'ok'> &
Omit<TypedBody<TJSON, any, THeaders>, 'formData'> & {
/**
* The status read-only property of the Response interface contains the HTTP status codes of the response.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/status
*
* You can use `Response.ok` to check if the status is in the range 200-299.
*/
status: TStatusCode;
/**
* The statusText read-only property of the Response interface contains the status message corresponding to the HTTP status code in Response.status.
* For example, this would be OK for a status code 200, Continue for 100, Not Found for 404.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText
*/
statusText: TStatusCode extends keyof StatusTextMap ? StatusTextMap[TStatusCode] : string;
} & {
/**
* The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.
*/
ok: TStatusCode extends OkStatusCode ? true : false;
};
export type TypedResponseCtor = Omit<typeof Response, 'json'> & {
new <TStatusCode extends StatusCode = 200>(
body: BodyInit | null | undefined,
init: (TypedResponseInit<TStatusCode> & { status: TStatusCode }) | undefined,
): TypedResponse<any, Record<string, string>, TStatusCode>;
new (
body: BodyInit | null | undefined,
init: ResponseInit | undefined,
): TypedResponse<any, Record<string, string>, 200>;
new (body?: BodyInit | null | undefined): TypedResponse<any, Record<string, string>, 200>;
/**
* The `json()` static method of the `Response` interface returns a `Response` that contains the provided JSON data as body,
* and a `Content-Type` header which is set to `application/json`. The response status, status message, and additional headers can also be set.
* @param data The JSON data to be used as the response body.
* @param options An options object containing settings for the response, including the status code, status text, and headers. This is the same as the options parameter of the `Response()` constructor.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static
*/
json<TJSON, TStatusCode extends StatusCode>(
data: TJSON,
options: TypedResponseInit<TStatusCode>,
): TypedResponse<TJSON, Record<string, string>, TStatusCode>;
/**
* The `json()` static method of the `Response` interface returns a `Response` that contains the provided JSON data as body,
* and a `Content-Type` header which is set to `application/json`.
* @param data The JSON data to be used as the response body.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static
*/
json<TJSON>(data: TJSON): TypedResponse<TJSON, Record<string, string>, 200>;
/**
* The redirect() static method of the Response interface returns a Response resulting in a redirect to the specified URL.
* @param url The URL that the new response is to originate from.
* @param status An optional status code for the response (e.g., 302.)
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static
*/
redirect<TStatusCode extends StatusCode>(
url: string | URL,
status: TStatusCode,
): TypedResponse<any, Record<string, string>, TStatusCode>;
/**
* The redirect() static method of the Response interface returns a Response resulting in a redirect to the specified URL.
* @param url The URL that the new response is to originate from.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static
*/
redirect(url: string | URL): TypedResponse<any, Record<string, string>, 302>;
};
export type TypedResponseWithJSONStatusMap<TResponseJSONStatusMap extends StatusCodeMap<any>> = {
[TStatusCode in keyof TResponseJSONStatusMap]: TStatusCode extends StatusCode
? TypedResponse<TResponseJSONStatusMap[TStatusCode], Record<string, string>, TStatusCode>
: never;
}[keyof TResponseJSONStatusMap];
export type HTTPMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'PATCH'
| 'DELETE'
| 'HEAD'
| 'OPTIONS'
| 'CONNECT'
| 'TRACE';
export type TypedRequestInit<
THeaders extends Record<string, string>,
TMethod extends HTTPMethod,
TFormData extends Record<string, FormDataEntryValue | undefined>,
> = Omit<RequestInit, 'method' | 'headers' | 'body'> & {
method: TMethod;
headers: TypedHeaders<THeaders>;
body?: Exclude<BodyInit, FormData> | TFormData;
};
export type TypedRequest<
TJSON = any,
TFormData extends Record<string, FormDataEntryValue | undefined> = Record<
string,
FormDataEntryValue | undefined
>,
THeaders extends Record<string, string> = Record<string, string>,
TMethod extends HTTPMethod = HTTPMethod,
TQueryParams extends Record<string, string | string[]> = Record<string, string | string[]>,
TPathParams extends Record<string, any> = Record<string, any>,
> = Omit<Request, 'json' | 'method' | 'headers' | 'formData'> &
TypedBody<TJSON, TFormData, THeaders> & {
method: TMethod;
parsedUrl: TypedURL<TQueryParams>;
params: TPathParams;
query: TQueryParams;
};
export type TypedRequestCtor = new <
THeaders extends Record<string, string>,
TMethod extends HTTPMethod,
TQueryParams extends Record<string, string | string[]>,
TFormData extends Record<string, FormDataEntryValue | undefined>,
>(
input: string | TypedURL<TQueryParams>,
init?: TypedRequestInit<THeaders, TMethod, TFormData>,
) => TypedRequest<any, TFormData, THeaders, TMethod, TQueryParams, any>;
export interface TypedURLSearchParams<TMap extends Record<string, string | string[]>> {
append<TName extends keyof TMap>(
name: TName,
value: TMap[TName] extends any[] ? TMap[TName][1] : TMap[TName],
): void;
delete(name: keyof TMap): void;
get<TName extends keyof TMap>(
name: TName,
): TMap[TName] extends any[] ? TMap[TName][1] : TMap[TName];
getAll<TName extends keyof TMap>(
name: TName,
): TMap[TName] extends any[] ? TMap[TName] : [TMap[TName]];
set<TName extends keyof TMap>(
name: TName,
value: TMap[TName] extends any[] ? TMap[TName][1] : TMap[TName],
): void;
sort(): void;
toString(): string;
forEach(
callbackfn: <TName extends keyof TMap>(
value: TMap[TName] extends any[] ? TMap[TName][1] : TMap[TName],
name: TName,
parent: TypedURLSearchParams<TMap>,
) => void,
thisArg?: any,
): void;
}
export type TypedURLSearchParamsCtor = new <TMap extends Record<string, string | string[]>>(
init?: TMap,
) => TypedURLSearchParams<TMap>;
export type TypedURL<TQueryParams extends Record<string, string | string[]>> = Omit<
URL,
'searchParams'
> & {
searchParams: TypedURLSearchParams<TQueryParams>;
};
export type TypedURLCtor = new <TQueryParams extends Record<string, string | string[]>>(
input: string,
base?: string | TypedURL<any>,
) => TypedURL<TQueryParams>;
export interface TypedFormData<
TMap extends Record<string, FormDataEntryValue | undefined> = Record<
string,
FormDataEntryValue | undefined
>,
> {
append<TName extends keyof TMap>(
name: TName,
value: TMap[TName] extends any[] ? TMap[TName][0] : TMap[TName],
fileName?: string,
): void;
delete(name: keyof TMap): void;
get<TName extends keyof TMap>(
name: TName,
): TMap[TName] extends any[] ? TMap[TName][0] : TMap[TName];
getAll<TName extends keyof TMap>(
name: TName,
): TMap[TName] extends any[] ? TMap[TName] : TMap[TName][];
has<TName extends string>(
name: TName,
): TName extends keyof TMap ? (TMap[TName] extends Maybe ? boolean : true) : false;
set<TName extends keyof TMap>(
name: TName,
value: TMap[TName] extends any[] ? TMap[TName][0] : TMap[TName],
fileName?: string,
): void;
forEach(
callbackfn: <TName extends keyof TMap>(value: TMap[TName], key: TName, parent: this) => void,
thisArg?: any,
): void;
entries(): IterableIterator<[keyof TMap, TMap[keyof TMap]]>;
keys(): IterableIterator<keyof TMap>;
values(): IterableIterator<TMap[keyof TMap]>;
[Symbol.iterator](): IterableIterator<[keyof TMap, TMap[keyof TMap]]>;
}