import type { BunextPageModuleServerReturn, BunxRouteParams } from "../types"; type Params = { serverRes: BunextPageModuleServerReturn; routeParams?: BunxRouteParams; }; /** * Builds the redirect Response for a server function that returned * `redirect`. An explicit `status_code` wins; otherwise `permanent` picks * 301, and the default is 302. Headers from `response_options` (e.g. a * Set-Cookie on login) and `res_transform` apply to the redirect too. */ export default async function grabRedirectResponse({ serverRes, routeParams, }: Params): Promise { const redirect = serverRes.redirect!; const status = redirect.status_code || (redirect.permanent ? 301 : 302); const headers = new Headers(serverRes.response_options?.headers); headers.set("Location", redirect.destination); const res = new Response(null, { status, headers }); // Same precedence as HTML responses if (routeParams?.res_transform) { return await routeParams.res_transform(res); } if (serverRes.res_transform) { return await serverRes.res_transform(res); } return res; }