better-call 1.0.25-beta.2 → 1.0.25

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 { APIError, CookieOptions, CookiePrefixOptions, Endpoint, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie } from "./router-BFJOSfmb.cjs";
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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie } from "./router-DeSuoHCx.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, CookieOptions, CookiePrefixOptions, Endpoint, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, toResponse };
14
14
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { APIError, CookieOptions, CookiePrefixOptions, Endpoint, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie } from "./router-D0KeMgM6.js";
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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie } from "./router-DRUBbubk.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, CookieOptions, CookiePrefixOptions, Endpoint, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, 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, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, toResponse };
14
14
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -114,6 +114,12 @@ var InternalAPIError = class extends Error {
114
114
  } : void 0;
115
115
  }
116
116
  };
117
+ var BetterCallError = class extends Error {
118
+ constructor(message) {
119
+ super(message);
120
+ this.name = "BetterCallError";
121
+ }
122
+ };
117
123
  const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
118
124
 
119
125
  //#endregion
@@ -408,19 +414,20 @@ const serializeSignedCookie = async (key, value, secret, opt) => {
408
414
  //#region src/context.ts
409
415
  const createInternalContext = async (context, { options, path }) => {
410
416
  const headers = new Headers();
417
+ let responseStatus = void 0;
411
418
  const { data, error } = await runValidation(options, context);
412
419
  if (error) throw new APIError(400, {
413
420
  message: error.message,
414
421
  code: "VALIDATION_ERROR"
415
422
  });
416
- 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;
417
- const requestCookies = requestHeaders?.get("cookie");
423
+ 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 : new Headers();
424
+ const requestCookies = requestHeaders.get("cookie");
418
425
  const parsedCookies = requestCookies ? parseCookies(requestCookies) : void 0;
419
426
  const internalContext = {
420
427
  ...context,
421
428
  body: data.body,
422
429
  query: data.query,
423
- path: context.path || path,
430
+ path,
424
431
  context: "context" in context && context.context ? context.context : {},
425
432
  returned: void 0,
426
433
  headers: context?.headers,
@@ -468,6 +475,9 @@ const createInternalContext = async (context, { options, path }) => {
468
475
  error: (status, body, headers$1) => {
469
476
  return new APIError(status, body, headers$1);
470
477
  },
478
+ setStatus: (status) => {
479
+ responseStatus = status;
480
+ },
471
481
  json: (json, routerResponse) => {
472
482
  if (!context.asResponse) return json;
473
483
  return {
@@ -476,7 +486,10 @@ const createInternalContext = async (context, { options, path }) => {
476
486
  _flag: "json"
477
487
  };
478
488
  },
479
- responseHeaders: headers
489
+ responseHeaders: headers,
490
+ get responseStatus() {
491
+ return responseStatus;
492
+ }
480
493
  };
481
494
  for (const middleware of options.use || []) {
482
495
  const response = await middleware({
@@ -495,43 +508,10 @@ const createInternalContext = async (context, { options, path }) => {
495
508
  return internalContext;
496
509
  };
497
510
 
498
- //#endregion
499
- //#region src/middleware.ts
500
- function createMiddleware(optionsOrHandler, handler) {
501
- const internalHandler = async (inputCtx) => {
502
- const context = inputCtx;
503
- const _handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handler;
504
- const internalContext = await createInternalContext(context, {
505
- options: typeof optionsOrHandler === "function" ? {} : optionsOrHandler,
506
- path: "/"
507
- });
508
- if (!_handler) throw new Error("handler must be defined");
509
- const response = await _handler(internalContext);
510
- const headers = internalContext.responseHeaders;
511
- return context.returnHeaders ? {
512
- headers,
513
- response
514
- } : response;
515
- };
516
- internalHandler.options = typeof optionsOrHandler === "function" ? {} : optionsOrHandler;
517
- return internalHandler;
518
- }
519
- createMiddleware.create = (opts) => {
520
- function fn(optionsOrHandler, handler) {
521
- if (typeof optionsOrHandler === "function") return createMiddleware({ use: opts?.use }, optionsOrHandler);
522
- if (!handler) throw new Error("Middleware handler is required");
523
- return createMiddleware({
524
- ...optionsOrHandler,
525
- method: "*",
526
- use: [...opts?.use || [], ...optionsOrHandler.use || []]
527
- }, handler);
528
- }
529
- return fn;
530
- };
531
-
532
511
  //#endregion
533
512
  //#region src/endpoint.ts
534
513
  const createEndpoint = (path, options, handler) => {
514
+ if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
535
515
  const internalHandler = async (...inputCtx) => {
536
516
  const context = inputCtx[0] || {};
537
517
  const internalContext = await createInternalContext(context, {
@@ -547,9 +527,20 @@ const createEndpoint = (path, options, handler) => {
547
527
  throw e;
548
528
  });
549
529
  const headers = internalContext.responseHeaders;
550
- return context.asResponse ? toResponse(response, { headers }) : context.returnHeaders ? {
530
+ const status = internalContext.responseStatus;
531
+ return context.asResponse ? toResponse(response, {
532
+ headers,
533
+ status
534
+ }) : context.returnHeaders ? context.returnStatus ? {
535
+ headers,
536
+ response,
537
+ status
538
+ } : {
551
539
  headers,
552
540
  response
541
+ } : context.returnStatus ? {
542
+ response,
543
+ status
553
544
  } : response;
554
545
  };
555
546
  internalHandler.options = options;
@@ -565,6 +556,40 @@ createEndpoint.create = (opts) => {
565
556
  };
566
557
  };
567
558
 
559
+ //#endregion
560
+ //#region src/middleware.ts
561
+ function createMiddleware(optionsOrHandler, handler) {
562
+ const internalHandler = async (inputCtx) => {
563
+ const context = inputCtx;
564
+ const _handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handler;
565
+ const internalContext = await createInternalContext(context, {
566
+ options: typeof optionsOrHandler === "function" ? {} : optionsOrHandler,
567
+ path: "/"
568
+ });
569
+ if (!_handler) throw new Error("handler must be defined");
570
+ const response = await _handler(internalContext);
571
+ const headers = internalContext.responseHeaders;
572
+ return context.returnHeaders ? {
573
+ headers,
574
+ response
575
+ } : response;
576
+ };
577
+ internalHandler.options = typeof optionsOrHandler === "function" ? {} : optionsOrHandler;
578
+ return internalHandler;
579
+ }
580
+ createMiddleware.create = (opts) => {
581
+ function fn(optionsOrHandler, handler) {
582
+ if (typeof optionsOrHandler === "function") return createMiddleware({ use: opts?.use }, optionsOrHandler);
583
+ if (!handler) throw new Error("Middleware handler is required");
584
+ return createMiddleware({
585
+ ...optionsOrHandler,
586
+ method: "*",
587
+ use: [...opts?.use || [], ...optionsOrHandler.use || []]
588
+ }, handler);
589
+ }
590
+ return fn;
591
+ };
592
+
568
593
  //#endregion
569
594
  //#region node_modules/.pnpm/zod@4.0.1/node_modules/zod/v4/core/core.js
570
595
  /** A special constant with type `never` */
@@ -2517,5 +2542,5 @@ const createRouter = (endpoints, config$1) => {
2517
2542
  };
2518
2543
 
2519
2544
  //#endregion
2520
- export { APIError, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, toResponse };
2545
+ export { APIError, BetterCallError, _statusCode, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, toResponse };
2521
2546
  //# sourceMappingURL=index.js.map