better-call 0.0.5-beta.14 → 0.0.5-beta.16
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 +17 -2
- package/dist/client.cjs +2 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +23 -0
- package/dist/client.d.ts +23 -0
- package/dist/client.js +2 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -177
- package/dist/index.d.ts +4 -177
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/router-Ch0Zw3Im.d.cts +190 -0
- package/dist/router-Ch0Zw3Im.d.ts +190 -0
- package/package.json +3 -2
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
type CookiePrefixOptions = "host" | "secure";
|
|
10
|
+
|
|
11
|
+
interface EndpointOptions {
|
|
12
|
+
method: Method | Method[];
|
|
13
|
+
body?: ZodSchema;
|
|
14
|
+
query?: ZodSchema;
|
|
15
|
+
params?: ZodSchema<any>;
|
|
16
|
+
/**
|
|
17
|
+
* If true headers will be required to be passed in the context
|
|
18
|
+
*/
|
|
19
|
+
requireHeaders?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* If true request object will be required
|
|
22
|
+
*/
|
|
23
|
+
requireRequest?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* List of endpoints that will be called before this endpoint
|
|
26
|
+
*/
|
|
27
|
+
use?: Endpoint[];
|
|
28
|
+
}
|
|
29
|
+
type Endpoint<Handler extends (ctx: any) => Promise<any> = (ctx: any) => Promise<any>, Option extends EndpointOptions = EndpointOptions> = {
|
|
30
|
+
path: string;
|
|
31
|
+
options: Option;
|
|
32
|
+
headers?: Headers;
|
|
33
|
+
} & Handler;
|
|
34
|
+
type InferParamPath<Path> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
35
|
+
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
36
|
+
} : Path extends `${infer _Start}:${infer Param}` ? {
|
|
37
|
+
[K in Param]: string;
|
|
38
|
+
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
39
|
+
type InferParamWildCard<Path> = Path extends `${infer _Start}/*:${infer Param}/${infer Rest}` | `${infer _Start}/**:${infer Param}/${infer Rest}` ? {
|
|
40
|
+
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
41
|
+
} : Path extends `${infer _Start}/*` ? {
|
|
42
|
+
[K in "_"]: string;
|
|
43
|
+
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
44
|
+
type Prettify<T> = {
|
|
45
|
+
[key in keyof T]: T[key];
|
|
46
|
+
} & {};
|
|
47
|
+
interface CookieOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Max age in seconds
|
|
50
|
+
*/
|
|
51
|
+
maxAge?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Domain
|
|
54
|
+
*/
|
|
55
|
+
domain?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Path
|
|
58
|
+
*/
|
|
59
|
+
path?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Secure
|
|
62
|
+
*/
|
|
63
|
+
secure?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* HttpOnly
|
|
66
|
+
*/
|
|
67
|
+
httpOnly?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* SameSite
|
|
70
|
+
*/
|
|
71
|
+
sameSite?: "strict" | "lax" | "none";
|
|
72
|
+
/**
|
|
73
|
+
* Expires
|
|
74
|
+
*/
|
|
75
|
+
expires?: Date;
|
|
76
|
+
}
|
|
77
|
+
type ContextTools = {
|
|
78
|
+
/**
|
|
79
|
+
* Set header
|
|
80
|
+
*
|
|
81
|
+
* If it's called outside of a request it will just be ignored.
|
|
82
|
+
*/
|
|
83
|
+
setHeader: (key: string, value: string) => void;
|
|
84
|
+
/**
|
|
85
|
+
* cookie setter.
|
|
86
|
+
*
|
|
87
|
+
* If it's called outside of a request it will just be ignored.
|
|
88
|
+
*/
|
|
89
|
+
setCookie: (key: string, value: string, options?: CookieOptions) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Get cookie value
|
|
92
|
+
*
|
|
93
|
+
* If it's called outside of a request it will just be ignored.
|
|
94
|
+
*/
|
|
95
|
+
getCookie: (key: string, value: string, options?: CookieOptions) => string | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Set signed cookie
|
|
98
|
+
*/
|
|
99
|
+
setSignedCookie: (key: string, value: string, secret: string | BufferSource, options?: CookieOptions) => Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Get signed cookie value
|
|
102
|
+
*/
|
|
103
|
+
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | undefined>;
|
|
104
|
+
};
|
|
105
|
+
type Context<Path extends string, Opts extends EndpointOptions> = InferBody<Opts> & InferParam<Path> & InferMethod<Opts["method"]> & InferHeaders<Opts> & InferRequest<Opts> & InferQuery<Opts["query"]>;
|
|
106
|
+
type InferUse<Opts extends EndpointOptions> = Opts["use"] extends Endpoint[] ? {
|
|
107
|
+
context: UnionToIntersection<Awaited<ReturnType<Opts["use"][number]>>>;
|
|
108
|
+
} : {};
|
|
109
|
+
type InferUseOptions<Opts extends EndpointOptions> = Opts["use"] extends Array<infer U> ? UnionToIntersection<U extends Endpoint ? U["options"] : {
|
|
110
|
+
body?: {};
|
|
111
|
+
requireRequest?: boolean;
|
|
112
|
+
requireHeaders?: boolean;
|
|
113
|
+
}> : {
|
|
114
|
+
body?: {};
|
|
115
|
+
requireRequest?: boolean;
|
|
116
|
+
requireHeaders?: boolean;
|
|
117
|
+
};
|
|
118
|
+
type InferMethod<M extends Method | Method[]> = M extends Array<Method> ? {
|
|
119
|
+
method: M[number];
|
|
120
|
+
} : {
|
|
121
|
+
method?: M;
|
|
122
|
+
};
|
|
123
|
+
type InferHeaders<Opt extends EndpointOptions, HeaderReq = Opt["requireHeaders"]> = HeaderReq extends true ? {
|
|
124
|
+
headers: Headers;
|
|
125
|
+
} : InferUseOptions<Opt>["requireHeaders"] extends true ? {
|
|
126
|
+
headers: Headers;
|
|
127
|
+
} : {
|
|
128
|
+
headers?: Headers;
|
|
129
|
+
};
|
|
130
|
+
type InferRequest<Opt extends EndpointOptions, RequestReq = Opt["requireRequest"]> = RequestReq extends true ? {
|
|
131
|
+
request: Request;
|
|
132
|
+
} : InferUseOptions<Opt>["requireRequest"] extends true ? {
|
|
133
|
+
request: Request;
|
|
134
|
+
} : {
|
|
135
|
+
request?: Request;
|
|
136
|
+
};
|
|
137
|
+
type InferQuery<Query> = Query extends ZodSchema ? Query extends ZodOptional<any> ? {
|
|
138
|
+
query?: z.infer<Query>;
|
|
139
|
+
} : {
|
|
140
|
+
query: z.infer<Query>;
|
|
141
|
+
} : {
|
|
142
|
+
query?: undefined;
|
|
143
|
+
};
|
|
144
|
+
type InferParam<Path extends string, ParamPath extends InferParamPath<Path> = InferParamPath<Path>, WildCard extends InferParamWildCard<Path> = InferParamWildCard<Path>> = ParamPath extends undefined ? WildCard extends undefined ? {
|
|
145
|
+
params?: Record<string, string>;
|
|
146
|
+
} : {
|
|
147
|
+
params: WildCard;
|
|
148
|
+
} : {
|
|
149
|
+
params: Prettify<ParamPath & (WildCard extends undefined ? {} : WildCard)>;
|
|
150
|
+
};
|
|
151
|
+
type EndpointResponse = Record<string, any> | string | boolean | number | void | undefined;
|
|
152
|
+
type Handler<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse, Extra extends Record<string, any> = {}> = (ctx: Prettify<Context<Path, Opts> & InferUse<Opts> & ContextTools> & Extra) => Promise<R>;
|
|
153
|
+
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "*";
|
|
154
|
+
type InferBody<Opts extends EndpointOptions, Body extends ZodSchema | undefined = Opts["body"] & (undefined extends InferUseOptions<Opts>["body"] ? {} : InferUseOptions<Opts>["body"])> = Body extends ZodSchema ? Body extends ZodOptional<any> ? {
|
|
155
|
+
body?: Prettify<z.infer<Body>>;
|
|
156
|
+
} : {
|
|
157
|
+
body: Prettify<z.infer<Body>>;
|
|
158
|
+
} : {
|
|
159
|
+
body?: undefined;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
interface RouterConfig {
|
|
163
|
+
/**
|
|
164
|
+
* Throw error if error occurred other than APIError
|
|
165
|
+
*/
|
|
166
|
+
throwError?: boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Handle error
|
|
169
|
+
*/
|
|
170
|
+
onError?: (e: unknown) => void | Promise<void> | Response | Promise<Response>;
|
|
171
|
+
/**
|
|
172
|
+
* Base path for the router
|
|
173
|
+
*/
|
|
174
|
+
basePath?: string;
|
|
175
|
+
/**
|
|
176
|
+
* Middlewares for the router
|
|
177
|
+
*/
|
|
178
|
+
routerMiddleware?: {
|
|
179
|
+
path: string;
|
|
180
|
+
middleware: Endpoint;
|
|
181
|
+
}[];
|
|
182
|
+
extraContext?: Record<string, any>;
|
|
183
|
+
}
|
|
184
|
+
declare const createRouter: <E extends Endpoint, Config extends RouterConfig>(endpoints: Record<string, E>, config?: Config) => {
|
|
185
|
+
handler: (request: Request) => Promise<Response>;
|
|
186
|
+
endpoints: Record<string, E>;
|
|
187
|
+
};
|
|
188
|
+
type Router = ReturnType<typeof createRouter>;
|
|
189
|
+
|
|
190
|
+
export { type Context as C, type EndpointOptions as E, type Handler as H, type InferBody as I, type Method as M, type Prettify as P, type Router as R, type EndpointResponse as a, type HasRequiredKeys as b, type InferRequest as c, type InferHeaders as d, type ContextTools as e, type Endpoint as f, createRouter as g, type InferParamPath as h, type InferParamWildCard as i, type CookieOptions as j, type InferUse as k, type InferUseOptions as l, type InferMethod as m, type InferQuery as n, type InferParam as o };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-call",
|
|
3
|
-
"version": "0.0.5-beta.
|
|
3
|
+
"version": "0.0.5-beta.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"build": "tsup"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@
|
|
15
|
+
"@biomejs/biome": "^1.8.3",
|
|
16
16
|
"@types/bun": "latest",
|
|
17
17
|
"bumpp": "^9.4.1",
|
|
18
18
|
"tsup": "^8.2.3",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"typescript": "^5.0.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
+
"@better-fetch/fetch": "^1.1.4",
|
|
27
28
|
"rou3": "^0.5.1"
|
|
28
29
|
},
|
|
29
30
|
"exports": {
|