Skip to content

Commit f97b320

Browse files
committed
include HEAD method in allow header
1 parent b104000 commit f97b320

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

handler.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,23 @@ export function route<C>(
8383
}
8484

8585
export function methods<C>(methods: Record<string, Handler<C>>): Handler<C> {
86-
const methodMap = new Map<string, Handler<C>>();
87-
for (const method in methods) {
88-
methodMap.set(method, methods[method]);
89-
}
86+
const map = new Map(Object.entries(methods));
87+
map.delete("HEAD");
88+
const allow = (() => {
89+
const list = Array.from(map.keys());
90+
if (map.has("GET")) {
91+
list.push("HEAD");
92+
}
93+
return list.sort().join(", ");
94+
})();
9095
return (req, ctx) => {
91-
const handler = methodMap.get(req.method) ?? fail(
96+
const handler = map.get(req.method) ?? fail(
9297
new HttpError(
9398
`Method ${req.method} is not allowed for the URL`,
9499
"MethodNotAllowed",
95100
{
96101
headers: [
97-
["allow", Array.from(methodMap.keys()).join(", ")],
102+
["allow", allow],
98103
],
99104
},
100105
),

0 commit comments

Comments
 (0)