better-call 1.3.3 → 2.0.0-beta.2
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/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +13 -17
- package/dist/client.d.mts +13 -17
- package/dist/client.mjs.map +1 -1
- package/dist/context.cjs +10 -9
- package/dist/context.cjs.map +1 -1
- package/dist/context.d.cts +129 -323
- package/dist/context.d.mts +129 -323
- package/dist/context.mjs +10 -9
- package/dist/context.mjs.map +1 -1
- package/dist/cookies.cjs +1 -1
- package/dist/cookies.d.cts +1 -12
- package/dist/cookies.d.mts +1 -12
- package/dist/cookies.mjs +1 -1
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.mjs.map +1 -1
- package/dist/endpoint.cjs +21 -7
- package/dist/endpoint.cjs.map +1 -1
- package/dist/endpoint.d.cts +197 -392
- package/dist/endpoint.d.mts +197 -392
- package/dist/endpoint.mjs +21 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/error.cjs +1 -1
- package/dist/error.cjs.map +1 -1
- package/dist/error.mjs +1 -1
- package/dist/error.mjs.map +1 -1
- package/dist/helper.d.cts +2 -3
- package/dist/helper.d.mts +2 -3
- package/dist/index.cjs +3 -10
- package/dist/index.d.cts +9 -8
- package/dist/index.d.mts +9 -8
- package/dist/index.mjs +3 -4
- package/dist/middleware.cjs +59 -13
- package/dist/middleware.cjs.map +1 -1
- package/dist/middleware.d.cts +85 -42
- package/dist/middleware.d.mts +85 -42
- package/dist/middleware.mjs +59 -13
- package/dist/middleware.mjs.map +1 -1
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.mts +1 -1
- package/dist/node.mjs.map +1 -1
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.cts +1 -1
- package/dist/openapi.d.mts +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/router.cjs.map +1 -1
- package/dist/router.mjs.map +1 -1
- package/dist/to-response.cjs +1 -1
- package/dist/to-response.cjs.map +1 -1
- package/dist/to-response.mjs +1 -1
- package/dist/to-response.mjs.map +1 -1
- package/dist/types.d.cts +142 -0
- package/dist/types.d.mts +142 -0
- package/dist/validator.cjs +1 -1
- package/dist/validator.cjs.map +1 -1
- package/dist/validator.mjs +1 -1
- package/dist/validator.mjs.map +1 -1
- package/package.json +3 -3
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "./standard-schema.mjs";
|
|
2
|
+
import { InferParamPath, InferParamWildCard, IsEmptyObject, Prettify, UnionToIntersection } from "./helper.mjs";
|
|
3
|
+
import { Middleware } from "./middleware.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/types.d.ts
|
|
6
|
+
type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
|
|
7
|
+
/**
|
|
8
|
+
* Resolves a method type parameter to its effective runtime type.
|
|
9
|
+
*/
|
|
10
|
+
type ResolveMethod<M> = M extends Array<infer U> ? U : M extends "*" ? HTTPMethod : M;
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a body schema to its output type.
|
|
13
|
+
*/
|
|
14
|
+
type ResolveBody<S, Meta = undefined> = Meta extends {
|
|
15
|
+
$Infer: {
|
|
16
|
+
body: infer B;
|
|
17
|
+
};
|
|
18
|
+
} ? B : S extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<S> : any;
|
|
19
|
+
/**
|
|
20
|
+
* Resolves a query schema to its output type.
|
|
21
|
+
*/
|
|
22
|
+
type ResolveQuery<S, Meta = undefined> = Meta extends {
|
|
23
|
+
$Infer: {
|
|
24
|
+
query: infer Q;
|
|
25
|
+
};
|
|
26
|
+
} ? Q : S extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<S> : Record<string, any> | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Resolves body schema to its input type (for InputContext at call-site).
|
|
29
|
+
*/
|
|
30
|
+
type ResolveBodyInput<S, Meta = undefined> = Meta extends {
|
|
31
|
+
$Infer: {
|
|
32
|
+
body: infer B;
|
|
33
|
+
};
|
|
34
|
+
} ? B : S extends StandardSchemaV1 ? StandardSchemaV1.InferInput<S> : undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Resolves query schema to its input type (for InputContext at call-site).
|
|
37
|
+
*/
|
|
38
|
+
type ResolveQueryInput<S, Meta = undefined> = Meta extends {
|
|
39
|
+
$Infer: {
|
|
40
|
+
query: infer Q;
|
|
41
|
+
};
|
|
42
|
+
} ? Q : S extends StandardSchemaV1 ? StandardSchemaV1.InferInput<S> : Record<string, any> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Resolves error schema to its input type (for InputContext at call-site).
|
|
45
|
+
*/
|
|
46
|
+
type ResolveErrorInput<S, Meta = undefined> = Meta extends {
|
|
47
|
+
$Infer: {
|
|
48
|
+
error: infer E;
|
|
49
|
+
};
|
|
50
|
+
} ? E : S extends StandardSchemaV1 ? StandardSchemaV1.InferInput<S> : undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Constraint: body is `never` for GET/HEAD methods.
|
|
53
|
+
*/
|
|
54
|
+
type BodyOption<M, B extends object | undefined = undefined> = M extends "GET" | "HEAD" | ("GET" | "HEAD")[] ? {
|
|
55
|
+
body?: never;
|
|
56
|
+
} : {
|
|
57
|
+
body?: B;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Infer param types from a path string.
|
|
61
|
+
*/
|
|
62
|
+
type InferParam<Path extends string> = [Path] extends [never] ? Record<string, any> | undefined : IsEmptyObject<InferParamPath<Path> & InferParamWildCard<Path>> extends true ? Record<string, any> | undefined : Prettify<InferParamPath<Path> & InferParamWildCard<Path>>;
|
|
63
|
+
/**
|
|
64
|
+
* Infer param input (required vs optional based on whether path has params).
|
|
65
|
+
*/
|
|
66
|
+
type InferParamInput<Path extends string> = [Path] extends [never] ? {
|
|
67
|
+
params?: Record<string, any>;
|
|
68
|
+
} : IsEmptyObject<InferParamPath<Path> & InferParamWildCard<Path>> extends true ? {
|
|
69
|
+
params?: Record<string, any>;
|
|
70
|
+
} : {
|
|
71
|
+
params: Prettify<InferParamPath<Path> & InferParamWildCard<Path>>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Infer body input from an already-resolved body type.
|
|
75
|
+
* Body is the plain resolved type (not a schema).
|
|
76
|
+
*/
|
|
77
|
+
type InferBodyInput<Body> = undefined extends Body ? {
|
|
78
|
+
body?: Body;
|
|
79
|
+
} : {
|
|
80
|
+
body: Body;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Infer query input from an already-resolved query type.
|
|
84
|
+
* Query is the plain resolved type (not a schema).
|
|
85
|
+
*/
|
|
86
|
+
type InferQueryInput<Query> = undefined extends Query ? {
|
|
87
|
+
query?: Query;
|
|
88
|
+
} : {
|
|
89
|
+
query: Query;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Infer method input: required for wildcard, optional for arrays and single methods.
|
|
93
|
+
*/
|
|
94
|
+
type InferMethodInput<M> = 0 extends 1 & M ? {
|
|
95
|
+
method?: HTTPMethod | undefined;
|
|
96
|
+
} : M extends "*" ? {
|
|
97
|
+
method: HTTPMethod;
|
|
98
|
+
} : M extends Array<any> ? {
|
|
99
|
+
method?: M[number] | undefined;
|
|
100
|
+
} : {
|
|
101
|
+
method?: M | undefined;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Infer request input.
|
|
105
|
+
*/
|
|
106
|
+
type InferRequestInput<ReqRequest extends boolean> = 0 extends 1 & ReqRequest ? {
|
|
107
|
+
request?: Request;
|
|
108
|
+
} : ReqRequest extends true ? {
|
|
109
|
+
request: Request;
|
|
110
|
+
} : {
|
|
111
|
+
request?: Request;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Infer headers input.
|
|
115
|
+
*/
|
|
116
|
+
type InferHeadersInput<ReqHeaders extends boolean> = 0 extends 1 & ReqHeaders ? {
|
|
117
|
+
headers?: HeadersInit;
|
|
118
|
+
} : ReqHeaders extends true ? {
|
|
119
|
+
headers: HeadersInit;
|
|
120
|
+
} : {
|
|
121
|
+
headers?: HeadersInit;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Infer the use (middleware) context union.
|
|
125
|
+
* Guards against `any` and `[]` to avoid poisoning the Context type.
|
|
126
|
+
*/
|
|
127
|
+
type InferUse<Opts extends Middleware[] | undefined> = 0 extends 1 & Opts ? any : Opts extends Middleware[] ? Opts extends [] ? {} : UnionToIntersection<Awaited<ReturnType<Opts[number]>>> : {};
|
|
128
|
+
/**
|
|
129
|
+
* The full InputContext type for the Endpoint call signature.
|
|
130
|
+
* Body and Query are already-resolved plain types.
|
|
131
|
+
*/
|
|
132
|
+
type InputContext<Path extends string, M, Body, Query, ReqHeaders extends boolean, ReqRequest extends boolean> = InferBodyInput<Body> & InferMethodInput<M> & InferQueryInput<Query> & InferParamInput<Path> & InferRequestInput<ReqRequest> & InferHeadersInput<ReqHeaders> & {
|
|
133
|
+
asResponse?: boolean;
|
|
134
|
+
returnHeaders?: boolean;
|
|
135
|
+
returnStatus?: boolean;
|
|
136
|
+
use?: Middleware[];
|
|
137
|
+
path?: string;
|
|
138
|
+
context?: Record<string, any>;
|
|
139
|
+
};
|
|
140
|
+
//#endregion
|
|
141
|
+
export { BodyOption, HTTPMethod, InferParam, InferUse, InputContext, ResolveBody, ResolveBodyInput, ResolveErrorInput, ResolveMethod, ResolveQuery, ResolveQueryInput };
|
|
142
|
+
//# sourceMappingURL=types.d.mts.map
|
package/dist/validator.cjs
CHANGED
package/dist/validator.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.cjs","names":[],"sources":["../src/validator.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"validator.cjs","names":[],"sources":["../src/validator.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"./standard-schema\";\n\ntype ValidationResponse =\n\t| {\n\t\t\tdata: {\n\t\t\t\tbody: any;\n\t\t\t\tquery: any;\n\t\t\t};\n\t\t\terror: null;\n\t }\n\t| {\n\t\t\tdata: null;\n\t\t\terror: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t};\n\t };\n\n/**\n * Runs validation on body and query\n * @returns error and data object\n */\nexport async function runValidation(\n\toptions: {\n\t\tbody?: StandardSchemaV1;\n\t\tquery?: StandardSchemaV1;\n\t\trequireHeaders?: boolean;\n\t\trequireRequest?: boolean;\n\t},\n\tcontext: Record<string, any> = {},\n): Promise<ValidationResponse> {\n\tconst request = {\n\t\tbody: context.body,\n\t\tquery: context.query,\n\t} as {\n\t\tbody: any;\n\t\tquery: any;\n\t};\n\tif (options.body) {\n\t\tconst result = await options.body[\"~standard\"].validate(context.body);\n\t\tif (result.issues) {\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: fromError(result.issues, \"body\"),\n\t\t\t};\n\t\t}\n\t\trequest.body = result.value;\n\t}\n\n\tif (options.query) {\n\t\tconst result = await options.query[\"~standard\"].validate(context.query);\n\t\tif (result.issues) {\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: fromError(result.issues, \"query\"),\n\t\t\t};\n\t\t}\n\t\trequest.query = result.value;\n\t}\n\tif (options.requireHeaders && !context.headers) {\n\t\treturn {\n\t\t\tdata: null,\n\t\t\terror: { message: \"Headers is required\", issues: [] },\n\t\t};\n\t}\n\tif (options.requireRequest && !context.request) {\n\t\treturn {\n\t\t\tdata: null,\n\t\t\terror: { message: \"Request is required\", issues: [] },\n\t\t};\n\t}\n\treturn {\n\t\tdata: request,\n\t\terror: null,\n\t};\n}\n\nfunction fromError(\n\terror: readonly StandardSchemaV1.Issue[],\n\tvalidating: string,\n) {\n\tconst message = error\n\t\t.map((e) => {\n\t\t\treturn `[${e.path?.length ? `${validating}.` + e.path.map((x) => (typeof x === \"object\" ? x.key : x)).join(\".\") : validating}] ${e.message}`;\n\t\t})\n\t\t.join(\"; \");\n\n\treturn {\n\t\tmessage,\n\t\tissues: error,\n\t};\n}\n"],"mappings":";;;;;;AAsBA,eAAsB,cACrB,SAMA,UAA+B,EAAE,EACH;CAC9B,MAAM,UAAU;EACf,MAAM,QAAQ;EACd,OAAO,QAAQ;EACf;AAID,KAAI,QAAQ,MAAM;EACjB,MAAM,SAAS,MAAM,QAAQ,KAAK,aAAa,SAAS,QAAQ,KAAK;AACrE,MAAI,OAAO,OACV,QAAO;GACN,MAAM;GACN,OAAO,UAAU,OAAO,QAAQ,OAAO;GACvC;AAEF,UAAQ,OAAO,OAAO;;AAGvB,KAAI,QAAQ,OAAO;EAClB,MAAM,SAAS,MAAM,QAAQ,MAAM,aAAa,SAAS,QAAQ,MAAM;AACvE,MAAI,OAAO,OACV,QAAO;GACN,MAAM;GACN,OAAO,UAAU,OAAO,QAAQ,QAAQ;GACxC;AAEF,UAAQ,QAAQ,OAAO;;AAExB,KAAI,QAAQ,kBAAkB,CAAC,QAAQ,QACtC,QAAO;EACN,MAAM;EACN,OAAO;GAAE,SAAS;GAAuB,QAAQ,EAAE;GAAE;EACrD;AAEF,KAAI,QAAQ,kBAAkB,CAAC,QAAQ,QACtC,QAAO;EACN,MAAM;EACN,OAAO;GAAE,SAAS;GAAuB,QAAQ,EAAE;GAAE;EACrD;AAEF,QAAO;EACN,MAAM;EACN,OAAO;EACP;;AAGF,SAAS,UACR,OACA,YACC;AAOD,QAAO;EACN,SAPe,MACd,KAAK,MAAM;AACX,UAAO,IAAI,EAAE,MAAM,SAAS,GAAG,WAAW,KAAK,EAAE,KAAK,KAAK,MAAO,OAAO,MAAM,WAAW,EAAE,MAAM,EAAG,CAAC,KAAK,IAAI,GAAG,WAAW,IAAI,EAAE;IAClI,CACD,KAAK,KAAK;EAIX,QAAQ;EACR"}
|
package/dist/validator.mjs
CHANGED
package/dist/validator.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.mjs","names":[],"sources":["../src/validator.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"validator.mjs","names":[],"sources":["../src/validator.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"./standard-schema\";\n\ntype ValidationResponse =\n\t| {\n\t\t\tdata: {\n\t\t\t\tbody: any;\n\t\t\t\tquery: any;\n\t\t\t};\n\t\t\terror: null;\n\t }\n\t| {\n\t\t\tdata: null;\n\t\t\terror: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t};\n\t };\n\n/**\n * Runs validation on body and query\n * @returns error and data object\n */\nexport async function runValidation(\n\toptions: {\n\t\tbody?: StandardSchemaV1;\n\t\tquery?: StandardSchemaV1;\n\t\trequireHeaders?: boolean;\n\t\trequireRequest?: boolean;\n\t},\n\tcontext: Record<string, any> = {},\n): Promise<ValidationResponse> {\n\tconst request = {\n\t\tbody: context.body,\n\t\tquery: context.query,\n\t} as {\n\t\tbody: any;\n\t\tquery: any;\n\t};\n\tif (options.body) {\n\t\tconst result = await options.body[\"~standard\"].validate(context.body);\n\t\tif (result.issues) {\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: fromError(result.issues, \"body\"),\n\t\t\t};\n\t\t}\n\t\trequest.body = result.value;\n\t}\n\n\tif (options.query) {\n\t\tconst result = await options.query[\"~standard\"].validate(context.query);\n\t\tif (result.issues) {\n\t\t\treturn {\n\t\t\t\tdata: null,\n\t\t\t\terror: fromError(result.issues, \"query\"),\n\t\t\t};\n\t\t}\n\t\trequest.query = result.value;\n\t}\n\tif (options.requireHeaders && !context.headers) {\n\t\treturn {\n\t\t\tdata: null,\n\t\t\terror: { message: \"Headers is required\", issues: [] },\n\t\t};\n\t}\n\tif (options.requireRequest && !context.request) {\n\t\treturn {\n\t\t\tdata: null,\n\t\t\terror: { message: \"Request is required\", issues: [] },\n\t\t};\n\t}\n\treturn {\n\t\tdata: request,\n\t\terror: null,\n\t};\n}\n\nfunction fromError(\n\terror: readonly StandardSchemaV1.Issue[],\n\tvalidating: string,\n) {\n\tconst message = error\n\t\t.map((e) => {\n\t\t\treturn `[${e.path?.length ? `${validating}.` + e.path.map((x) => (typeof x === \"object\" ? x.key : x)).join(\".\") : validating}] ${e.message}`;\n\t\t})\n\t\t.join(\"; \");\n\n\treturn {\n\t\tmessage,\n\t\tissues: error,\n\t};\n}\n"],"mappings":";;;;;AAsBA,eAAsB,cACrB,SAMA,UAA+B,EAAE,EACH;CAC9B,MAAM,UAAU;EACf,MAAM,QAAQ;EACd,OAAO,QAAQ;EACf;AAID,KAAI,QAAQ,MAAM;EACjB,MAAM,SAAS,MAAM,QAAQ,KAAK,aAAa,SAAS,QAAQ,KAAK;AACrE,MAAI,OAAO,OACV,QAAO;GACN,MAAM;GACN,OAAO,UAAU,OAAO,QAAQ,OAAO;GACvC;AAEF,UAAQ,OAAO,OAAO;;AAGvB,KAAI,QAAQ,OAAO;EAClB,MAAM,SAAS,MAAM,QAAQ,MAAM,aAAa,SAAS,QAAQ,MAAM;AACvE,MAAI,OAAO,OACV,QAAO;GACN,MAAM;GACN,OAAO,UAAU,OAAO,QAAQ,QAAQ;GACxC;AAEF,UAAQ,QAAQ,OAAO;;AAExB,KAAI,QAAQ,kBAAkB,CAAC,QAAQ,QACtC,QAAO;EACN,MAAM;EACN,OAAO;GAAE,SAAS;GAAuB,QAAQ,EAAE;GAAE;EACrD;AAEF,KAAI,QAAQ,kBAAkB,CAAC,QAAQ,QACtC,QAAO;EACN,MAAM;EACN,OAAO;GAAE,SAAS;GAAuB,QAAQ,EAAE;GAAE;EACrD;AAEF,QAAO;EACN,MAAM;EACN,OAAO;EACP;;AAGF,SAAS,UACR,OACA,YACC;AAOD,QAAO;EACN,SAPe,MACd,KAAK,MAAM;AACX,UAAO,IAAI,EAAE,MAAM,SAAS,GAAG,WAAW,KAAK,EAAE,KAAK,KAAK,MAAO,OAAO,MAAM,WAAW,EAAE,MAAM,EAAG,CAAC,KAAK,IAAI,GAAG,WAAW,IAAI,EAAE;IAClI,CACD,KAAK,KAAK;EAIX,QAAQ;EACR"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-call",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"@types/body-parser": "^1.19.6",
|
|
17
17
|
"@types/express": "^5.0.3",
|
|
18
18
|
"@types/set-cookie-parser": "^2.4.10",
|
|
19
|
-
"@types/supertest": "^
|
|
19
|
+
"@types/supertest": "^7.2.0",
|
|
20
20
|
"body-parser": "^2.2.0",
|
|
21
21
|
"express": "^5.1.0",
|
|
22
22
|
"supertest": "^7.1.4"
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"./node": {
|
|
73
|
-
"dev-source": "./src/node.ts",
|
|
73
|
+
"dev-source": "./src/adapters/node/index.ts",
|
|
74
74
|
"import": {
|
|
75
75
|
"types": "./dist/node.d.mts",
|
|
76
76
|
"default": "./dist/node.mjs"
|