feat: add status and headers overwrite for HandlerContext.render (#725)
This commit is contained in:
+12
-2
@@ -28,6 +28,7 @@ import {
|
||||
MiddlewareRoute,
|
||||
Plugin,
|
||||
RenderFunction,
|
||||
RenderOptions,
|
||||
Route,
|
||||
RouteModule,
|
||||
UnknownPage,
|
||||
@@ -490,7 +491,7 @@ export class ServerContext {
|
||||
params: Record<string, string>,
|
||||
error?: unknown,
|
||||
) => {
|
||||
return async (data?: Data) => {
|
||||
return async (data?: Data, options?: RenderOptions) => {
|
||||
if (route.component === undefined) {
|
||||
throw new Error("This page does not have a component to render.");
|
||||
}
|
||||
@@ -538,7 +539,16 @@ export class ServerContext {
|
||||
headers["content-security-policy"] = directive;
|
||||
}
|
||||
}
|
||||
return new Response(body, { status, headers });
|
||||
return new Response(body, {
|
||||
status: options?.status ?? status,
|
||||
statusText: options?.statusText,
|
||||
headers: options?.headers
|
||||
? {
|
||||
...headers,
|
||||
...options.headers,
|
||||
}
|
||||
: headers,
|
||||
});
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
+7
-1
@@ -72,10 +72,16 @@ export interface RouteConfig {
|
||||
csp?: boolean;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-empty-interface
|
||||
export interface RenderOptions extends ResponseInit {}
|
||||
|
||||
export interface HandlerContext<Data = unknown, State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
params: Record<string, string>;
|
||||
render: (data?: Data) => Response | Promise<Response>;
|
||||
render: (
|
||||
data?: Data,
|
||||
options?: RenderOptions,
|
||||
) => Response | Promise<Response>;
|
||||
renderNotFound: () => Response | Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ import * as $27 from "./routes/not_found.ts";
|
||||
import * as $28 from "./routes/params.tsx";
|
||||
import * as $29 from "./routes/props/[id].tsx";
|
||||
import * as $30 from "./routes/static.tsx";
|
||||
import * as $31 from "./routes/wildcard.tsx";
|
||||
import * as $31 from "./routes/status_overwrite.tsx";
|
||||
import * as $32 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/ReturningNull.tsx";
|
||||
import * as $$2 from "./islands/RootFragment.tsx";
|
||||
@@ -76,7 +77,8 @@ const manifest = {
|
||||
"./routes/params.tsx": $28,
|
||||
"./routes/props/[id].tsx": $29,
|
||||
"./routes/static.tsx": $30,
|
||||
"./routes/wildcard.tsx": $31,
|
||||
"./routes/status_overwrite.tsx": $31,
|
||||
"./routes/wildcard.tsx": $32,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export default function Page() {
|
||||
return <div>This is HTML</div>;
|
||||
}
|
||||
|
||||
export const handler = {
|
||||
GET(req: Request, { render }: HandlerContext) {
|
||||
return render(undefined, {
|
||||
status: 401,
|
||||
headers: { "x-some-header": "foo" },
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -148,6 +148,18 @@ Deno.test("/intercept_args - GET html", async () => {
|
||||
assertStringIncludes(body, "<div>intercepted</div>");
|
||||
});
|
||||
|
||||
Deno.test("/status_overwrite", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/status_overwrite", {
|
||||
headers: { "accept": "text/html" },
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, Status.Unauthorized);
|
||||
assertEquals(resp.headers.get("x-some-header"), "foo");
|
||||
const body = await resp.text();
|
||||
assertStringIncludes(body, "<div>This is HTML</div>");
|
||||
});
|
||||
|
||||
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||
const resp = await router(
|
||||
new Request("https://fresh.deno.dev/api/get_only", {
|
||||
|
||||
Reference in New Issue
Block a user