diff --git a/packages/fresh/src/fs_routes.ts b/packages/fresh/src/fs_routes.ts index 0203910b..b10cc58d 100644 --- a/packages/fresh/src/fs_routes.ts +++ b/packages/fresh/src/fs_routes.ts @@ -39,11 +39,15 @@ export interface FsRouteFile { css: string[]; } -// deno-lint-ignore no-explicit-any -function isFreshFile(mod: any): mod is FreshFsMod { +function isFreshFile( + // deno-lint-ignore no-explicit-any + mod: any, + commandType: CommandType, +): mod is FreshFsMod { if (mod === null || typeof mod !== "object") return false; return typeof mod.default === "function" || + commandType === CommandType.Middleware && Array.isArray(mod.default) || typeof mod.config === "object" || typeof mod.handlers === "object" || typeof mod.handlers === "function" || typeof mod.handler === "object" || typeof mod.handler === "function"; @@ -73,7 +77,7 @@ export function fsItemsToCommands( switch (type) { case CommandType.Middleware: { if (isLazy(rawMod)) continue; - const { handlers, mod } = validateFsMod(filePath, rawMod); + const { handlers, mod } = validateFsMod(filePath, rawMod, type); let middlewares = (handlers ?? mod.default) as unknown as | Middleware @@ -97,7 +101,7 @@ export function fsItemsToCommands( continue; } case CommandType.Layout: { - const { handlers, mod } = validateFsMod(filePath, rawMod); + const { handlers, mod } = validateFsMod(filePath, rawMod, type); if (handlers !== null) { warnInvalidRoute("Layout does not support handlers"); } @@ -107,7 +111,7 @@ export function fsItemsToCommands( continue; } case CommandType.Error: { - const { handlers, mod } = validateFsMod(filePath, rawMod); + const { handlers, mod } = validateFsMod(filePath, rawMod, type); commands.push(newErrorCmd( pattern, { @@ -121,7 +125,7 @@ export function fsItemsToCommands( continue; } case CommandType.NotFound: { - const { handlers, mod } = validateFsMod(filePath, rawMod); + const { handlers, mod } = validateFsMod(filePath, rawMod, type); commands.push(newNotFoundCmd({ config: mod.config, component: mod.default, @@ -131,7 +135,7 @@ export function fsItemsToCommands( continue; } case CommandType.App: { - const { mod } = validateFsMod(filePath, rawMod); + const { mod } = validateFsMod(filePath, rawMod, type); if (mod.default === undefined) continue; commands.push(newAppCmd(mod.default)); @@ -148,7 +152,7 @@ export function fsItemsToCommands( }, async (span) => { try { const result = await rawMod(); - return normalizeRoute(filePath, result, routePattern); + return normalizeRoute(filePath, result, routePattern, type); } catch (err) { recordSpanError(span, err); throw err; @@ -162,7 +166,7 @@ export function fsItemsToCommands( config.routeOverride = item.overrideConfig?.routeOverride ?? routePattern; } else { - normalized = normalizeRoute(filePath, rawMod, routePattern); + normalized = normalizeRoute(filePath, rawMod, routePattern, type); if (rawMod.config) { config = rawMod.config; } @@ -301,11 +305,12 @@ function getRoutePathScore(char: string, s: string, i: number): number { export function validateFsMod( filePath: string, mod: unknown, + commandType: CommandType, ): { handlers: RouteHandler | HandlerFn[] | null; mod: FreshFsMod; } { - if (!isFreshFile(mod)) { + if (!isFreshFile(mod, commandType)) { throw new Error( `Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${filePath}`, ); @@ -325,8 +330,9 @@ function normalizeRoute( filePath: string, rawMod: FreshFsMod, routePattern: string, + commandType: CommandType, ): Route { - const { handlers, mod } = validateFsMod(filePath, rawMod); + const { handlers, mod } = validateFsMod(filePath, rawMod, commandType); return { config: { diff --git a/packages/plugin-vite/demo/routes/tests/middlewares/_middleware.ts b/packages/plugin-vite/demo/routes/tests/middlewares/_middleware.ts new file mode 100644 index 00000000..1d887df2 --- /dev/null +++ b/packages/plugin-vite/demo/routes/tests/middlewares/_middleware.ts @@ -0,0 +1,13 @@ +import type { Middleware } from "@fresh/core"; + +const middleware1: Middleware<{ text: string }> = async (ctx) => { + ctx.state.text = "A"; + return await ctx.next(); +}; + +const middleware2: Middleware<{ text: string }> = async (ctx) => { + ctx.state.text += "B"; + return await ctx.next(); +}; + +export default [middleware1, middleware2]; diff --git a/packages/plugin-vite/demo/routes/tests/middlewares/index.tsx b/packages/plugin-vite/demo/routes/tests/middlewares/index.tsx new file mode 100644 index 00000000..c2e19183 --- /dev/null +++ b/packages/plugin-vite/demo/routes/tests/middlewares/index.tsx @@ -0,0 +1,4 @@ +import type { HandlerFn } from "@fresh/core"; + +export const handler: HandlerFn = (ctx) => + new Response(ctx.state.text); diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index 5844d49a..af86b66a 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -474,3 +474,19 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite build - support _middleware Array", + fn: async () => { + await launchProd( + { cwd: viteResult.tmp }, + async (address) => { + const res = await fetch(`${address}/tests/middlewares`); + const text = await res.text(); + expect(text).toEqual("AB"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/packages/plugin-vite/tests/dev_server_test.ts b/packages/plugin-vite/tests/dev_server_test.ts index 7a721c14..4006026d 100644 --- a/packages/plugin-vite/tests/dev_server_test.ts +++ b/packages/plugin-vite/tests/dev_server_test.ts @@ -419,3 +419,14 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite dev - support _middleware Array", + fn: async () => { + const res = await fetch(`${demoServer.address()}/tests/middlewares`); + const text = await res.text(); + expect(text).toEqual("AB"); + }, + sanitizeOps: false, + sanitizeResources: false, +});