better-call 1.0.28-beta.1 → 1.0.28
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 +87 -0
- package/dist/client.d.cts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/index.cjs +188 -37
- 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 +174 -23
- package/dist/index.js.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/{router-BV-cToj2.d.cts → router-NaFkuy-s.d.ts} +10 -10
- package/dist/{router-CuwMJjp1.d.ts → router-rGV6mTr8.d.cts} +10 -10
- package/package.json +1 -1
- package/dist/utils-C1kQcShE.cjs +0 -3
- package/dist/utils-Cotdrp_v.js +0 -174
- package/dist/utils-Cotdrp_v.js.map +0 -1
- package/dist/utils-JxxvmTPm.cjs +0 -222
- package/dist/utils-JxxvmTPm.cjs.map +0 -1
- package/dist/utils-yiQwA3Vf.js +0 -3
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-rGV6mTr8.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-NaFkuy-s.js";
|
|
2
2
|
|
|
3
3
|
//#region src/to-response.d.ts
|
|
4
4
|
type JSONResponse = {
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,178 @@
|
|
|
1
|
-
import { APIError, BetterCallError, getBody, hideInternalStackFrames, isAPIError, makeErrorForHideStackFrame, statusCodes, tryDecode } from "./utils-Cotdrp_v.js";
|
|
2
1
|
import { getWebcryptoSubtle } from "@better-auth/utils";
|
|
3
2
|
import { addRoute, createRouter as createRouter$1, findAllRoutes, findRoute } from "rou3";
|
|
4
3
|
|
|
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, allowedMediaTypes) {
|
|
128
|
+
const contentType = request.headers.get("content-type") || "";
|
|
129
|
+
const normalizedContentType = contentType.toLowerCase();
|
|
130
|
+
if (!request.body) return;
|
|
131
|
+
if (allowedMediaTypes && allowedMediaTypes.length > 0) {
|
|
132
|
+
if (!allowedMediaTypes.some((allowed) => {
|
|
133
|
+
const normalizedContentTypeBase = normalizedContentType.split(";")[0].trim();
|
|
134
|
+
const normalizedAllowed = allowed.toLowerCase().trim();
|
|
135
|
+
return normalizedContentTypeBase === normalizedAllowed || normalizedContentTypeBase.includes(normalizedAllowed);
|
|
136
|
+
})) throw new APIError(415, {
|
|
137
|
+
message: `Content-Type "${contentType}" is not allowed. Allowed types: ${allowedMediaTypes.join(", ")}`,
|
|
138
|
+
code: "UNSUPPORTED_MEDIA_TYPE"
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (normalizedContentType.includes("application/json")) return await request.json();
|
|
142
|
+
if (normalizedContentType.includes("application/x-www-form-urlencoded")) {
|
|
143
|
+
const formData = await request.formData();
|
|
144
|
+
const result = {};
|
|
145
|
+
formData.forEach((value, key) => {
|
|
146
|
+
result[key] = value.toString();
|
|
147
|
+
});
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
if (normalizedContentType.includes("multipart/form-data")) {
|
|
151
|
+
const formData = await request.formData();
|
|
152
|
+
const result = {};
|
|
153
|
+
formData.forEach((value, key) => {
|
|
154
|
+
result[key] = value;
|
|
155
|
+
});
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
if (normalizedContentType.includes("text/plain")) return await request.text();
|
|
159
|
+
if (normalizedContentType.includes("application/octet-stream")) return await request.arrayBuffer();
|
|
160
|
+
if (normalizedContentType.includes("application/pdf") || normalizedContentType.includes("image/") || normalizedContentType.includes("video/")) return await request.blob();
|
|
161
|
+
if (normalizedContentType.includes("application/stream") || request.body instanceof ReadableStream) return request.body;
|
|
162
|
+
return await request.text();
|
|
163
|
+
}
|
|
164
|
+
function isAPIError(error) {
|
|
165
|
+
return error instanceof APIError || error?.name === "APIError";
|
|
166
|
+
}
|
|
167
|
+
function tryDecode(str) {
|
|
168
|
+
try {
|
|
169
|
+
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
170
|
+
} catch {
|
|
171
|
+
return str;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
5
176
|
//#region src/to-response.ts
|
|
6
177
|
function isJSONSerializable(value) {
|
|
7
178
|
if (value === void 0) return false;
|
|
@@ -255,26 +426,6 @@ const serializeSignedCookie = async (key, value, secret, opt) => {
|
|
|
255
426
|
const createInternalContext = async (context, { options, path }) => {
|
|
256
427
|
const headers = new Headers();
|
|
257
428
|
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
|
-
}
|
|
278
429
|
const { data, error } = await runValidation(options, context);
|
|
279
430
|
if (error) throw new APIError(400, {
|
|
280
431
|
message: error.message,
|
|
@@ -2349,14 +2500,14 @@ const createRouter = (endpoints, config$1) => {
|
|
|
2349
2500
|
});
|
|
2350
2501
|
const handler = route.data;
|
|
2351
2502
|
try {
|
|
2352
|
-
const
|
|
2503
|
+
const allowedMediaTypes = handler.options.metadata?.allowedMediaTypes || config$1?.allowedMediaTypes;
|
|
2353
2504
|
const context = {
|
|
2354
2505
|
path,
|
|
2355
2506
|
method: request.method,
|
|
2356
2507
|
headers: request.headers,
|
|
2357
2508
|
params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
|
|
2358
2509
|
request,
|
|
2359
|
-
body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request,
|
|
2510
|
+
body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request, allowedMediaTypes),
|
|
2360
2511
|
query,
|
|
2361
2512
|
_flag: "router",
|
|
2362
2513
|
asResponse: true,
|