better-call 1.0.28 → 1.1.0-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/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-rGV6mTr8.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, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-DguGh6Qa.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-NaFkuy-s.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, createEndpoint, createInternalContext, createMiddleware, createRouter, generator, getCookieKey, getHTML, hideInternalStackFrames, makeErrorForHideStackFrame, parseCookies, serializeCookie, serializeSignedCookie, statusCodes } from "./router-EvGJRGFD.js";
2
2
 
3
3
  //#region src/to-response.d.ts
4
4
  type JSONResponse = {
package/dist/index.js CHANGED
@@ -133,10 +133,16 @@ async function getBody(request, allowedMediaTypes) {
133
133
  const normalizedContentTypeBase = normalizedContentType.split(";")[0].trim();
134
134
  const normalizedAllowed = allowed.toLowerCase().trim();
135
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
- });
136
+ })) {
137
+ if (!normalizedContentType) throw new APIError(415, {
138
+ message: `Content-Type is required. Allowed types: ${allowedMediaTypes.join(", ")}`,
139
+ code: "UNSUPPORTED_MEDIA_TYPE"
140
+ });
141
+ throw new APIError(415, {
142
+ message: `Content-Type "${contentType}" is not allowed. Allowed types: ${allowedMediaTypes.join(", ")}`,
143
+ code: "UNSUPPORTED_MEDIA_TYPE"
144
+ });
145
+ }
140
146
  }
141
147
  if (normalizedContentType.includes("application/json")) return await request.json();
142
148
  if (normalizedContentType.includes("application/x-www-form-urlencoded")) {
@@ -212,12 +218,14 @@ function toResponse(data, init) {
212
218
  const body$1 = data.body;
213
219
  const routerResponse = data.routerResponse;
214
220
  if (routerResponse instanceof Response) return routerResponse;
215
- const headers$1 = new Headers({
216
- ...routerResponse?.headers,
217
- ...data.headers,
218
- ...init?.headers,
219
- "Content-Type": "application/json"
220
- });
221
+ const headers$1 = new Headers();
222
+ if (routerResponse?.headers) {
223
+ const headers$2 = new Headers(routerResponse.headers);
224
+ for (const [key, value] of headers$2.entries()) headers$2.set(key, value);
225
+ }
226
+ if (data.headers) for (const [key, value] of new Headers(data.headers).entries()) headers$1.set(key, value);
227
+ if (init?.headers) for (const [key, value] of new Headers(init.headers).entries()) headers$1.set(key, value);
228
+ headers$1.set("Content-Type", "application/json");
221
229
  return new Response(JSON.stringify(body$1), {
222
230
  ...routerResponse,
223
231
  headers: headers$1,
@@ -438,7 +446,7 @@ const createInternalContext = async (context, { options, path }) => {
438
446
  ...context,
439
447
  body: data.body,
440
448
  query: data.query,
441
- path: context.path || path,
449
+ path: context.path || path || "virtual:",
442
450
  context: "context" in context && context.context ? context.context : {},
443
451
  returned: void 0,
444
452
  headers: context?.headers,
@@ -521,7 +529,10 @@ const createInternalContext = async (context, { options, path }) => {
521
529
 
522
530
  //#endregion
523
531
  //#region src/endpoint.ts
524
- const createEndpoint = (path, options, handler) => {
532
+ function createEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
533
+ const path = typeof pathOrOptions === "string" ? pathOrOptions : void 0;
534
+ const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
535
+ const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
525
536
  if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new BetterCallError("Body is not allowed with GET or HEAD methods");
526
537
  const internalHandler = async (...inputCtx) => {
527
538
  const context = inputCtx[0] || {};
@@ -557,7 +568,7 @@ const createEndpoint = (path, options, handler) => {
557
568
  internalHandler.options = options;
558
569
  internalHandler.path = path;
559
570
  return internalHandler;
560
- };
571
+ }
561
572
  createEndpoint.create = (opts) => {
562
573
  return (path, options, handler) => {
563
574
  return createEndpoint(path, {
@@ -2381,7 +2392,7 @@ async function generator(endpoints, config$1) {
2381
2392
  const components = { schemas: {} };
2382
2393
  Object.entries(endpoints).forEach(([_, value]) => {
2383
2394
  const options = value.options;
2384
- if (options.metadata?.SERVER_ONLY) return;
2395
+ if (!value.path || options.metadata?.SERVER_ONLY) return;
2385
2396
  if (options.method === "GET") paths[value.path] = { get: {
2386
2397
  tags: ["Default", ...options.metadata?.openapi?.tags || []],
2387
2398
  description: options.metadata?.openapi?.description,
@@ -2470,7 +2481,7 @@ const createRouter = (endpoints, config$1) => {
2470
2481
  const router = createRouter$1();
2471
2482
  const middlewareRouter = createRouter$1();
2472
2483
  for (const endpoint of Object.values(endpoints)) {
2473
- if (!endpoint.options) continue;
2484
+ if (!endpoint.options || !endpoint.path) continue;
2474
2485
  if (endpoint.options?.metadata?.SERVER_ONLY) continue;
2475
2486
  const methods = Array.isArray(endpoint.options?.method) ? endpoint.options.method : [endpoint.options?.method];
2476
2487
  for (const method of methods) addRoute(router, method, endpoint.path, endpoint);