chore: simplify ext -> content type mapping code (#1368)

This commit is contained in:
Lautaro Suarez
2023-06-28 22:13:59 +02:00
committed by GitHub
parent db357973ab
commit e0a1809452
+16 -17
View File
@@ -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;
},
};