better-call 1.0.27 → 1.0.28-beta.2

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-DxWRTWmk.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-rGV6mTr8.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-D1f_-c2B.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-NaFkuy-s.js";
2
2
 
3
3
  //#region src/to-response.d.ts
4
4
  type JSONResponse = {
package/dist/index.js CHANGED
@@ -124,11 +124,22 @@ const APIError = makeErrorForHideStackFrame(InternalAPIError, Error);
124
124
 
125
125
  //#endregion
126
126
  //#region src/utils.ts
127
- async function getBody(request) {
127
+ async function getBody(request, allowedMediaTypes) {
128
128
  const contentType = request.headers.get("content-type") || "";
129
+ const normalizedContentType = contentType.toLowerCase();
129
130
  if (!request.body) return;
130
- if (contentType.includes("application/json")) return await request.json();
131
- if (contentType.includes("application/x-www-form-urlencoded")) {
131
+ if (allowedMediaTypes && allowedMediaTypes.length > 0) {
132
+ if (!allowedMediaTypes.some((allowed) => {
133
+ const normalizedContentTypeBase = normalizedContentType.split(";")[0].trim();
134
+ const normalizedAllowed = allowed.toLowerCase().trim();
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
+ });
140
+ }
141
+ if (normalizedContentType.includes("application/json")) return await request.json();
142
+ if (normalizedContentType.includes("application/x-www-form-urlencoded")) {
132
143
  const formData = await request.formData();
133
144
  const result = {};
134
145
  formData.forEach((value, key) => {
@@ -136,7 +147,7 @@ async function getBody(request) {
136
147
  });
137
148
  return result;
138
149
  }
139
- if (contentType.includes("multipart/form-data")) {
150
+ if (normalizedContentType.includes("multipart/form-data")) {
140
151
  const formData = await request.formData();
141
152
  const result = {};
142
153
  formData.forEach((value, key) => {
@@ -144,10 +155,10 @@ async function getBody(request) {
144
155
  });
145
156
  return result;
146
157
  }
147
- if (contentType.includes("text/plain")) return await request.text();
148
- if (contentType.includes("application/octet-stream")) return await request.arrayBuffer();
149
- if (contentType.includes("application/pdf") || contentType.includes("image/") || contentType.includes("video/")) return await request.blob();
150
- if (contentType.includes("application/stream") || request.body instanceof ReadableStream) return request.body;
158
+ if (normalizedContentType.includes("text/plain")) return await request.text();
159
+ if (normalizedContentType.includes("application/octet-stream")) return await request.arrayBuffer();
160
+ if (normalizedContentType.includes("application/pdf") || normalizedContentType.includes("image/") || normalizedContentType.includes("video/")) return await request.blob();
161
+ if (normalizedContentType.includes("application/stream") || request.body instanceof ReadableStream) return request.body;
151
162
  return await request.text();
152
163
  }
153
164
  function isAPIError(error) {
@@ -2488,19 +2499,20 @@ const createRouter = (endpoints, config$1) => {
2488
2499
  else query[key] = value;
2489
2500
  });
2490
2501
  const handler = route.data;
2491
- const context = {
2492
- path,
2493
- method: request.method,
2494
- headers: request.headers,
2495
- params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
2496
- request,
2497
- body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request),
2498
- query,
2499
- _flag: "router",
2500
- asResponse: true,
2501
- context: config$1?.routerContext
2502
- };
2503
2502
  try {
2503
+ const allowedMediaTypes = handler.options.metadata?.allowedMediaTypes || config$1?.allowedMediaTypes;
2504
+ const context = {
2505
+ path,
2506
+ method: request.method,
2507
+ headers: request.headers,
2508
+ params: route.params ? JSON.parse(JSON.stringify(route.params)) : {},
2509
+ request,
2510
+ body: handler.options.disableBody ? void 0 : await getBody(handler.options.cloneRequest ? request.clone() : request, allowedMediaTypes),
2511
+ query,
2512
+ _flag: "router",
2513
+ asResponse: true,
2514
+ context: config$1?.routerContext
2515
+ };
2504
2516
  const middlewareRoutes = findAllRoutes(middlewareRouter, "*", path);
2505
2517
  if (middlewareRoutes?.length) for (const { data: middleware, params } of middlewareRoutes) {
2506
2518
  const res = await middleware({