From a560c17e7bfb52c20c7b865efee7cd3cbd3706de Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Mon, 24 Jun 2024 15:04:21 +0200 Subject: [PATCH] feat: add default GET handler (#2542) closes https://github.com/denoland/fresh/issues/2530 --- src/plugins/fs_routes/mod.ts | 14 +++++-- src/plugins/fs_routes/mod_test.tsx | 62 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index b65f51016d5..0a2faf8dd1a 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -284,14 +284,20 @@ export async function fsRoutes( } } - if (routeMod.component !== null) { - components.push(routeMod.component); - } - const handlers = routeMod.handlers; const routePath = routeMod.config?.routeOverride ?? pathToPattern(normalized.slice(1)); + if (routeMod.component !== null) { + components.push(routeMod.component); + const missingGetHandler = handlers !== null && + isHandlerByMethod(handlers) && + !Object.keys(handlers).includes("GET"); + if (missingGetHandler) { + app.get(routePath, renderMiddleware(components, undefined)); + } + } + if ( handlers === null || (isHandlerByMethod(handlers) && Object.keys(handlers).length === 0) diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index b0ecef54a59..a5b2208c159 100644 --- a/src/plugins/fs_routes/mod_test.tsx +++ b/src/plugins/fs_routes/mod_test.tsx @@ -1088,3 +1088,65 @@ Deno.test("fsRoutes - sortRoutePaths", () => { ]; expect(routes).toEqual(sorted); }); + +Deno.test("fsRoutes - registers default GET route for component without GET handler", async () => { + const server = await createServer<{ value: boolean }>({ + "routes/noGetHandler.tsx": { + default: (ctx) => { + return

{ctx.state.value ? "true" : "false"}

; + }, + handlers: { + POST: () => new Response("POST"), + }, + }, + }); + + const postRes = await server.post("/noGetHandler"); + expect(postRes.status).toEqual(200); + expect(postRes.headers.get("Content-Type")).toEqual( + "text/plain;charset=UTF-8", + ); + expect(await postRes.text()).toEqual("POST"); + + const getRes = await server.get("/noGetHandler"); + expect(getRes.status).toEqual(200); + expect(getRes.headers.get("Content-Type")).toEqual( + "text/html; charset=utf-8", + ); + expect(await getRes.text()).toContain( + "

false

", + ); +}); + +Deno.test("fsRoutes - default GET route doesn't override existing handler", async () => { + const server = await createServer<{ value: boolean }>({ + "routes/withGetHandler.tsx": { + default: (ctx) => { + return

{ctx.state.value ? "true" : "false"}

; + }, + handlers: { + POST: () => new Response("POST"), + GET: (ctx) => { + ctx.state.value = true; + return page(); + }, + }, + }, + }); + + const postRes = await server.post("/withGetHandler"); + expect(postRes.status).toEqual(200); + expect(postRes.headers.get("Content-Type")).toEqual( + "text/plain;charset=UTF-8", + ); + expect(await postRes.text()).toEqual("POST"); + + const getRes = await server.get("/withGetHandler"); + expect(getRes.status).toEqual(200); + expect(getRes.headers.get("Content-Type")).toEqual( + "text/html; charset=utf-8", + ); + expect(await getRes.text()).toContain( + "

true

", + ); +});