Skip to content

Commit

Permalink
feat: add default GET handler (#2542)
Browse files Browse the repository at this point in the history
closes #2530
  • Loading branch information
deer committed Jun 24, 2024
1 parent 77bd7d0 commit a560c17
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,20 @@ export async function fsRoutes<State>(
}
}

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)
Expand Down
62 changes: 62 additions & 0 deletions src/plugins/fs_routes/mod_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <h1>{ctx.state.value ? "true" : "false"}</h1>;
},
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(
"<h1>false</h1>",
);
});

Deno.test("fsRoutes - default GET route doesn't override existing handler", async () => {
const server = await createServer<{ value: boolean }>({
"routes/withGetHandler.tsx": {
default: (ctx) => {
return <h1>{ctx.state.value ? "true" : "false"}</h1>;
},
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(
"<h1>true</h1>",
);
});

0 comments on commit a560c17

Please sign in to comment.