From 3a44b9033086d23660f3285ac54fc614de0bff7c Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Mon, 23 Dec 2024 16:39:28 +0100 Subject: [PATCH 1/3] allow array of functions as fresh file for middleware files --- src/plugins/fs_routes/mod.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index 7e9cb6c5605..8eb147640eb 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -43,7 +43,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 { From a05543644cd0470539cdbfd83e1e07bc27211f96 Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Mon, 23 Dec 2024 16:54:37 +0100 Subject: [PATCH 2/3] adapt tests and types --- src/plugins/fs_routes/mod.ts | 4 ++- src/plugins/fs_routes/mod_test.tsx | 48 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index 8eb147640eb..2dcf8348b27 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -34,7 +34,9 @@ export interface FreshFsItem { handlers?: RouteHandler; default?: | AnyComponent> - | AsyncAnyComponent>; + | AsyncAnyComponent> + | ((ctx: FreshReqContext<{ text: string }>) => Promise) + | ((ctx: FreshReqContext<{ text: string }>) => Promise)[]; } // deno-lint-ignore no-explicit-any diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index 261a2dec2de..616af669c59 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": { From e3888a3fa01919a94ec7c2758a90190f44d257c8 Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Mon, 23 Dec 2024 17:18:08 +0100 Subject: [PATCH 3/3] adapt types --- src/plugins/fs_routes/mod.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index 2dcf8348b27..e3c4b376025 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, @@ -35,8 +35,7 @@ export interface FreshFsItem { default?: | AnyComponent> | AsyncAnyComponent> - | ((ctx: FreshReqContext<{ text: string }>) => Promise) - | ((ctx: FreshReqContext<{ text: string }>) => Promise)[]; + | Middleware; } // deno-lint-ignore no-explicit-any