From 5a0972beb8ff815b1d96e11db86aa07cb48d6f62 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Fri, 10 Apr 2026 11:54:17 +0100 Subject: [PATCH] Update API routes function pass #3 --- dist/functions/server/handle-routes.js | 9 +++++++- dist/types/index.d.ts | 8 ++++--- package.json | 2 +- src/functions/server/handle-routes.ts | 18 ++++++++++++--- src/types/index.ts | 31 +++++++++++++++++--------- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/dist/functions/server/handle-routes.js b/dist/functions/server/handle-routes.js index c7e1ffd..a584311 100644 --- a/dist/functions/server/handle-routes.js +++ b/dist/functions/server/handle-routes.js @@ -55,9 +55,16 @@ export default async function ({ req }) { return res; } if (res) { - return Response.json(_.omit(res, "bunext_api_route_res_options"), { + let final_res = Response.json(_.omit(res, [ + "bunext_api_route_res_options", + "bunext_api_route_res_transform_fn", + ]), { ...(res.bunext_api_route_res_options || undefined), }); + if (res.bunext_api_route_res_transform_fn) { + final_res = await res.bunext_api_route_res_transform_fn(final_res); + } + return final_res; } return undefined; } diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 055e097..6731400 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -295,11 +295,13 @@ export type GrabTSXModuleBatchMap = { tsx: string; page_file_path: string; }; -export type BunextAPIRouteHandler = (params: BunxRouteParams) => Promise)> | Response | (T & Pick); export type BunextAPIRouteJSONRes = { bunext_api_route_res_options?: ResponseInit; + bunext_api_route_res_transform_fn?: (res: Response) => Promise | Response; } & { [k: string]: any; }; +export type BunextAPIRouteHandlerObjectReturn = Response | (T & Pick); +export type BunextAPIRouteHandler = (params: BunxRouteParams) => Promise> | Response | BunextAPIRouteHandlerObjectReturn; diff --git a/package.json b/package.json index 6a67dc3..ec19db9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.68", + "version": "1.0.69", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/functions/server/handle-routes.ts b/src/functions/server/handle-routes.ts index fbb60eb..5708d7a 100644 --- a/src/functions/server/handle-routes.ts +++ b/src/functions/server/handle-routes.ts @@ -91,9 +91,21 @@ export default async function ({ req }: Params): Promise { } if (res) { - return Response.json(_.omit(res, "bunext_api_route_res_options"), { - ...(res.bunext_api_route_res_options || undefined), - }); + let final_res = Response.json( + _.omit(res, [ + "bunext_api_route_res_options", + "bunext_api_route_res_transform_fn", + ]), + { + ...(res.bunext_api_route_res_options || undefined), + }, + ); + + if (res.bunext_api_route_res_transform_fn) { + final_res = await res.bunext_api_route_res_transform_fn(final_res); + } + + return final_res; } return undefined; diff --git a/src/types/index.ts b/src/types/index.ts index 2675c1d..02b986a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -340,6 +340,24 @@ export type GrabTSXModuleBatchMap = { page_file_path: string; }; +export type BunextAPIRouteJSONRes = { + bunext_api_route_res_options?: ResponseInit; + bunext_api_route_res_transform_fn?: ( + res: Response, + ) => Promise | Response; +} & { + [k: string]: any; +}; + +export type BunextAPIRouteHandlerObjectReturn = + | Response + | (T & + Pick< + BunextAPIRouteJSONRes, + | "bunext_api_route_res_options" + | "bunext_api_route_res_transform_fn" + >); + export type BunextAPIRouteHandler< T extends BunextAPIRouteJSONRes = { [k: string]: any; @@ -347,15 +365,6 @@ export type BunextAPIRouteHandler< > = ( params: BunxRouteParams, ) => - | Promise< - | Response - | (T & Pick) - > + | Promise> | Response - | (T & Pick); - -export type BunextAPIRouteJSONRes = { - bunext_api_route_res_options?: ResponseInit; -} & { - [k: string]: any; -}; + | BunextAPIRouteHandlerObjectReturn;