Update API routes function

This commit is contained in:
Benjamin Toby 2026-04-10 11:34:22 +01:00
parent 7fb1784b95
commit 6477f446d1
5 changed files with 56 additions and 28 deletions

View File

@ -28,8 +28,8 @@ export default async function ({ req }) {
const contentLength = req.headers.get("content-length"); const contentLength = req.headers.get("content-length");
if (contentLength) { if (contentLength) {
const size = parseInt(contentLength, 10); const size = parseInt(contentLength, 10);
if ((config?.maxRequestBodyMB && if ((config?.max_request_body_mb &&
size > config.maxRequestBodyMB * MBInBytes) || size > config.max_request_body_mb * MBInBytes) ||
size > ServerDefaultRequestBodyLimitBytes) { size > ServerDefaultRequestBodyLimitBytes) {
return Response.json({ return Response.json({
success: false, success: false,
@ -42,11 +42,18 @@ export default async function ({ req }) {
}); });
} }
} }
const res = await module["default"]({ const target_module = (module["default"] ||
module["handler"]);
const res = await target_module?.({
...routeParams, ...routeParams,
}); });
if (is_dev) { if (res instanceof Response) {
res.headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); if (is_dev) {
res.headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
}
return res;
} }
return res; return Response.json(res, {
...(res.bunext_api_route_res_options || undefined),
});
} }

12
dist/types/index.d.ts vendored
View File

@ -121,11 +121,7 @@ export type APIResponseObject<T extends {
redirect?: string; redirect?: string;
}; };
export type BunextServerRouteConfig = { export type BunextServerRouteConfig = {
maxRequestBodyMB?: number; max_request_body_mb?: number;
};
export type PageDistGenParams = {
pageName: string;
page_file: string;
}; };
export type LivePageDistGenParams = { export type LivePageDistGenParams = {
component?: FC; component?: FC;
@ -299,3 +295,9 @@ export type GrabTSXModuleBatchMap = {
tsx: string; tsx: string;
page_file_path: string; page_file_path: string;
}; };
export type BunextAPIRouteHandler<T extends BunextAPIRouteJSONRes = BunextAPIRouteJSONRes & {
[k: string]: any;
}> = (params: BunxRouteParams) => Promise<Response | T> | Response | T;
export type BunextAPIRouteJSONRes = {
bunext_api_route_res_options?: ResponseInit;
};

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"version": "1.0.63", "version": "1.0.64",
"main": "dist/index.js", "main": "dist/index.js",
"module": "index.ts", "module": "index.ts",
"dependencies": { "dependencies": {

View File

@ -1,5 +1,8 @@
import type { Server } from "bun"; import type {
import type { BunextServerRouteConfig, BunxRouteParams } from "../../types"; BunextAPIRouteHandler,
BunextServerRouteConfig,
BunxRouteParams,
} from "../../types";
import grabRouteParams from "../../utils/grab-route-params"; import grabRouteParams from "../../utils/grab-route-params";
import grabConstants from "../../utils/grab-constants"; import grabConstants from "../../utils/grab-constants";
import grabRouter from "../../utils/grab-router"; import grabRouter from "../../utils/grab-router";
@ -50,8 +53,8 @@ export default async function ({ req }: Params): Promise<Response> {
const size = parseInt(contentLength, 10); const size = parseInt(contentLength, 10);
if ( if (
(config?.maxRequestBodyMB && (config?.max_request_body_mb &&
size > config.maxRequestBodyMB * MBInBytes) || size > config.max_request_body_mb * MBInBytes) ||
size > ServerDefaultRequestBodyLimitBytes size > ServerDefaultRequestBodyLimitBytes
) { ) {
return Response.json( return Response.json(
@ -69,13 +72,24 @@ export default async function ({ req }: Params): Promise<Response> {
} }
} }
const res: Response = await module["default"]({ const target_module = (module["default"] ||
...routeParams, module["handler"]) as BunextAPIRouteHandler;
} as BunxRouteParams);
if (is_dev) { const res = await target_module?.({
res.headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); ...routeParams,
});
if (res instanceof Response) {
if (is_dev) {
res.headers.set(
"Cache-Control",
"no-cache, no-store, must-revalidate",
);
}
return res;
} }
return res; return Response.json(res, {
...(res.bunext_api_route_res_options || undefined),
});
} }

View File

@ -142,12 +142,7 @@ export type APIResponseObject<
}; };
export type BunextServerRouteConfig = { export type BunextServerRouteConfig = {
maxRequestBodyMB?: number; max_request_body_mb?: number;
};
export type PageDistGenParams = {
pageName: string;
page_file: string;
}; };
export type LivePageDistGenParams = { export type LivePageDistGenParams = {
@ -344,3 +339,13 @@ export type GrabTSXModuleBatchMap = {
tsx: string; tsx: string;
page_file_path: string; page_file_path: string;
}; };
export type BunextAPIRouteHandler<
T extends BunextAPIRouteJSONRes = BunextAPIRouteJSONRes & {
[k: string]: any;
},
> = (params: BunxRouteParams) => Promise<Response | T> | Response | T;
export type BunextAPIRouteJSONRes = {
bunext_api_route_res_options?: ResponseInit;
};