better-call 1.1.6 → 1.1.8
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 +35 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/adapters/node/request.cjs +125 -0
- package/dist/adapters/node/request.cjs.map +1 -0
- package/dist/{node.d.ts → adapters/node/request.d.cts} +2 -6
- package/dist/adapters/node/request.d.mts +16 -0
- package/dist/{node.js → adapters/node/request.mjs} +2 -13
- package/dist/adapters/node/request.mjs.map +1 -0
- package/dist/client.cjs +3 -3
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +13 -15
- package/dist/client.d.mts +53 -0
- package/dist/{client.js → client.mjs} +3 -3
- package/dist/client.mjs.map +1 -0
- package/dist/context.cjs +103 -0
- package/dist/context.cjs.map +1 -0
- package/dist/context.d.cts +340 -0
- package/dist/context.d.mts +340 -0
- package/dist/context.mjs +103 -0
- package/dist/context.mjs.map +1 -0
- package/dist/cookies.cjs +87 -0
- package/dist/cookies.cjs.map +1 -0
- package/dist/cookies.d.cts +103 -0
- package/dist/cookies.d.mts +103 -0
- package/dist/cookies.mjs +84 -0
- package/dist/cookies.mjs.map +1 -0
- package/dist/crypto.cjs +39 -0
- package/dist/crypto.cjs.map +1 -0
- package/dist/crypto.mjs +36 -0
- package/dist/crypto.mjs.map +1 -0
- package/dist/endpoint.cjs +70 -0
- package/dist/endpoint.cjs.map +1 -0
- package/dist/endpoint.d.cts +428 -0
- package/dist/endpoint.d.mts +428 -0
- package/dist/endpoint.mjs +70 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/error.cjs +140 -7
- package/dist/error.cjs.map +1 -0
- package/dist/error.d.cts +103 -2
- package/dist/{error2.d.ts → error.d.mts} +5 -59
- package/dist/{error2.js → error.mjs} +2 -2
- package/dist/{error2.js.map → error.mjs.map} +1 -1
- package/dist/helper.d.cts +12 -0
- package/dist/helper.d.mts +12 -0
- package/dist/index.cjs +19 -829
- package/dist/index.d.cts +11 -15
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +10 -0
- package/dist/middleware.cjs +39 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +123 -0
- package/dist/middleware.d.mts +123 -0
- package/dist/middleware.mjs +39 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/node.cjs +4 -151
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +2 -13
- package/dist/node.d.mts +9 -0
- package/dist/node.mjs +15 -0
- package/dist/node.mjs.map +1 -0
- package/dist/openapi.cjs +191 -0
- package/dist/openapi.cjs.map +1 -0
- package/dist/openapi.d.cts +113 -0
- package/dist/openapi.d.mts +113 -0
- package/dist/openapi.mjs +189 -0
- package/dist/openapi.mjs.map +1 -0
- package/dist/router.cjs +117 -0
- package/dist/router.cjs.map +1 -0
- package/dist/router.d.cts +3 -1088
- package/dist/router.d.mts +97 -0
- package/dist/router.mjs +116 -0
- package/dist/router.mjs.map +1 -0
- package/dist/standard-schema.d.cts +59 -0
- package/dist/standard-schema.d.mts +59 -0
- package/dist/to-response.cjs +96 -0
- package/dist/to-response.cjs.map +1 -0
- package/dist/to-response.d.cts +12 -0
- package/dist/to-response.d.mts +12 -0
- package/dist/to-response.mjs +96 -0
- package/dist/to-response.mjs.map +1 -0
- package/dist/utils.cjs +86 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.mjs +82 -0
- package/dist/utils.mjs.map +1 -0
- package/dist/validator.cjs +58 -0
- package/dist/validator.cjs.map +1 -0
- package/dist/validator.mjs +57 -0
- package/dist/validator.mjs.map +1 -0
- package/package.json +15 -15
- package/dist/client.d.ts +0 -55
- package/dist/client.js.map +0 -1
- package/dist/error.d.ts +0 -2
- package/dist/error.js +0 -3
- package/dist/error2.cjs +0 -171
- package/dist/error2.cjs.map +0 -1
- package/dist/error2.d.cts +0 -157
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -15
- package/dist/index.js +0 -819
- package/dist/index.js.map +0 -1
- package/dist/node.js.map +0 -1
- package/dist/router.d.ts +0 -1182
package/dist/router.d.ts
DELETED
|
@@ -1,1182 +0,0 @@
|
|
|
1
|
-
import { c as StandardSchemaV1, r as Status, s as statusCodes, t as APIError } from "./error2.js";
|
|
2
|
-
|
|
3
|
-
//#region src/helper.d.ts
|
|
4
|
-
type RequiredKeysOf<BaseType extends object> = Exclude<{ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never }[keyof BaseType], undefined>;
|
|
5
|
-
type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
|
|
6
|
-
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
7
|
-
type IsEmptyObject<T> = keyof T extends never ? true : false;
|
|
8
|
-
type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
|
|
9
|
-
type MergeObject<T extends Record<string, any> | never, S extends Record<string, any> | never> = T extends never ? S : S extends never ? T : T & S;
|
|
10
|
-
type InferParamPath<Path> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? { [K in Param | keyof InferParamPath<Rest>]: string } : Path extends `${infer _Start}:${infer Param}` ? { [K in Param]: string } : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : {};
|
|
11
|
-
type InferParamWildCard<Path> = Path extends `${infer _Start}/*:${infer Param}/${infer Rest}` | `${infer _Start}/**:${infer Param}/${infer Rest}` ? { [K in Param | keyof InferParamPath<Rest>]: string } : Path extends `${infer _Start}/*` ? { [K in "_"]: string } : Path extends `${infer _Start}/${infer Rest}` ? InferParamWildCard<Rest> : {};
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/cookies.d.ts
|
|
14
|
-
type CookiePrefixOptions = "host" | "secure";
|
|
15
|
-
type CookieOptions = {
|
|
16
|
-
/**
|
|
17
|
-
* Domain of the cookie
|
|
18
|
-
*
|
|
19
|
-
* The Domain attribute specifies which server can receive a cookie. If specified, cookies are
|
|
20
|
-
* available on the specified server and its subdomains. If the it is not
|
|
21
|
-
* specified, the cookies are available on the server that sets it but not on
|
|
22
|
-
* its subdomains.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* `domain: "example.com"`
|
|
26
|
-
*/
|
|
27
|
-
domain?: string;
|
|
28
|
-
/**
|
|
29
|
-
* A lifetime of a cookie. Permanent cookies are deleted after the date specified in the
|
|
30
|
-
* Expires attribute:
|
|
31
|
-
*
|
|
32
|
-
* Expires has been available for longer than Max-Age, however Max-Age is less error-prone, and
|
|
33
|
-
* takes precedence when both are set. The rationale behind this is that when you set an
|
|
34
|
-
* Expires date and time, they're relative to the client the cookie is being set on. If the
|
|
35
|
-
* server is set to a different time, this could cause errors
|
|
36
|
-
*/
|
|
37
|
-
expires?: Date;
|
|
38
|
-
/**
|
|
39
|
-
* Forbids JavaScript from accessing the cookie, for example, through the Document.cookie
|
|
40
|
-
* property. Note that a cookie that has been created with HttpOnly will still be sent with
|
|
41
|
-
* JavaScript-initiated requests, for example, when calling XMLHttpRequest.send() or fetch().
|
|
42
|
-
* This mitigates attacks against cross-site scripting
|
|
43
|
-
*/
|
|
44
|
-
httpOnly?: boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Indicates the number of seconds until the cookie expires. A zero or negative number will
|
|
47
|
-
* expire the cookie immediately. If both Expires and Max-Age are set, Max-Age has precedence.
|
|
48
|
-
*
|
|
49
|
-
* @example 604800 - 7 days
|
|
50
|
-
*/
|
|
51
|
-
maxAge?: number;
|
|
52
|
-
/**
|
|
53
|
-
* Indicates the path that must exist in the requested URL for the browser to send the Cookie
|
|
54
|
-
* header.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* "/docs"
|
|
58
|
-
* // -> the request paths /docs, /docs/, /docs/Web/, and /docs/Web/HTTP will all match. the request paths /, /fr/docs will not match.
|
|
59
|
-
*/
|
|
60
|
-
path?: string;
|
|
61
|
-
/**
|
|
62
|
-
* Indicates that the cookie is sent to the server only when a request is made with the https:
|
|
63
|
-
* scheme (except on localhost), and therefore, is more resistant to man-in-the-middle attacks.
|
|
64
|
-
*/
|
|
65
|
-
secure?: boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Controls whether or not a cookie is sent with cross-site requests, providing some protection
|
|
68
|
-
* against cross-site request forgery attacks (CSRF).
|
|
69
|
-
*
|
|
70
|
-
* Strict - Means that the browser sends the cookie only for same-site requests, that is,
|
|
71
|
-
* requests originating from the same site that set the cookie. If a request originates from a
|
|
72
|
-
* different domain or scheme (even with the same domain), no cookies with the SameSite=Strict
|
|
73
|
-
* attribute are sent.
|
|
74
|
-
*
|
|
75
|
-
* Lax - Means that the cookie is not sent on cross-site requests, such as on requests to load
|
|
76
|
-
* images or frames, but is sent when a user is navigating to the origin site from an external
|
|
77
|
-
* site (for example, when following a link). This is the default behavior if the SameSite
|
|
78
|
-
* attribute is not specified.
|
|
79
|
-
*
|
|
80
|
-
* None - Means that the browser sends the cookie with both cross-site and same-site requests.
|
|
81
|
-
* The Secure attribute must also be set when setting this value.
|
|
82
|
-
*/
|
|
83
|
-
sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
|
|
84
|
-
/**
|
|
85
|
-
* Indicates that the cookie should be stored using partitioned storage. Note that if this is
|
|
86
|
-
* set, the Secure directive must also be set.
|
|
87
|
-
*
|
|
88
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
|
|
89
|
-
*/
|
|
90
|
-
partitioned?: boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Cooke Prefix
|
|
93
|
-
*
|
|
94
|
-
* - secure: `__Secure-` -> `__Secure-cookie-name`
|
|
95
|
-
* - host: `__Host-` -> `__Host-cookie-name`
|
|
96
|
-
*
|
|
97
|
-
* `secure` must be set to true to use prefixes
|
|
98
|
-
*/
|
|
99
|
-
prefix?: CookiePrefixOptions;
|
|
100
|
-
};
|
|
101
|
-
declare const getCookieKey: (key: string, prefix?: CookiePrefixOptions) => string | undefined;
|
|
102
|
-
/**
|
|
103
|
-
* Parse an HTTP Cookie header string and returning an object of all cookie
|
|
104
|
-
* name-value pairs.
|
|
105
|
-
*
|
|
106
|
-
* Inspired by https://github.com/unjs/cookie-es/blob/main/src/cookie/parse.ts
|
|
107
|
-
*
|
|
108
|
-
* @param str the string representing a `Cookie` header value
|
|
109
|
-
*/
|
|
110
|
-
declare function parseCookies(str: string): Map<string, string>;
|
|
111
|
-
declare const serializeCookie: (key: string, value: string, opt?: CookieOptions) => string;
|
|
112
|
-
declare const serializeSignedCookie: (key: string, value: string, secret: string, opt?: CookieOptions) => Promise<string>;
|
|
113
|
-
//#endregion
|
|
114
|
-
//#region src/context.d.ts
|
|
115
|
-
type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
116
|
-
type Method = HTTPMethod | "*";
|
|
117
|
-
type InferBodyInput<Options extends EndpointOptions | MiddlewareOptions, Body = (Options["metadata"] extends {
|
|
118
|
-
$Infer: {
|
|
119
|
-
body: infer B;
|
|
120
|
-
};
|
|
121
|
-
} ? B : Options["body"] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Options["body"]> : undefined)> = undefined extends Body ? {
|
|
122
|
-
body?: Body;
|
|
123
|
-
} : {
|
|
124
|
-
body: Body;
|
|
125
|
-
};
|
|
126
|
-
type InferBody<Options extends EndpointOptions | MiddlewareOptions> = Options["metadata"] extends {
|
|
127
|
-
$Infer: {
|
|
128
|
-
body: infer Body;
|
|
129
|
-
};
|
|
130
|
-
} ? Body : Options["body"] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Options["body"]> : any;
|
|
131
|
-
type InferQueryInput<Options extends EndpointOptions | MiddlewareOptions, Query = (Options["metadata"] extends {
|
|
132
|
-
$Infer: {
|
|
133
|
-
query: infer Query;
|
|
134
|
-
};
|
|
135
|
-
} ? Query : Options["query"] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<Options["query"]> : Record<string, any> | undefined)> = undefined extends Query ? {
|
|
136
|
-
query?: Query;
|
|
137
|
-
} : {
|
|
138
|
-
query: Query;
|
|
139
|
-
};
|
|
140
|
-
type InferQuery<Options extends EndpointOptions | MiddlewareOptions> = Options["metadata"] extends {
|
|
141
|
-
$Infer: {
|
|
142
|
-
query: infer Query;
|
|
143
|
-
};
|
|
144
|
-
} ? Query : Options["query"] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Options["query"]> : Record<string, any> | undefined;
|
|
145
|
-
type InferMethod<Options extends EndpointOptions> = Options["method"] extends Array<Method> ? Options["method"][number] : Options["method"] extends "*" ? HTTPMethod : Options["method"];
|
|
146
|
-
type InferInputMethod<Options extends EndpointOptions, Method = (Options["method"] extends Array<any> ? Options["method"][number] : Options["method"] extends "*" ? HTTPMethod : Options["method"] | undefined)> = undefined extends Method ? {
|
|
147
|
-
method?: Method;
|
|
148
|
-
} : {
|
|
149
|
-
method: Method;
|
|
150
|
-
};
|
|
151
|
-
type InferParam<Path extends string> = [Path] extends [never] ? Record<string, any> | undefined : IsEmptyObject<InferParamPath<Path> & InferParamWildCard<Path>> extends true ? Record<string, any> | undefined : Prettify<InferParamPath<Path> & InferParamWildCard<Path>>;
|
|
152
|
-
type InferParamInput<Path extends string> = [Path] extends [never] ? {
|
|
153
|
-
params?: Record<string, any>;
|
|
154
|
-
} : IsEmptyObject<InferParamPath<Path> & InferParamWildCard<Path>> extends true ? {
|
|
155
|
-
params?: Record<string, any>;
|
|
156
|
-
} : {
|
|
157
|
-
params: Prettify<InferParamPath<Path> & InferParamWildCard<Path>>;
|
|
158
|
-
};
|
|
159
|
-
type InferRequest<Option extends EndpointOptions | MiddlewareOptions> = Option["requireRequest"] extends true ? Request : Request | undefined;
|
|
160
|
-
type InferRequestInput<Option extends EndpointOptions | MiddlewareOptions> = Option["requireRequest"] extends true ? {
|
|
161
|
-
request: Request;
|
|
162
|
-
} : {
|
|
163
|
-
request?: Request;
|
|
164
|
-
};
|
|
165
|
-
type InferHeaders<Option extends EndpointOptions | MiddlewareOptions> = Option["requireHeaders"] extends true ? Headers : Headers | undefined;
|
|
166
|
-
type InferHeadersInput<Option extends EndpointOptions | MiddlewareOptions> = Option["requireHeaders"] extends true ? {
|
|
167
|
-
headers: HeadersInit;
|
|
168
|
-
} : {
|
|
169
|
-
headers?: HeadersInit;
|
|
170
|
-
};
|
|
171
|
-
type InferUse<Opts extends EndpointOptions["use"]> = Opts extends Middleware[] ? UnionToIntersection<Awaited<ReturnType<Opts[number]>>> : {};
|
|
172
|
-
type InferMiddlewareBody<Options extends MiddlewareOptions> = Options["body"] extends StandardSchemaV1<infer T> ? T : any;
|
|
173
|
-
type InferMiddlewareQuery<Options extends MiddlewareOptions> = Options["query"] extends StandardSchemaV1<infer T> ? T : Record<string, any> | undefined;
|
|
174
|
-
type InputContext<Path extends string, Options extends EndpointOptions> = InferBodyInput<Options> & InferInputMethod<Options> & InferQueryInput<Options> & InferParamInput<Path> & InferRequestInput<Options> & InferHeadersInput<Options> & {
|
|
175
|
-
asResponse?: boolean;
|
|
176
|
-
returnHeaders?: boolean;
|
|
177
|
-
returnStatus?: boolean;
|
|
178
|
-
use?: Middleware[];
|
|
179
|
-
path?: string;
|
|
180
|
-
};
|
|
181
|
-
declare const createInternalContext: (context: InputContext<any, any>, {
|
|
182
|
-
options,
|
|
183
|
-
path
|
|
184
|
-
}: {
|
|
185
|
-
options: EndpointOptions;
|
|
186
|
-
path?: string;
|
|
187
|
-
}) => Promise<{
|
|
188
|
-
body: any;
|
|
189
|
-
query: any;
|
|
190
|
-
path: string;
|
|
191
|
-
context: {};
|
|
192
|
-
returned: any;
|
|
193
|
-
headers: HeadersInit | undefined;
|
|
194
|
-
request: Request | undefined;
|
|
195
|
-
params: Record<string, any> | undefined;
|
|
196
|
-
method: any;
|
|
197
|
-
setHeader: (key: string, value: string) => void;
|
|
198
|
-
getHeader: (key: string) => string | null;
|
|
199
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
200
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | false | null>;
|
|
201
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
202
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
203
|
-
redirect: (url: string) => {
|
|
204
|
-
status: keyof typeof statusCodes | Status;
|
|
205
|
-
body: ({
|
|
206
|
-
message?: string;
|
|
207
|
-
code?: string;
|
|
208
|
-
cause?: unknown;
|
|
209
|
-
} & Record<string, any>) | undefined;
|
|
210
|
-
headers: HeadersInit;
|
|
211
|
-
statusCode: number;
|
|
212
|
-
name: string;
|
|
213
|
-
message: string;
|
|
214
|
-
stack?: string;
|
|
215
|
-
cause?: unknown;
|
|
216
|
-
} & {
|
|
217
|
-
errorStack: string | undefined;
|
|
218
|
-
};
|
|
219
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
220
|
-
message?: string;
|
|
221
|
-
code?: string;
|
|
222
|
-
} | undefined, headers?: HeadersInit) => {
|
|
223
|
-
status: keyof typeof statusCodes | Status;
|
|
224
|
-
body: ({
|
|
225
|
-
message?: string;
|
|
226
|
-
code?: string;
|
|
227
|
-
cause?: unknown;
|
|
228
|
-
} & Record<string, any>) | undefined;
|
|
229
|
-
headers: HeadersInit;
|
|
230
|
-
statusCode: number;
|
|
231
|
-
name: string;
|
|
232
|
-
message: string;
|
|
233
|
-
stack?: string;
|
|
234
|
-
cause?: unknown;
|
|
235
|
-
} & {
|
|
236
|
-
errorStack: string | undefined;
|
|
237
|
-
};
|
|
238
|
-
setStatus: (status: Status) => void;
|
|
239
|
-
json: (json: Record<string, any>, routerResponse?: {
|
|
240
|
-
status?: number;
|
|
241
|
-
headers?: Record<string, string>;
|
|
242
|
-
response?: Response;
|
|
243
|
-
body?: Record<string, any>;
|
|
244
|
-
} | Response) => Record<string, any>;
|
|
245
|
-
responseHeaders: Headers;
|
|
246
|
-
responseStatus: Status | undefined;
|
|
247
|
-
asResponse?: boolean;
|
|
248
|
-
returnHeaders?: boolean;
|
|
249
|
-
returnStatus?: boolean;
|
|
250
|
-
use?: Middleware[];
|
|
251
|
-
} | {
|
|
252
|
-
body: any;
|
|
253
|
-
query: any;
|
|
254
|
-
path: string;
|
|
255
|
-
context: {};
|
|
256
|
-
returned: any;
|
|
257
|
-
headers: HeadersInit | undefined;
|
|
258
|
-
request: Request | undefined;
|
|
259
|
-
params: Record<string, any> | undefined;
|
|
260
|
-
method: any;
|
|
261
|
-
setHeader: (key: string, value: string) => void;
|
|
262
|
-
getHeader: (key: string) => string | null;
|
|
263
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
264
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | false | null>;
|
|
265
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
266
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
267
|
-
redirect: (url: string) => {
|
|
268
|
-
status: keyof typeof statusCodes | Status;
|
|
269
|
-
body: ({
|
|
270
|
-
message?: string;
|
|
271
|
-
code?: string;
|
|
272
|
-
cause?: unknown;
|
|
273
|
-
} & Record<string, any>) | undefined;
|
|
274
|
-
headers: HeadersInit;
|
|
275
|
-
statusCode: number;
|
|
276
|
-
name: string;
|
|
277
|
-
message: string;
|
|
278
|
-
stack?: string;
|
|
279
|
-
cause?: unknown;
|
|
280
|
-
} & {
|
|
281
|
-
errorStack: string | undefined;
|
|
282
|
-
};
|
|
283
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
284
|
-
message?: string;
|
|
285
|
-
code?: string;
|
|
286
|
-
} | undefined, headers?: HeadersInit) => {
|
|
287
|
-
status: keyof typeof statusCodes | Status;
|
|
288
|
-
body: ({
|
|
289
|
-
message?: string;
|
|
290
|
-
code?: string;
|
|
291
|
-
cause?: unknown;
|
|
292
|
-
} & Record<string, any>) | undefined;
|
|
293
|
-
headers: HeadersInit;
|
|
294
|
-
statusCode: number;
|
|
295
|
-
name: string;
|
|
296
|
-
message: string;
|
|
297
|
-
stack?: string;
|
|
298
|
-
cause?: unknown;
|
|
299
|
-
} & {
|
|
300
|
-
errorStack: string | undefined;
|
|
301
|
-
};
|
|
302
|
-
setStatus: (status: Status) => void;
|
|
303
|
-
json: (json: Record<string, any>, routerResponse?: {
|
|
304
|
-
status?: number;
|
|
305
|
-
headers?: Record<string, string>;
|
|
306
|
-
response?: Response;
|
|
307
|
-
body?: Record<string, any>;
|
|
308
|
-
} | Response) => Record<string, any>;
|
|
309
|
-
responseHeaders: Headers;
|
|
310
|
-
responseStatus: Status | undefined;
|
|
311
|
-
asResponse?: boolean;
|
|
312
|
-
returnHeaders?: boolean;
|
|
313
|
-
returnStatus?: boolean;
|
|
314
|
-
use?: Middleware[];
|
|
315
|
-
} | {
|
|
316
|
-
body: any;
|
|
317
|
-
query: any;
|
|
318
|
-
path: string;
|
|
319
|
-
context: {};
|
|
320
|
-
returned: any;
|
|
321
|
-
headers: HeadersInit | undefined;
|
|
322
|
-
request: Request | undefined;
|
|
323
|
-
params: Record<string, any> | undefined;
|
|
324
|
-
method: any;
|
|
325
|
-
setHeader: (key: string, value: string) => void;
|
|
326
|
-
getHeader: (key: string) => string | null;
|
|
327
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
328
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | false | null>;
|
|
329
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
330
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
331
|
-
redirect: (url: string) => {
|
|
332
|
-
status: keyof typeof statusCodes | Status;
|
|
333
|
-
body: ({
|
|
334
|
-
message?: string;
|
|
335
|
-
code?: string;
|
|
336
|
-
cause?: unknown;
|
|
337
|
-
} & Record<string, any>) | undefined;
|
|
338
|
-
headers: HeadersInit;
|
|
339
|
-
statusCode: number;
|
|
340
|
-
name: string;
|
|
341
|
-
message: string;
|
|
342
|
-
stack?: string;
|
|
343
|
-
cause?: unknown;
|
|
344
|
-
} & {
|
|
345
|
-
errorStack: string | undefined;
|
|
346
|
-
};
|
|
347
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
348
|
-
message?: string;
|
|
349
|
-
code?: string;
|
|
350
|
-
} | undefined, headers?: HeadersInit) => {
|
|
351
|
-
status: keyof typeof statusCodes | Status;
|
|
352
|
-
body: ({
|
|
353
|
-
message?: string;
|
|
354
|
-
code?: string;
|
|
355
|
-
cause?: unknown;
|
|
356
|
-
} & Record<string, any>) | undefined;
|
|
357
|
-
headers: HeadersInit;
|
|
358
|
-
statusCode: number;
|
|
359
|
-
name: string;
|
|
360
|
-
message: string;
|
|
361
|
-
stack?: string;
|
|
362
|
-
cause?: unknown;
|
|
363
|
-
} & {
|
|
364
|
-
errorStack: string | undefined;
|
|
365
|
-
};
|
|
366
|
-
setStatus: (status: Status) => void;
|
|
367
|
-
json: (json: Record<string, any>, routerResponse?: {
|
|
368
|
-
status?: number;
|
|
369
|
-
headers?: Record<string, string>;
|
|
370
|
-
response?: Response;
|
|
371
|
-
body?: Record<string, any>;
|
|
372
|
-
} | Response) => Record<string, any>;
|
|
373
|
-
responseHeaders: Headers;
|
|
374
|
-
responseStatus: Status | undefined;
|
|
375
|
-
asResponse?: boolean;
|
|
376
|
-
returnHeaders?: boolean;
|
|
377
|
-
returnStatus?: boolean;
|
|
378
|
-
use?: Middleware[];
|
|
379
|
-
} | {
|
|
380
|
-
body: any;
|
|
381
|
-
query: any;
|
|
382
|
-
path: string;
|
|
383
|
-
context: {};
|
|
384
|
-
returned: any;
|
|
385
|
-
headers: HeadersInit | undefined;
|
|
386
|
-
request: Request | undefined;
|
|
387
|
-
params: Record<string, any> | undefined;
|
|
388
|
-
method: any;
|
|
389
|
-
setHeader: (key: string, value: string) => void;
|
|
390
|
-
getHeader: (key: string) => string | null;
|
|
391
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
392
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | false | null>;
|
|
393
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
394
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
395
|
-
redirect: (url: string) => {
|
|
396
|
-
status: keyof typeof statusCodes | Status;
|
|
397
|
-
body: ({
|
|
398
|
-
message?: string;
|
|
399
|
-
code?: string;
|
|
400
|
-
cause?: unknown;
|
|
401
|
-
} & Record<string, any>) | undefined;
|
|
402
|
-
headers: HeadersInit;
|
|
403
|
-
statusCode: number;
|
|
404
|
-
name: string;
|
|
405
|
-
message: string;
|
|
406
|
-
stack?: string;
|
|
407
|
-
cause?: unknown;
|
|
408
|
-
} & {
|
|
409
|
-
errorStack: string | undefined;
|
|
410
|
-
};
|
|
411
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
412
|
-
message?: string;
|
|
413
|
-
code?: string;
|
|
414
|
-
} | undefined, headers?: HeadersInit) => {
|
|
415
|
-
status: keyof typeof statusCodes | Status;
|
|
416
|
-
body: ({
|
|
417
|
-
message?: string;
|
|
418
|
-
code?: string;
|
|
419
|
-
cause?: unknown;
|
|
420
|
-
} & Record<string, any>) | undefined;
|
|
421
|
-
headers: HeadersInit;
|
|
422
|
-
statusCode: number;
|
|
423
|
-
name: string;
|
|
424
|
-
message: string;
|
|
425
|
-
stack?: string;
|
|
426
|
-
cause?: unknown;
|
|
427
|
-
} & {
|
|
428
|
-
errorStack: string | undefined;
|
|
429
|
-
};
|
|
430
|
-
setStatus: (status: Status) => void;
|
|
431
|
-
json: (json: Record<string, any>, routerResponse?: {
|
|
432
|
-
status?: number;
|
|
433
|
-
headers?: Record<string, string>;
|
|
434
|
-
response?: Response;
|
|
435
|
-
body?: Record<string, any>;
|
|
436
|
-
} | Response) => Record<string, any>;
|
|
437
|
-
responseHeaders: Headers;
|
|
438
|
-
responseStatus: Status | undefined;
|
|
439
|
-
asResponse?: boolean;
|
|
440
|
-
returnHeaders?: boolean;
|
|
441
|
-
returnStatus?: boolean;
|
|
442
|
-
use?: Middleware[];
|
|
443
|
-
}>;
|
|
444
|
-
//#endregion
|
|
445
|
-
//#region src/middleware.d.ts
|
|
446
|
-
interface MiddlewareOptions extends Omit<EndpointOptions, "method"> {}
|
|
447
|
-
type MiddlewareResponse = null | void | undefined | Record<string, any>;
|
|
448
|
-
type MiddlewareContext<Options extends MiddlewareOptions, Context = {}> = EndpointContext<string, Options & {
|
|
449
|
-
method: "*";
|
|
450
|
-
}> & {
|
|
451
|
-
/**
|
|
452
|
-
* Method
|
|
453
|
-
*
|
|
454
|
-
* The request method
|
|
455
|
-
*/
|
|
456
|
-
method: string;
|
|
457
|
-
/**
|
|
458
|
-
* Path
|
|
459
|
-
*
|
|
460
|
-
* The path of the endpoint
|
|
461
|
-
*/
|
|
462
|
-
path: string;
|
|
463
|
-
/**
|
|
464
|
-
* Body
|
|
465
|
-
*
|
|
466
|
-
* The body object will be the parsed JSON from the request and validated
|
|
467
|
-
* against the body schema if it exists
|
|
468
|
-
*/
|
|
469
|
-
body: InferMiddlewareBody<Options>;
|
|
470
|
-
/**
|
|
471
|
-
* Query
|
|
472
|
-
*
|
|
473
|
-
* The query object will be the parsed query string from the request
|
|
474
|
-
* and validated against the query schema if it exists
|
|
475
|
-
*/
|
|
476
|
-
query: InferMiddlewareQuery<Options>;
|
|
477
|
-
/**
|
|
478
|
-
* Params
|
|
479
|
-
*
|
|
480
|
-
* If the path is `/user/:id` and the request is `/user/1` then the
|
|
481
|
-
* params will
|
|
482
|
-
* be `{ id: "1" }` and if the path includes a wildcard like `/user/*`
|
|
483
|
-
* then the
|
|
484
|
-
* params will be `{ _: "1" }` where `_` is the wildcard key. If the
|
|
485
|
-
* wildcard
|
|
486
|
-
* is named like `/user/**:name` then the params will be `{ name: string }`
|
|
487
|
-
*/
|
|
488
|
-
params: string;
|
|
489
|
-
/**
|
|
490
|
-
* Request object
|
|
491
|
-
*
|
|
492
|
-
* If `requireRequest` is set to true in the endpoint options this will be
|
|
493
|
-
* required
|
|
494
|
-
*/
|
|
495
|
-
request: InferRequest<Options>;
|
|
496
|
-
/**
|
|
497
|
-
* Headers
|
|
498
|
-
*
|
|
499
|
-
* If `requireHeaders` is set to true in the endpoint options this will be
|
|
500
|
-
* required
|
|
501
|
-
*/
|
|
502
|
-
headers: InferHeaders<Options>;
|
|
503
|
-
/**
|
|
504
|
-
* Set header
|
|
505
|
-
*
|
|
506
|
-
* If it's called outside of a request it will just be ignored.
|
|
507
|
-
*/
|
|
508
|
-
setHeader: (key: string, value: string) => void;
|
|
509
|
-
/**
|
|
510
|
-
* Get header
|
|
511
|
-
*
|
|
512
|
-
* If it's called outside of a request it will just return null
|
|
513
|
-
*
|
|
514
|
-
* @param key - The key of the header
|
|
515
|
-
* @returns
|
|
516
|
-
*/
|
|
517
|
-
getHeader: (key: string) => string | null;
|
|
518
|
-
/**
|
|
519
|
-
* JSON
|
|
520
|
-
*
|
|
521
|
-
* a helper function to create a JSON response with
|
|
522
|
-
* the correct headers
|
|
523
|
-
* and status code. If `asResponse` is set to true in
|
|
524
|
-
* the context then
|
|
525
|
-
* it will return a Response object instead of the
|
|
526
|
-
* JSON object.
|
|
527
|
-
*
|
|
528
|
-
* @param json - The JSON object to return
|
|
529
|
-
* @param routerResponse - The response object to
|
|
530
|
-
* return if `asResponse` is
|
|
531
|
-
* true in the context this will take precedence
|
|
532
|
-
*/
|
|
533
|
-
json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
|
|
534
|
-
status?: number;
|
|
535
|
-
headers?: Record<string, string>;
|
|
536
|
-
response?: Response;
|
|
537
|
-
} | Response) => Promise<R>;
|
|
538
|
-
/**
|
|
539
|
-
* Middleware context
|
|
540
|
-
*/
|
|
541
|
-
context: Prettify<Context>;
|
|
542
|
-
};
|
|
543
|
-
declare function createMiddleware<Options extends MiddlewareOptions, R>(options: Options, handler: (context: MiddlewareContext<Options>) => Promise<R>): <InputCtx extends MiddlewareInputContext<Options>>(inputContext: InputCtx) => Promise<R>;
|
|
544
|
-
declare function createMiddleware<Options extends MiddlewareOptions, R>(handler: (context: MiddlewareContext<Options>) => Promise<R>): <InputCtx extends MiddlewareInputContext<Options>>(inputContext: InputCtx) => Promise<R>;
|
|
545
|
-
declare namespace createMiddleware {
|
|
546
|
-
var create: <E extends {
|
|
547
|
-
use?: Middleware[];
|
|
548
|
-
}>(opts?: E) => {
|
|
549
|
-
<Options extends MiddlewareOptions, R>(options: Options, handler: (ctx: MiddlewareContext<Options, InferUse<E["use"]>>) => Promise<R>): (inputContext: MiddlewareInputContext<Options>) => Promise<R>;
|
|
550
|
-
<Options extends MiddlewareOptions, R_1>(handler: (ctx: MiddlewareContext<Options, InferUse<E["use"]>>) => Promise<R_1>): (inputContext: MiddlewareInputContext<Options>) => Promise<R_1>;
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
type MiddlewareInputContext<Options extends MiddlewareOptions> = InferBodyInput<Options> & InferQueryInput<Options> & InferRequestInput<Options> & InferHeadersInput<Options> & {
|
|
554
|
-
asResponse?: boolean;
|
|
555
|
-
returnHeaders?: boolean;
|
|
556
|
-
use?: Middleware[];
|
|
557
|
-
};
|
|
558
|
-
type Middleware<Options extends MiddlewareOptions = MiddlewareOptions, Handler extends (inputCtx: any) => Promise<any> = any> = Handler & {
|
|
559
|
-
options: Options;
|
|
560
|
-
};
|
|
561
|
-
//#endregion
|
|
562
|
-
//#region src/openapi.d.ts
|
|
563
|
-
type OpenAPISchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
|
|
564
|
-
interface OpenAPIParameter {
|
|
565
|
-
in: "query" | "path" | "header" | "cookie";
|
|
566
|
-
name?: string;
|
|
567
|
-
description?: string;
|
|
568
|
-
required?: boolean;
|
|
569
|
-
schema?: {
|
|
570
|
-
type: OpenAPISchemaType;
|
|
571
|
-
format?: string;
|
|
572
|
-
items?: {
|
|
573
|
-
type: OpenAPISchemaType;
|
|
574
|
-
};
|
|
575
|
-
enum?: string[];
|
|
576
|
-
minLength?: number;
|
|
577
|
-
description?: string;
|
|
578
|
-
default?: string;
|
|
579
|
-
example?: string;
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
interface Path$1 {
|
|
583
|
-
get?: {
|
|
584
|
-
tags?: string[];
|
|
585
|
-
operationId?: string;
|
|
586
|
-
description?: string;
|
|
587
|
-
security?: [{
|
|
588
|
-
bearerAuth: string[];
|
|
589
|
-
}];
|
|
590
|
-
parameters?: OpenAPIParameter[];
|
|
591
|
-
responses?: { [key in string]: {
|
|
592
|
-
description?: string;
|
|
593
|
-
content: {
|
|
594
|
-
"application/json": {
|
|
595
|
-
schema: {
|
|
596
|
-
type?: OpenAPISchemaType;
|
|
597
|
-
properties?: Record<string, any>;
|
|
598
|
-
required?: string[];
|
|
599
|
-
$ref?: string;
|
|
600
|
-
};
|
|
601
|
-
};
|
|
602
|
-
};
|
|
603
|
-
} };
|
|
604
|
-
};
|
|
605
|
-
post?: {
|
|
606
|
-
tags?: string[];
|
|
607
|
-
operationId?: string;
|
|
608
|
-
description?: string;
|
|
609
|
-
security?: [{
|
|
610
|
-
bearerAuth: string[];
|
|
611
|
-
}];
|
|
612
|
-
parameters?: OpenAPIParameter[];
|
|
613
|
-
requestBody?: {
|
|
614
|
-
content: {
|
|
615
|
-
"application/json": {
|
|
616
|
-
schema: {
|
|
617
|
-
type?: OpenAPISchemaType;
|
|
618
|
-
properties?: Record<string, any>;
|
|
619
|
-
required?: string[];
|
|
620
|
-
$ref?: string;
|
|
621
|
-
};
|
|
622
|
-
};
|
|
623
|
-
};
|
|
624
|
-
};
|
|
625
|
-
responses?: { [key in string]: {
|
|
626
|
-
description?: string;
|
|
627
|
-
content: {
|
|
628
|
-
"application/json": {
|
|
629
|
-
schema: {
|
|
630
|
-
type?: OpenAPISchemaType;
|
|
631
|
-
properties?: Record<string, any>;
|
|
632
|
-
required?: string[];
|
|
633
|
-
$ref?: string;
|
|
634
|
-
};
|
|
635
|
-
};
|
|
636
|
-
};
|
|
637
|
-
} };
|
|
638
|
-
};
|
|
639
|
-
}
|
|
640
|
-
declare function generator(endpoints: Record<string, Endpoint>, config?: {
|
|
641
|
-
url: string;
|
|
642
|
-
}): Promise<{
|
|
643
|
-
openapi: string;
|
|
644
|
-
info: {
|
|
645
|
-
title: string;
|
|
646
|
-
description: string;
|
|
647
|
-
version: string;
|
|
648
|
-
};
|
|
649
|
-
components: {
|
|
650
|
-
schemas: {};
|
|
651
|
-
};
|
|
652
|
-
security: {
|
|
653
|
-
apiKeyCookie: never[];
|
|
654
|
-
}[];
|
|
655
|
-
servers: {
|
|
656
|
-
url: string | undefined;
|
|
657
|
-
}[];
|
|
658
|
-
tags: {
|
|
659
|
-
name: string;
|
|
660
|
-
description: string;
|
|
661
|
-
}[];
|
|
662
|
-
paths: Record<string, Path$1>;
|
|
663
|
-
}>;
|
|
664
|
-
declare const getHTML: (apiReference: Record<string, any>, config?: {
|
|
665
|
-
logo?: string;
|
|
666
|
-
theme?: string;
|
|
667
|
-
title?: string;
|
|
668
|
-
description?: string;
|
|
669
|
-
}) => string;
|
|
670
|
-
//#endregion
|
|
671
|
-
//#region src/endpoint.d.ts
|
|
672
|
-
interface EndpointBaseOptions {
|
|
673
|
-
/**
|
|
674
|
-
* Query Schema
|
|
675
|
-
*/
|
|
676
|
-
query?: StandardSchemaV1;
|
|
677
|
-
/**
|
|
678
|
-
* Error Schema
|
|
679
|
-
*/
|
|
680
|
-
error?: StandardSchemaV1;
|
|
681
|
-
/**
|
|
682
|
-
* If true headers will be required to be passed in the context
|
|
683
|
-
*/
|
|
684
|
-
requireHeaders?: boolean;
|
|
685
|
-
/**
|
|
686
|
-
* If true request object will be required
|
|
687
|
-
*/
|
|
688
|
-
requireRequest?: boolean;
|
|
689
|
-
/**
|
|
690
|
-
* Clone the request object from the router
|
|
691
|
-
*/
|
|
692
|
-
cloneRequest?: boolean;
|
|
693
|
-
/**
|
|
694
|
-
* If true the body will be undefined
|
|
695
|
-
*/
|
|
696
|
-
disableBody?: boolean;
|
|
697
|
-
/**
|
|
698
|
-
* Endpoint metadata
|
|
699
|
-
*/
|
|
700
|
-
metadata?: {
|
|
701
|
-
/**
|
|
702
|
-
* Open API definition
|
|
703
|
-
*/
|
|
704
|
-
openapi?: {
|
|
705
|
-
summary?: string;
|
|
706
|
-
description?: string;
|
|
707
|
-
tags?: string[];
|
|
708
|
-
operationId?: string;
|
|
709
|
-
parameters?: OpenAPIParameter[];
|
|
710
|
-
requestBody?: {
|
|
711
|
-
content: {
|
|
712
|
-
"application/json": {
|
|
713
|
-
schema: {
|
|
714
|
-
type?: OpenAPISchemaType;
|
|
715
|
-
properties?: Record<string, any>;
|
|
716
|
-
required?: string[];
|
|
717
|
-
$ref?: string;
|
|
718
|
-
};
|
|
719
|
-
};
|
|
720
|
-
};
|
|
721
|
-
};
|
|
722
|
-
responses?: {
|
|
723
|
-
[status: string]: {
|
|
724
|
-
description: string;
|
|
725
|
-
content?: {
|
|
726
|
-
"application/json"?: {
|
|
727
|
-
schema: {
|
|
728
|
-
type?: OpenAPISchemaType;
|
|
729
|
-
properties?: Record<string, any>;
|
|
730
|
-
required?: string[];
|
|
731
|
-
$ref?: string;
|
|
732
|
-
};
|
|
733
|
-
};
|
|
734
|
-
"text/plain"?: {
|
|
735
|
-
schema?: {
|
|
736
|
-
type?: OpenAPISchemaType;
|
|
737
|
-
properties?: Record<string, any>;
|
|
738
|
-
required?: string[];
|
|
739
|
-
$ref?: string;
|
|
740
|
-
};
|
|
741
|
-
};
|
|
742
|
-
"text/html"?: {
|
|
743
|
-
schema?: {
|
|
744
|
-
type?: OpenAPISchemaType;
|
|
745
|
-
properties?: Record<string, any>;
|
|
746
|
-
required?: string[];
|
|
747
|
-
$ref?: string;
|
|
748
|
-
};
|
|
749
|
-
};
|
|
750
|
-
};
|
|
751
|
-
};
|
|
752
|
-
};
|
|
753
|
-
};
|
|
754
|
-
/**
|
|
755
|
-
* Infer body and query type from ts interface
|
|
756
|
-
*
|
|
757
|
-
* useful for generic and dynamic types
|
|
758
|
-
*
|
|
759
|
-
* @example
|
|
760
|
-
* ```ts
|
|
761
|
-
* const endpoint = createEndpoint("/path", {
|
|
762
|
-
* method: "POST",
|
|
763
|
-
* body: z.record(z.string()),
|
|
764
|
-
* $Infer: {
|
|
765
|
-
* body: {} as {
|
|
766
|
-
* type: InferTypeFromOptions<Option> // custom type inference
|
|
767
|
-
* }
|
|
768
|
-
* }
|
|
769
|
-
* }, async(ctx)=>{
|
|
770
|
-
* const body = ctx.body
|
|
771
|
-
* })
|
|
772
|
-
* ```
|
|
773
|
-
*/
|
|
774
|
-
$Infer?: {
|
|
775
|
-
/**
|
|
776
|
-
* Body
|
|
777
|
-
*/
|
|
778
|
-
body?: any;
|
|
779
|
-
/**
|
|
780
|
-
* Query
|
|
781
|
-
*/
|
|
782
|
-
query?: Record<string, any>;
|
|
783
|
-
};
|
|
784
|
-
/**
|
|
785
|
-
* If enabled, endpoint won't be exposed over a router
|
|
786
|
-
* @deprecated Use path-less endpoints instead
|
|
787
|
-
*/
|
|
788
|
-
SERVER_ONLY?: boolean;
|
|
789
|
-
/**
|
|
790
|
-
* If enabled, endpoint won't be exposed as an action to the client
|
|
791
|
-
* @deprecated Use path-less endpoints instead
|
|
792
|
-
*/
|
|
793
|
-
isAction?: boolean;
|
|
794
|
-
/**
|
|
795
|
-
* Defines the places where the endpoint will be available
|
|
796
|
-
*
|
|
797
|
-
* Possible options:
|
|
798
|
-
* - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client
|
|
799
|
-
* - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client
|
|
800
|
-
* - `http` - the endpoint is only exposed to the router
|
|
801
|
-
* @default "rpc"
|
|
802
|
-
*/
|
|
803
|
-
scope?: "rpc" | "server" | "http";
|
|
804
|
-
/**
|
|
805
|
-
* List of allowed media types (MIME types) for the endpoint
|
|
806
|
-
*
|
|
807
|
-
* if provided, only the media types in the list will be allowed to be passed in the body
|
|
808
|
-
*
|
|
809
|
-
* @example
|
|
810
|
-
* ```ts
|
|
811
|
-
* const endpoint = createEndpoint("/path", {
|
|
812
|
-
* method: "POST",
|
|
813
|
-
* allowedMediaTypes: ["application/json", "application/x-www-form-urlencoded"],
|
|
814
|
-
* }, async(ctx)=>{
|
|
815
|
-
* const body = ctx.body
|
|
816
|
-
* })
|
|
817
|
-
* ```
|
|
818
|
-
*/
|
|
819
|
-
allowedMediaTypes?: string[];
|
|
820
|
-
/**
|
|
821
|
-
* Extra metadata
|
|
822
|
-
*/
|
|
823
|
-
[key: string]: any;
|
|
824
|
-
};
|
|
825
|
-
/**
|
|
826
|
-
* List of middlewares to use
|
|
827
|
-
*/
|
|
828
|
-
use?: Middleware[];
|
|
829
|
-
/**
|
|
830
|
-
* A callback to run before any API error is throw or returned
|
|
831
|
-
*
|
|
832
|
-
* @param e - The API error
|
|
833
|
-
* @returns - The response to return
|
|
834
|
-
*/
|
|
835
|
-
onAPIError?: (e: APIError) => void | Promise<void>;
|
|
836
|
-
/**
|
|
837
|
-
* A callback to run before a validation error is thrown
|
|
838
|
-
* You can customize the validation error message by throwing your own APIError
|
|
839
|
-
*/
|
|
840
|
-
onValidationError?: ({
|
|
841
|
-
issues,
|
|
842
|
-
message
|
|
843
|
-
}: {
|
|
844
|
-
message: string;
|
|
845
|
-
issues: readonly StandardSchemaV1.Issue[];
|
|
846
|
-
}) => void | Promise<void>;
|
|
847
|
-
}
|
|
848
|
-
type EndpointBodyMethodOptions = {
|
|
849
|
-
/**
|
|
850
|
-
* Request Method
|
|
851
|
-
*/
|
|
852
|
-
method: "POST" | "PUT" | "DELETE" | "PATCH" | ("POST" | "PUT" | "DELETE" | "PATCH")[];
|
|
853
|
-
/**
|
|
854
|
-
* Body Schema
|
|
855
|
-
*/
|
|
856
|
-
body?: StandardSchemaV1;
|
|
857
|
-
} | {
|
|
858
|
-
/**
|
|
859
|
-
* Request Method
|
|
860
|
-
*/
|
|
861
|
-
method: "GET" | "HEAD" | ("GET" | "HEAD")[];
|
|
862
|
-
/**
|
|
863
|
-
* Body Schema
|
|
864
|
-
*/
|
|
865
|
-
body?: never;
|
|
866
|
-
} | {
|
|
867
|
-
/**
|
|
868
|
-
* Request Method
|
|
869
|
-
*/
|
|
870
|
-
method: "*";
|
|
871
|
-
/**
|
|
872
|
-
* Body Schema
|
|
873
|
-
*/
|
|
874
|
-
body?: StandardSchemaV1;
|
|
875
|
-
} | {
|
|
876
|
-
/**
|
|
877
|
-
* Request Method
|
|
878
|
-
*/
|
|
879
|
-
method: ("POST" | "PUT" | "DELETE" | "PATCH" | "GET" | "HEAD")[];
|
|
880
|
-
/**
|
|
881
|
-
* Body Schema
|
|
882
|
-
*/
|
|
883
|
-
body?: StandardSchemaV1;
|
|
884
|
-
};
|
|
885
|
-
type EndpointOptions = EndpointBaseOptions & EndpointBodyMethodOptions;
|
|
886
|
-
type EndpointContext<Path extends string, Options extends EndpointOptions, Context = {}> = {
|
|
887
|
-
/**
|
|
888
|
-
* Method
|
|
889
|
-
*
|
|
890
|
-
* The request method
|
|
891
|
-
*/
|
|
892
|
-
method: InferMethod<Options>;
|
|
893
|
-
/**
|
|
894
|
-
* Path
|
|
895
|
-
*
|
|
896
|
-
* The path of the endpoint
|
|
897
|
-
*/
|
|
898
|
-
path: Path;
|
|
899
|
-
/**
|
|
900
|
-
* Body
|
|
901
|
-
*
|
|
902
|
-
* The body object will be the parsed JSON from the request and validated
|
|
903
|
-
* against the body schema if it exists.
|
|
904
|
-
*/
|
|
905
|
-
body: InferBody<Options>;
|
|
906
|
-
/**
|
|
907
|
-
* Query
|
|
908
|
-
*
|
|
909
|
-
* The query object will be the parsed query string from the request
|
|
910
|
-
* and validated against the query schema if it exists
|
|
911
|
-
*/
|
|
912
|
-
query: InferQuery<Options>;
|
|
913
|
-
/**
|
|
914
|
-
* Params
|
|
915
|
-
*
|
|
916
|
-
* If the path is `/user/:id` and the request is `/user/1` then the params will
|
|
917
|
-
* be `{ id: "1" }` and if the path includes a wildcard like `/user/*` then the
|
|
918
|
-
* params will be `{ _: "1" }` where `_` is the wildcard key. If the wildcard
|
|
919
|
-
* is named like `/user/**:name` then the params will be `{ name: string }`
|
|
920
|
-
*/
|
|
921
|
-
params: InferParam<Path>;
|
|
922
|
-
/**
|
|
923
|
-
* Request object
|
|
924
|
-
*
|
|
925
|
-
* If `requireRequest` is set to true in the endpoint options this will be
|
|
926
|
-
* required
|
|
927
|
-
*/
|
|
928
|
-
request: InferRequest<Options>;
|
|
929
|
-
/**
|
|
930
|
-
* Headers
|
|
931
|
-
*
|
|
932
|
-
* If `requireHeaders` is set to true in the endpoint options this will be
|
|
933
|
-
* required
|
|
934
|
-
*/
|
|
935
|
-
headers: InferHeaders<Options>;
|
|
936
|
-
/**
|
|
937
|
-
* Set header
|
|
938
|
-
*
|
|
939
|
-
* If it's called outside of a request it will just be ignored.
|
|
940
|
-
*/
|
|
941
|
-
setHeader: (key: string, value: string) => void;
|
|
942
|
-
/**
|
|
943
|
-
* Set the response status code
|
|
944
|
-
*/
|
|
945
|
-
setStatus: (status: Status) => void;
|
|
946
|
-
/**
|
|
947
|
-
* Get header
|
|
948
|
-
*
|
|
949
|
-
* If it's called outside of a request it will just return null
|
|
950
|
-
*
|
|
951
|
-
* @param key - The key of the header
|
|
952
|
-
* @returns
|
|
953
|
-
*/
|
|
954
|
-
getHeader: (key: string) => string | null;
|
|
955
|
-
/**
|
|
956
|
-
* Get a cookie value from the request
|
|
957
|
-
*
|
|
958
|
-
* @param key - The key of the cookie
|
|
959
|
-
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
960
|
-
* @returns - The value of the cookie
|
|
961
|
-
*/
|
|
962
|
-
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
963
|
-
/**
|
|
964
|
-
* Get a signed cookie value from the request
|
|
965
|
-
*
|
|
966
|
-
* @param key - The key of the cookie
|
|
967
|
-
* @param secret - The secret of the signed cookie
|
|
968
|
-
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
969
|
-
* @returns - The value of the cookie or null if the cookie is not found or false if the signature is invalid
|
|
970
|
-
*/
|
|
971
|
-
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | null | false>;
|
|
972
|
-
/**
|
|
973
|
-
* Set a cookie value in the response
|
|
974
|
-
*
|
|
975
|
-
* @param key - The key of the cookie
|
|
976
|
-
* @param value - The value to set
|
|
977
|
-
* @param options - The options of the cookie
|
|
978
|
-
* @returns - The cookie string
|
|
979
|
-
*/
|
|
980
|
-
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
981
|
-
/**
|
|
982
|
-
* Set signed cookie
|
|
983
|
-
*
|
|
984
|
-
* @param key - The key of the cookie
|
|
985
|
-
* @param value - The value to set
|
|
986
|
-
* @param secret - The secret to sign the cookie with
|
|
987
|
-
* @param options - The options of the cookie
|
|
988
|
-
* @returns - The cookie string
|
|
989
|
-
*/
|
|
990
|
-
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
991
|
-
/**
|
|
992
|
-
* JSON
|
|
993
|
-
*
|
|
994
|
-
* a helper function to create a JSON response with
|
|
995
|
-
* the correct headers
|
|
996
|
-
* and status code. If `asResponse` is set to true in
|
|
997
|
-
* the context then
|
|
998
|
-
* it will return a Response object instead of the
|
|
999
|
-
* JSON object.
|
|
1000
|
-
*
|
|
1001
|
-
* @param json - The JSON object to return
|
|
1002
|
-
* @param routerResponse - The response object to
|
|
1003
|
-
* return if `asResponse` is
|
|
1004
|
-
* true in the context this will take precedence
|
|
1005
|
-
*/
|
|
1006
|
-
json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
|
|
1007
|
-
status?: number;
|
|
1008
|
-
headers?: Record<string, string>;
|
|
1009
|
-
response?: Response;
|
|
1010
|
-
body?: Record<string, string>;
|
|
1011
|
-
} | Response) => Promise<R>;
|
|
1012
|
-
/**
|
|
1013
|
-
* Middleware context
|
|
1014
|
-
*/
|
|
1015
|
-
context: Prettify<Context & InferUse<Options["use"]>>;
|
|
1016
|
-
/**
|
|
1017
|
-
* Redirect to a new URL
|
|
1018
|
-
*/
|
|
1019
|
-
redirect: (url: string) => APIError;
|
|
1020
|
-
/**
|
|
1021
|
-
* Return error
|
|
1022
|
-
*/
|
|
1023
|
-
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
1024
|
-
message?: string;
|
|
1025
|
-
code?: string;
|
|
1026
|
-
} & Record<string, any>, headers?: HeadersInit) => APIError;
|
|
1027
|
-
};
|
|
1028
|
-
type EndpointHandler<Path extends string, Options extends EndpointOptions, R> = (context: EndpointContext<Path, Options>) => Promise<R>;
|
|
1029
|
-
declare function createEndpoint<Path extends string, Options extends EndpointOptions, R>(path: Path, options: Options, handler: EndpointHandler<Path, Options, R>): StrictEndpoint<Path, Options, R>;
|
|
1030
|
-
declare function createEndpoint<Options extends EndpointOptions, R>(options: Options, handler: EndpointHandler<never, Options, R>): StrictEndpoint<never, Options, R>;
|
|
1031
|
-
declare namespace createEndpoint {
|
|
1032
|
-
var create: <E extends {
|
|
1033
|
-
use?: Middleware[];
|
|
1034
|
-
}>(opts?: E) => <Path extends string, Opts extends EndpointOptions, R extends Promise<any>>(path: Path, options: Opts, handler: (ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => R) => StrictEndpoint<Path, Opts & {
|
|
1035
|
-
use: any[];
|
|
1036
|
-
}, any>;
|
|
1037
|
-
}
|
|
1038
|
-
type StrictEndpoint<Path extends string, Options extends EndpointOptions, R = any> = {
|
|
1039
|
-
(context: InputContext<Path, Options> & {
|
|
1040
|
-
asResponse: true;
|
|
1041
|
-
}): Promise<Response>;
|
|
1042
|
-
(context: InputContext<Path, Options> & {
|
|
1043
|
-
returnHeaders: true;
|
|
1044
|
-
returnStatus: true;
|
|
1045
|
-
}): Promise<{
|
|
1046
|
-
headers: Headers;
|
|
1047
|
-
status: number;
|
|
1048
|
-
response: Awaited<R>;
|
|
1049
|
-
}>;
|
|
1050
|
-
(context: InputContext<Path, Options> & {
|
|
1051
|
-
returnHeaders: true;
|
|
1052
|
-
returnStatus: false;
|
|
1053
|
-
}): Promise<{
|
|
1054
|
-
headers: Headers;
|
|
1055
|
-
response: Awaited<R>;
|
|
1056
|
-
}>;
|
|
1057
|
-
(context: InputContext<Path, Options> & {
|
|
1058
|
-
returnHeaders: false;
|
|
1059
|
-
returnStatus: true;
|
|
1060
|
-
}): Promise<{
|
|
1061
|
-
status: number;
|
|
1062
|
-
response: Awaited<R>;
|
|
1063
|
-
}>;
|
|
1064
|
-
(context: InputContext<Path, Options> & {
|
|
1065
|
-
returnHeaders: false;
|
|
1066
|
-
returnStatus: false;
|
|
1067
|
-
}): Promise<R>;
|
|
1068
|
-
(context: InputContext<Path, Options> & {
|
|
1069
|
-
returnHeaders: true;
|
|
1070
|
-
}): Promise<{
|
|
1071
|
-
headers: Headers;
|
|
1072
|
-
response: Awaited<R>;
|
|
1073
|
-
}>;
|
|
1074
|
-
(context: InputContext<Path, Options> & {
|
|
1075
|
-
returnStatus: true;
|
|
1076
|
-
}): Promise<{
|
|
1077
|
-
status: number;
|
|
1078
|
-
response: Awaited<R>;
|
|
1079
|
-
}>;
|
|
1080
|
-
(context?: InputContext<Path, Options>): Promise<R>;
|
|
1081
|
-
options: Options;
|
|
1082
|
-
path: Path;
|
|
1083
|
-
};
|
|
1084
|
-
type Endpoint<Path extends string = string, Options extends EndpointOptions = EndpointOptions, Handler extends (inputCtx: any) => Promise<any> = (inputCtx: any) => Promise<any>> = Handler & {
|
|
1085
|
-
options: Options;
|
|
1086
|
-
path: Path;
|
|
1087
|
-
};
|
|
1088
|
-
//#endregion
|
|
1089
|
-
//#region src/router.d.ts
|
|
1090
|
-
interface RouterConfig {
|
|
1091
|
-
throwError?: boolean;
|
|
1092
|
-
onError?: (e: unknown) => void | Promise<void> | Response | Promise<Response>;
|
|
1093
|
-
basePath?: string;
|
|
1094
|
-
routerMiddleware?: Array<{
|
|
1095
|
-
path: string;
|
|
1096
|
-
middleware: Middleware;
|
|
1097
|
-
}>;
|
|
1098
|
-
/**
|
|
1099
|
-
* additional Context that needs to passed to endpoints
|
|
1100
|
-
*
|
|
1101
|
-
* this will be available on `ctx.context` on endpoints
|
|
1102
|
-
*/
|
|
1103
|
-
routerContext?: Record<string, any>;
|
|
1104
|
-
/**
|
|
1105
|
-
* A callback to run before any response
|
|
1106
|
-
*/
|
|
1107
|
-
onResponse?: (res: Response) => any | Promise<any>;
|
|
1108
|
-
/**
|
|
1109
|
-
* A callback to run before any request
|
|
1110
|
-
*/
|
|
1111
|
-
onRequest?: (req: Request) => any | Promise<any>;
|
|
1112
|
-
/**
|
|
1113
|
-
* List of allowed media types (MIME types) for the router
|
|
1114
|
-
*
|
|
1115
|
-
* if provided, only the media types in the list will be allowed to be passed in the body.
|
|
1116
|
-
*
|
|
1117
|
-
* If an endpoint has allowed media types, it will override the router's allowed media types.
|
|
1118
|
-
*
|
|
1119
|
-
* @example
|
|
1120
|
-
* ```ts
|
|
1121
|
-
* const router = createRouter({
|
|
1122
|
-
* allowedMediaTypes: ["application/json", "application/x-www-form-urlencoded"],
|
|
1123
|
-
* })
|
|
1124
|
-
*/
|
|
1125
|
-
allowedMediaTypes?: string[];
|
|
1126
|
-
/**
|
|
1127
|
-
* Skip trailing slashes
|
|
1128
|
-
*
|
|
1129
|
-
* @default false
|
|
1130
|
-
*/
|
|
1131
|
-
skipTrailingSlashes?: boolean;
|
|
1132
|
-
/**
|
|
1133
|
-
* Open API route configuration
|
|
1134
|
-
*/
|
|
1135
|
-
openapi?: {
|
|
1136
|
-
/**
|
|
1137
|
-
* Disable openapi route
|
|
1138
|
-
*
|
|
1139
|
-
* @default false
|
|
1140
|
-
*/
|
|
1141
|
-
disabled?: boolean;
|
|
1142
|
-
/**
|
|
1143
|
-
* A path to display open api using scalar
|
|
1144
|
-
*
|
|
1145
|
-
* @default "/api/reference"
|
|
1146
|
-
*/
|
|
1147
|
-
path?: string;
|
|
1148
|
-
/**
|
|
1149
|
-
* Scalar Configuration
|
|
1150
|
-
*/
|
|
1151
|
-
scalar?: {
|
|
1152
|
-
/**
|
|
1153
|
-
* Title
|
|
1154
|
-
* @default "Open API Reference"
|
|
1155
|
-
*/
|
|
1156
|
-
title?: string;
|
|
1157
|
-
/**
|
|
1158
|
-
* Description
|
|
1159
|
-
*
|
|
1160
|
-
* @default "Better Call Open API Reference"
|
|
1161
|
-
*/
|
|
1162
|
-
description?: string;
|
|
1163
|
-
/**
|
|
1164
|
-
* Logo URL
|
|
1165
|
-
*/
|
|
1166
|
-
logo?: string;
|
|
1167
|
-
/**
|
|
1168
|
-
* Scalar theme
|
|
1169
|
-
* @default "saturn"
|
|
1170
|
-
*/
|
|
1171
|
-
theme?: string;
|
|
1172
|
-
};
|
|
1173
|
-
};
|
|
1174
|
-
}
|
|
1175
|
-
declare const createRouter: <E extends Record<string, Endpoint>, Config extends RouterConfig>(endpoints: E, config?: Config) => {
|
|
1176
|
-
handler: (request: Request) => Promise<Response>;
|
|
1177
|
-
endpoints: E;
|
|
1178
|
-
};
|
|
1179
|
-
type Router = ReturnType<typeof createRouter>;
|
|
1180
|
-
//#endregion
|
|
1181
|
-
export { RequiredKeysOf as $, InferMiddlewareQuery as A, createInternalContext as B, InferBody as C, InferInputMethod as D, InferHeadersInput as E, InferRequest as F, serializeCookie as G, CookiePrefixOptions as H, InferRequestInput as I, InferParamPath as J, serializeSignedCookie as K, InferUse as L, InferParamInput as M, InferQuery as N, InferMethod as O, InferQueryInput as P, Prettify as Q, InputContext as R, HTTPMethod as S, InferHeaders as T, getCookieKey as U, CookieOptions as V, parseCookies as W, IsEmptyObject as X, InferParamWildCard as Y, MergeObject as Z, MiddlewareContext as _, EndpointBaseOptions as a, MiddlewareResponse as b, EndpointOptions as c, OpenAPIParameter as d, UnionToIntersection as et, OpenAPISchemaType as f, Middleware as g, getHTML as h, Endpoint as i, InferParam as j, InferMiddlewareBody as k, StrictEndpoint as l, generator as m, RouterConfig as n, EndpointBodyMethodOptions as o, Path$1 as p, HasRequiredKeys as q, createRouter as r, EndpointContext as s, Router as t, createEndpoint as u, MiddlewareInputContext as v, InferBodyInput as w, createMiddleware as x, MiddlewareOptions as y, Method as z };
|
|
1182
|
-
//# sourceMappingURL=router.d.ts.map
|