better-call 1.0.27 → 1.0.28-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.
- package/dist/client.d.cts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/index.cjs +48 -187
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +34 -173
- package/dist/index.js.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/{router-DxWRTWmk.d.cts → router-BV-cToj2.d.cts} +31 -1
- package/dist/{router-D1f_-c2B.d.ts → router-CuwMJjp1.d.ts} +31 -1
- package/dist/utils-C1kQcShE.cjs +3 -0
- package/dist/utils-Cotdrp_v.js +174 -0
- package/dist/utils-Cotdrp_v.js.map +1 -0
- package/dist/utils-JxxvmTPm.cjs +222 -0
- package/dist/utils-JxxvmTPm.cjs.map +1 -0
- package/dist/utils-yiQwA3Vf.js +3 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIError, BetterCallError, CookieOptions, CookiePrefixOptions, Endpoint, EndpointBaseOptions, EndpointBodyMethodOptions, EndpointContext, EndpointOptions, HTTPMethod, HasRequiredKeys, InferBody, InferBodyInput, InferHeaders, InferHeadersInput, InferInputMethod, InferMethod, InferMiddlewareBody, InferMiddlewareQuery, InferParam, InferParamInput, InferParamPath, InferParamWildCard, InferQuery, InferQueryInput, InferRequest, InferRequestInput, InferUse, InputContext, IsEmptyObject, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-
|
|
1
|
+
import { APIError, BetterCallError, CookieOptions, CookiePrefixOptions, Endpoint, EndpointBaseOptions, EndpointBodyMethodOptions, EndpointContext, EndpointOptions, HTTPMethod, HasRequiredKeys, InferBody, InferBodyInput, InferHeaders, InferHeadersInput, InferInputMethod, InferMethod, InferMiddlewareBody, InferMiddlewareQuery, InferParam, InferParamInput, InferParamPath, InferParamWildCard, InferQuery, InferQueryInput, InferRequest, InferRequestInput, InferUse, InputContext, IsEmptyObject, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-BV-cToj2.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/to-response.d.ts
|
|
4
4
|
type JSONResponse = {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIError, BetterCallError, CookieOptions, CookiePrefixOptions, Endpoint, EndpointBaseOptions, EndpointBodyMethodOptions, EndpointContext, EndpointOptions, HTTPMethod, HasRequiredKeys, InferBody, InferBodyInput, InferHeaders, InferHeadersInput, InferInputMethod, InferMethod, InferMiddlewareBody, InferMiddlewareQuery, InferParam, InferParamInput, InferParamPath, InferParamWildCard, InferQuery, InferQueryInput, InferRequest, InferRequestInput, InferUse, InputContext, IsEmptyObject, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-
|
|
1
|
+
import { APIError, BetterCallError, CookieOptions, CookiePrefixOptions, Endpoint, EndpointBaseOptions, EndpointBodyMethodOptions, EndpointContext, EndpointOptions, HTTPMethod, HasRequiredKeys, InferBody, InferBodyInput, InferHeaders, InferHeadersInput, InferInputMethod, InferMethod, InferMiddlewareBody, InferMiddlewareQuery, InferParam, InferParamInput, InferParamPath, InferParamWildCard, InferQuery, InferQueryInput, InferRequest, InferRequestInput, InferUse, InputContext, IsEmptyObject, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-CuwMJjp1.js";
|
|
2
2
|
|
|
3
3
|
//#region src/to-response.d.ts
|
|
4
4
|
type JSONResponse = {
|
package/dist/index.js
CHANGED
|
@@ -1,167 +1,7 @@
|
|
|
1
|
+
import { APIError, BetterCallError, getBody, hideInternalStackFrames, isAPIError, makeErrorForHideStackFrame, statusCodes, tryDecode } from "./utils-Cotdrp_v.js";
|
|
1
2
|
import { getWebcryptoSubtle } from "@better-auth/utils";
|
|
2
3
|
import { addRoute, createRouter as createRouter$1, findAllRoutes, findRoute } from "rou3";
|
|
3
4
|
|
|
4
|
-
//#region src/error.ts
|
|
5
|
-
function isErrorStackTraceLimitWritable() {
|
|
6
|
-
const desc = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit");
|
|
7
|
-
if (desc === void 0) return Object.isExtensible(Error);
|
|
8
|
-
return Object.prototype.hasOwnProperty.call(desc, "writable") ? desc.writable : desc.set !== void 0;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Hide internal stack frames from the error stack trace.
|
|
12
|
-
*/
|
|
13
|
-
function hideInternalStackFrames(stack) {
|
|
14
|
-
const lines = stack.split("\n at ");
|
|
15
|
-
if (lines.length <= 1) return stack;
|
|
16
|
-
lines.splice(1, 1);
|
|
17
|
-
return lines.join("\n at ");
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Creates a custom error class that hides stack frames.
|
|
21
|
-
*/
|
|
22
|
-
function makeErrorForHideStackFrame(Base, clazz) {
|
|
23
|
-
class HideStackFramesError extends Base {
|
|
24
|
-
#hiddenStack;
|
|
25
|
-
constructor(...args) {
|
|
26
|
-
if (isErrorStackTraceLimitWritable()) {
|
|
27
|
-
const limit = Error.stackTraceLimit;
|
|
28
|
-
Error.stackTraceLimit = 0;
|
|
29
|
-
super(...args);
|
|
30
|
-
Error.stackTraceLimit = limit;
|
|
31
|
-
} else super(...args);
|
|
32
|
-
const stack = (/* @__PURE__ */ new Error()).stack;
|
|
33
|
-
if (stack) this.#hiddenStack = hideInternalStackFrames(stack.replace(/^Error/, this.name));
|
|
34
|
-
}
|
|
35
|
-
get errorStack() {
|
|
36
|
-
return this.#hiddenStack;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
Object.defineProperty(HideStackFramesError.prototype, "constructor", {
|
|
40
|
-
get() {
|
|
41
|
-
return clazz;
|
|
42
|
-
},
|
|
43
|
-
enumerable: false,
|
|
44
|
-
configurable: true
|
|
45
|
-
});
|
|
46
|
-
return HideStackFramesError;
|
|
47
|
-
}
|
|
48
|
-
const statusCodes = {
|
|
49
|
-
OK: 200,
|
|
50
|
-
CREATED: 201,
|
|
51
|
-
ACCEPTED: 202,
|
|
52
|
-
NO_CONTENT: 204,
|
|
53
|
-
MULTIPLE_CHOICES: 300,
|
|
54
|
-
MOVED_PERMANENTLY: 301,
|
|
55
|
-
FOUND: 302,
|
|
56
|
-
SEE_OTHER: 303,
|
|
57
|
-
NOT_MODIFIED: 304,
|
|
58
|
-
TEMPORARY_REDIRECT: 307,
|
|
59
|
-
BAD_REQUEST: 400,
|
|
60
|
-
UNAUTHORIZED: 401,
|
|
61
|
-
PAYMENT_REQUIRED: 402,
|
|
62
|
-
FORBIDDEN: 403,
|
|
63
|
-
NOT_FOUND: 404,
|
|
64
|
-
METHOD_NOT_ALLOWED: 405,
|
|
65
|
-
NOT_ACCEPTABLE: 406,
|
|
66
|
-
PROXY_AUTHENTICATION_REQUIRED: 407,
|
|
67
|
-
REQUEST_TIMEOUT: 408,
|
|
68
|
-
CONFLICT: 409,
|
|
69
|
-
GONE: 410,
|
|
70
|
-
LENGTH_REQUIRED: 411,
|
|
71
|
-
PRECONDITION_FAILED: 412,
|
|
72
|
-
PAYLOAD_TOO_LARGE: 413,
|
|
73
|
-
URI_TOO_LONG: 414,
|
|
74
|
-
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
75
|
-
RANGE_NOT_SATISFIABLE: 416,
|
|
76
|
-
EXPECTATION_FAILED: 417,
|
|
77
|
-
"I'M_A_TEAPOT": 418,
|
|
78
|
-
MISDIRECTED_REQUEST: 421,
|
|
79
|
-
UNPROCESSABLE_ENTITY: 422,
|
|
80
|
-
LOCKED: 423,
|
|
81
|
-
FAILED_DEPENDENCY: 424,
|
|
82
|
-
TOO_EARLY: 425,
|
|
83
|
-
UPGRADE_REQUIRED: 426,
|
|
84
|
-
PRECONDITION_REQUIRED: 428,
|
|
85
|
-
TOO_MANY_REQUESTS: 429,
|
|
86
|
-
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
|
|
87
|
-
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
|
|
88
|
-
INTERNAL_SERVER_ERROR: 500,
|
|
89
|
-
NOT_IMPLEMENTED: 501,
|
|
90
|
-
BAD_GATEWAY: 502,
|
|
91
|
-
SERVICE_UNAVAILABLE: 503,
|
|
92
|
-
GATEWAY_TIMEOUT: 504,
|
|
93
|
-
HTTP_VERSION_NOT_SUPPORTED: 505,
|
|
94
|
-
VARIANT_ALSO_NEGOTIATES: 506,
|
|
95
|
-
INSUFFICIENT_STORAGE: 507,
|
|
96
|
-
LOOP_DETECTED: 508,
|
|
97
|
-
NOT_EXTENDED: 510,
|
|
98
|
-
NETWORK_AUTHENTICATION_REQUIRED: 511
|
|
99
|
-
};
|
|
100
|
-
var InternalAPIError = class extends Error {
|
|
101
|
-
constructor(status = "INTERNAL_SERVER_ERROR", body = void 0, headers = {}, statusCode = typeof status === "number" ? status : statusCodes[status]) {
|
|
102
|
-
super(body?.message, body?.cause ? { cause: body.cause } : void 0);
|
|
103
|
-
this.status = status;
|
|
104
|
-
this.body = body;
|
|
105
|
-
this.headers = headers;
|
|
106
|
-
this.statusCode = statusCode;
|
|
107
|
-
this.name = "APIError";
|
|
108
|
-
this.status = status;
|
|
109
|
-
this.headers = headers;
|
|
110
|
-
this.statusCode = statusCode;
|
|
111
|
-
this.body = body ? {
|
|
112
|
-
code: body?.message?.toUpperCase().replace(/ /g, "_").replace(/[^A-Z0-9_]/g, ""),
|
|
113
|
-
...body
|
|
114
|
-
} : void 0;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
var BetterCallError = class extends Error {
|
|
118
|
-
constructor(message) {
|
|
119
|
-
super(message);
|
|
120
|
-
this.name = "BetterCallError";
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
|
|
124
|
-
|
|
125
|
-
//#endregion
|
|
126
|
-
//#region src/utils.ts
|
|
127
|
-
async function getBody(request) {
|
|
128
|
-
const contentType = request.headers.get("content-type") || "";
|
|
129
|
-
if (!request.body) return;
|
|
130
|
-
if (contentType.includes("application/json")) return await request.json();
|
|
131
|
-
if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
132
|
-
const formData = await request.formData();
|
|
133
|
-
const result = {};
|
|
134
|
-
formData.forEach((value, key) => {
|
|
135
|
-
result[key] = value.toString();
|
|
136
|
-
});
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
if (contentType.includes("multipart/form-data")) {
|
|
140
|
-
const formData = await request.formData();
|
|
141
|
-
const result = {};
|
|
142
|
-
formData.forEach((value, key) => {
|
|
143
|
-
result[key] = value;
|
|
144
|
-
});
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
if (contentType.includes("text/plain")) return await request.text();
|
|
148
|
-
if (contentType.includes("application/octet-stream")) return await request.arrayBuffer();
|
|
149
|
-
if (contentType.includes("application/pdf") || contentType.includes("image/") || contentType.includes("video/")) return await request.blob();
|
|
150
|
-
if (contentType.includes("application/stream") || request.body instanceof ReadableStream) return request.body;
|
|
151
|
-
return await request.text();
|
|
152
|
-
}
|
|
153
|
-
function isAPIError(error) {
|
|
154
|
-
return error instanceof APIError || error?.name === "APIError";
|
|
155
|
-
}
|
|
156
|
-
function tryDecode(str) {
|
|
157
|
-
try {
|
|
158
|
-
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
159
|
-
} catch {
|
|
160
|
-
return str;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
//#endregion
|
|
165
5
|
//#region src/to-response.ts
|
|
166
6
|
function isJSONSerializable(value) {
|
|
167
7
|
if (value === void 0) return false;
|
|
@@ -415,6 +255,26 @@ const serializeSignedCookie = async (key, value, secret, opt) => {
|
|
|
415
255
|
const createInternalContext = async (context, { options, path }) => {
|
|
416
256
|
const headers = new Headers();
|
|
417
257
|
let responseStatus = void 0;
|
|
258
|
+
if (context.request && !context.body && !options.disableBody) {
|
|
259
|
+
const { getBody: getBody$1 } = await import("./utils-yiQwA3Vf.js");
|
|
260
|
+
const allowedContentTypes = options.metadata?.allowedContentTypes;
|
|
261
|
+
context.body = await getBody$1(context.request, allowedContentTypes);
|
|
262
|
+
} else if (context.request && options.metadata?.allowedContentTypes) {
|
|
263
|
+
const allowedContentTypes = options.metadata.allowedContentTypes;
|
|
264
|
+
if (allowedContentTypes.length > 0) {
|
|
265
|
+
const contentType = context.request.headers.get("content-type") || "";
|
|
266
|
+
if (contentType && context.request.body) {
|
|
267
|
+
if (!allowedContentTypes.some((allowed) => {
|
|
268
|
+
const normalizedContentType = contentType.toLowerCase().split(";")[0].trim();
|
|
269
|
+
const normalizedAllowed = allowed.toLowerCase().trim();
|
|
270
|
+
return normalizedContentType === normalizedAllowed || normalizedContentType.includes(normalizedAllowed);
|
|
271
|
+
})) throw new APIError(415, {
|
|
272
|
+
message: `Content-Type "${contentType}" is not allowed. Allowed types: ${allowedContentTypes.join(", ")}`,
|
|
273
|
+
code: "UNSUPPORTED_MEDIA_TYPE"
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
418
278
|
const { data, error } = await runValidation(options, context);
|
|
419
279
|
if (error) throw new APIError(400, {
|
|
420
280
|
message: error.message,
|
|
@@ -2488,19 +2348,20 @@ const createRouter = (endpoints, config$1) => {
|
|
|
2488
2348
|
else query[key] = value;
|
|
2489
2349
|
});
|
|
2490
2350
|
const handler = route.data;
|
|
2491
|
-
const context = {
|
|
2492
|
-
path,
|
|
2493
|
-
method: request.method,
|
|
2494
|
-
headers: request.headers,
|
|
2495
|
-
params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
|
|
2496
|
-
request,
|
|
2497
|
-
body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request),
|
|
2498
|
-
query,
|
|
2499
|
-
_flag: "router",
|
|
2500
|
-
asResponse: true,
|
|
2501
|
-
context: config$1?.routerContext
|
|
2502
|
-
};
|
|
2503
2351
|
try {
|
|
2352
|
+
const allowedContentTypes = handler.options.metadata?.allowedContentTypes || config$1?.allowedContentTypes;
|
|
2353
|
+
const context = {
|
|
2354
|
+
path,
|
|
2355
|
+
method: request.method,
|
|
2356
|
+
headers: request.headers,
|
|
2357
|
+
params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
|
|
2358
|
+
request,
|
|
2359
|
+
body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request, allowedContentTypes),
|
|
2360
|
+
query,
|
|
2361
|
+
_flag: "router",
|
|
2362
|
+
asResponse: true,
|
|
2363
|
+
context: config$1?.routerContext
|
|
2364
|
+
};
|
|
2504
2365
|
const middlewareRoutes = findAllRoutes(middlewareRouter, "*", path);
|
|
2505
2366
|
if (middlewareRoutes?.length) for (const { data: middleware, params } of middlewareRoutes) {
|
|
2506
2367
|
const res = await middleware({
|