From e0a1809452030e0d7a3716f1249006df717817b8 Mon Sep 17 00:00:00 2001 From: Lautaro Suarez <67484166+lausuarez02@users.noreply.github.com> Date: Wed, 28 Jun 2023 22:13:59 +0200 Subject: [PATCH] chore: simplify ext -> content type mapping code (#1368) --- www/routes/raw.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/www/routes/raw.ts b/www/routes/raw.ts index eaf6ef5a..a0f08145 100644 --- a/www/routes/raw.ts +++ b/www/routes/raw.ts @@ -2,9 +2,20 @@ import { RouteConfig } from "$fresh/server.ts"; import { MultiHandler } from "$fresh/server.ts"; import { parse } from "$std/semver/mod.ts"; import VERSIONS from "../../versions.json" assert { type: "json" }; +import { extname } from "$std/path/mod.ts"; const BASE_URL = "https://raw.githubusercontent.com/denoland/fresh/"; +const contentTypes = new Map([ + [".html", "text/plain"], + [".ts", "application/typescript"], + [".js", "application/javascript"], + [".tsx", "text/tsx"], + [".jsx", "text/jsx"], + [".json", "application/json"], + [".wasm", "application/wasm"], +]); + export const handler: MultiHandler = { async GET(req, ctx) { const accept = req.headers.get("Accept"); @@ -37,23 +48,11 @@ export const handler: MultiHandler = { return response; } - if (isHTML) { - response.headers.set("Content-Type", "text/plain"); - } else if (path.endsWith(".ts")) { - response.headers.set("Content-Type", "application/typescript"); - } else if (path.endsWith(".js")) { - response.headers.set("Content-Type", "application/javascript"); - } else if (path.endsWith(".tsx")) { - response.headers.set("Content-Type", "text/tsx"); - } else if (path.endsWith(".jsx")) { - response.headers.set("Content-Type", "text/jsx"); - } else if (path.endsWith(".json")) { - response.headers.set("Content-Type", "application/json"); - } else if (path.endsWith(".wasm")) { - response.headers.set("Content-Type", "application/wasm"); - } else { - response.headers.set("Content-Type", "text/plain"); - } + const value = isHTML + ? "text/plain" + : contentTypes.get(extname(path)) ?? "text/plain"; + response.headers.set("Content-Type", value); + return response; }, };