better-call 0.0.5-beta.2 → 0.0.5-beta.20
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 +75 -32
- package/dist/client.cjs +2 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +36 -0
- package/dist/client.d.ts +36 -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 +88 -171
- package/dist/index.d.ts +88 -171
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/router-CAklHiJN.d.cts +190 -0
- package/dist/router-CAklHiJN.d.ts +190 -0
- package/package.json +9 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,192 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { E as EndpointOptions, a as EndpointResponse, H as Handler, b as HasRequiredKeys, C as Context, M as Method, P as Prettify, I as InferBody, c as InferRequest, d as InferHeaders, e as ContextTools, f as Endpoint } from './router-CAklHiJN.cjs';
|
|
2
|
+
export { j as CookieOptions, m as InferMethod, o as InferParam, h as InferParamPath, i as InferParamWildCard, n as InferQuery, k as InferUse, l as InferUseOptions, R as Router, g as createRouter } from './router-CAklHiJN.cjs';
|
|
3
|
+
import 'zod';
|
|
3
4
|
|
|
4
|
-
type Middleware<E extends Record<string, string>> = (ctx: Context<string, EndpointOptions, E>) => Promise<{
|
|
5
|
-
context: E;
|
|
6
|
-
} | void>;
|
|
7
|
-
declare const createMiddleware: <E extends Record<string, any>, M extends Middleware<E>>(middleware: M) => <Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R, Awaited<ReturnType<M>> extends {
|
|
8
|
-
context: infer C;
|
|
9
|
-
} ? C extends Record<string, any> ? C : {} : {}>) => {
|
|
10
|
-
(...ctx: HasRequiredKeys$1<Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
11
|
-
context: infer C;
|
|
12
|
-
} ? C extends Record<string, any> ? C : {} : {}>> extends true ? [Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
13
|
-
context: infer C;
|
|
14
|
-
} ? C extends Record<string, any> ? C : {} : {}>] : [Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
15
|
-
context: infer C;
|
|
16
|
-
} ? C extends Record<string, any> ? C : {} : {}>?]): Promise<R>;
|
|
17
|
-
path: Path;
|
|
18
|
-
options: Opts;
|
|
19
|
-
middleware: M;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
type RequiredKeysOf<BaseType extends object> = Exclude<{
|
|
23
|
-
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never;
|
|
24
|
-
}[keyof BaseType], undefined>;
|
|
25
|
-
type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
|
|
26
|
-
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "*";
|
|
27
|
-
interface EndpointOptions {
|
|
28
|
-
method: Method | Method[];
|
|
29
|
-
body?: ZodSchema;
|
|
30
|
-
query?: ZodSchema;
|
|
31
|
-
params?: ZodSchema<any>;
|
|
32
|
-
/**
|
|
33
|
-
* If true headers will be required to be passed in the context
|
|
34
|
-
*/
|
|
35
|
-
requireHeaders?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* If true request object will be required
|
|
38
|
-
*/
|
|
39
|
-
requireRequest?: boolean;
|
|
40
|
-
}
|
|
41
|
-
type InferParamPath<Path> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
42
|
-
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
43
|
-
} : Path extends `${infer _Start}:${infer Param}` ? {
|
|
44
|
-
[K in Param]: string;
|
|
45
|
-
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
46
|
-
type InferParamWildCard<Path> = Path extends `${infer _Start}/*:${infer Param}/${infer Rest}` | `${infer _Start}/**:${infer Param}/${infer Rest}` ? {
|
|
47
|
-
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
48
|
-
} : Path extends `${infer _Start}/*` ? {
|
|
49
|
-
[K in "_"]: string;
|
|
50
|
-
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
51
|
-
type Prettify<T> = {
|
|
52
|
-
[key in keyof T]: T[key];
|
|
53
|
-
} & {};
|
|
54
|
-
type ContextTools = {
|
|
55
|
-
setHeader: (key: string, value: string) => void;
|
|
56
|
-
setCookie: (key: string, value: string) => void;
|
|
57
|
-
getCookie: (key: string) => string | undefined;
|
|
58
|
-
};
|
|
59
|
-
type Context<Path extends string, Opts extends EndpointOptions, Extra extends Record<string, any> = {}> = InferBody<Opts["body"]> & InferParam<Path> & InferMethod<Opts['method']> & InferHeaders<Opts["requireHeaders"]> & InferRequest<Opts["requireRequest"]> & InferQuery<Opts["query"]> & Extra;
|
|
60
|
-
type InferMethod<M extends Method | Method[]> = M extends Array<Method> ? {
|
|
61
|
-
method: M[number];
|
|
62
|
-
} : {
|
|
63
|
-
method?: M;
|
|
64
|
-
};
|
|
65
|
-
type InferHeaders<HeaderReq> = HeaderReq extends true ? {
|
|
66
|
-
headers: Headers;
|
|
67
|
-
} : {
|
|
68
|
-
headers?: Headers;
|
|
69
|
-
};
|
|
70
|
-
type InferRequest<RequestReq> = RequestReq extends true ? {
|
|
71
|
-
request: Request;
|
|
72
|
-
} : {
|
|
73
|
-
request?: Request;
|
|
74
|
-
};
|
|
75
|
-
type InferBody<Body> = Body extends ZodSchema ? Body extends ZodOptional<any> ? {
|
|
76
|
-
body?: z.infer<Body>;
|
|
77
|
-
} : {
|
|
78
|
-
body: z.infer<Body>;
|
|
79
|
-
} : {
|
|
80
|
-
body?: undefined;
|
|
81
|
-
};
|
|
82
|
-
type InferQuery<Query> = Query extends ZodSchema ? Query extends ZodOptional<any> ? {
|
|
83
|
-
query?: z.infer<Query>;
|
|
84
|
-
} : {
|
|
85
|
-
query: z.infer<Query>;
|
|
86
|
-
} : {
|
|
87
|
-
query?: undefined;
|
|
88
|
-
};
|
|
89
|
-
type InferParam<Path extends string, ParamPath extends InferParamPath<Path> = InferParamPath<Path>, WildCard extends InferParamWildCard<Path> = InferParamWildCard<Path>> = ParamPath extends undefined ? WildCard extends undefined ? {
|
|
90
|
-
params?: undefined;
|
|
91
|
-
} : {
|
|
92
|
-
params: WildCard;
|
|
93
|
-
} : {
|
|
94
|
-
params: ParamPath & (WildCard extends undefined ? {} : WildCard);
|
|
95
|
-
};
|
|
96
|
-
type EndpointResponse = Record<string, any> | string | boolean | number | void | undefined;
|
|
97
|
-
type Handler<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse, Extra extends Record<string, any> = Record<string, any>> = (ctx: Context<Path, Opts, Extra>) => Promise<R>;
|
|
98
5
|
interface EndpointConfig {
|
|
99
6
|
/**
|
|
100
7
|
* Throw when the response isn't in 200 range
|
|
101
8
|
*/
|
|
102
9
|
throwOnError?: boolean;
|
|
103
10
|
}
|
|
104
|
-
declare function
|
|
105
|
-
(...ctx: HasRequiredKeys<Context<Path, Opts
|
|
11
|
+
declare function createEndpointCreator<T extends Record<string, any>>(): <Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R, T>) => {
|
|
12
|
+
(...ctx: HasRequiredKeys<Context<Path, Opts>> extends true ? [Context<Path, Opts>] : [(Context<Path, Opts> | undefined)?]): Promise<R>;
|
|
13
|
+
path: Path;
|
|
14
|
+
options: Opts;
|
|
15
|
+
method: Method | Method[];
|
|
16
|
+
headers: Headers;
|
|
17
|
+
};
|
|
18
|
+
declare function createEndpoint<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R>): {
|
|
19
|
+
(...ctx: HasRequiredKeys<Context<Path, Opts>> extends true ? [Context<Path, Opts>] : [Context<Path, Opts>?]): Promise<R>;
|
|
106
20
|
path: Path;
|
|
107
21
|
options: Opts;
|
|
108
22
|
method: Method | Method[];
|
|
109
23
|
headers: Headers;
|
|
110
|
-
middleware: Middleware<any> | undefined;
|
|
111
24
|
};
|
|
112
|
-
type Endpoint = {
|
|
113
|
-
path: string;
|
|
114
|
-
options: EndpointOptions;
|
|
115
|
-
middleware?: Middleware<any>;
|
|
116
|
-
headers?: Headers;
|
|
117
|
-
} & ((ctx: any) => Promise<any>);
|
|
118
25
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
26
|
+
type MiddlewareHandler<Opts extends EndpointOptions, R extends EndpointResponse, Extra extends Record<string, any> = {}> = (ctx: Prettify<InferBody<Opts> & InferRequest<Opts> & InferHeaders<Opts> & {
|
|
27
|
+
params?: Record<string, string>;
|
|
28
|
+
query?: Record<string, string>;
|
|
29
|
+
} & ContextTools> & Extra) => Promise<R>;
|
|
30
|
+
declare function createMiddleware<Opts extends EndpointOptions, R extends EndpointResponse>(optionsOrHandler: MiddlewareHandler<Opts, R>): Endpoint<Handler<string, Opts, R>, Opts>;
|
|
31
|
+
declare function createMiddleware<Opts extends Omit<EndpointOptions, "method">, R extends EndpointResponse>(optionsOrHandler: Opts, handler: MiddlewareHandler<Opts & {
|
|
32
|
+
method: "*";
|
|
33
|
+
}, R>): Endpoint<Handler<string, Opts & {
|
|
34
|
+
method: "*";
|
|
35
|
+
}, R>, Opts & {
|
|
36
|
+
method: "*";
|
|
37
|
+
}>;
|
|
38
|
+
declare const createMiddlewareCreator: <ExtraContext extends Record<string, any> = {}>() => {
|
|
39
|
+
<Opts extends EndpointOptions, R extends EndpointResponse>(optionsOrHandler: MiddlewareHandler<Opts, R, ExtraContext>): Endpoint<Handler<string, Opts, R>, Opts>;
|
|
40
|
+
<Opts extends Omit<EndpointOptions, "method">, R_1 extends EndpointResponse>(optionsOrHandler: Opts, handler: MiddlewareHandler<Opts & {
|
|
41
|
+
method: "*";
|
|
42
|
+
}, R_1, ExtraContext>): Endpoint<Handler<string, Opts & {
|
|
43
|
+
method: "*";
|
|
44
|
+
}, R_1>, Opts & {
|
|
45
|
+
method: "*";
|
|
46
|
+
}>;
|
|
135
47
|
};
|
|
48
|
+
type Middleware<Opts extends EndpointOptions = EndpointOptions, R extends EndpointResponse = EndpointResponse> = (opts: Opts, handler: (ctx: {
|
|
49
|
+
body?: InferBody<Opts>;
|
|
50
|
+
params?: Record<string, string>;
|
|
51
|
+
query?: Record<string, string>;
|
|
52
|
+
}) => Promise<R>) => Endpoint;
|
|
136
53
|
|
|
137
54
|
declare function getBody(request: Request): Promise<any>;
|
|
138
55
|
declare function shouldSerialize(body: any): boolean;
|
|
139
56
|
declare const statusCode: {
|
|
140
57
|
OK: number;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
"I'
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
58
|
+
CREATED: number;
|
|
59
|
+
ACCEPTED: number;
|
|
60
|
+
NO_CONTENT: number;
|
|
61
|
+
MULTIPLE_CHOICES: number;
|
|
62
|
+
MOVED_PERMANENTLY: number;
|
|
63
|
+
FOUND: number;
|
|
64
|
+
SEE_OTHER: number;
|
|
65
|
+
NOT_MODIFIED: number;
|
|
66
|
+
TEMPORARY_REDIRECT: number;
|
|
67
|
+
BAD_REQUEST: number;
|
|
68
|
+
UNAUTHORIZED: number;
|
|
69
|
+
PAYMENT_REQUIRED: number;
|
|
70
|
+
FORBIDDEN: number;
|
|
71
|
+
NOT_FOUND: number;
|
|
72
|
+
METHOD_NOT_ALLOWED: number;
|
|
73
|
+
NOT_ACCEPTABLE: number;
|
|
74
|
+
PROXY_AUTHENTICATION_REQUIRED: number;
|
|
75
|
+
REQUEST_TIMEOUT: number;
|
|
76
|
+
CONFLICT: number;
|
|
77
|
+
GONE: number;
|
|
78
|
+
LENGTH_REQUIRED: number;
|
|
79
|
+
PRECONDITION_FAILED: number;
|
|
80
|
+
PAYLOAD_TOO_LARGE: number;
|
|
81
|
+
URI_TOO_LONG: number;
|
|
82
|
+
UNSUPPORTED_MEDIA_TYPE: number;
|
|
83
|
+
RANGE_NOT_SATISFIABLE: number;
|
|
84
|
+
EXPECTATION_FAILED: number;
|
|
85
|
+
"I'M_A_TEAPOT": number;
|
|
86
|
+
MISDIRECTED_REQUEST: number;
|
|
87
|
+
UNPROCESSABLE_ENTITY: number;
|
|
88
|
+
LOCKED: number;
|
|
89
|
+
FAILED_DEPENDENCY: number;
|
|
90
|
+
TOO_EARLY: number;
|
|
91
|
+
UPGRADE_REQUIRED: number;
|
|
92
|
+
PRECONDITION_REQUIRED: number;
|
|
93
|
+
TOO_MANY_REQUESTS: number;
|
|
94
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: number;
|
|
95
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: number;
|
|
96
|
+
INTERNAL_SERVER_ERROR: number;
|
|
97
|
+
NOT_IMPLEMENTED: number;
|
|
98
|
+
BAD_GATEWAY: number;
|
|
99
|
+
SERVICE_UNAVAILABLE: number;
|
|
100
|
+
GATEWAY_TIMEOUT: number;
|
|
101
|
+
HTTP_VERSION_NOT_SUPPORTED: number;
|
|
102
|
+
VARIANT_ALSO_NEGOTIATES: number;
|
|
103
|
+
INSUFFICIENT_STORAGE: number;
|
|
104
|
+
LOOP_DETECTED: number;
|
|
105
|
+
NOT_EXTENDED: number;
|
|
106
|
+
NETWORK_AUTHENTICATION_REQUIRED: number;
|
|
190
107
|
};
|
|
191
108
|
|
|
192
109
|
type Status = keyof typeof statusCode;
|
|
@@ -196,4 +113,4 @@ declare class APIError extends Error {
|
|
|
196
113
|
constructor(status: Status, body?: Record<string, any>);
|
|
197
114
|
}
|
|
198
115
|
|
|
199
|
-
export { APIError,
|
|
116
|
+
export { APIError, Context, ContextTools, Endpoint, type EndpointConfig, EndpointOptions, EndpointResponse, Handler, InferBody, InferHeaders, InferRequest, Method, type Middleware, type MiddlewareHandler, Prettify, createEndpoint, createEndpointCreator, createMiddleware, createMiddlewareCreator, getBody, shouldSerialize, statusCode };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,192 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { E as EndpointOptions, a as EndpointResponse, H as Handler, b as HasRequiredKeys, C as Context, M as Method, P as Prettify, I as InferBody, c as InferRequest, d as InferHeaders, e as ContextTools, f as Endpoint } from './router-CAklHiJN.js';
|
|
2
|
+
export { j as CookieOptions, m as InferMethod, o as InferParam, h as InferParamPath, i as InferParamWildCard, n as InferQuery, k as InferUse, l as InferUseOptions, R as Router, g as createRouter } from './router-CAklHiJN.js';
|
|
3
|
+
import 'zod';
|
|
3
4
|
|
|
4
|
-
type Middleware<E extends Record<string, string>> = (ctx: Context<string, EndpointOptions, E>) => Promise<{
|
|
5
|
-
context: E;
|
|
6
|
-
} | void>;
|
|
7
|
-
declare const createMiddleware: <E extends Record<string, any>, M extends Middleware<E>>(middleware: M) => <Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R, Awaited<ReturnType<M>> extends {
|
|
8
|
-
context: infer C;
|
|
9
|
-
} ? C extends Record<string, any> ? C : {} : {}>) => {
|
|
10
|
-
(...ctx: HasRequiredKeys$1<Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
11
|
-
context: infer C;
|
|
12
|
-
} ? C extends Record<string, any> ? C : {} : {}>> extends true ? [Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
13
|
-
context: infer C;
|
|
14
|
-
} ? C extends Record<string, any> ? C : {} : {}>] : [Context<Path, Opts, Awaited<ReturnType<M>> extends {
|
|
15
|
-
context: infer C;
|
|
16
|
-
} ? C extends Record<string, any> ? C : {} : {}>?]): Promise<R>;
|
|
17
|
-
path: Path;
|
|
18
|
-
options: Opts;
|
|
19
|
-
middleware: M;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
type RequiredKeysOf<BaseType extends object> = Exclude<{
|
|
23
|
-
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never;
|
|
24
|
-
}[keyof BaseType], undefined>;
|
|
25
|
-
type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
|
|
26
|
-
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "*";
|
|
27
|
-
interface EndpointOptions {
|
|
28
|
-
method: Method | Method[];
|
|
29
|
-
body?: ZodSchema;
|
|
30
|
-
query?: ZodSchema;
|
|
31
|
-
params?: ZodSchema<any>;
|
|
32
|
-
/**
|
|
33
|
-
* If true headers will be required to be passed in the context
|
|
34
|
-
*/
|
|
35
|
-
requireHeaders?: boolean;
|
|
36
|
-
/**
|
|
37
|
-
* If true request object will be required
|
|
38
|
-
*/
|
|
39
|
-
requireRequest?: boolean;
|
|
40
|
-
}
|
|
41
|
-
type InferParamPath<Path> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
42
|
-
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
43
|
-
} : Path extends `${infer _Start}:${infer Param}` ? {
|
|
44
|
-
[K in Param]: string;
|
|
45
|
-
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
46
|
-
type InferParamWildCard<Path> = Path extends `${infer _Start}/*:${infer Param}/${infer Rest}` | `${infer _Start}/**:${infer Param}/${infer Rest}` ? {
|
|
47
|
-
[K in Param | keyof InferParamPath<Rest>]: string;
|
|
48
|
-
} : Path extends `${infer _Start}/*` ? {
|
|
49
|
-
[K in "_"]: string;
|
|
50
|
-
} : Path extends `${infer _Start}/${infer Rest}` ? InferParamPath<Rest> : undefined;
|
|
51
|
-
type Prettify<T> = {
|
|
52
|
-
[key in keyof T]: T[key];
|
|
53
|
-
} & {};
|
|
54
|
-
type ContextTools = {
|
|
55
|
-
setHeader: (key: string, value: string) => void;
|
|
56
|
-
setCookie: (key: string, value: string) => void;
|
|
57
|
-
getCookie: (key: string) => string | undefined;
|
|
58
|
-
};
|
|
59
|
-
type Context<Path extends string, Opts extends EndpointOptions, Extra extends Record<string, any> = {}> = InferBody<Opts["body"]> & InferParam<Path> & InferMethod<Opts['method']> & InferHeaders<Opts["requireHeaders"]> & InferRequest<Opts["requireRequest"]> & InferQuery<Opts["query"]> & Extra;
|
|
60
|
-
type InferMethod<M extends Method | Method[]> = M extends Array<Method> ? {
|
|
61
|
-
method: M[number];
|
|
62
|
-
} : {
|
|
63
|
-
method?: M;
|
|
64
|
-
};
|
|
65
|
-
type InferHeaders<HeaderReq> = HeaderReq extends true ? {
|
|
66
|
-
headers: Headers;
|
|
67
|
-
} : {
|
|
68
|
-
headers?: Headers;
|
|
69
|
-
};
|
|
70
|
-
type InferRequest<RequestReq> = RequestReq extends true ? {
|
|
71
|
-
request: Request;
|
|
72
|
-
} : {
|
|
73
|
-
request?: Request;
|
|
74
|
-
};
|
|
75
|
-
type InferBody<Body> = Body extends ZodSchema ? Body extends ZodOptional<any> ? {
|
|
76
|
-
body?: z.infer<Body>;
|
|
77
|
-
} : {
|
|
78
|
-
body: z.infer<Body>;
|
|
79
|
-
} : {
|
|
80
|
-
body?: undefined;
|
|
81
|
-
};
|
|
82
|
-
type InferQuery<Query> = Query extends ZodSchema ? Query extends ZodOptional<any> ? {
|
|
83
|
-
query?: z.infer<Query>;
|
|
84
|
-
} : {
|
|
85
|
-
query: z.infer<Query>;
|
|
86
|
-
} : {
|
|
87
|
-
query?: undefined;
|
|
88
|
-
};
|
|
89
|
-
type InferParam<Path extends string, ParamPath extends InferParamPath<Path> = InferParamPath<Path>, WildCard extends InferParamWildCard<Path> = InferParamWildCard<Path>> = ParamPath extends undefined ? WildCard extends undefined ? {
|
|
90
|
-
params?: undefined;
|
|
91
|
-
} : {
|
|
92
|
-
params: WildCard;
|
|
93
|
-
} : {
|
|
94
|
-
params: ParamPath & (WildCard extends undefined ? {} : WildCard);
|
|
95
|
-
};
|
|
96
|
-
type EndpointResponse = Record<string, any> | string | boolean | number | void | undefined;
|
|
97
|
-
type Handler<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse, Extra extends Record<string, any> = Record<string, any>> = (ctx: Context<Path, Opts, Extra>) => Promise<R>;
|
|
98
5
|
interface EndpointConfig {
|
|
99
6
|
/**
|
|
100
7
|
* Throw when the response isn't in 200 range
|
|
101
8
|
*/
|
|
102
9
|
throwOnError?: boolean;
|
|
103
10
|
}
|
|
104
|
-
declare function
|
|
105
|
-
(...ctx: HasRequiredKeys<Context<Path, Opts
|
|
11
|
+
declare function createEndpointCreator<T extends Record<string, any>>(): <Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R, T>) => {
|
|
12
|
+
(...ctx: HasRequiredKeys<Context<Path, Opts>> extends true ? [Context<Path, Opts>] : [(Context<Path, Opts> | undefined)?]): Promise<R>;
|
|
13
|
+
path: Path;
|
|
14
|
+
options: Opts;
|
|
15
|
+
method: Method | Method[];
|
|
16
|
+
headers: Headers;
|
|
17
|
+
};
|
|
18
|
+
declare function createEndpoint<Path extends string, Opts extends EndpointOptions, R extends EndpointResponse>(path: Path, options: Opts, handler: Handler<Path, Opts, R>): {
|
|
19
|
+
(...ctx: HasRequiredKeys<Context<Path, Opts>> extends true ? [Context<Path, Opts>] : [Context<Path, Opts>?]): Promise<R>;
|
|
106
20
|
path: Path;
|
|
107
21
|
options: Opts;
|
|
108
22
|
method: Method | Method[];
|
|
109
23
|
headers: Headers;
|
|
110
|
-
middleware: Middleware<any> | undefined;
|
|
111
24
|
};
|
|
112
|
-
type Endpoint = {
|
|
113
|
-
path: string;
|
|
114
|
-
options: EndpointOptions;
|
|
115
|
-
middleware?: Middleware<any>;
|
|
116
|
-
headers?: Headers;
|
|
117
|
-
} & ((ctx: any) => Promise<any>);
|
|
118
25
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
26
|
+
type MiddlewareHandler<Opts extends EndpointOptions, R extends EndpointResponse, Extra extends Record<string, any> = {}> = (ctx: Prettify<InferBody<Opts> & InferRequest<Opts> & InferHeaders<Opts> & {
|
|
27
|
+
params?: Record<string, string>;
|
|
28
|
+
query?: Record<string, string>;
|
|
29
|
+
} & ContextTools> & Extra) => Promise<R>;
|
|
30
|
+
declare function createMiddleware<Opts extends EndpointOptions, R extends EndpointResponse>(optionsOrHandler: MiddlewareHandler<Opts, R>): Endpoint<Handler<string, Opts, R>, Opts>;
|
|
31
|
+
declare function createMiddleware<Opts extends Omit<EndpointOptions, "method">, R extends EndpointResponse>(optionsOrHandler: Opts, handler: MiddlewareHandler<Opts & {
|
|
32
|
+
method: "*";
|
|
33
|
+
}, R>): Endpoint<Handler<string, Opts & {
|
|
34
|
+
method: "*";
|
|
35
|
+
}, R>, Opts & {
|
|
36
|
+
method: "*";
|
|
37
|
+
}>;
|
|
38
|
+
declare const createMiddlewareCreator: <ExtraContext extends Record<string, any> = {}>() => {
|
|
39
|
+
<Opts extends EndpointOptions, R extends EndpointResponse>(optionsOrHandler: MiddlewareHandler<Opts, R, ExtraContext>): Endpoint<Handler<string, Opts, R>, Opts>;
|
|
40
|
+
<Opts extends Omit<EndpointOptions, "method">, R_1 extends EndpointResponse>(optionsOrHandler: Opts, handler: MiddlewareHandler<Opts & {
|
|
41
|
+
method: "*";
|
|
42
|
+
}, R_1, ExtraContext>): Endpoint<Handler<string, Opts & {
|
|
43
|
+
method: "*";
|
|
44
|
+
}, R_1>, Opts & {
|
|
45
|
+
method: "*";
|
|
46
|
+
}>;
|
|
135
47
|
};
|
|
48
|
+
type Middleware<Opts extends EndpointOptions = EndpointOptions, R extends EndpointResponse = EndpointResponse> = (opts: Opts, handler: (ctx: {
|
|
49
|
+
body?: InferBody<Opts>;
|
|
50
|
+
params?: Record<string, string>;
|
|
51
|
+
query?: Record<string, string>;
|
|
52
|
+
}) => Promise<R>) => Endpoint;
|
|
136
53
|
|
|
137
54
|
declare function getBody(request: Request): Promise<any>;
|
|
138
55
|
declare function shouldSerialize(body: any): boolean;
|
|
139
56
|
declare const statusCode: {
|
|
140
57
|
OK: number;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
"I'
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
58
|
+
CREATED: number;
|
|
59
|
+
ACCEPTED: number;
|
|
60
|
+
NO_CONTENT: number;
|
|
61
|
+
MULTIPLE_CHOICES: number;
|
|
62
|
+
MOVED_PERMANENTLY: number;
|
|
63
|
+
FOUND: number;
|
|
64
|
+
SEE_OTHER: number;
|
|
65
|
+
NOT_MODIFIED: number;
|
|
66
|
+
TEMPORARY_REDIRECT: number;
|
|
67
|
+
BAD_REQUEST: number;
|
|
68
|
+
UNAUTHORIZED: number;
|
|
69
|
+
PAYMENT_REQUIRED: number;
|
|
70
|
+
FORBIDDEN: number;
|
|
71
|
+
NOT_FOUND: number;
|
|
72
|
+
METHOD_NOT_ALLOWED: number;
|
|
73
|
+
NOT_ACCEPTABLE: number;
|
|
74
|
+
PROXY_AUTHENTICATION_REQUIRED: number;
|
|
75
|
+
REQUEST_TIMEOUT: number;
|
|
76
|
+
CONFLICT: number;
|
|
77
|
+
GONE: number;
|
|
78
|
+
LENGTH_REQUIRED: number;
|
|
79
|
+
PRECONDITION_FAILED: number;
|
|
80
|
+
PAYLOAD_TOO_LARGE: number;
|
|
81
|
+
URI_TOO_LONG: number;
|
|
82
|
+
UNSUPPORTED_MEDIA_TYPE: number;
|
|
83
|
+
RANGE_NOT_SATISFIABLE: number;
|
|
84
|
+
EXPECTATION_FAILED: number;
|
|
85
|
+
"I'M_A_TEAPOT": number;
|
|
86
|
+
MISDIRECTED_REQUEST: number;
|
|
87
|
+
UNPROCESSABLE_ENTITY: number;
|
|
88
|
+
LOCKED: number;
|
|
89
|
+
FAILED_DEPENDENCY: number;
|
|
90
|
+
TOO_EARLY: number;
|
|
91
|
+
UPGRADE_REQUIRED: number;
|
|
92
|
+
PRECONDITION_REQUIRED: number;
|
|
93
|
+
TOO_MANY_REQUESTS: number;
|
|
94
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: number;
|
|
95
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: number;
|
|
96
|
+
INTERNAL_SERVER_ERROR: number;
|
|
97
|
+
NOT_IMPLEMENTED: number;
|
|
98
|
+
BAD_GATEWAY: number;
|
|
99
|
+
SERVICE_UNAVAILABLE: number;
|
|
100
|
+
GATEWAY_TIMEOUT: number;
|
|
101
|
+
HTTP_VERSION_NOT_SUPPORTED: number;
|
|
102
|
+
VARIANT_ALSO_NEGOTIATES: number;
|
|
103
|
+
INSUFFICIENT_STORAGE: number;
|
|
104
|
+
LOOP_DETECTED: number;
|
|
105
|
+
NOT_EXTENDED: number;
|
|
106
|
+
NETWORK_AUTHENTICATION_REQUIRED: number;
|
|
190
107
|
};
|
|
191
108
|
|
|
192
109
|
type Status = keyof typeof statusCode;
|
|
@@ -196,4 +113,4 @@ declare class APIError extends Error {
|
|
|
196
113
|
constructor(status: Status, body?: Record<string, any>);
|
|
197
114
|
}
|
|
198
115
|
|
|
199
|
-
export { APIError,
|
|
116
|
+
export { APIError, Context, ContextTools, Endpoint, type EndpointConfig, EndpointOptions, EndpointResponse, Handler, InferBody, InferHeaders, InferRequest, Method, type Middleware, type MiddlewareHandler, Prettify, createEndpoint, createEndpointCreator, createMiddleware, createMiddlewareCreator, getBody, shouldSerialize, statusCode };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var $=Object.defineProperty;var Q=(r,e,t)=>e in r?$(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var l=(r,e,t)=>Q(r,typeof e!="symbol"?e+"":e,t);import{ZodError as G}from"zod";import"zod";function j(r,e){if(typeof r=="function")return m("*",{method:"*"},r);if(!e)throw new Error("Middleware handler is required");return m("*",{...r,method:"*"},e)}var X=()=>{function r(e,t){if(typeof e=="function")return m("*",{method:"*"},e);if(!t)throw new Error("Middleware handler is required");return m("*",{...e,method:"*"},t)}return r};var y=class extends Error{constructor(t,n){super(`API Error: ${t} ${n?.message??""}`,{cause:n});l(this,"status");l(this,"body");this.status=t,this.body=n??{},this.stack="",this.name="BetterCallAPIError"}};var g={name:"HMAC",hash:"SHA-256"},w=async r=>{let e=typeof r=="string"?new TextEncoder().encode(r):r;return await crypto.subtle.importKey("raw",e,g,!1,["sign","verify"])},W=async(r,e)=>{let t=await w(e),n=await crypto.subtle.sign(g.name,t,new TextEncoder().encode(r));return btoa(String.fromCharCode(...new Uint8Array(n)))},v=async(r,e,t)=>{try{let n=atob(r),s=new Uint8Array(n.length);for(let i=0,o=n.length;i<o;i++)s[i]=n.charCodeAt(i);return await crypto.subtle.verify(g,t,s,new TextEncoder().encode(e))}catch{return!1}},K=/^[\w!#$%&'*.^`|~+-]+$/,F=/^[ !#-:<-[\]-~]*$/,R=(r,e)=>r.trim().split(";").reduce((n,s)=>{s=s.trim();let i=s.indexOf("=");if(i===-1)return n;let o=s.substring(0,i).trim();if(e&&e!==o||!K.test(o))return n;let p=s.substring(i+1).trim();return p.startsWith('"')&&p.endsWith('"')&&(p=p.slice(1,-1)),F.test(p)&&(n[o]=decodeURIComponent(p)),n},{}),I=async(r,e,t)=>{let n={},s=await w(e);for(let[i,o]of Object.entries(R(r,t))){let p=o.lastIndexOf(".");if(p<1)continue;let a=o.substring(0,p),d=o.substring(p+1);if(d.length!==44||!d.endsWith("="))continue;let u=await v(d,a,s);n[i]=u?a:!1}return n},k=(r,e,t={})=>{let n=`${r}=${e}`;if(r.startsWith("__Secure-")&&!t.secure)throw new Error("__Secure- Cookie must have Secure attributes");if(r.startsWith("__Host-")){if(!t.secure)throw new Error("__Host- Cookie must have Secure attributes");if(t.path!=="/")throw new Error('__Host- Cookie must have Path attributes with "/"');if(t.domain)throw new Error("__Host- Cookie must not have Domain attributes")}if(t&&typeof t.maxAge=="number"&&t.maxAge>=0){if(t.maxAge>3456e4)throw new Error("Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.");n+=`; Max-Age=${Math.floor(t.maxAge)}`}if(t.domain&&t.prefix!=="host"&&(n+=`; Domain=${t.domain}`),t.path&&(n+=`; Path=${t.path}`),t.expires){if(t.expires.getTime()-Date.now()>3456e7)throw new Error("Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.");n+=`; Expires=${t.expires.toUTCString()}`}if(t.httpOnly&&(n+="; HttpOnly"),t.secure&&(n+="; Secure"),t.sameSite&&(n+=`; SameSite=${t.sameSite.charAt(0).toUpperCase()+t.sameSite.slice(1)}`),t.partitioned){if(!t.secure)throw new Error("Partitioned Cookie must have Secure attributes");n+="; Partitioned"}return n},x=(r,e,t)=>(e=encodeURIComponent(e),k(r,e,t)),h=async(r,e,t,n={})=>{let s=await W(e,t);return e=`${e}.${s}`,e=encodeURIComponent(e),k(r,e,n)};var b=(r,e,t)=>{if(!r)return;let n=e;if(t==="secure")n="__Secure-"+e;else if(t==="host")n="__Host-"+e;else return;return R(r,n)[n]},A=(r,e,t,n)=>{let s;n?.prefix==="secure"?s=x("__Secure-"+e,t,{path:"/",...n,secure:!0}):n?.prefix==="host"?s=x("__Host-"+e,t,{...n,path:"/",secure:!0,domain:void 0}):s=x(e,t,{path:"/",...n}),r.append("Set-Cookie",s)},H=async(r,e,t,n,s)=>{let i;s?.prefix==="secure"?i=await h("__Secure-"+e,t,n,{path:"/",...s,secure:!0}):s?.prefix==="host"?i=await h("__Host-"+e,t,n,{...s,path:"/",secure:!0,domain:void 0}):i=await h(e,t,n,{path:"/",...s}),r.append("Set-Cookie",i)},N=async(r,e,t,n)=>{let s=r.get("Cookie");if(!s)return;let i=t;return n==="secure"?i="__Secure-"+t:n==="host"&&(i="__Host-"+t),(await I(s,e,i))[i]};function fe(){return(r,e,t)=>m(r,e,t)}function m(r,e,t){let n=new Headers,s=async(...i)=>{let o={setHeader(a,d){n.set(a,d)},setCookie(a,d,u){A(n,a,d,u)},getCookie(a,d){let u=i[0]?.headers;return b(u?.get("Cookie")||"",a,d)},getSignedCookie(a,d,u){let c=i[0]?.headers;if(!c)throw new TypeError("Headers are required");return N(c,d,a,u)},async setSignedCookie(a,d,u,c){await H(n,a,d,u,c)},...i[0]||{},context:{}};if(e.use?.length)for(let a of e.use){let d=await a(o),u=d.options?.body?d.options.body.parse(o.body):void 0;d&&(o={...o,body:u?{...u,...o.body}:o.body,context:{...o.context||{},...d}})}try{let a=e.body?e.body.parse(o.body):o.body;o={...o,body:a?{...a,...o.body}:o.body},o.query=e.query?e.query.parse(o.query):o.query,o.params=e.params?e.params.parse(o.params):o.params}catch(a){throw a instanceof G?new y("BAD_REQUEST",{message:a.message,details:a.errors}):a}if(e.requireHeaders&&!o.headers)throw new y("BAD_REQUEST",{message:"Headers are required"});if(e.requireRequest&&!o.request)throw new y("BAD_REQUEST",{message:"Request is required"});return await t(o)};return s.path=r,s.options=e,s.method=e.method,s.headers=n,s}import{createRouter as q,addRoute as C,findRoute as B}from"rou3";async function D(r){let e=r.headers.get("content-type")||"";if(r.body){if(e.includes("application/json"))return await r.json();if(e.includes("application/x-www-form-urlencoded")){let t=await r.formData(),n={};return t.forEach((s,i)=>{n[i]=s.toString()}),n}if(e.includes("multipart/form-data")){let t=await r.formData(),n={};return t.forEach((s,i)=>{n[i]=s}),n}return e.includes("text/plain")?await r.text():e.includes("application/octet-stream")?await r.arrayBuffer():e.includes("application/pdf")||e.includes("image/")||e.includes("video/")?await r.blob():e.includes("application/stream")||r.body instanceof ReadableStream?r.body:await r.text()}}function U(r){return typeof r=="object"&&r!==null&&!(r instanceof Blob)&&!(r instanceof FormData)}var M={OK:200,CREATED:201,ACCEPTED:202,NO_CONTENT:204,MULTIPLE_CHOICES:300,MOVED_PERMANENTLY:301,FOUND:302,SEE_OTHER:303,NOT_MODIFIED:304,TEMPORARY_REDIRECT:307,BAD_REQUEST:400,UNAUTHORIZED:401,PAYMENT_REQUIRED:402,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_ALLOWED:405,NOT_ACCEPTABLE:406,PROXY_AUTHENTICATION_REQUIRED:407,REQUEST_TIMEOUT:408,CONFLICT:409,GONE:410,LENGTH_REQUIRED:411,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,URI_TOO_LONG:414,UNSUPPORTED_MEDIA_TYPE:415,RANGE_NOT_SATISFIABLE:416,EXPECTATION_FAILED:417,"I'M_A_TEAPOT":418,MISDIRECTED_REQUEST:421,UNPROCESSABLE_ENTITY:422,LOCKED:423,FAILED_DEPENDENCY:424,TOO_EARLY:425,UPGRADE_REQUIRED:426,PRECONDITION_REQUIRED:428,TOO_MANY_REQUESTS:429,REQUEST_HEADER_FIELDS_TOO_LARGE:431,UNAVAILABLE_FOR_LEGAL_REASONS:451,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504,HTTP_VERSION_NOT_SUPPORTED:505,VARIANT_ALSO_NEGOTIATES:506,INSUFFICIENT_STORAGE:507,LOOP_DETECTED:508,NOT_EXTENDED:510,NETWORK_AUTHENTICATION_REQUIRED:511};var he=(r,e)=>{let t=Object.values(r),n=q();for(let o of t)if(Array.isArray(o.options?.method))for(let p of o.options.method)C(n,p,o.path,o);else C(n,o.options.method,o.path,o);let s=q();for(let o of e?.routerMiddleware||[])C(s,"*",o.path,o.middleware);return{handler:async o=>{let p=new URL(o.url),a=p.pathname;e?.basePath&&(a=a.split(e.basePath)[1]);let d=o.method,u=B(n,d,a),c=u?.data,O=await D(o),P=o.headers,T=Object.fromEntries(p.searchParams),S=B(s,"*",a)?.data;if(!c)return new Response(null,{status:404,statusText:"Not Found"});try{let f={};if(S){let _=await S({path:a,method:d,headers:P,params:u?.params,request:o,body:O,query:T,...e?.extraContext});_&&(f={..._,...f})}let E=await c({path:a,method:d,headers:P,params:u?.params,request:o,body:O,query:T,...f,...e?.extraContext});if(E instanceof Response)return E;let L=U(E)?JSON.stringify(E):E;return new Response(L,{headers:c.headers})}catch(f){if(e?.onError){let E=await e.onError(f);if(E instanceof Response)return E}if(f instanceof y)return new Response(f.body?JSON.stringify(f.body):null,{status:M[f.status],statusText:f.status,headers:c.headers});if(e?.throwError)throw f;return new Response(null,{status:500,statusText:"Internal Server Error"})}},endpoints:r}};import"zod";export{y as APIError,m as createEndpoint,fe as createEndpointCreator,j as createMiddleware,X as createMiddlewareCreator,he as createRouter,D as getBody,U as shouldSerialize,M as statusCode};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|