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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user