better-call 1.0.2 → 1.0.4-beta.1

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