better-call 0.3.3 → 1.0.0-beta.4

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.
@@ -1,426 +0,0 @@
1
- import { ZodSchema, ZodOptional, z } from 'zod';
2
-
3
- type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? Intersection & Union : never;
4
- type RequiredKeysOf<BaseType extends object> = Exclude<{
5
- [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never;
6
- }[keyof BaseType], undefined>;
7
- type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
8
- /**
9
- * this function will return a json response and
10
- * infers the type of the body
11
- */
12
- declare const json: <T>(body: T, option?: {
13
- status?: number;
14
- statusText?: string;
15
- headers?: Record<string, string>;
16
- /**
17
- * this body will take precedence over the body in the options if both are provided.
18
- * This is useful if you want to return body without inferring the type.
19
- */
20
- body?: any;
21
- }) => {
22
- response: {
23
- body: any;
24
- status: number;
25
- statusText: string;
26
- headers: Record<string, string> | undefined;
27
- };
28
- body: T;
29
- _flag: "json";
30
- };
31
-
32
- type Cookie = Record<string, string>;
33
- type SignedCookie = Record<string, string | false>;
34
- type PartitionCookieConstraint = {
35
- partition: true;
36
- secure: true;
37
- } | {
38
- partition?: boolean;
39
- secure?: boolean;
40
- };
41
- type SecureCookieConstraint = {
42
- secure: true;
43
- };
44
- type HostCookieConstraint = {
45
- secure: true;
46
- path: "/";
47
- domain?: undefined;
48
- };
49
- type CookieOptions = {
50
- domain?: string;
51
- expires?: Date;
52
- httpOnly?: boolean;
53
- maxAge?: number;
54
- path?: string;
55
- secure?: boolean;
56
- signingSecret?: string;
57
- sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
58
- partitioned?: boolean;
59
- prefix?: CookiePrefixOptions;
60
- } & PartitionCookieConstraint;
61
- type CookiePrefixOptions = "host" | "secure";
62
- type CookieConstraint<Name> = Name extends `__Secure-${string}` ? CookieOptions & SecureCookieConstraint : Name extends `__Host-${string}` ? CookieOptions & HostCookieConstraint : CookieOptions;
63
- declare const parse: (cookie: string, name?: string) => Cookie;
64
- declare const parseSigned: (cookie: string, secret: string | BufferSource, name?: string) => Promise<SignedCookie>;
65
- declare const serialize: <Name extends string>(name: Name, value: string, opt?: CookieConstraint<Name>) => string;
66
- declare const serializeSigned: (name: string, value: string, secret: string | BufferSource, opt?: CookieOptions) => Promise<string>;
67
- declare const signCookieValue: (value: string, secret: string | BufferSource) => Promise<string>;
68
-
69
- declare function getBody(request: Request): Promise<any>;
70
- declare function shouldSerialize(body: any): boolean;
71
- declare const statusCode: {
72
- OK: number;
73
- CREATED: number;
74
- ACCEPTED: number;
75
- NO_CONTENT: number;
76
- MULTIPLE_CHOICES: number;
77
- MOVED_PERMANENTLY: number;
78
- FOUND: number;
79
- SEE_OTHER: number;
80
- NOT_MODIFIED: number;
81
- TEMPORARY_REDIRECT: number;
82
- BAD_REQUEST: number;
83
- UNAUTHORIZED: number;
84
- PAYMENT_REQUIRED: number;
85
- FORBIDDEN: number;
86
- NOT_FOUND: number;
87
- METHOD_NOT_ALLOWED: number;
88
- NOT_ACCEPTABLE: number;
89
- PROXY_AUTHENTICATION_REQUIRED: number;
90
- REQUEST_TIMEOUT: number;
91
- CONFLICT: number;
92
- GONE: number;
93
- LENGTH_REQUIRED: number;
94
- PRECONDITION_FAILED: number;
95
- PAYLOAD_TOO_LARGE: number;
96
- URI_TOO_LONG: number;
97
- UNSUPPORTED_MEDIA_TYPE: number;
98
- RANGE_NOT_SATISFIABLE: number;
99
- EXPECTATION_FAILED: number;
100
- "I'M_A_TEAPOT": number;
101
- MISDIRECTED_REQUEST: number;
102
- UNPROCESSABLE_ENTITY: number;
103
- LOCKED: number;
104
- FAILED_DEPENDENCY: number;
105
- TOO_EARLY: number;
106
- UPGRADE_REQUIRED: number;
107
- PRECONDITION_REQUIRED: number;
108
- TOO_MANY_REQUESTS: number;
109
- REQUEST_HEADER_FIELDS_TOO_LARGE: number;
110
- UNAVAILABLE_FOR_LEGAL_REASONS: number;
111
- INTERNAL_SERVER_ERROR: number;
112
- NOT_IMPLEMENTED: number;
113
- BAD_GATEWAY: number;
114
- SERVICE_UNAVAILABLE: number;
115
- GATEWAY_TIMEOUT: number;
116
- HTTP_VERSION_NOT_SUPPORTED: number;
117
- VARIANT_ALSO_NEGOTIATES: number;
118
- INSUFFICIENT_STORAGE: number;
119
- LOOP_DETECTED: number;
120
- NOT_EXTENDED: number;
121
- NETWORK_AUTHENTICATION_REQUIRED: number;
122
- };
123
-
124
- type Status = keyof typeof statusCode;
125
- declare class APIError extends Error {
126
- status: Status;
127
- headers: Headers;
128
- body: {
129
- code?: string;
130
- message?: string;
131
- [key: string]: any;
132
- };
133
- constructor(status: Status, body?: Record<string, any>, headers?: Headers);
134
- }
135
-
136
- type OpenAPISchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
137
- interface OpenAPIParameter {
138
- in: "query" | "path" | "header" | "cookie";
139
- name?: string;
140
- description?: string;
141
- required?: boolean;
142
- schema?: {
143
- type: OpenAPISchemaType;
144
- format?: string;
145
- items?: {
146
- type: OpenAPISchemaType;
147
- };
148
- enum?: string[];
149
- minLength?: number;
150
- description?: string;
151
- default?: string;
152
- example?: string;
153
- };
154
- }
155
- interface EndpointOptions {
156
- /**
157
- * Request Method
158
- */
159
- method: Method | Method[];
160
- /**
161
- * Body Schema
162
- */
163
- body?: ZodSchema;
164
- /**
165
- * Query Schema
166
- */
167
- query?: ZodSchema;
168
- /**
169
- * If true headers will be required to be passed in the context
170
- */
171
- requireHeaders?: boolean;
172
- /**
173
- * If true request object will be required
174
- */
175
- requireRequest?: boolean;
176
- /**
177
- * List of endpoints that will be called before this endpoint
178
- */
179
- use?: Endpoint[];
180
- /**
181
- * Endpoint metadata
182
- */
183
- metadata?: {
184
- /**
185
- * If true this endpoint will only be available on the server
186
- */
187
- SERVER_ONLY?: boolean;
188
- $Infer?: {
189
- body?: Record<string, any>;
190
- };
191
- openapi?: {
192
- summary?: string;
193
- description?: string;
194
- tags?: string[];
195
- operationId?: string;
196
- parameters?: OpenAPIParameter[];
197
- requestBody?: {
198
- content: {
199
- "application/json": {
200
- schema: {
201
- type?: OpenAPISchemaType;
202
- properties?: Record<string, any>;
203
- required?: string[];
204
- $ref?: string;
205
- };
206
- };
207
- };
208
- };
209
- responses?: {
210
- [status: string]: {
211
- description: string;
212
- content?: {
213
- "application/json"?: {
214
- schema: {
215
- type?: OpenAPISchemaType;
216
- properties?: Record<string, any>;
217
- required?: string[];
218
- $ref?: string;
219
- };
220
- };
221
- "text/plain"?: {
222
- schema?: {
223
- type?: OpenAPISchemaType;
224
- properties?: Record<string, any>;
225
- required?: string[];
226
- $ref?: string;
227
- };
228
- };
229
- "text/html"?: {
230
- schema?: {
231
- type?: OpenAPISchemaType;
232
- properties?: Record<string, any>;
233
- required?: string[];
234
- $ref?: string;
235
- };
236
- };
237
- };
238
- };
239
- };
240
- };
241
- [key: string]: any;
242
- };
243
- }
244
- type Endpoint<Handler extends (ctx: any) => Promise<any> = (ctx: any) => Promise<any>, Option extends EndpointOptions = EndpointOptions> = {
245
- path: string;
246
- options: Option;
247
- headers?: Headers;
248
- } & Handler;
249
- type InferParamPath<Path> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
250
- [K in Param | keyof InferParamPath<Rest>]: string;
251
- } : Path extends `${infer _Start}:${infer Param}` ? {
252
- [K in Param]: string;
253
- } : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
254
- type InferParamWildCard<Path> = Path extends `${infer _Start}/*:${infer Param}/${infer Rest}` | `${infer _Start}/**:${infer Param}/${infer Rest}` ? {
255
- [K in Param | keyof InferParamPath<Rest>]: string;
256
- } : Path extends `${infer _Start}/*` ? {
257
- [K in "_"]: string;
258
- } : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
259
- type Prettify<T> = {
260
- [key in keyof T]: T[key];
261
- } & {};
262
- type ContextTools = {
263
- /**
264
- * the current path
265
- */
266
- path: string;
267
- /**
268
- * Set header
269
- *
270
- * If it's called outside of a request it will just be ignored.
271
- */
272
- setHeader: (key: string, value: string) => void;
273
- /**
274
- * cookie setter.
275
- *
276
- * If it's called outside of a request it will just be ignored.
277
- */
278
- setCookie: (key: string, value: string, options?: CookieOptions) => void;
279
- /**
280
- * Get cookie value
281
- *
282
- * If it's called outside of a request it will just be ignored.
283
- */
284
- getCookie: (key: string, prefix?: CookiePrefixOptions) => string | undefined;
285
- /**
286
- * Set signed cookie
287
- */
288
- setSignedCookie: (key: string, value: string, secret: string | BufferSource, options?: CookieOptions) => Promise<void>;
289
- /**
290
- * Get signed cookie value
291
- */
292
- getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | undefined>;
293
- /**
294
- * Redirect to url
295
- */
296
- redirect: (url: string) => APIError;
297
- /**
298
- * json response helper
299
- */
300
- json: typeof json;
301
- /**
302
- * response header
303
- */
304
- responseHeader: Headers;
305
- };
306
- type Context<Path extends string, Opts extends EndpointOptions> = InferBody<Opts> & InferParam<Path> & InferMethod<Opts["method"]> & InferHeaders<Opts> & InferRequest<Opts> & InferQuery<Opts["query"]> & {
307
- /**
308
- * This is used internally.
309
- * But you can use it to change the response form.
310
- *
311
- * - `json` will be set only when using `ctx.json` helper.
312
- * - `router` will be set when the handler is called from the router.
313
- * You can use this to change the response form to be a `Response` object.
314
- * - `default` will be used normally when the handler is called as a normal function.
315
- *
316
- * @internal
317
- */
318
- _flag?: "json" | "router" | "default";
319
- /**
320
- * Force the response to be a `Response` object.
321
- */
322
- asResponse?: boolean;
323
- };
324
- type InferUse<Opts extends EndpointOptions["use"]> = Opts extends Endpoint[] ? {
325
- context: UnionToIntersection<Awaited<ReturnType<Opts[number]>>>;
326
- } : {};
327
- type InferUseOptions<Opts extends EndpointOptions> = Opts["use"] extends Array<infer U> ? UnionToIntersection<U extends Endpoint ? U["options"] : {
328
- body?: {};
329
- requireRequest?: boolean;
330
- requireHeaders?: boolean;
331
- }> : {
332
- body?: {};
333
- requireRequest?: boolean;
334
- requireHeaders?: boolean;
335
- };
336
- type InferMethod<M extends Method | Method[]> = M extends Array<Method> ? {
337
- method: M[number];
338
- } : {
339
- method?: M;
340
- };
341
- type InferHeaders<Opt extends EndpointOptions, HeaderReq = Opt["requireHeaders"]> = HeaderReq extends true ? {
342
- headers: Headers;
343
- } : InferUseOptions<Opt>["requireHeaders"] extends true ? {
344
- headers: Headers;
345
- } : {
346
- headers?: Headers;
347
- };
348
- type InferRequest<Opt extends EndpointOptions, RequestReq = Opt["requireRequest"]> = RequestReq extends true ? {
349
- request: Request;
350
- } : InferUseOptions<Opt>["requireRequest"] extends true ? {
351
- request: Request;
352
- } : {
353
- request?: Request;
354
- };
355
- type InferQuery<Query> = Query extends ZodSchema ? Query extends ZodOptional<any> ? {
356
- query?: z.infer<Query>;
357
- } : {
358
- query: z.infer<Query>;
359
- } : {
360
- query?: undefined;
361
- };
362
- type InferParam<Path extends string, ParamPath extends InferParamPath<Path> = InferParamPath<Path>, WildCard extends InferParamWildCard<Path> = InferParamWildCard<Path>> = ParamPath extends undefined ? WildCard extends undefined ? {
363
- params?: Record<string, string>;
364
- } : {
365
- params: WildCard;
366
- } : {
367
- params: Prettify<ParamPath & (WildCard extends undefined ? {} : WildCard)>;
368
- };
369
- type EndpointBody = Record<string, any> | string | boolean | number | void | undefined | null | unknown;
370
- type EndpointResponse = {
371
- response: {
372
- status?: number;
373
- statusText?: string;
374
- headers?: Headers;
375
- body: any;
376
- };
377
- body: EndpointBody;
378
- _flag: "json";
379
- } | EndpointBody;
380
- type Handler<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse> = (ctx: Prettify<Context<Path, Opts> & InferUse<Opts["use"]> & ContextTools>) => Promise<R>;
381
- type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "*";
382
- type InferBody<Opts extends EndpointOptions, Body extends ZodSchema | undefined = Opts["body"] & (undefined extends InferUseOptions<Opts>["body"] ? {} : InferUseOptions<Opts>["body"])> = Opts["metadata"] extends {
383
- $Infer: {
384
- body: infer B;
385
- };
386
- } ? {
387
- body: B;
388
- } : Body extends ZodSchema ? Body extends ZodOptional<any> ? {
389
- body?: Prettify<z.infer<Body>>;
390
- } : {
391
- body: Prettify<z.infer<Body>>;
392
- } : {
393
- body?: undefined;
394
- };
395
-
396
- interface RouterConfig {
397
- /**
398
- * Throw error if error occurred other than APIError
399
- */
400
- throwError?: boolean;
401
- /**
402
- * Handle error
403
- */
404
- onError?: (e: unknown) => void | Promise<void> | Response | Promise<Response>;
405
- /**
406
- * Base path for the router
407
- */
408
- basePath?: string;
409
- /**
410
- * Middlewares for the router
411
- */
412
- routerMiddleware?: {
413
- path: string;
414
- middleware: Endpoint;
415
- }[];
416
- extraContext?: Record<string, any>;
417
- onResponse?: (res: Response) => any | Promise<any>;
418
- onRequest?: (req: Request) => any | Promise<any>;
419
- }
420
- declare const createRouter: <E extends Record<string, Endpoint>, Config extends RouterConfig>(endpoints: E, config?: Config) => {
421
- handler: (request: Request) => Promise<Response>;
422
- endpoints: E;
423
- };
424
- type Router = ReturnType<typeof createRouter>;
425
-
426
- export { APIError as A, serialize as B, type Context as C, serializeSigned as D, type Endpoint as E, signCookieValue as F, type RequiredKeysOf as G, type HasRequiredKeys as H, type InferUse as I, json as J, type Method as M, type OpenAPISchemaType as O, type Prettify as P, type Router as R, type SignedCookie as S, type UnionToIntersection as U, type EndpointOptions as a, type EndpointResponse as b, type ContextTools as c, type Handler as d, type InferBody as e, type InferRequest as f, type InferHeaders as g, type InferUseOptions as h, type CookiePrefixOptions as i, type CookieOptions as j, type RouterConfig as k, createRouter as l, getBody as m, statusCode as n, type OpenAPIParameter as o, type InferParamPath as p, type InferParamWildCard as q, type InferMethod as r, shouldSerialize as s, type InferQuery as t, type InferParam as u, type EndpointBody as v, type Cookie as w, type CookieConstraint as x, parse as y, parseSigned as z };