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

fix(plugins/fs_routes): error when exporting middleware array with createDefine().middleware #2799

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,7 +34,8 @@ export interface FreshFsItem<State> {
handlers?: RouteHandler<unknown, State>;
default?:
| AnyComponent<PageProps<unknown, State>>
| AsyncAnyComponent<PageProps<unknown, State>>;
| AsyncAnyComponent<PageProps<unknown, State>>
| Middleware<State>;
}

// deno-lint-ignore no-explicit-any
Expand All @@ -43,7 +44,9 @@ function isFreshFile<State>(mod: any): mod is FreshFsItem<State> {
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 {
Expand Down
48 changes: 48 additions & 0 deletions src/plugins/fs_routes/mod_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
files: Record<string, string | Uint8Array | FreshFsItem<T>>,
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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) => <div>{ctx.state.text}</div> },
});

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": {
Expand Down
Loading