fets 0.4.14 → 0.4.15
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/cjs/Response.js +80 -0
- package/cjs/client/auth/oauth.js +34 -0
- package/cjs/client/createClient.js +133 -0
- package/cjs/client/index.js +6 -0
- package/cjs/client/plugins/useClientCookieStore.js +31 -0
- package/cjs/client/types.js +0 -0
- package/cjs/createRouter.js +299 -0
- package/cjs/index.js +16 -0
- package/cjs/plugins/ajv.js +213 -0
- package/cjs/plugins/openapi.js +171 -0
- package/cjs/plugins/utils.js +31 -0
- package/cjs/swagger-ui-html.js +3 -0
- package/cjs/typed-fetch.js +0 -0
- package/cjs/types.js +0 -0
- package/cjs/utils.js +73 -0
- package/cjs/zod/types.js +7 -0
- package/cjs/zod/zod.js +92 -0
- package/esm/Response.js +74 -0
- package/esm/client/auth/oauth.js +31 -0
- package/esm/client/createClient.js +128 -0
- package/esm/client/index.js +3 -0
- package/esm/client/plugins/useClientCookieStore.js +27 -0
- package/esm/client/types.js +0 -0
- package/esm/createRouter.js +293 -0
- package/esm/index.js +7 -0
- package/esm/plugins/ajv.js +208 -0
- package/esm/plugins/openapi.js +166 -0
- package/esm/plugins/utils.js +27 -0
- package/esm/swagger-ui-html.js +1 -0
- package/esm/typed-fetch.js +0 -0
- package/esm/types.js +0 -0
- package/esm/utils.js +69 -0
- package/esm/zod/types.js +3 -0
- package/esm/zod/zod.js +88 -0
- package/package.json +1 -1
- package/typings/Response.d.cts +28 -0
- package/typings/Response.d.ts +28 -0
- package/typings/client/auth/oauth.d.cts +150 -0
- package/typings/client/auth/oauth.d.ts +150 -0
- package/typings/client/createClient.d.cts +34 -0
- package/typings/client/createClient.d.ts +34 -0
- package/typings/client/index.d.cts +3 -0
- package/typings/client/index.d.ts +3 -0
- package/typings/client/plugins/useClientCookieStore.d.cts +3 -0
- package/typings/client/plugins/useClientCookieStore.d.ts +3 -0
- package/typings/client/types.d.cts +426 -0
- package/typings/client/types.d.ts +426 -0
- package/typings/createRouter.d.cts +6 -0
- package/typings/createRouter.d.ts +6 -0
- package/typings/index.d.cts +7 -0
- package/typings/index.d.ts +7 -0
- package/typings/plugins/ajv.d.cts +4 -0
- package/typings/plugins/ajv.d.ts +4 -0
- package/typings/plugins/openapi.d.cts +33 -0
- package/typings/plugins/openapi.d.ts +33 -0
- package/typings/plugins/utils.d.cts +1 -0
- package/typings/plugins/utils.d.ts +1 -0
- package/typings/swagger-ui-html.d.cts +2 -0
- package/typings/swagger-ui-html.d.ts +2 -0
- package/typings/typed-fetch.d.cts +232 -0
- package/typings/typed-fetch.d.ts +232 -0
- package/typings/types.d.cts +261 -0
- package/typings/types.d.ts +261 -0
- package/typings/utils.d.cts +31 -0
- package/typings/utils.d.ts +31 -0
- package/typings/zod/types.d.cts +39 -0
- package/typings/zod/types.d.ts +39 -0
- package/typings/zod/zod.d.cts +2 -0
- package/typings/zod/zod.d.ts +2 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type { Pipe, Strings, Tuples } from 'hotscript';
|
|
2
|
+
import type { FromSchema as FromSchemaOriginal, JSONSchema as JSONSchemaOrBoolean } from 'json-schema-to-ts';
|
|
3
|
+
import type { ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler } from '@whatwg-node/server';
|
|
4
|
+
import type { SwaggerUIOpts } from './plugins/openapi.cjs';
|
|
5
|
+
import type { LazySerializedResponse } from './Response.cjs';
|
|
6
|
+
import type { HTTPMethod, StatusCode, TypedRequest, TypedResponse, TypedResponseWithJSONStatusMap } from './typed-fetch.cjs';
|
|
7
|
+
import type { AddRouteWithZodSchemasOpts, RouteZodSchemas, TypedRequestFromRouteZodSchemas, TypedResponseFromRouteZodSchemas } from './zod/types.cjs';
|
|
8
|
+
export { TypedRequest as RouterRequest };
|
|
9
|
+
export type Simplify<T> = {
|
|
10
|
+
[KeyType in keyof T]: Simplify<T[KeyType]>;
|
|
11
|
+
} & {};
|
|
12
|
+
export type JSONSerializer = (obj: any) => string;
|
|
13
|
+
export type JSONSchema = Exclude<JSONSchemaOrBoolean, boolean>;
|
|
14
|
+
export interface OpenAPIInfo {
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
version?: string;
|
|
18
|
+
license?: {
|
|
19
|
+
name?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export type OpenAPIPathObject = Record<string, OpenAPIOperationObject> & {
|
|
24
|
+
parameters?: OpenAPIParameterObject[];
|
|
25
|
+
};
|
|
26
|
+
export interface OpenAPIParameterObject {
|
|
27
|
+
name: string;
|
|
28
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
29
|
+
required?: boolean;
|
|
30
|
+
schema?: any;
|
|
31
|
+
}
|
|
32
|
+
export interface OpenAPIRequestBodyObject {
|
|
33
|
+
content?: Record<string, OpenAPIMediaTypeObject>;
|
|
34
|
+
}
|
|
35
|
+
export interface OpenAPIOperationObject {
|
|
36
|
+
operationId?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
tags?: string[];
|
|
39
|
+
parameters?: OpenAPIParameterObject[];
|
|
40
|
+
requestBody?: OpenAPIRequestBodyObject;
|
|
41
|
+
responses?: Record<string | number, OpenAPIResponseObject>;
|
|
42
|
+
}
|
|
43
|
+
export interface OpenAPIResponseObject {
|
|
44
|
+
description?: string;
|
|
45
|
+
content?: Record<string, OpenAPIMediaTypeObject>;
|
|
46
|
+
}
|
|
47
|
+
export interface OpenAPIMediaTypeObject {
|
|
48
|
+
schema?: any;
|
|
49
|
+
}
|
|
50
|
+
export type OpenAPIDocument = {
|
|
51
|
+
openapi?: string;
|
|
52
|
+
info?: OpenAPIInfo;
|
|
53
|
+
servers?: {
|
|
54
|
+
url: string;
|
|
55
|
+
}[] | string[];
|
|
56
|
+
paths?: Record<string, OpenAPIPathObject>;
|
|
57
|
+
components?: unknown;
|
|
58
|
+
};
|
|
59
|
+
export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase> extends OpenAPIDocument {
|
|
60
|
+
endpoint?: string | false;
|
|
61
|
+
components?: TComponents;
|
|
62
|
+
}
|
|
63
|
+
export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
|
|
64
|
+
endpoint?: string | false;
|
|
65
|
+
}
|
|
66
|
+
export interface RouterOptions<TServerContext, TComponents extends RouterComponentsBase> extends ServerAdapterOptions<TServerContext> {
|
|
67
|
+
base?: string;
|
|
68
|
+
plugins?: RouterPlugin<TServerContext>[];
|
|
69
|
+
openAPI?: RouterOpenAPIOptions<TComponents>;
|
|
70
|
+
swaggerUI?: RouterSwaggerUIOptions;
|
|
71
|
+
}
|
|
72
|
+
export type RouterComponentsBase = {
|
|
73
|
+
schemas?: Record<string, JSONSchema>;
|
|
74
|
+
};
|
|
75
|
+
export type FromSchema<T> = T extends JSONSchema ? FromSchemaOriginal<T, {
|
|
76
|
+
deserialize: T extends T['properties'][keyof T['properties']] ? false : [
|
|
77
|
+
{
|
|
78
|
+
pattern: {
|
|
79
|
+
type: 'string';
|
|
80
|
+
format: 'binary';
|
|
81
|
+
};
|
|
82
|
+
output: File;
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
pattern: {
|
|
86
|
+
type: 'number';
|
|
87
|
+
format: 'int64';
|
|
88
|
+
};
|
|
89
|
+
output: bigint | number;
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
pattern: {
|
|
93
|
+
type: 'integer';
|
|
94
|
+
format: 'int64';
|
|
95
|
+
};
|
|
96
|
+
output: bigint | number;
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
}> : never;
|
|
100
|
+
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;
|
|
101
|
+
export type PromiseOrValue<T> = T | Promise<T>;
|
|
102
|
+
export type StatusCodeMap<T> = {
|
|
103
|
+
[TKey in StatusCode]?: T;
|
|
104
|
+
};
|
|
105
|
+
export type TypedRouterHandlerTypeConfig<TPath extends string, TRequestJSON = any, TRequestFormData extends Record<string, FormDataEntryValue> = Record<string, FormDataEntryValue>, 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>> = {
|
|
106
|
+
request: {
|
|
107
|
+
json?: TRequestJSON;
|
|
108
|
+
formData?: TRequestFormData;
|
|
109
|
+
headers?: TRequestHeaders;
|
|
110
|
+
query?: TRequestQueryParams;
|
|
111
|
+
params?: TRequestPathParams;
|
|
112
|
+
};
|
|
113
|
+
responses?: TResponseJSONStatusMap;
|
|
114
|
+
};
|
|
115
|
+
export type TypedRequestFromTypeConfig<TMethod extends HTTPMethod, TPath extends string, TTypeConfig extends TypedRouterHandlerTypeConfig<TPath>> = TTypeConfig extends {
|
|
116
|
+
request: Required<TypedRouterHandlerTypeConfig<TPath>>['request'];
|
|
117
|
+
} ? 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>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>;
|
|
118
|
+
export type TypedResponseFromTypeConfig<TTypeConfig extends TypedRouterHandlerTypeConfig<string>> = TTypeConfig extends {
|
|
119
|
+
responses: infer TResponses;
|
|
120
|
+
} ? TResponses extends StatusCodeMap<any> ? TypedResponseWithJSONStatusMap<TResponses> : never : TypedResponse;
|
|
121
|
+
export interface RouterBaseObject<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> {
|
|
122
|
+
openAPIDocument: OpenAPIDocument;
|
|
123
|
+
handle: ServerAdapterRequestHandler<TServerContext>;
|
|
124
|
+
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>>;
|
|
125
|
+
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>>;
|
|
126
|
+
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>>;
|
|
127
|
+
__client: TRouterSDK;
|
|
128
|
+
__onRouterInitHooks: OnRouterInitHook<TServerContext>[];
|
|
129
|
+
}
|
|
130
|
+
export type Router<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> = ServerAdapter<TServerContext, RouterBaseObject<TServerContext, TComponents, TRouterSDK>>;
|
|
131
|
+
export type OnRouteHook<TServerContext> = (payload: OnRouteHookPayload<TServerContext>) => void;
|
|
132
|
+
export type RouteHandler<TServerContext = {}, TTypedRequest extends TypedRequest = TypedRequest, TTypedResponse extends TypedResponse = TypedResponse> = (
|
|
133
|
+
/**
|
|
134
|
+
* The request object represents the incoming HTTP request.
|
|
135
|
+
* This object implements [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface.
|
|
136
|
+
*/
|
|
137
|
+
request: TTypedRequest, context: TServerContext) => PromiseOrValue<TTypedResponse | void>;
|
|
138
|
+
export type OnRouteHookPayload<TServerContext> = {
|
|
139
|
+
operationId?: string;
|
|
140
|
+
description?: string;
|
|
141
|
+
tags?: string[];
|
|
142
|
+
method: HTTPMethod;
|
|
143
|
+
path: string;
|
|
144
|
+
schemas?: RouteSchemas | RouteZodSchemas;
|
|
145
|
+
openAPIDocument: OpenAPIDocument;
|
|
146
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
147
|
+
};
|
|
148
|
+
export type OnRouterInitHook<TServerContext> = (router: Router<TServerContext, any, any>) => void;
|
|
149
|
+
export type OnSerializeResponsePayload<TServerContext> = {
|
|
150
|
+
request: TypedRequest;
|
|
151
|
+
path: string;
|
|
152
|
+
serverContext: TServerContext;
|
|
153
|
+
lazyResponse: LazySerializedResponse;
|
|
154
|
+
};
|
|
155
|
+
export type OnSerializeResponseHook<TServerContext> = (payload: OnSerializeResponsePayload<TServerContext>) => void;
|
|
156
|
+
export type RouterPlugin<TServerContext> = ServerAdapterPlugin<TServerContext> & {
|
|
157
|
+
onRouterInit?: OnRouterInitHook<TServerContext>;
|
|
158
|
+
onRoute?: OnRouteHook<TServerContext>;
|
|
159
|
+
onSerializeResponse?: OnSerializeResponseHook<TServerContext>;
|
|
160
|
+
};
|
|
161
|
+
export type RouteSchemas = {
|
|
162
|
+
request?: {
|
|
163
|
+
headers?: JSONSchema;
|
|
164
|
+
params?: JSONSchema;
|
|
165
|
+
query?: JSONSchema;
|
|
166
|
+
json?: JSONSchema;
|
|
167
|
+
formData?: JSONSchema;
|
|
168
|
+
};
|
|
169
|
+
responses?: StatusCodeMap<JSONSchema>;
|
|
170
|
+
};
|
|
171
|
+
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> ? {
|
|
172
|
+
json?: TJSONBody;
|
|
173
|
+
formData?: TFormData;
|
|
174
|
+
headers?: THeaders;
|
|
175
|
+
query?: TQueryParams;
|
|
176
|
+
params?: TPathParam;
|
|
177
|
+
} : never;
|
|
178
|
+
export type RouterSDK<TPath extends string = string, TTypedRequest extends TypedRequest = TypedRequest, TTypedResponse extends TypedResponse = TypedResponse> = {
|
|
179
|
+
[TPathKey in TPath]: {
|
|
180
|
+
[TMethod in Lowercase<TTypedRequest['method']>]: (opts?: RouterSDKOpts<TTypedRequest, TTypedRequest['method']>) => Promise<Exclude<TTypedResponse, undefined>>;
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
export type FromSchemaWithComponents<TComponents, TSchema extends JSONSchema> = TComponents extends {
|
|
184
|
+
schemas: Record<string, JSONSchema>;
|
|
185
|
+
} ? FromSchema<{
|
|
186
|
+
components: TComponents;
|
|
187
|
+
} & TSchema> : FromSchema<TSchema>;
|
|
188
|
+
export type TypedRequestFromRouteSchemas<TComponents extends RouterComponentsBase, TRouteSchemas extends RouteSchemas, TMethod extends HTTPMethod, TPath extends string> = TRouteSchemas extends {
|
|
189
|
+
request: Required<RouteSchemas>['request'];
|
|
190
|
+
} ? TypedRequest<TRouteSchemas['request'] extends {
|
|
191
|
+
json: JSONSchema;
|
|
192
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['json']> : any, TRouteSchemas['request'] extends {
|
|
193
|
+
formData: JSONSchema;
|
|
194
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['formData']> extends Record<string, FormDataEntryValue> ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['formData']> : Record<string, FormDataEntryValue> : Record<string, FormDataEntryValue>, TRouteSchemas['request'] extends {
|
|
195
|
+
headers: JSONSchema;
|
|
196
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']> extends Record<string, string> ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']> : Record<string, string> : Record<string, string>, TMethod, TRouteSchemas['request'] extends {
|
|
197
|
+
query: JSONSchema;
|
|
198
|
+
} ? 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 {
|
|
199
|
+
params: JSONSchema;
|
|
200
|
+
} ? 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>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>;
|
|
201
|
+
export type TypedResponseFromRouteSchemas<TComponents extends RouterComponentsBase, TRouteSchemas extends RouteSchemas> = TRouteSchemas extends {
|
|
202
|
+
responses: StatusCodeMap<JSONSchema>;
|
|
203
|
+
} ? TypedResponseWithJSONStatusMap<{
|
|
204
|
+
[TStatusCode in keyof TRouteSchemas['responses']]: TRouteSchemas['responses'][TStatusCode] extends JSONSchema ? FromSchemaWithComponents<TComponents, TRouteSchemas['responses'][TStatusCode]> : never;
|
|
205
|
+
}> : TypedResponse;
|
|
206
|
+
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>> = {
|
|
207
|
+
schemas: TRouteSchemas;
|
|
208
|
+
} & AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>;
|
|
209
|
+
export type AddRouteWithTypesOpts<TServerContext, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequest<any, Record<string, FormDataEntryValue>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>, TTypedResponse extends TypedResponse> = {
|
|
210
|
+
operationId?: string;
|
|
211
|
+
description?: string;
|
|
212
|
+
method?: TMethod;
|
|
213
|
+
tags?: string[];
|
|
214
|
+
internal?: boolean;
|
|
215
|
+
path: TPath;
|
|
216
|
+
handler: RouteHandler<TServerContext, TTypedRequest, TTypedResponse> | RouteHandler<TServerContext, TTypedRequest, TTypedResponse>[];
|
|
217
|
+
};
|
|
218
|
+
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 {
|
|
219
|
+
[TParamTypeKey in TParamType]?: infer TParamTypeValue;
|
|
220
|
+
} ? TParamTypeValue : never : never : never;
|
|
221
|
+
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;
|
|
222
|
+
export type RouterClient<TRouter extends Router<any, any, any>> = TRouter['__client'];
|
|
223
|
+
export type RouterInput<TRouter extends Router<any, any, any>> = {
|
|
224
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
225
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (requestParams?: infer TRequestParams) => any ? Required<TRequestParams> : never;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
export type RouterJsonPostInput<TRouter extends Router<any, any, any>> = {
|
|
229
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
230
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterInput<TRouter>[TPath][TMethod] extends {
|
|
231
|
+
json: infer TJSON;
|
|
232
|
+
} ? TJSON : never;
|
|
233
|
+
}['post'];
|
|
234
|
+
};
|
|
235
|
+
export type RouterJsonPostSuccessOutput<TRouter extends Router<any, any, any>> = {
|
|
236
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
237
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterOutput<TRouter>[TPath][TMethod][200];
|
|
238
|
+
}['post'];
|
|
239
|
+
};
|
|
240
|
+
export type RouterOutput<TRouter extends Router<any, any, any>> = {
|
|
241
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
242
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (requestParams?: any) => Promise<infer TTypedResponse> ? {
|
|
243
|
+
[TStatusCode in StatusCode]: TTypedResponse extends TypedResponse<infer TJSONBody, any, TStatusCode> ? TJSONBody : never;
|
|
244
|
+
} : never;
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
export type RouterComponentSchema<TRouter extends Router<any, any, any>, TName extends string> = TRouter extends Router<any, infer TComponents, any> ? TComponents extends {
|
|
248
|
+
schemas: Record<string, JSONSchema>;
|
|
249
|
+
} ? FromSchema<TComponents['schemas'][TName]> : never : never;
|
|
250
|
+
export type ExtractPathParamsWithBrackets<TPath extends string> = Pipe<TPath, [
|
|
251
|
+
Strings.Split<'/' | ';'>,
|
|
252
|
+
Tuples.Filter<Strings.StartsWith<'{'>>,
|
|
253
|
+
Tuples.Map<Strings.Trim<'{' | '}'>>,
|
|
254
|
+
Tuples.ToUnion
|
|
255
|
+
]>;
|
|
256
|
+
export type ExtractPathParamsWithPattern<TPath extends string> = Pipe<TPath, [
|
|
257
|
+
Strings.Split<'/'>,
|
|
258
|
+
Tuples.Filter<Strings.StartsWith<':'>>,
|
|
259
|
+
Tuples.Map<Strings.Trim<':'>>,
|
|
260
|
+
Tuples.ToUnion
|
|
261
|
+
]>;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type { Pipe, Strings, Tuples } from 'hotscript';
|
|
2
|
+
import type { FromSchema as FromSchemaOriginal, JSONSchema as JSONSchemaOrBoolean } from 'json-schema-to-ts';
|
|
3
|
+
import type { ServerAdapter, ServerAdapterOptions, ServerAdapterPlugin, ServerAdapterRequestHandler } from '@whatwg-node/server';
|
|
4
|
+
import type { SwaggerUIOpts } from './plugins/openapi.js';
|
|
5
|
+
import type { LazySerializedResponse } from './Response.js';
|
|
6
|
+
import type { HTTPMethod, StatusCode, TypedRequest, TypedResponse, TypedResponseWithJSONStatusMap } from './typed-fetch.js';
|
|
7
|
+
import type { AddRouteWithZodSchemasOpts, RouteZodSchemas, TypedRequestFromRouteZodSchemas, TypedResponseFromRouteZodSchemas } from './zod/types.js';
|
|
8
|
+
export { TypedRequest as RouterRequest };
|
|
9
|
+
export type Simplify<T> = {
|
|
10
|
+
[KeyType in keyof T]: Simplify<T[KeyType]>;
|
|
11
|
+
} & {};
|
|
12
|
+
export type JSONSerializer = (obj: any) => string;
|
|
13
|
+
export type JSONSchema = Exclude<JSONSchemaOrBoolean, boolean>;
|
|
14
|
+
export interface OpenAPIInfo {
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
version?: string;
|
|
18
|
+
license?: {
|
|
19
|
+
name?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export type OpenAPIPathObject = Record<string, OpenAPIOperationObject> & {
|
|
24
|
+
parameters?: OpenAPIParameterObject[];
|
|
25
|
+
};
|
|
26
|
+
export interface OpenAPIParameterObject {
|
|
27
|
+
name: string;
|
|
28
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
29
|
+
required?: boolean;
|
|
30
|
+
schema?: any;
|
|
31
|
+
}
|
|
32
|
+
export interface OpenAPIRequestBodyObject {
|
|
33
|
+
content?: Record<string, OpenAPIMediaTypeObject>;
|
|
34
|
+
}
|
|
35
|
+
export interface OpenAPIOperationObject {
|
|
36
|
+
operationId?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
tags?: string[];
|
|
39
|
+
parameters?: OpenAPIParameterObject[];
|
|
40
|
+
requestBody?: OpenAPIRequestBodyObject;
|
|
41
|
+
responses?: Record<string | number, OpenAPIResponseObject>;
|
|
42
|
+
}
|
|
43
|
+
export interface OpenAPIResponseObject {
|
|
44
|
+
description?: string;
|
|
45
|
+
content?: Record<string, OpenAPIMediaTypeObject>;
|
|
46
|
+
}
|
|
47
|
+
export interface OpenAPIMediaTypeObject {
|
|
48
|
+
schema?: any;
|
|
49
|
+
}
|
|
50
|
+
export type OpenAPIDocument = {
|
|
51
|
+
openapi?: string;
|
|
52
|
+
info?: OpenAPIInfo;
|
|
53
|
+
servers?: {
|
|
54
|
+
url: string;
|
|
55
|
+
}[] | string[];
|
|
56
|
+
paths?: Record<string, OpenAPIPathObject>;
|
|
57
|
+
components?: unknown;
|
|
58
|
+
};
|
|
59
|
+
export interface RouterOpenAPIOptions<TComponents extends RouterComponentsBase> extends OpenAPIDocument {
|
|
60
|
+
endpoint?: string | false;
|
|
61
|
+
components?: TComponents;
|
|
62
|
+
}
|
|
63
|
+
export interface RouterSwaggerUIOptions extends SwaggerUIOpts {
|
|
64
|
+
endpoint?: string | false;
|
|
65
|
+
}
|
|
66
|
+
export interface RouterOptions<TServerContext, TComponents extends RouterComponentsBase> extends ServerAdapterOptions<TServerContext> {
|
|
67
|
+
base?: string;
|
|
68
|
+
plugins?: RouterPlugin<TServerContext>[];
|
|
69
|
+
openAPI?: RouterOpenAPIOptions<TComponents>;
|
|
70
|
+
swaggerUI?: RouterSwaggerUIOptions;
|
|
71
|
+
}
|
|
72
|
+
export type RouterComponentsBase = {
|
|
73
|
+
schemas?: Record<string, JSONSchema>;
|
|
74
|
+
};
|
|
75
|
+
export type FromSchema<T> = T extends JSONSchema ? FromSchemaOriginal<T, {
|
|
76
|
+
deserialize: T extends T['properties'][keyof T['properties']] ? false : [
|
|
77
|
+
{
|
|
78
|
+
pattern: {
|
|
79
|
+
type: 'string';
|
|
80
|
+
format: 'binary';
|
|
81
|
+
};
|
|
82
|
+
output: File;
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
pattern: {
|
|
86
|
+
type: 'number';
|
|
87
|
+
format: 'int64';
|
|
88
|
+
};
|
|
89
|
+
output: bigint | number;
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
pattern: {
|
|
93
|
+
type: 'integer';
|
|
94
|
+
format: 'int64';
|
|
95
|
+
};
|
|
96
|
+
output: bigint | number;
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
}> : never;
|
|
100
|
+
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;
|
|
101
|
+
export type PromiseOrValue<T> = T | Promise<T>;
|
|
102
|
+
export type StatusCodeMap<T> = {
|
|
103
|
+
[TKey in StatusCode]?: T;
|
|
104
|
+
};
|
|
105
|
+
export type TypedRouterHandlerTypeConfig<TPath extends string, TRequestJSON = any, TRequestFormData extends Record<string, FormDataEntryValue> = Record<string, FormDataEntryValue>, 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>> = {
|
|
106
|
+
request: {
|
|
107
|
+
json?: TRequestJSON;
|
|
108
|
+
formData?: TRequestFormData;
|
|
109
|
+
headers?: TRequestHeaders;
|
|
110
|
+
query?: TRequestQueryParams;
|
|
111
|
+
params?: TRequestPathParams;
|
|
112
|
+
};
|
|
113
|
+
responses?: TResponseJSONStatusMap;
|
|
114
|
+
};
|
|
115
|
+
export type TypedRequestFromTypeConfig<TMethod extends HTTPMethod, TPath extends string, TTypeConfig extends TypedRouterHandlerTypeConfig<TPath>> = TTypeConfig extends {
|
|
116
|
+
request: Required<TypedRouterHandlerTypeConfig<TPath>>['request'];
|
|
117
|
+
} ? 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>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>;
|
|
118
|
+
export type TypedResponseFromTypeConfig<TTypeConfig extends TypedRouterHandlerTypeConfig<string>> = TTypeConfig extends {
|
|
119
|
+
responses: infer TResponses;
|
|
120
|
+
} ? TResponses extends StatusCodeMap<any> ? TypedResponseWithJSONStatusMap<TResponses> : never : TypedResponse;
|
|
121
|
+
export interface RouterBaseObject<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> {
|
|
122
|
+
openAPIDocument: OpenAPIDocument;
|
|
123
|
+
handle: ServerAdapterRequestHandler<TServerContext>;
|
|
124
|
+
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>>;
|
|
125
|
+
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>>;
|
|
126
|
+
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>>;
|
|
127
|
+
__client: TRouterSDK;
|
|
128
|
+
__onRouterInitHooks: OnRouterInitHook<TServerContext>[];
|
|
129
|
+
}
|
|
130
|
+
export type Router<TServerContext, TComponents extends RouterComponentsBase, TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse>> = ServerAdapter<TServerContext, RouterBaseObject<TServerContext, TComponents, TRouterSDK>>;
|
|
131
|
+
export type OnRouteHook<TServerContext> = (payload: OnRouteHookPayload<TServerContext>) => void;
|
|
132
|
+
export type RouteHandler<TServerContext = {}, TTypedRequest extends TypedRequest = TypedRequest, TTypedResponse extends TypedResponse = TypedResponse> = (
|
|
133
|
+
/**
|
|
134
|
+
* The request object represents the incoming HTTP request.
|
|
135
|
+
* This object implements [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) interface.
|
|
136
|
+
*/
|
|
137
|
+
request: TTypedRequest, context: TServerContext) => PromiseOrValue<TTypedResponse | void>;
|
|
138
|
+
export type OnRouteHookPayload<TServerContext> = {
|
|
139
|
+
operationId?: string;
|
|
140
|
+
description?: string;
|
|
141
|
+
tags?: string[];
|
|
142
|
+
method: HTTPMethod;
|
|
143
|
+
path: string;
|
|
144
|
+
schemas?: RouteSchemas | RouteZodSchemas;
|
|
145
|
+
openAPIDocument: OpenAPIDocument;
|
|
146
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
147
|
+
};
|
|
148
|
+
export type OnRouterInitHook<TServerContext> = (router: Router<TServerContext, any, any>) => void;
|
|
149
|
+
export type OnSerializeResponsePayload<TServerContext> = {
|
|
150
|
+
request: TypedRequest;
|
|
151
|
+
path: string;
|
|
152
|
+
serverContext: TServerContext;
|
|
153
|
+
lazyResponse: LazySerializedResponse;
|
|
154
|
+
};
|
|
155
|
+
export type OnSerializeResponseHook<TServerContext> = (payload: OnSerializeResponsePayload<TServerContext>) => void;
|
|
156
|
+
export type RouterPlugin<TServerContext> = ServerAdapterPlugin<TServerContext> & {
|
|
157
|
+
onRouterInit?: OnRouterInitHook<TServerContext>;
|
|
158
|
+
onRoute?: OnRouteHook<TServerContext>;
|
|
159
|
+
onSerializeResponse?: OnSerializeResponseHook<TServerContext>;
|
|
160
|
+
};
|
|
161
|
+
export type RouteSchemas = {
|
|
162
|
+
request?: {
|
|
163
|
+
headers?: JSONSchema;
|
|
164
|
+
params?: JSONSchema;
|
|
165
|
+
query?: JSONSchema;
|
|
166
|
+
json?: JSONSchema;
|
|
167
|
+
formData?: JSONSchema;
|
|
168
|
+
};
|
|
169
|
+
responses?: StatusCodeMap<JSONSchema>;
|
|
170
|
+
};
|
|
171
|
+
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> ? {
|
|
172
|
+
json?: TJSONBody;
|
|
173
|
+
formData?: TFormData;
|
|
174
|
+
headers?: THeaders;
|
|
175
|
+
query?: TQueryParams;
|
|
176
|
+
params?: TPathParam;
|
|
177
|
+
} : never;
|
|
178
|
+
export type RouterSDK<TPath extends string = string, TTypedRequest extends TypedRequest = TypedRequest, TTypedResponse extends TypedResponse = TypedResponse> = {
|
|
179
|
+
[TPathKey in TPath]: {
|
|
180
|
+
[TMethod in Lowercase<TTypedRequest['method']>]: (opts?: RouterSDKOpts<TTypedRequest, TTypedRequest['method']>) => Promise<Exclude<TTypedResponse, undefined>>;
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
export type FromSchemaWithComponents<TComponents, TSchema extends JSONSchema> = TComponents extends {
|
|
184
|
+
schemas: Record<string, JSONSchema>;
|
|
185
|
+
} ? FromSchema<{
|
|
186
|
+
components: TComponents;
|
|
187
|
+
} & TSchema> : FromSchema<TSchema>;
|
|
188
|
+
export type TypedRequestFromRouteSchemas<TComponents extends RouterComponentsBase, TRouteSchemas extends RouteSchemas, TMethod extends HTTPMethod, TPath extends string> = TRouteSchemas extends {
|
|
189
|
+
request: Required<RouteSchemas>['request'];
|
|
190
|
+
} ? TypedRequest<TRouteSchemas['request'] extends {
|
|
191
|
+
json: JSONSchema;
|
|
192
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['json']> : any, TRouteSchemas['request'] extends {
|
|
193
|
+
formData: JSONSchema;
|
|
194
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['formData']> extends Record<string, FormDataEntryValue> ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['formData']> : Record<string, FormDataEntryValue> : Record<string, FormDataEntryValue>, TRouteSchemas['request'] extends {
|
|
195
|
+
headers: JSONSchema;
|
|
196
|
+
} ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']> extends Record<string, string> ? FromSchemaWithComponents<TComponents, TRouteSchemas['request']['headers']> : Record<string, string> : Record<string, string>, TMethod, TRouteSchemas['request'] extends {
|
|
197
|
+
query: JSONSchema;
|
|
198
|
+
} ? 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 {
|
|
199
|
+
params: JSONSchema;
|
|
200
|
+
} ? 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>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>;
|
|
201
|
+
export type TypedResponseFromRouteSchemas<TComponents extends RouterComponentsBase, TRouteSchemas extends RouteSchemas> = TRouteSchemas extends {
|
|
202
|
+
responses: StatusCodeMap<JSONSchema>;
|
|
203
|
+
} ? TypedResponseWithJSONStatusMap<{
|
|
204
|
+
[TStatusCode in keyof TRouteSchemas['responses']]: TRouteSchemas['responses'][TStatusCode] extends JSONSchema ? FromSchemaWithComponents<TComponents, TRouteSchemas['responses'][TStatusCode]> : never;
|
|
205
|
+
}> : TypedResponse;
|
|
206
|
+
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>> = {
|
|
207
|
+
schemas: TRouteSchemas;
|
|
208
|
+
} & AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>;
|
|
209
|
+
export type AddRouteWithTypesOpts<TServerContext, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequest<any, Record<string, FormDataEntryValue>, Record<string, string>, TMethod, Record<string, string | string[]>, Record<ExtractPathParamsWithPattern<TPath>, string>>, TTypedResponse extends TypedResponse> = {
|
|
210
|
+
operationId?: string;
|
|
211
|
+
description?: string;
|
|
212
|
+
method?: TMethod;
|
|
213
|
+
tags?: string[];
|
|
214
|
+
internal?: boolean;
|
|
215
|
+
path: TPath;
|
|
216
|
+
handler: RouteHandler<TServerContext, TTypedRequest, TTypedResponse> | RouteHandler<TServerContext, TTypedRequest, TTypedResponse>[];
|
|
217
|
+
};
|
|
218
|
+
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 {
|
|
219
|
+
[TParamTypeKey in TParamType]?: infer TParamTypeValue;
|
|
220
|
+
} ? TParamTypeValue : never : never : never;
|
|
221
|
+
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;
|
|
222
|
+
export type RouterClient<TRouter extends Router<any, any, any>> = TRouter['__client'];
|
|
223
|
+
export type RouterInput<TRouter extends Router<any, any, any>> = {
|
|
224
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
225
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (requestParams?: infer TRequestParams) => any ? Required<TRequestParams> : never;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
export type RouterJsonPostInput<TRouter extends Router<any, any, any>> = {
|
|
229
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
230
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterInput<TRouter>[TPath][TMethod] extends {
|
|
231
|
+
json: infer TJSON;
|
|
232
|
+
} ? TJSON : never;
|
|
233
|
+
}['post'];
|
|
234
|
+
};
|
|
235
|
+
export type RouterJsonPostSuccessOutput<TRouter extends Router<any, any, any>> = {
|
|
236
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
237
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterOutput<TRouter>[TPath][TMethod][200];
|
|
238
|
+
}['post'];
|
|
239
|
+
};
|
|
240
|
+
export type RouterOutput<TRouter extends Router<any, any, any>> = {
|
|
241
|
+
[TPath in keyof RouterClient<TRouter>]: {
|
|
242
|
+
[TMethod in keyof RouterClient<TRouter>[TPath]]: RouterClient<TRouter>[TPath][TMethod] extends (requestParams?: any) => Promise<infer TTypedResponse> ? {
|
|
243
|
+
[TStatusCode in StatusCode]: TTypedResponse extends TypedResponse<infer TJSONBody, any, TStatusCode> ? TJSONBody : never;
|
|
244
|
+
} : never;
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
export type RouterComponentSchema<TRouter extends Router<any, any, any>, TName extends string> = TRouter extends Router<any, infer TComponents, any> ? TComponents extends {
|
|
248
|
+
schemas: Record<string, JSONSchema>;
|
|
249
|
+
} ? FromSchema<TComponents['schemas'][TName]> : never : never;
|
|
250
|
+
export type ExtractPathParamsWithBrackets<TPath extends string> = Pipe<TPath, [
|
|
251
|
+
Strings.Split<'/' | ';'>,
|
|
252
|
+
Tuples.Filter<Strings.StartsWith<'{'>>,
|
|
253
|
+
Tuples.Map<Strings.Trim<'{' | '}'>>,
|
|
254
|
+
Tuples.ToUnion
|
|
255
|
+
]>;
|
|
256
|
+
export type ExtractPathParamsWithPattern<TPath extends string> = Pipe<TPath, [
|
|
257
|
+
Strings.Split<'/'>,
|
|
258
|
+
Tuples.Filter<Strings.StartsWith<':'>>,
|
|
259
|
+
Tuples.Map<Strings.Trim<':'>>,
|
|
260
|
+
Tuples.ToUnion
|
|
261
|
+
]>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { FetchAPI } from '@whatwg-node/server';
|
|
2
|
+
import { HTTPMethod, TypedRequest, TypedResponse } from './typed-fetch';
|
|
3
|
+
import { OnRouteHook, OpenAPIDocument, RouteHandler, RouteSchemas } from './types';
|
|
4
|
+
export interface PatternHandlersObj<TServerContext> {
|
|
5
|
+
pattern: URLPattern;
|
|
6
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
7
|
+
}
|
|
8
|
+
interface AddHandlerToMethodOpts<TServerContext> {
|
|
9
|
+
operationId?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
tags?: string[];
|
|
12
|
+
method: HTTPMethod;
|
|
13
|
+
path: string;
|
|
14
|
+
schemas?: RouteSchemas;
|
|
15
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
16
|
+
internal?: boolean;
|
|
17
|
+
onRouteHooks: OnRouteHook<TServerContext>[];
|
|
18
|
+
openAPIDocument: OpenAPIDocument;
|
|
19
|
+
basePath: string;
|
|
20
|
+
fetchAPI: FetchAPI;
|
|
21
|
+
handlersByPatternByMethod: Map<HTTPMethod, Map<URLPattern, RouteHandler<TServerContext, TypedRequest, TypedResponse>[]>>;
|
|
22
|
+
internalPatternsByMethod: Map<HTTPMethod, Set<URLPattern>>;
|
|
23
|
+
patternHandlerObjByMethod: Map<HTTPMethod, PatternHandlersObj<TServerContext>[]>;
|
|
24
|
+
}
|
|
25
|
+
declare global {
|
|
26
|
+
interface URLPattern {
|
|
27
|
+
isPattern?: boolean;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export declare function addHandlersToMethod<TServerContext>({ operationId, description, tags, method, path, schemas, handlers, internal, onRouteHooks, openAPIDocument, basePath, fetchAPI, handlersByPatternByMethod, internalPatternsByMethod, patternHandlerObjByMethod, }: AddHandlerToMethodOpts<TServerContext>): void;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { FetchAPI } from '@whatwg-node/server';
|
|
2
|
+
import { HTTPMethod, TypedRequest, TypedResponse } from './typed-fetch';
|
|
3
|
+
import { OnRouteHook, OpenAPIDocument, RouteHandler, RouteSchemas } from './types';
|
|
4
|
+
export interface PatternHandlersObj<TServerContext> {
|
|
5
|
+
pattern: URLPattern;
|
|
6
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
7
|
+
}
|
|
8
|
+
interface AddHandlerToMethodOpts<TServerContext> {
|
|
9
|
+
operationId?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
tags?: string[];
|
|
12
|
+
method: HTTPMethod;
|
|
13
|
+
path: string;
|
|
14
|
+
schemas?: RouteSchemas;
|
|
15
|
+
handlers: RouteHandler<TServerContext, TypedRequest, TypedResponse>[];
|
|
16
|
+
internal?: boolean;
|
|
17
|
+
onRouteHooks: OnRouteHook<TServerContext>[];
|
|
18
|
+
openAPIDocument: OpenAPIDocument;
|
|
19
|
+
basePath: string;
|
|
20
|
+
fetchAPI: FetchAPI;
|
|
21
|
+
handlersByPatternByMethod: Map<HTTPMethod, Map<URLPattern, RouteHandler<TServerContext, TypedRequest, TypedResponse>[]>>;
|
|
22
|
+
internalPatternsByMethod: Map<HTTPMethod, Set<URLPattern>>;
|
|
23
|
+
patternHandlerObjByMethod: Map<HTTPMethod, PatternHandlersObj<TServerContext>[]>;
|
|
24
|
+
}
|
|
25
|
+
declare global {
|
|
26
|
+
interface URLPattern {
|
|
27
|
+
isPattern?: boolean;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export declare function addHandlersToMethod<TServerContext>({ operationId, description, tags, method, path, schemas, handlers, internal, onRouteHooks, openAPIDocument, basePath, fetchAPI, handlersByPatternByMethod, internalPatternsByMethod, patternHandlerObjByMethod, }: AddHandlerToMethodOpts<TServerContext>): void;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { HTTPMethod, TypedRequest, TypedResponse, TypedResponseWithJSONStatusMap } from '../typed-fetch';
|
|
2
|
+
import { AddRouteWithTypesOpts, StatusCodeMap } from '../types';
|
|
3
|
+
export type ZodType = {
|
|
4
|
+
_output: any;
|
|
5
|
+
safeParse(input: any): any;
|
|
6
|
+
};
|
|
7
|
+
export type InferZodType<T extends ZodType> = T['_output'];
|
|
8
|
+
export type RouteZodSchemas = {
|
|
9
|
+
request?: {
|
|
10
|
+
json?: ZodType;
|
|
11
|
+
formData?: ZodType;
|
|
12
|
+
headers?: ZodType;
|
|
13
|
+
params?: ZodType;
|
|
14
|
+
query?: ZodType;
|
|
15
|
+
};
|
|
16
|
+
responses?: StatusCodeMap<ZodType>;
|
|
17
|
+
};
|
|
18
|
+
export type TypedRequestFromRouteZodSchemas<TRouteZodSchemas extends RouteZodSchemas, TMethod extends HTTPMethod> = TRouteZodSchemas extends {
|
|
19
|
+
request: Required<RouteZodSchemas>['request'];
|
|
20
|
+
} ? TypedRequest<TRouteZodSchemas['request'] extends {
|
|
21
|
+
json: ZodType;
|
|
22
|
+
} ? InferZodType<TRouteZodSchemas['request']['json']> : any, TRouteZodSchemas['request'] extends {
|
|
23
|
+
formData: ZodType;
|
|
24
|
+
} ? InferZodType<TRouteZodSchemas['request']['formData']> : Record<string, FormDataEntryValue>, TRouteZodSchemas['request'] extends {
|
|
25
|
+
headers: ZodType;
|
|
26
|
+
} ? InferZodType<TRouteZodSchemas['request']['headers']> : Record<string, string>, TMethod, TRouteZodSchemas['request'] extends {
|
|
27
|
+
query: ZodType;
|
|
28
|
+
} ? InferZodType<TRouteZodSchemas['request']['query']> : Record<string, string | string[]>, TRouteZodSchemas['request'] extends {
|
|
29
|
+
params: ZodType;
|
|
30
|
+
} ? InferZodType<TRouteZodSchemas['request']['params']> : Record<string, any>> : TypedRequest<any, Record<string, FormDataEntryValue>, Record<string, string>, TMethod>;
|
|
31
|
+
export type TypedResponseFromRouteZodSchemas<TRouteZodSchemas extends RouteZodSchemas> = TRouteZodSchemas extends {
|
|
32
|
+
responses: StatusCodeMap<ZodType>;
|
|
33
|
+
} ? TypedResponseWithJSONStatusMap<{
|
|
34
|
+
[TStatusCode in keyof TRouteZodSchemas['responses']]: TRouteZodSchemas['responses'][TStatusCode] extends ZodType ? InferZodType<TRouteZodSchemas['responses'][TStatusCode]> : never;
|
|
35
|
+
}> : TypedResponse;
|
|
36
|
+
export type AddRouteWithZodSchemasOpts<TServerContext, TRouteZodSchemas extends RouteZodSchemas, TMethod extends HTTPMethod, TPath extends string, TTypedRequest extends TypedRequestFromRouteZodSchemas<TRouteZodSchemas, TMethod>, TTypedResponse extends TypedResponseFromRouteZodSchemas<TRouteZodSchemas>> = {
|
|
37
|
+
schemas: TRouteZodSchemas;
|
|
38
|
+
} & AddRouteWithTypesOpts<TServerContext, TMethod, TPath, TTypedRequest, TTypedResponse>;
|
|
39
|
+
export declare function isZodSchema(value: any): value is ZodType;
|