refactor: use named status codes from std internally (#417)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Abdulaziz
2022-07-30 15:35:31 +00:00
committed by GitHub
co-authored by Luca Casonato
parent ba20756c17
commit 1dc1f87fc3
6 changed files with 46 additions and 40 deletions
+8 -5
View File
@@ -4,6 +4,7 @@ import {
fromFileUrl,
RequestHandler,
router,
Status,
toFileUrl,
typeByExtension,
walk,
@@ -32,7 +33,6 @@ import {
import { render as internalRender } from "./render.tsx";
import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts";
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
interface RouterState {
state: Record<string, unknown>;
}
@@ -278,7 +278,7 @@ export class ServerContext {
const url = new URL(req.url);
if (url.pathname.length > 1 && url.pathname.endsWith("/")) {
url.pathname = url.pathname.slice(0, -1);
return Response.redirect(url.href, 307);
return Response.redirect(url.href, Status.TemporaryRedirect);
}
return withMiddlewares(req, connInfo, inner);
};
@@ -449,7 +449,7 @@ export class ServerContext {
};
for (const route of this.#routes) {
const createRender = genRender(route, 200);
const createRender = genRender(route, Status.OK);
if (typeof route.handler === "function") {
routes[route.pattern] = (req, ctx, params) =>
(route.handler as Handler)(req, {
@@ -469,7 +469,7 @@ export class ServerContext {
}
}
const unknownHandlerRender = genRender(this.#notFound, 404);
const unknownHandlerRender = genRender(this.#notFound, Status.NotFound);
const unknownHandler: router.Handler<RouterState> = (
req,
ctx,
@@ -482,7 +482,10 @@ export class ServerContext {
},
);
const errorHandlerRender = genRender(this.#error, 500);
const errorHandlerRender = genRender(
this.#error,
Status.InternalServerError,
);
const errorHandler: router.ErrorHandler<RouterState> = (
req,
ctx,
+1
View File
@@ -11,6 +11,7 @@ export type {
Handler as RequestHandler,
ServeInit,
} from "https://deno.land/std@0.150.0/http/server.ts";
export { Status } from "https://deno.land/std@0.150.0/http/http_status.ts";
export {
typeByExtension,
} from "https://deno.land/std@0.150.0/media_types/mod.ts";
+1
View File
@@ -1,5 +1,6 @@
import { ServerContext } from "./context.ts";
import { serve } from "./deps.ts";
export { Status } from "./deps.ts";
import {
AppModule,
ErrorPageModule,