better-call 1.1.4 → 1.1.6

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/index.js CHANGED
@@ -1,140 +1,8 @@
1
+ import { a as makeErrorForHideStackFrame, i as hideInternalStackFrames, n as BetterCallError, o as statusCodes, r as ValidationError, t as APIError } from "./error2.js";
1
2
  import { getWebcryptoSubtle } from "@better-auth/utils";
2
3
  import { addRoute, createRouter as createRouter$1, findAllRoutes, findRoute } from "rou3";
3
4
  import { ZodObject, ZodOptional } from "zod";
4
5
 
5
- //#region src/error.ts
6
- function isErrorStackTraceLimitWritable() {
7
- const desc = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit");
8
- if (desc === void 0) return Object.isExtensible(Error);
9
- return Object.prototype.hasOwnProperty.call(desc, "writable") ? desc.writable : desc.set !== void 0;
10
- }
11
- /**
12
- * Hide internal stack frames from the error stack trace.
13
- */
14
- function hideInternalStackFrames(stack) {
15
- const lines = stack.split("\n at ");
16
- if (lines.length <= 1) return stack;
17
- lines.splice(1, 1);
18
- return lines.join("\n at ");
19
- }
20
- /**
21
- * Creates a custom error class that hides stack frames.
22
- */
23
- function makeErrorForHideStackFrame(Base, clazz) {
24
- class HideStackFramesError extends Base {
25
- #hiddenStack;
26
- constructor(...args) {
27
- if (isErrorStackTraceLimitWritable()) {
28
- const limit = Error.stackTraceLimit;
29
- Error.stackTraceLimit = 0;
30
- super(...args);
31
- Error.stackTraceLimit = limit;
32
- } else super(...args);
33
- const stack = (/* @__PURE__ */ new Error()).stack;
34
- if (stack) this.#hiddenStack = hideInternalStackFrames(stack.replace(/^Error/, this.name));
35
- }
36
- get errorStack() {
37
- return this.#hiddenStack;
38
- }
39
- }
40
- Object.defineProperty(HideStackFramesError.prototype, "constructor", {
41
- get() {
42
- return clazz;
43
- },
44
- enumerable: false,
45
- configurable: true
46
- });
47
- return HideStackFramesError;
48
- }
49
- const statusCodes = {
50
- OK: 200,
51
- CREATED: 201,
52
- ACCEPTED: 202,
53
- NO_CONTENT: 204,
54
- MULTIPLE_CHOICES: 300,
55
- MOVED_PERMANENTLY: 301,
56
- FOUND: 302,
57
- SEE_OTHER: 303,
58
- NOT_MODIFIED: 304,
59
- TEMPORARY_REDIRECT: 307,
60
- BAD_REQUEST: 400,
61
- UNAUTHORIZED: 401,
62
- PAYMENT_REQUIRED: 402,
63
- FORBIDDEN: 403,
64
- NOT_FOUND: 404,
65
- METHOD_NOT_ALLOWED: 405,
66
- NOT_ACCEPTABLE: 406,
67
- PROXY_AUTHENTICATION_REQUIRED: 407,
68
- REQUEST_TIMEOUT: 408,
69
- CONFLICT: 409,
70
- GONE: 410,
71
- LENGTH_REQUIRED: 411,
72
- PRECONDITION_FAILED: 412,
73
- PAYLOAD_TOO_LARGE: 413,
74
- URI_TOO_LONG: 414,
75
- UNSUPPORTED_MEDIA_TYPE: 415,
76
- RANGE_NOT_SATISFIABLE: 416,
77
- EXPECTATION_FAILED: 417,
78
- "I'M_A_TEAPOT": 418,
79
- MISDIRECTED_REQUEST: 421,
80
- UNPROCESSABLE_ENTITY: 422,
81
- LOCKED: 423,
82
- FAILED_DEPENDENCY: 424,
83
- TOO_EARLY: 425,
84
- UPGRADE_REQUIRED: 426,
85
- PRECONDITION_REQUIRED: 428,
86
- TOO_MANY_REQUESTS: 429,
87
- REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
88
- UNAVAILABLE_FOR_LEGAL_REASONS: 451,
89
- INTERNAL_SERVER_ERROR: 500,
90
- NOT_IMPLEMENTED: 501,
91
- BAD_GATEWAY: 502,
92
- SERVICE_UNAVAILABLE: 503,
93
- GATEWAY_TIMEOUT: 504,
94
- HTTP_VERSION_NOT_SUPPORTED: 505,
95
- VARIANT_ALSO_NEGOTIATES: 506,
96
- INSUFFICIENT_STORAGE: 507,
97
- LOOP_DETECTED: 508,
98
- NOT_EXTENDED: 510,
99
- NETWORK_AUTHENTICATION_REQUIRED: 511
100
- };
101
- var InternalAPIError = class extends Error {
102
- constructor(status = "INTERNAL_SERVER_ERROR", body = void 0, headers = {}, statusCode = typeof status === "number" ? status : statusCodes[status]) {
103
- super(body?.message, body?.cause ? { cause: body.cause } : void 0);
104
- this.status = status;
105
- this.body = body;
106
- this.headers = headers;
107
- this.statusCode = statusCode;
108
- this.name = "APIError";
109
- this.status = status;
110
- this.headers = headers;
111
- this.statusCode = statusCode;
112
- this.body = body ? {
113
- code: body?.message?.toUpperCase().replace(/ /g, "_").replace(/[^A-Z0-9_]/g, ""),
114
- ...body
115
- } : void 0;
116
- }
117
- };
118
- var ValidationError = class extends InternalAPIError {
119
- constructor(message, issues) {
120
- super(400, {
121
- message,
122
- code: "VALIDATION_ERROR"
123
- });
124
- this.message = message;
125
- this.issues = issues;
126
- this.issues = issues;
127
- }
128
- };
129
- var BetterCallError = class extends Error {
130
- constructor(message) {
131
- super(message);
132
- this.name = "BetterCallError";
133
- }
134
- };
135
- const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
136
-
137
- //#endregion
138
6
  //#region src/utils.ts
139
7
  const jsonContentTypeRegex = /^application\/([a-z0-9.+-]*\+)?json/i;
140
8
  async function getBody(request, allowedMediaTypes) {
@@ -563,6 +431,7 @@ function createEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
563
431
  const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
564
432
  const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
565
433
  if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
434
+ if (path && /\/{2,}/.test(path)) throw new BetterCallError("Path cannot contain consecutive slashes");
566
435
  const internalHandler = async (...inputCtx) => {
567
436
  const context = inputCtx[0] || {};
568
437
  const { data: internalContext, error: validationError } = await tryCatch(createInternalContext(context, {
@@ -861,7 +730,8 @@ const createRouter = (endpoints, config) => {
861
730
  if (config?.routerMiddleware?.length) for (const { path, middleware } of config.routerMiddleware) addRoute(middlewareRouter, "*", path, middleware);
862
731
  const processRequest = async (request) => {
863
732
  const url = new URL(request.url);
864
- const path = config?.basePath ? url.pathname.split(config.basePath).reduce((acc, curr, index) => {
733
+ const pathname = url.pathname;
734
+ const path = config?.basePath && config.basePath !== "/" ? pathname.split(config.basePath).reduce((acc, curr, index) => {
865
735
  if (index !== 0) if (index > 1) acc.push(`${config.basePath}${curr}`);
866
736
  else acc.push(curr);
867
737
  return acc;
@@ -870,7 +740,15 @@ const createRouter = (endpoints, config) => {
870
740
  status: 404,
871
741
  statusText: "Not Found"
872
742
  });
743
+ if (/\/{2,}/.test(path)) return new Response(null, {
744
+ status: 404,
745
+ statusText: "Not Found"
746
+ });
873
747
  const route = findRoute(router, request.method, path);
748
+ if (path.endsWith("/") !== route?.data?.path?.endsWith("/") && !config?.skipTrailingSlashes) return new Response(null, {
749
+ status: 404,
750
+ statusText: "Not Found"
751
+ });
874
752
  if (!route?.data) return new Response(null, {
875
753
  status: 404,
876
754
  statusText: "Not Found"