Skip to content

Commit

Permalink
chore: simplify ext -> content type mapping code (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
lausuarez02 committed Jun 28, 2023
1 parent db35797 commit e0a1809
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions www/routes/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
},
};
Expand Down

0 comments on commit e0a1809

Please sign in to comment.