-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtypes.ts
More file actions
639 lines (586 loc) · 19.9 KB
/
Copy pathtypes.ts
File metadata and controls
639 lines (586 loc) · 19.9 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import type { Pipe, Strings, Tuples } from 'hotscript';
import type {
FromSchema as FromSchemaOriginal,
JSONSchema as JSONSchemaOrBoolean,
} from 'json-schema-to-ts';
import type {
ServerAdapter,
ServerAdapterOptions,
ServerAdapterPlugin,
ServerAdapterRequestHandler,
} from '@whatwg-node/server';
import type { SwaggerUIOpts } from './plugins/openapi.js';
import type { LazySerializedResponse } from './Response.js';
import type {
HTTPMethod,
StatusCode,
TypedRequest,
TypedResponse,
TypedResponseWithJSONStatusMap,
} from './typed-fetch.js';
import type {
AddRouteWithZodSchemasOpts,
RouteZodSchemas,
TypedRequestFromRouteZodSchemas,
TypedResponseFromRouteZodSchemas,
} from './zod/types.js';
export { TypedRequest as RouterRequest };
export type Simplify<T> = { [KeyType in keyof T]: Simplify<T[KeyType]> } & {};
export type JSONSerializer = (obj: any) => string;
export type JSONSchema = Exclude<JSONSchemaOrBoolean, boolean>;
export interface OpenAPIInfo {
title?: string | undefined;
description?: string | undefined;
version?: string | undefined;
license?:
| {
name?: string | undefined;
url?: string | undefined;
}
| undefined;
}
export type OpenAPIPathObject = Record<string, OpenAPIOperationObject> & {
parameters?: OpenAPIParameterObject[] | undefined;
};
export interface OpenAPIParameterObject {
name: string;
in: 'path' | 'query' | 'header' | 'cookie';
required?: boolean | undefined;
schema?: any;
}
export interface OpenAPIRequestBodyObject {
content?: Record<string, OpenAPIMediaTypeObject> | undefined;
required?: boolean | undefined;
}
export interface OpenAPIOperationObject {
operationId?: string | undefined;
description?: string | undefined;
tags?: string[] | undefined;
parameters?: OpenAPIParameterObject[] | undefined;
requestBody?: OpenAPIRequestBodyObject | undefined;
responses?: Record<string | number, OpenAPIResponseObject> | undefined;
}
export interface OpenAPIResponseObject {
description?: string | undefined;
content?: Record<string, OpenAPIMediaTypeObject> | undefined;
}
export interface OpenAPIMediaTypeObject {
schema?: any;
}
export type OpenAPIDocument = {
openapi?: string | undefined;
info?: OpenAPIInfo | undefined;
servers?:
| {
url: string;
}[]
| undefined;
paths?: Record<string, OpenAPIPathObject> | undefined;
components?: unknown;
};
export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase>
extends OpenAPIDocument {
endpoint?: string | false | undefined;
components?: TComponents | undefined;
}
export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
endpoint?: string | false | undefined;
}
// I've created a PR to fix this in @whatwg-node/server https://github.com/ardatan/whatwg-node/issues/391
// Interface 'RouterOptions<TServerContext, TComponents>' incorrectly extends interface 'ServerAdapterOptions<TServerContext>'.
// Types of property 'plugins' are incompatible.
// Type 'RouterPlugin<TServerContext>[] | undefined' is not assignable to type 'ServerAdapterPlugin<TServerContext>[]'.
// Type 'undefined' is not assignable to type 'ServerAdapterPlugin<TServerContext>[]'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export interface RouterOptions<TServerContext, TComponents extends RouterComponentsBase>
extends ServerAdapterOptions<TServerContext> {
base?: string | undefined;
plugins?: RouterPlugin<TServerContext>[] | undefined;
openAPI?: RouterOpenAPIOptions<TComponents> | undefined;
swaggerUI?: RouterSwaggerUIOptions | undefined;
}
export type RouterComponentsBase = {
schemas: Record<string, JSONSchema>;
};
/*
Maybe later;
type IntRange<
START extends number,
END extends number,
ARR extends unknown[] = [],
ACC extends number = never
> = ARR['length'] extends END
? ACC | START | END
: IntRange<START, END, [...ARR, 1], ARR[START] extends undefined ? ACC : ACC | ARR['length']>
type RangedJSONSchema<T extends { minimum: number; maximum: number }> = T extends {
minimum: infer MIN;
maximum: infer MAX;
} ? MIN extends number ? MAX extends number ? IntRange<MIN, MAX> : never : never : never;
*/
export type FromSchema<T> =
/* T extends { type: 'integer'; minimum: number; maximum: number } ? RangedJSONSchema<T> : */ T extends JSONSchema
? FromSchemaOriginal<
T,
{
deserialize: T extends T['properties'][keyof T['properties']]
? false
: [
{
pattern: {
type: 'string';
format: 'binary';
};
output: File;
},
{
pattern: {
type: 'number';
format: 'int64';
};
output: bigint;
},
{
pattern: {
type: 'integer';
format: 'int64';
};
output: bigint;
},
];
}
>
: never;
export type FromRouterComponentSchema<
TRouter extends Router<any, any, any>,
TName extends string,
> = TRouter extends Router<any, infer TComponents, any>
? TComponents extends Required<RouterComponentsBase>
? FromSchema<TComponents['schemas'][TName]>
: never
: never;
export type PromiseOrValue<T> = T | Promise<T>;
export type StatusCodeMap<T> = {
[TKey in StatusCode]?: T | undefined;
};
export type TypedRouterHandlerTypeConfig<
TPath extends string,
TRequestJSON = any,
TRequestFormData extends Record<string, FormDataEntryValue | undefined> = Record<
string,
FormDataEntryValue | undefined
>,
TRequestHeaders extends Record<string, string> = Record<string, string>,
TRequestQueryParams extends Record<string, string | string[]> = Record<string, string | string[]>,
TRequestPathParams extends Record<string, any> = Record<
ExtractPathParamsWithPattern<TPath>,
string
>,
TResponseJSONStatusMap extends StatusCodeMap<any> = StatusCodeMap<any>,
> = {
request: {
json?: TRequestJSON | undefined;
formData?: TRequestFormData | undefined;
headers?: TRequestHeaders | undefined;
query?: TRequestQueryParams | undefined;
params?: TRequestPathParams | undefined;
};
responses?: TResponseJSONStatusMap | undefined;
};
export type TypedRequestFromTypeConfig<
TMethod extends HTTPMethod,
TPath extends string,
TTypeConfig extends TypedRouterHandlerTypeConfig<TPath>,
> = TTypeConfig extends { request: Required<TypedRouterHandlerTypeConfig<TPath>>['request'] }
? TTypeConfig extends TypedRouterHandlerTypeConfig<
TPath,
infer TRequestJSON,
infer TRequestFormData,
infer TRequestHeaders,
infer TRequestQueryParams,
infer TRequestPathParams
>
? TypedRequest<
TRequestJSON,
TRequestFormData,
TRequestHeaders,
TMethod,
TRequestQueryParams,
TRequestPathParams
>
: never
: TypedRequest<
any,
Record<string, FormDataEntryValue | undefined>,
Record<string, string>,
TMethod,
Record<string, string | string[]>,
Record<ExtractPathParamsWithPattern<TPath>, string>
>;
export type TypedResponseFromTypeConfig<TTypeConfig extends TypedRouterHandlerTypeConfig<string>> =
TTypeConfig extends {
responses: infer TResponses;
}
? TResponses extends StatusCodeMap<any>
? TypedResponseWithJSONStatusMap<TResponses>
: never
: TypedResponse;
export interface RouterBaseObject<
TServerContext,
TComponents extends RouterComponentsBase,
TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>,
> {
openAPIDocument: OpenAPIDocument;
handle: ServerAdapterRequestHandler<TServerContext>;
route<
TRouteSchemas extends RouteSchemas,
TMethod extends HTTPMethod,
TPath extends string,
TTypedRequest extends TypedRequestFromRouteSchemas<TComponents, TRouteSchemas, TMethod, TPath>,
TTypedResponse extends TypedResponseFromRouteSchemas<TComponents, TRouteSchemas>,
>(
opts: AddRouteWithSchemasOpts<
TServerContext,
TComponents,
TRouteSchemas,
TMethod,
TPath,
TTypedRequest,
TTypedResponse
>,
): Router<
TServerContext,
TComponents,
TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>
>;
route<
TRouteZodSchemas extends RouteZodSchemas,
TMethod extends HTTPMethod,
TPath extends string,
TTypedRequest extends TypedRequestFromRouteZodSchemas<TRouteZodSchemas, TMethod>,
TTypedResponse extends TypedResponseFromRouteZodSchemas<TRouteZodSchemas>,
>(
opts: AddRouteWithZodSchemasOpts<
TServerContext,
TRouteZodSchemas,
TMethod,
TPath,
TTypedRequest,
TTypedResponse
>,
): Router<
TServerContext,
TComponents,
TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>
>;
route<
TTypeConfig extends TypedRouterHandlerTypeConfig<TPath>,
TMethod extends HTTPMethod,
TPath extends string,
TTypedRequest extends TypedRequestFromTypeConfig<
TMethod,
TPath,
TTypeConfig
> = TypedRequestFromTypeConfig<TMethod, TPath, TTypeConfig>,
TTypedResponse extends
TypedResponseFromTypeConfig<TTypeConfig> = TypedResponseFromTypeConfig<TTypeConfig>,
>(
opts: AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>,
): Router<
TServerContext,
TComponents,
TRouterSDK & RouterSDK<TPath, TTypedRequest, TTypedResponse>
>;
__client: TRouterSDK;
__onRouterInitHooks: OnRouterInitHook<TServerContext>[];
}
export type Router<
TServerContext,
TComponents extends RouterComponentsBase,
TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>,
> = ServerAdapter<TServerContext, RouterBaseObject<TServerContext, TComponents, TRouterSDK>>;
export type OnRouteHook<TServerContext> = (payload: OnRouteHookPayload<TServerContext>) => void;
export type RouteHandler<
TServerContext = {},
TTypedRequest extends TypedRequest = TypedRequest,
TTypedResponse extends TypedResponse = TypedResponse,
> = (
/**
* The request object represents the incoming HTTP request.
* This object implements [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface.
*/
request: TTypedRequest,
context: TServerContext,
) => PromiseOrValue<TTypedResponse | void>;
// TODO: Remove Response from here
export type OnRouteHookPayload<TServerContext> = {
operationId?: string | undefined;
description?: string | undefined;
tags?: string[] | undefined;
method: HTTPMethod;
path: string;
schemas?: RouteSchemas | RouteZodSchemas | undefined;
openAPIDocument: OpenAPIDocument;
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
};
export type OnRouterInitHook<TServerContext> = (router: Router<TServerContext, any, any>) => void;
export type OnSerializeResponsePayload<TServerContext> = {
request: TypedRequest;
path: string;
serverContext: TServerContext;
lazyResponse: LazySerializedResponse;
};
export type OnSerializeResponseHook<TServerContext> = (
payload: OnSerializeResponsePayload<TServerContext>,
) => void;
export type RouterPlugin<TServerContext> = ServerAdapterPlugin<TServerContext> & {
onRouterInit?: OnRouterInitHook<TServerContext> | undefined;
onRoute?: OnRouteHook<TServerContext> | undefined;
onSerializeResponse?: OnSerializeResponseHook<TServerContext> | undefined;
};
export type RouteSchemas = {
request?:
| {
headers?: JSONSchema | undefined;
params?: JSONSchema | undefined;
query?: JSONSchema | undefined;
json?: JSONSchema | undefined;
formData?: JSONSchema | undefined;
}
| undefined;
responses?: StatusCodeMap<JSONSchema> | undefined;
};
export type RouterSDKOpts<
TTypedRequest extends TypedRequest = TypedRequest,
TMethod extends HTTPMethod = HTTPMethod,
> = TTypedRequest extends TypedRequest<
infer TJSONBody,
infer TFormData,
infer THeaders,
TMethod,
infer TQueryParams,
infer TPathParam
>
? {
json?: TJSONBody | undefined;
formData?: TFormData | undefined;
headers?: THeaders | undefined;
query?: TQueryParams | undefined;
params?: TPathParam | undefined;
}
: never;
export type RouterSDK<
TPath extends string = string,
TTypedRequest extends TypedRequest = TypedRequest,
TTypedResponse extends TypedResponse = TypedResponse,
> = {
[TPathKey in TPath]: {
[TMethod in Lowercase<TTypedRequest['method']>]: (
opts?: RouterSDKOpts<TTypedRequest, TTypedRequest['method']> | undefined,
) => Promise<Exclude<TTypedResponse, undefined>>;
};
};
export type FromSchemaWithComponents<
TComponents,
TSchema extends JSONSchema,
> = TComponents extends {
schemas: Record<string, JSONSchema>;
}
? FromSchema<
{
components: TComponents;
} & TSchema
>
: FromSchema<TSchema>;
export type TypedRequestFromRouteSchemas<
TComponents extends RouterComponentsBase,
TRouteSchemas extends RouteSchemas,
TMethod extends HTTPMethod,
TPath extends string,
> = TRouteSchemas extends { request: Required<RouteSchemas>['request'] }
? TypedRequest<
TRouteSchemas['request'] extends { json: JSONSchema }
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['json']>
: any,
TRouteSchemas['request'] extends { formData: JSONSchema }
? FromSchemaWithComponents<
TComponents,
TRouteSchemas['request']['formData']
> extends Record<string, FormDataEntryValue | undefined>
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['formData']>
: Record<string, FormDataEntryValue | undefined>
: Record<string, FormDataEntryValue | undefined>,
TRouteSchemas['request'] extends { headers: JSONSchema }
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']> extends Record<
string,
string
>
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']>
: Record<string, string>
: Record<string, string>,
TMethod,
TRouteSchemas['request'] extends { query: JSONSchema }
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['query']> extends Record<
string,
string | string[]
>
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['query']>
: Record<string, string | string[]>
: Record<string, string | string[]>,
TRouteSchemas['request'] extends { params: JSONSchema }
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['params']> extends Record<
string,
any
>
? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['params']>
: Record<ExtractPathParamsWithPattern<TPath>, string>
: Record<ExtractPathParamsWithPattern<TPath>, string>
>
: TypedRequest<
any,
Record<string, FormDataEntryValue | undefined>,
Record<string, string>,
TMethod,
Record<string, string | string[]>,
Record<ExtractPathParamsWithPattern<TPath>, string>
>;
export type TypedResponseFromRouteSchemas<
TComponents extends RouterComponentsBase,
TRouteSchemas extends RouteSchemas,
> = TRouteSchemas extends { responses: StatusCodeMap<JSONSchema> }
? TypedResponseWithJSONStatusMap<{
[TStatusCode in keyof TRouteSchemas['responses']]: TRouteSchemas['responses'][TStatusCode] extends JSONSchema
? FromSchemaWithComponents<TComponents, TRouteSchemas['responses'][TStatusCode]>
: never;
}>
: TypedResponse;
export type AddRouteWithSchemasOpts<
TServerContext,
TComponents extends RouterComponentsBase,
TRouteSchemas extends RouteSchemas,
TMethod extends HTTPMethod,
TPath extends string,
TTypedRequest extends TypedRequestFromRouteSchemas<TComponents, TRouteSchemas, TMethod, TPath>,
TTypedResponse extends TypedResponseFromRouteSchemas<TComponents, TRouteSchemas>,
> = {
schemas: TRouteSchemas;
} & AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>;
export type AddRouteWithTypesOpts<
TServerContext,
TMethod extends HTTPMethod,
TPath extends string,
TTypedRequest extends TypedRequest<
any,
Record<string, FormDataEntryValue | undefined>,
Record<string, string>,
TMethod,
Record<string, string | string[]>,
Record<ExtractPathParamsWithPattern<TPath>, string>
>,
TTypedResponse extends TypedResponse,
> = {
operationId?: string | undefined;
description?: string | undefined;
method?: TMethod | undefined;
tags?: string[] | undefined;
internal?: boolean | undefined;
path: TPath;
handler:
| RouteHandler<TServerContext, TTypedRequest, TTypedResponse>
| RouteHandler<TServerContext, TTypedRequest, TTypedResponse>[];
};
export type RouteInput<
TRouter extends Router<any, any, {}>,
TPath extends string,
TMethod extends Lowercase<HTTPMethod> = 'post',
TParamType extends keyof RouterSDKOpts = 'json',
> = TRouter extends Router<any, any, infer TRouterSDK>
? TRouterSDK[TPath][TMethod] extends (requestParams?: infer TRequestParams) => any
? TRequestParams extends {
[TParamTypeKey in TParamType]?: infer TParamTypeValue;
}
? TParamTypeValue
: never
: never
: never;
export type RouteOutput<
TRouter extends Router<any, any, {}>,
TPath extends string,
TMethod extends Lowercase<HTTPMethod> = 'post',
TStatusCode extends StatusCode = 200,
> = TRouter extends Router<any, any, infer TRouterSDK>
? TRouterSDK extends RouterSDK
? TRouterSDK[TPath][TMethod] extends (...args: any[]) => Promise<infer TTypedResponse>
? TTypedResponse extends TypedResponse<infer TJSONBody, any, TStatusCode>
? TJSONBody
: never
: never
: never
: never;
export type RouterClient<TRouter extends Router<any, any, any>> = TRouter['__client'];
export type RouterInput<TRouter extends Router<any, any, any>> = {
[TPath in keyof RouterClient<TRouter>]: {
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (
requestParams?: infer TRequestParams | undefined,
) => any
? Required<TRequestParams>
: never;
};
};
export type RouterJsonPostInput<TRouter extends Router<any, any, any>> = {
[TPath in keyof RouterClient<TRouter>]: {
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterInput<TRouter>[TPath][TMethod] extends {
json: infer TJSON;
}
? TJSON
: never;
}['post'];
};
export type RouterJsonPostSuccessOutput<TRouter extends Router<any, any, any>> = {
[TPath in keyof RouterClient<TRouter>]: {
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterOutput<TRouter>[TPath][TMethod][200];
}['post'];
};
export type RouterOutput<TRouter extends Router<any, any, any>> = {
[TPath in keyof RouterClient<TRouter>]: {
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (
requestParams?: any,
) => Promise<infer TTypedResponse>
? {
[TStatusCode in StatusCode]: TTypedResponse extends TypedResponse<
infer TJSONBody,
any,
TStatusCode
>
? TJSONBody
: never;
}
: never;
};
};
export type RouterComponentSchema<
TRouter extends Router<any, any, any>,
TName extends string,
> = TRouter extends Router<any, infer TComponents, any>
? TComponents extends { schemas: Record<string, JSONSchema> }
? FromSchema<TComponents['schemas'][TName]>
: never
: never;
export type ExtractPathParamsWithBrackets<TPath extends string> = Pipe<
TPath,
[
Strings.Split<'/' | ';'>,
Tuples.Filter<Strings.StartsWith<'{'>>,
Tuples.Map<Strings.Trim<'{' | '}'>>,
Tuples.ToUnion,
]
>;
export type ExtractPathParamsWithPattern<TPath extends string> = Pipe<
TPath,
[
Strings.Split<'/'>,
Tuples.Filter<Strings.StartsWith<':'>>,
Tuples.Map<Strings.Trim<':'>>,
Tuples.ToUnion,
]
>;