better-call 1.1.3 → 1.1.4

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.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as statusCodes, A as InferMiddlewareQuery, B as createInternalContext, C as InferBody, D as InferInputMethod, E as InferHeadersInput, F as InferRequest, G as parseCookies, H as CookieOptions, I as InferRequestInput, J as APIError, K as serializeCookie, L as InferUse, M as InferParamInput, N as InferQuery, O as InferMethod, P as InferQueryInput, Q as makeErrorForHideStackFrame, R as InputContext, S as HTTPMethod, T as InferHeaders, U as CookiePrefixOptions, V as StandardSchemaV1, W as getCookieKey, X as Status, Y as BetterCallError, Z as hideInternalStackFrames, _ as MiddlewareContext, a as EndpointBaseOptions, at as Prettify, b as MiddlewareResponse, c as EndpointOptions, d as OpenAPIParameter, et as HasRequiredKeys, f as OpenAPISchemaType, g as Middleware, h as getHTML, i as Endpoint, it as MergeObject, j as InferParam, k as InferMiddlewareBody, l as StrictEndpoint, m as generator, n as RouterConfig, nt as InferParamWildCard, o as EndpointBodyMethodOptions, ot as RequiredKeysOf, p as Path, q as serializeSignedCookie, r as createRouter, rt as IsEmptyObject, s as EndpointContext, st as UnionToIntersection, t as Router, tt as InferParamPath, u as createEndpoint, v as MiddlewareInputContext, w as InferBodyInput, x as createMiddleware, y as MiddlewareOptions, z as Method } from "./router.cjs";
1
+ import { $ as statusCodes, A as InferMiddlewareQuery, B as createInternalContext, C as InferBody, D as InferInputMethod, E as InferHeadersInput, F as InferRequest, G as serializeCookie, H as CookiePrefixOptions, I as InferRequestInput, J as BetterCallError, K as serializeSignedCookie, L as InferUse, M as InferParamInput, N as InferQuery, O as InferMethod, P as InferQueryInput, Q as makeErrorForHideStackFrame, R as InputContext, S as HTTPMethod, T as InferHeaders, U as getCookieKey, V as CookieOptions, W as parseCookies, X as ValidationError, Y as Status, Z as hideInternalStackFrames, _ as MiddlewareContext, a as EndpointBaseOptions, at as MergeObject, b as MiddlewareResponse, c as EndpointOptions, ct as UnionToIntersection, d as OpenAPIParameter, et as StandardSchemaV1, f as OpenAPISchemaType, g as Middleware, h as getHTML, i as Endpoint, it as IsEmptyObject, j as InferParam, k as InferMiddlewareBody, l as StrictEndpoint, m as generator, n as RouterConfig, nt as InferParamPath, o as EndpointBodyMethodOptions, ot as Prettify, p as Path, q as APIError, r as createRouter, rt as InferParamWildCard, s as EndpointContext, st as RequiredKeysOf, t as Router, tt as HasRequiredKeys, u as createEndpoint, v as MiddlewareInputContext, w as InferBodyInput, x as createMiddleware, y as MiddlewareOptions, z as Method } from "./router.cjs";
2
2
 
3
3
  //#region src/to-response.d.ts
4
4
  type JSONResponse = {
@@ -10,5 +10,5 @@ type JSONResponse = {
10
10
  };
11
11
  declare function toResponse(data?: any, init?: ResponseInit): Response;
12
12
  //#endregion
13
- export { 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, JSONResponse, 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, toResponse };
13
+ export { 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, JSONResponse, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, ValidationError, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes, toResponse };
14
14
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as statusCodes, A as InferMiddlewareQuery, B as createInternalContext, C as InferBody, D as InferInputMethod, E as InferHeadersInput, F as InferRequest, G as parseCookies, H as CookieOptions, I as InferRequestInput, J as APIError, K as serializeCookie, L as InferUse, M as InferParamInput, N as InferQuery, O as InferMethod, P as InferQueryInput, Q as makeErrorForHideStackFrame, R as InputContext, S as HTTPMethod, T as InferHeaders, U as CookiePrefixOptions, V as StandardSchemaV1, W as getCookieKey, X as Status, Y as BetterCallError, Z as hideInternalStackFrames, _ as MiddlewareContext, a as EndpointBaseOptions, at as Prettify, b as MiddlewareResponse, c as EndpointOptions, d as OpenAPIParameter, et as HasRequiredKeys, f as OpenAPISchemaType, g as Middleware, h as getHTML, i as Endpoint, it as MergeObject, j as InferParam, k as InferMiddlewareBody, l as StrictEndpoint, m as generator, n as RouterConfig, nt as InferParamWildCard, o as EndpointBodyMethodOptions, ot as RequiredKeysOf, p as Path, q as serializeSignedCookie, r as createRouter, rt as IsEmptyObject, s as EndpointContext, st as UnionToIntersection, t as Router, tt as InferParamPath, u as createEndpoint, v as MiddlewareInputContext, w as InferBodyInput, x as createMiddleware, y as MiddlewareOptions, z as Method } from "./router.js";
1
+ import { $ as statusCodes, A as InferMiddlewareQuery, B as createInternalContext, C as InferBody, D as InferInputMethod, E as InferHeadersInput, F as InferRequest, G as serializeCookie, H as CookiePrefixOptions, I as InferRequestInput, J as BetterCallError, K as serializeSignedCookie, L as InferUse, M as InferParamInput, N as InferQuery, O as InferMethod, P as InferQueryInput, Q as makeErrorForHideStackFrame, R as InputContext, S as HTTPMethod, T as InferHeaders, U as getCookieKey, V as CookieOptions, W as parseCookies, X as ValidationError, Y as Status, Z as hideInternalStackFrames, _ as MiddlewareContext, a as EndpointBaseOptions, at as MergeObject, b as MiddlewareResponse, c as EndpointOptions, ct as UnionToIntersection, d as OpenAPIParameter, et as StandardSchemaV1, f as OpenAPISchemaType, g as Middleware, h as getHTML, i as Endpoint, it as IsEmptyObject, j as InferParam, k as InferMiddlewareBody, l as StrictEndpoint, m as generator, n as RouterConfig, nt as InferParamPath, o as EndpointBodyMethodOptions, ot as Prettify, p as Path, q as APIError, r as createRouter, rt as InferParamWildCard, s as EndpointContext, st as RequiredKeysOf, t as Router, tt as HasRequiredKeys, u as createEndpoint, v as MiddlewareInputContext, w as InferBodyInput, x as createMiddleware, y as MiddlewareOptions, z as Method } from "./router.js";
2
2
 
3
3
  //#region src/to-response.d.ts
4
4
  type JSONResponse = {
@@ -10,5 +10,5 @@ type JSONResponse = {
10
10
  };
11
11
  declare function toResponse(data?: any, init?: ResponseInit): Response;
12
12
  //#endregion
13
- export { 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, JSONResponse, 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, toResponse };
13
+ export { 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, JSONResponse, MergeObject, Method, Middleware, MiddlewareContext, MiddlewareInputContext, MiddlewareOptions, MiddlewareResponse, OpenAPIParameter, OpenAPISchemaType, Path, Prettify, RequiredKeysOf, Router, RouterConfig, StandardSchemaV1, Status, StrictEndpoint, UnionToIntersection, ValidationError, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes, toResponse };
14
14
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -115,6 +115,17 @@ var InternalAPIError = class extends Error {
115
115
  } : void 0;
116
116
  }
117
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
+ };
118
129
  var BetterCallError = class extends Error {
119
130
  constructor(message) {
120
131
  super(message);
@@ -179,6 +190,19 @@ function tryDecode(str) {
179
190
  return str;
180
191
  }
181
192
  }
193
+ async function tryCatch(promise) {
194
+ try {
195
+ return {
196
+ data: await promise,
197
+ error: null
198
+ };
199
+ } catch (error) {
200
+ return {
201
+ data: null,
202
+ error
203
+ };
204
+ }
205
+ }
182
206
 
183
207
  //#endregion
184
208
  //#region src/to-response.ts
@@ -300,11 +324,17 @@ async function runValidation(options, context = {}) {
300
324
  }
301
325
  if (options.requireHeaders && !context.headers) return {
302
326
  data: null,
303
- error: { message: "Headers is required" }
327
+ error: {
328
+ message: "Headers is required",
329
+ issues: []
330
+ }
304
331
  };
305
332
  if (options.requireRequest && !context.request) return {
306
333
  data: null,
307
- error: { message: "Request is required" }
334
+ error: {
335
+ message: "Request is required",
336
+ issues: []
337
+ }
308
338
  };
309
339
  return {
310
340
  data: request,
@@ -312,12 +342,12 @@ async function runValidation(options, context = {}) {
312
342
  };
313
343
  }
314
344
  function fromError(error, validating) {
315
- const errorMessages = [];
316
- for (const issue of error) {
317
- const message = issue.message;
318
- errorMessages.push(message);
319
- }
320
- return { message: `Invalid ${validating} parameters` };
345
+ return {
346
+ message: error.map((e) => {
347
+ return `[${e.path?.length ? `${validating}.` + e.path.map((x) => typeof x === "object" ? x.key : x).join(".") : validating}] ${e.message}`;
348
+ }).join("; "),
349
+ issues: error
350
+ };
321
351
  }
322
352
 
323
353
  //#endregion
@@ -437,10 +467,7 @@ const createInternalContext = async (context, { options, path }) => {
437
467
  const headers = new Headers();
438
468
  let responseStatus = void 0;
439
469
  const { data, error } = await runValidation(options, context);
440
- if (error) throw new APIError(400, {
441
- message: error.message,
442
- code: "VALIDATION_ERROR"
443
- });
470
+ if (error) throw new ValidationError(error.message, error.issues);
444
471
  const requestHeaders = "headers" in context ? context.headers instanceof Headers ? context.headers : new Headers(context.headers) : "request" in context && context.request instanceof Request ? context.request.headers : null;
445
472
  const requestCookies = requestHeaders?.get("cookie");
446
473
  const parsedCookies = requestCookies ? parseCookies(requestCookies) : void 0;
@@ -538,10 +565,21 @@ function createEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
538
565
  if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
539
566
  const internalHandler = async (...inputCtx) => {
540
567
  const context = inputCtx[0] || {};
541
- const internalContext = await createInternalContext(context, {
568
+ const { data: internalContext, error: validationError } = await tryCatch(createInternalContext(context, {
542
569
  options,
543
570
  path
544
- });
571
+ }));
572
+ if (validationError) {
573
+ if (!(validationError instanceof ValidationError)) throw validationError;
574
+ if (options.onValidationError) await options.onValidationError({
575
+ message: validationError.message,
576
+ issues: validationError.issues
577
+ });
578
+ throw new APIError(400, {
579
+ message: validationError.message,
580
+ code: "VALIDATION_ERROR"
581
+ });
582
+ }
545
583
  const response = await handler(internalContext).catch(async (e) => {
546
584
  if (isAPIError(e)) {
547
585
  const onAPIError = options.onAPIError;
@@ -899,5 +937,5 @@ const createRouter = (endpoints, config) => {
899
937
  };
900
938
 
901
939
  //#endregion
902
- export { APIError, BetterCallError, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes, toResponse };
940
+ export { APIError, BetterCallError, ValidationError, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes, toResponse };
903
941
  //# sourceMappingURL=index.js.map