feat: add ctx.renderNotFound() to explicitly render 404 template (#481)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Steven Yung
2022-08-09 19:23:17 +00:00
committed by GitHub
co-authored by Luca Casonato
parent b169d18ecb
commit 49092ed83a
6 changed files with 70 additions and 10 deletions
+5 -2
View File
@@ -448,6 +448,8 @@ export class ServerContext {
};
};
const createUnknownRender = genRender(this.#notFound, Status.NotFound);
for (const route of this.#routes) {
const createRender = genRender(route, Status.OK);
if (typeof route.handler === "function") {
@@ -456,6 +458,7 @@ export class ServerContext {
...ctx,
params,
render: createRender(req, params),
renderNotFound: createUnknownRender(req, {}),
});
} else {
for (const [method, handler] of Object.entries(route.handler)) {
@@ -464,12 +467,12 @@ export class ServerContext {
...ctx,
params,
render: createRender(req, params),
renderNotFound: createUnknownRender(req, {}),
});
}
}
}
const unknownHandlerRender = genRender(this.#notFound, Status.NotFound);
const unknownHandler: router.Handler<RouterState> = (
req,
ctx,
@@ -478,7 +481,7 @@ export class ServerContext {
req,
{
...ctx,
render: unknownHandlerRender(req, {}),
render: createUnknownRender(req, {}),
},
);
+1
View File
@@ -65,6 +65,7 @@ export interface HandlerContext<Data = unknown, State = Record<string, unknown>>
extends ConnInfo {
params: Record<string, string>;
render: (data?: Data) => Response | Promise<Response>;
renderNotFound: () => Response | Promise<Response>;
state: State;
}