Skip to content

Commit 23d9440

Browse files
committed
refactor: split out response header manager
1 parent d9bb98f commit 23d9440

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/cloudflare-worker/handler.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "../core";
1111
import { Cache } from "./cache";
1212
import demo from "./demo";
13+
import Header from "./headers";
1314
import { booleanize, normalize } from "./utils";
1415

1516
const router = Router();
@@ -130,14 +131,10 @@ async function generate(config: Record<string, string>): Promise<Response> {
130131
const cache_header =
131132
`max-age=${cache_time}` + (cache_time <= 0 ? ", no-store, no-cache" : ", public");
132133

133-
return new Response(await generator.generate(sanitized), {
134-
headers: {
135-
"Content-Type": "image/svg+xml",
136-
"Access-Control-Allow-Origin": "*",
137-
"Access-Control-Allow-Credentials": "true",
138-
"Cache-Control": cache_header,
139-
},
140-
});
134+
const headers = new Header().add("cors", "svg");
135+
headers.set("cache-control", cache_header);
136+
137+
return new Response(await generator.generate(sanitized), { headers });
141138
}
142139

143140
// handle path variable
@@ -153,11 +150,7 @@ router.get(
153150
router.get("*", async ({ query }: { query: Record<string, string> }) => {
154151
if (!query.username) {
155152
return new Response(demo, {
156-
headers: {
157-
"Content-Type": "text/html",
158-
"Access-Control-Allow-Origin": "*",
159-
"Access-Control-Allow-Credentials": "true",
160-
},
153+
headers: new Header().add("cors", "html"),
161154
});
162155
}
163156

src/cloudflare-worker/headers.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const source = {
2+
cors: {
3+
"Access-Control-Allow-Origin": "*",
4+
"Access-Control-Allow-Methods": "*",
5+
"Access-Control-Allow-Headers": "*",
6+
"Access-Control-Allow-Credentials": "true",
7+
},
8+
json: {
9+
"Content-Type": "application/json",
10+
},
11+
html: {
12+
"Content-Type": "text/html",
13+
},
14+
text: {
15+
"Content-Type": "text/plain",
16+
},
17+
svg: {
18+
"Content-Type": "image/svg+xml",
19+
},
20+
};
21+
22+
export default class Header extends Headers {
23+
constructor(headers?: Headers) {
24+
super(headers);
25+
}
26+
27+
add(...types: (keyof typeof source)[]): Headers {
28+
for (const type of types) {
29+
for (const [key, value] of Object.entries(source[type])) {
30+
this.set(key, value);
31+
}
32+
}
33+
34+
return this;
35+
}
36+
}

src/cloudflare-worker/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { handle } from "./handler";
2+
import Header from "./headers";
23

34
export default {
45
async fetch(
@@ -12,7 +13,7 @@ export default {
1213
console.error(err);
1314
return new Response((err as Error).message, {
1415
status: 500,
15-
headers: { "Content-Type": "text/plain" },
16+
headers: new Header().add("cors", "text"),
1617
});
1718
}
1819
},

0 commit comments

Comments
 (0)