better-call 1.0.26 → 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 CHANGED
@@ -1,4 +1,4 @@
1
- import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-CeSGPJlX.cjs";
1
+ import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-BV-cToj2.cjs";
2
2
  import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
3
3
 
4
4
  //#region src/client.d.ts
package/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-BH6Wndse.js";
1
+ import { Endpoint, HasRequiredKeys, Router, UnionToIntersection } from "./router-CuwMJjp1.js";
2
2
  import { BetterFetchOption, BetterFetchResponse } from "@better-fetch/fetch";
3
3
 
4
4
  //#region src/client.d.ts
package/dist/index.cjs CHANGED
@@ -1,170 +1,10 @@
1
1
  const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ const require_utils = require('./utils-JxxvmTPm.cjs');
2
3
  let __better_auth_utils = require("@better-auth/utils");
3
4
  __better_auth_utils = require_chunk.__toESM(__better_auth_utils);
4
5
  let rou3 = require("rou3");
5
6
  rou3 = require_chunk.__toESM(rou3);
6
7
 
7
- //#region src/error.ts
8
- function isErrorStackTraceLimitWritable() {
9
- const desc = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit");
10
- if (desc === void 0) return Object.isExtensible(Error);
11
- return Object.prototype.hasOwnProperty.call(desc, "writable") ? desc.writable : desc.set !== void 0;
12
- }
13
- /**
14
- * Hide internal stack frames from the error stack trace.
15
- */
16
- function hideInternalStackFrames(stack) {
17
- const lines = stack.split("\n at ");
18
- if (lines.length <= 1) return stack;
19
- lines.splice(1, 1);
20
- return lines.join("\n at ");
21
- }
22
- /**
23
- * Creates a custom error class that hides stack frames.
24
- */
25
- function makeErrorForHideStackFrame(Base, clazz) {
26
- class HideStackFramesError extends Base {
27
- #hiddenStack;
28
- constructor(...args) {
29
- if (isErrorStackTraceLimitWritable()) {
30
- const limit = Error.stackTraceLimit;
31
- Error.stackTraceLimit = 0;
32
- super(...args);
33
- Error.stackTraceLimit = limit;
34
- } else super(...args);
35
- const stack = (/* @__PURE__ */ new Error()).stack;
36
- if (stack) this.#hiddenStack = hideInternalStackFrames(stack.replace(/^Error/, this.name));
37
- }
38
- get errorStack() {
39
- return this.#hiddenStack;
40
- }
41
- }
42
- Object.defineProperty(HideStackFramesError.prototype, "constructor", {
43
- get() {
44
- return clazz;
45
- },
46
- enumerable: false,
47
- configurable: true
48
- });
49
- return HideStackFramesError;
50
- }
51
- const _statusCode = {
52
- OK: 200,
53
- CREATED: 201,
54
- ACCEPTED: 202,
55
- NO_CONTENT: 204,
56
- MULTIPLE_CHOICES: 300,
57
- MOVED_PERMANENTLY: 301,
58
- FOUND: 302,
59
- SEE_OTHER: 303,
60
- NOT_MODIFIED: 304,
61
- TEMPORARY_REDIRECT: 307,
62
- BAD_REQUEST: 400,
63
- UNAUTHORIZED: 401,
64
- PAYMENT_REQUIRED: 402,
65
- FORBIDDEN: 403,
66
- NOT_FOUND: 404,
67
- METHOD_NOT_ALLOWED: 405,
68
- NOT_ACCEPTABLE: 406,
69
- PROXY_AUTHENTICATION_REQUIRED: 407,
70
- REQUEST_TIMEOUT: 408,
71
- CONFLICT: 409,
72
- GONE: 410,
73
- LENGTH_REQUIRED: 411,
74
- PRECONDITION_FAILED: 412,
75
- PAYLOAD_TOO_LARGE: 413,
76
- URI_TOO_LONG: 414,
77
- UNSUPPORTED_MEDIA_TYPE: 415,
78
- RANGE_NOT_SATISFIABLE: 416,
79
- EXPECTATION_FAILED: 417,
80
- "I'M_A_TEAPOT": 418,
81
- MISDIRECTED_REQUEST: 421,
82
- UNPROCESSABLE_ENTITY: 422,
83
- LOCKED: 423,
84
- FAILED_DEPENDENCY: 424,
85
- TOO_EARLY: 425,
86
- UPGRADE_REQUIRED: 426,
87
- PRECONDITION_REQUIRED: 428,
88
- TOO_MANY_REQUESTS: 429,
89
- REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
90
- UNAVAILABLE_FOR_LEGAL_REASONS: 451,
91
- INTERNAL_SERVER_ERROR: 500,
92
- NOT_IMPLEMENTED: 501,
93
- BAD_GATEWAY: 502,
94
- SERVICE_UNAVAILABLE: 503,
95
- GATEWAY_TIMEOUT: 504,
96
- HTTP_VERSION_NOT_SUPPORTED: 505,
97
- VARIANT_ALSO_NEGOTIATES: 506,
98
- INSUFFICIENT_STORAGE: 507,
99
- LOOP_DETECTED: 508,
100
- NOT_EXTENDED: 510,
101
- NETWORK_AUTHENTICATION_REQUIRED: 511
102
- };
103
- var InternalAPIError = class extends Error {
104
- constructor(status = "INTERNAL_SERVER_ERROR", body = void 0, headers = {}, statusCode = typeof status === "number" ? status : _statusCode[status]) {
105
- super(body?.message, body?.cause ? { cause: body.cause } : void 0);
106
- this.status = status;
107
- this.body = body;
108
- this.headers = headers;
109
- this.statusCode = statusCode;
110
- this.name = "APIError";
111
- this.status = status;
112
- this.headers = headers;
113
- this.statusCode = statusCode;
114
- this.body = body ? {
115
- code: body?.message?.toUpperCase().replace(/ /g, "_").replace(/[^A-Z0-9_]/g, ""),
116
- ...body
117
- } : void 0;
118
- }
119
- };
120
- var BetterCallError = class extends Error {
121
- constructor(message) {
122
- super(message);
123
- this.name = "BetterCallError";
124
- }
125
- };
126
- const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
127
-
128
- //#endregion
129
- //#region src/utils.ts
130
- async function getBody(request) {
131
- const contentType = request.headers.get("content-type") || "";
132
- if (!request.body) return;
133
- if (contentType.includes("application/json")) return await request.json();
134
- if (contentType.includes("application/x-www-form-urlencoded")) {
135
- const formData = await request.formData();
136
- const result = {};
137
- formData.forEach((value, key) => {
138
- result[key] = value.toString();
139
- });
140
- return result;
141
- }
142
- if (contentType.includes("multipart/form-data")) {
143
- const formData = await request.formData();
144
- const result = {};
145
- formData.forEach((value, key) => {
146
- result[key] = value;
147
- });
148
- return result;
149
- }
150
- if (contentType.includes("text/plain")) return await request.text();
151
- if (contentType.includes("application/octet-stream")) return await request.arrayBuffer();
152
- if (contentType.includes("application/pdf") || contentType.includes("image/") || contentType.includes("video/")) return await request.blob();
153
- if (contentType.includes("application/stream") || request.body instanceof ReadableStream) return request.body;
154
- return await request.text();
155
- }
156
- function isAPIError(error) {
157
- return error instanceof APIError || error?.name === "APIError";
158
- }
159
- function tryDecode(str) {
160
- try {
161
- return str.includes("%") ? decodeURIComponent(str) : str;
162
- } catch {
163
- return str;
164
- }
165
- }
166
-
167
- //#endregion
168
8
  //#region src/to-response.ts
169
9
  function isJSONSerializable(value) {
170
10
  if (value === void 0) return false;
@@ -217,7 +57,7 @@ function toResponse(data, init) {
217
57
  statusText: init?.statusText ?? routerResponse?.statusText
218
58
  });
219
59
  }
220
- if (isAPIError(data)) return toResponse(data.body, {
60
+ if (require_utils.isAPIError(data)) return toResponse(data.body, {
221
61
  status: init?.status ?? data.statusCode,
222
62
  statusText: data.status.toString(),
223
63
  headers: init?.headers || data.headers
@@ -368,7 +208,7 @@ function parseCookies(str) {
368
208
  if (!cookies.has(key)) {
369
209
  let val = str.slice(eqIdx + 1, endIdx).trim();
370
210
  if (val.codePointAt(0) === 34) val = val.slice(1, -1);
371
- cookies.set(key, tryDecode(val));
211
+ cookies.set(key, require_utils.tryDecode(val));
372
212
  }
373
213
  index = endIdx + 1;
374
214
  }
@@ -418,8 +258,28 @@ const serializeSignedCookie = async (key, value, secret, opt) => {
418
258
  const createInternalContext = async (context, { options, path }) => {
419
259
  const headers = new Headers();
420
260
  let responseStatus = void 0;
261
+ if (context.request && !context.body && !options.disableBody) {
262
+ const { getBody: getBody$1 } = await Promise.resolve().then(() => require("./utils-C1kQcShE.cjs"));
263
+ const allowedContentTypes = options.metadata?.allowedContentTypes;
264
+ context.body = await getBody$1(context.request, allowedContentTypes);
265
+ } else if (context.request && options.metadata?.allowedContentTypes) {
266
+ const allowedContentTypes = options.metadata.allowedContentTypes;
267
+ if (allowedContentTypes.length > 0) {
268
+ const contentType = context.request.headers.get("content-type") || "";
269
+ if (contentType && context.request.body) {
270
+ if (!allowedContentTypes.some((allowed) => {
271
+ const normalizedContentType = contentType.toLowerCase().split(";")[0].trim();
272
+ const normalizedAllowed = allowed.toLowerCase().trim();
273
+ return normalizedContentType === normalizedAllowed || normalizedContentType.includes(normalizedAllowed);
274
+ })) throw new require_utils.APIError(415, {
275
+ message: `Content-Type "${contentType}" is not allowed. Allowed types: ${allowedContentTypes.join(", ")}`,
276
+ code: "UNSUPPORTED_MEDIA_TYPE"
277
+ });
278
+ }
279
+ }
280
+ }
421
281
  const { data, error } = await runValidation(options, context);
422
- if (error) throw new APIError(400, {
282
+ if (error) throw new require_utils.APIError(400, {
423
283
  message: error.message,
424
284
  code: "VALIDATION_ERROR"
425
285
  });
@@ -473,10 +333,10 @@ const createInternalContext = async (context, { options, path }) => {
473
333
  },
474
334
  redirect: (url) => {
475
335
  headers.set("location", url);
476
- return new APIError("FOUND", void 0, headers);
336
+ return new require_utils.APIError("FOUND", void 0, headers);
477
337
  },
478
338
  error: (status, body, headers$1) => {
479
- return new APIError(status, body, headers$1);
339
+ return new require_utils.APIError(status, body, headers$1);
480
340
  },
481
341
  setStatus: (status) => {
482
342
  responseStatus = status;
@@ -514,7 +374,7 @@ const createInternalContext = async (context, { options, path }) => {
514
374
  //#endregion
515
375
  //#region src/endpoint.ts
516
376
  const createEndpoint = (path, options, handler) => {
517
- if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
377
+ if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new require_utils.BetterCallError("Body is not allowed with GET or HEAD methods");
518
378
  const internalHandler = async (...inputCtx) => {
519
379
  const context = inputCtx[0] || {};
520
380
  const internalContext = await createInternalContext(context, {
@@ -522,7 +382,7 @@ const createEndpoint = (path, options, handler) => {
522
382
  path
523
383
  });
524
384
  const response = await handler(internalContext).catch(async (e) => {
525
- if (isAPIError(e)) {
385
+ if (require_utils.isAPIError(e)) {
526
386
  const onAPIError = options.onAPIError;
527
387
  if (onAPIError) await onAPIError(e);
528
388
  if (context.asResponse) return e;
@@ -2491,19 +2351,20 @@ const createRouter = (endpoints, config$1) => {
2491
2351
  else query[key] = value;
2492
2352
  });
2493
2353
  const handler = route.data;
2494
- const context = {
2495
- path,
2496
- method: request.method,
2497
- headers: request.headers,
2498
- params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
2499
- request,
2500
- body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request),
2501
- query,
2502
- _flag: "router",
2503
- asResponse: true,
2504
- context: config$1?.routerContext
2505
- };
2506
2354
  try {
2355
+ const allowedContentTypes = handler.options.metadata?.allowedContentTypes || config$1?.allowedContentTypes;
2356
+ const context = {
2357
+ path,
2358
+ method: request.method,
2359
+ headers: request.headers,
2360
+ params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
2361
+ request,
2362
+ body: handler.options.disableBody ? void 0 : await require_utils.getBody(handler.options.cloneRequest ? request.clone() : request, allowedContentTypes),
2363
+ query,
2364
+ _flag: "router",
2365
+ asResponse: true,
2366
+ context: config$1?.routerContext
2367
+ };
2507
2368
  const middlewareRoutes = (0, rou3.findAllRoutes)(middlewareRouter, "*", path);
2508
2369
  if (middlewareRoutes?.length) for (const { data: middleware, params } of middlewareRoutes) {
2509
2370
  const res = await middleware({
@@ -2519,11 +2380,11 @@ const createRouter = (endpoints, config$1) => {
2519
2380
  const errorResponse = await config$1.onError(error);
2520
2381
  if (errorResponse instanceof Response) return toResponse(errorResponse);
2521
2382
  } catch (error$1) {
2522
- if (isAPIError(error$1)) return toResponse(error$1);
2383
+ if (require_utils.isAPIError(error$1)) return toResponse(error$1);
2523
2384
  throw error$1;
2524
2385
  }
2525
2386
  if (config$1?.throwError) throw error;
2526
- if (isAPIError(error)) return toResponse(error);
2387
+ if (require_utils.isAPIError(error)) return toResponse(error);
2527
2388
  console.error(`# SERVER_ERROR: `, error);
2528
2389
  return new Response(null, {
2529
2390
  status: 500,
@@ -2545,9 +2406,8 @@ const createRouter = (endpoints, config$1) => {
2545
2406
  };
2546
2407
 
2547
2408
  //#endregion
2548
- exports.APIError = APIError;
2549
- exports.BetterCallError = BetterCallError;
2550
- exports._statusCode = _statusCode;
2409
+ exports.APIError = require_utils.APIError;
2410
+ exports.BetterCallError = require_utils.BetterCallError;
2551
2411
  exports.createEndpoint = createEndpoint;
2552
2412
  exports.createInternalContext = createInternalContext;
2553
2413
  exports.createMiddleware = createMiddleware;
@@ -2555,10 +2415,11 @@ exports.createRouter = createRouter;
2555
2415
  exports.generator = generator;
2556
2416
  exports.getCookieKey = getCookieKey;
2557
2417
  exports.getHTML = getHTML;
2558
- exports.hideInternalStackFrames = hideInternalStackFrames;
2559
- exports.makeErrorForHideStackFrame = makeErrorForHideStackFrame;
2418
+ exports.hideInternalStackFrames = require_utils.hideInternalStackFrames;
2419
+ exports.makeErrorForHideStackFrame = require_utils.makeErrorForHideStackFrame;
2560
2420
  exports.parseCookies = parseCookies;
2561
2421
  exports.serializeCookie = serializeCookie;
2562
2422
  exports.serializeSignedCookie = serializeSignedCookie;
2423
+ exports.statusCodes = require_utils.statusCodes;
2563
2424
  exports.toResponse = toResponse;
2564
2425
  //# sourceMappingURL=index.cjs.map