Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http/unstable): add support for multiple request methods on route #6003

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions http/unstable_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export interface Route {
*/
pattern: URLPattern;
/**
* Request method.
* Request method. This can be a string or an array of strings.
*
* @default {"GET"}
*/
method?: string;
method?: string | string[];
/**
* Request handler.
*/
Expand Down Expand Up @@ -61,7 +61,12 @@ export interface Route {
* {
* pattern: new URLPattern({ pathname: "/static/*" }),
* handler: (req: Request) => serveDir(req)
* }
* },
* {
* pattern: new URLPattern({ pathname: "/api" }),
* method: ["GET", "HEAD"],
* handler: (req: Request) => new Response(req.method === 'HEAD' ? null : 'ok'),
* },
* ];
*
* function defaultHandler(_req: Request) {
Expand Down Expand Up @@ -91,7 +96,12 @@ export function route(
return (request: Request, info?: Deno.ServeHandlerInfo) => {
for (const route of routes) {
const match = route.pattern.exec(request.url);
if (match && request.method === (route.method ?? "GET")) {
if (
match &&
(Array.isArray(route.method)
? route.method.includes(request.method)
: request.method === (route.method ?? "GET"))
) {
return route.handler(request, info, match);
}
}
Expand Down
20 changes: 20 additions & 0 deletions http/unstable_route_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const routes: Route[] = [
method: "POST",
handler: () => new Response("Done"),
},
{
pattern: new URLPattern({ pathname: "/resource" }),
method: ["GET", "HEAD"],
handler: (request: Request) =>
new Response(request.method === "HEAD" ? null : "Ok"),
},
];

function defaultHandler(request: Request) {
Expand Down Expand Up @@ -54,4 +60,18 @@ Deno.test("route()", async (t) => {
assertEquals(response?.status, 404);
assertEquals(await response?.text(), "/not-found");
});

await t.step("handles multiple methods", async () => {
const getMethodRequest = new Request("http://example.com/resource");
const getMethodResponse = await handler(getMethodRequest);
assertEquals(getMethodResponse?.status, 200);
assertEquals(await getMethodResponse?.text(), "Ok");

const headMethodRequest = new Request("http://example.com/resource", {
method: "HEAD",
});
const headMethodResponse = await handler(headMethodRequest);
assertEquals(headMethodResponse?.status, 200);
assertEquals(await headMethodResponse?.text(), "");
});
});