diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index 03add8d712b..ef37f183a1e 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -4,7 +4,7 @@ import type { WalkEntry } from "@std/fs/walk"; import * as path from "@std/path"; import type { RouteConfig } from "../../types.ts"; import type { RouteHandler } from "../../handlers.ts"; -import type { MiddlewareFn } from "../../middlewares/mod.ts"; +import type { Middleware, MiddlewareFn } from "../../middlewares/mod.ts"; import { type AsyncAnyComponent, renderMiddleware, @@ -34,7 +34,8 @@ export interface FreshFsItem { handlers?: RouteHandler; default?: | AnyComponent> - | AsyncAnyComponent>; + | AsyncAnyComponent> + | Middleware; } // deno-lint-ignore no-explicit-any @@ -43,7 +44,9 @@ function isFreshFile(mod: any): mod is FreshFsItem { typeof mod.default === "function" || typeof mod.config === "object" || typeof mod.handlers === "object" || typeof mod.handlers === "function" || typeof mod.handler === "object" || - typeof mod.handler === "function"; + typeof mod.handler === "function" || + (Array.isArray(mod.default) && mod.default.length > 0 && + mod.default.every((item: unknown) => typeof item === "function")); } export interface FsRoutesOptions { diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index 1f36890dd7f..ac4fcf6ab7d 100644 --- a/src/plugins/fs_routes/mod_test.tsx +++ b/src/plugins/fs_routes/mod_test.tsx @@ -13,6 +13,7 @@ import { type HandlerByMethod, type HandlerFn, page } from "../../handlers.ts"; import type { Method } from "../../router.ts"; import { parseHtml } from "../../../tests/test_utils.tsx"; import type { FreshContext } from "fresh"; +import { createDefine } from "../../define.ts"; async function createServer( files: Record>, @@ -189,6 +190,25 @@ Deno.test("fsRoutes - middleware", async () => { expect(await res.text()).toEqual("ok"); }); +Deno.test("fsRoutes - middleware using define", async () => { + const server = await createServer<{ text: string }>({ + "routes/index.ts": { handler: (ctx) => new Response(ctx.state.text) }, + "routes/_middleware.ts": { + default: createDefine<{ text: string }>() + .middleware( + (ctx) => { + ctx.state.text = "A"; + return ctx.next(); + }, + ), + }, + }); + + const res = await server.get("/"); + expect(res.status).toEqual(200); + expect(await res.text()).toEqual("ok"); +}); + Deno.test("fsRoutes - nested middlewares", async () => { const server = await createServer<{ text: string }>({ "routes/_middleware.ts": { @@ -239,6 +259,34 @@ Deno.test("fsRoutes - middleware array", async () => { expect(doc.body.firstChild?.textContent).toEqual("ABC"); }); +Deno.test("fsRoutes - middleware array using define", async () => { + const server = await createServer<{ text: string }>({ + "routes/_middleware.ts": { + default: createDefine<{ text: string }>().middleware([ + (ctx) => { + ctx.state.text = "A"; + return ctx.next(); + }, + (ctx) => { + ctx.state.text += "B"; + return ctx.next(); + }, + ]), + }, + "routes/foo/_middleware.ts": { + handler: (ctx) => { + ctx.state.text += "C"; + return ctx.next(); + }, + }, + "routes/foo/index.ts": { default: (ctx) =>
{ctx.state.text}
}, + }); + + const res = await server.get("/foo"); + const doc = parseHtml(await res.text()); + expect(doc.body.firstChild?.textContent).toEqual("ABC"); +}); + Deno.test("fsRoutes - combined", async () => { const server = await createServer<{ text: string }>({ "routes/foo/bar.ts": {