This commit is contained in:
Rizky 2024-12-11 10:15:24 +00:00
parent 99e5baeefb
commit 09a819dcf1
3 changed files with 17 additions and 6 deletions

View File

@ -70,7 +70,15 @@ export const useServerRouter = <T extends ReturnType<typeof newServerRouter>>(
}
return {
async handle(arg: ServerContext | SessionContext<any>) {
async handle(
arg: ServerContext | SessionContext<any>,
opt?: {
rewrite?: (arg: {
body: Bun.BodyInit;
headers?: Record<string, string>;
}) => Bun.BodyInit;
}
) {
const { url, req, handle } = arg;
const found = findRoute(rou, undefined, url.pathname);
if (found) {
@ -101,7 +109,7 @@ export const useServerRouter = <T extends ReturnType<typeof newServerRouter>>(
return new Response(JSON.stringify(result));
}
return handle(req);
return handle(req, opt);
},
};
};

View File

@ -9,7 +9,10 @@ export type SessionServerHandler = {
handle: (
arg: ServerContext,
opt?: {
rewrite?: (arg: { body: Bun.BodyInit; headers: Headers }) => Bun.BodyInit;
rewrite?: (arg: {
body: Bun.BodyInit;
headers?: Record<string, string>;
}) => Bun.BodyInit;
}
) => Promise<Response>;
};
@ -39,12 +42,12 @@ export const initSessionServer = <T>(
};
if (url.pathname.startsWith("/_session/")) {
const res = await session_router.handle(route_arg);
const res = await session_router.handle(route_arg, opt);
if (res) return res;
}
if (arg.router) {
const res = await arg.router.handle(route_arg);
const res = await arg.router.handle(route_arg, opt);
if (res) return res;
}

View File

@ -76,7 +76,7 @@ export type ServerContext = {
opt?: {
rewrite?: (arg: {
body: Bun.BodyInit;
headers: Response["headers"];
headers?: Record<string, string>;
}) => Bun.BodyInit;
}
) => Promise<Response>;