fix: bring back support for middleware Array (#3434)
Our validation didn't account for `_middleware` files exporting an Array of middlewares. Fixes https://github.com/denoland/fresh/issues/3400
This commit is contained in:
@@ -39,11 +39,15 @@ export interface FsRouteFile<State> {
|
||||
css: string[];
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
function isFreshFile<State>(mod: any): mod is FreshFsMod<State> {
|
||||
function isFreshFile<State>(
|
||||
// deno-lint-ignore no-explicit-any
|
||||
mod: any,
|
||||
commandType: CommandType,
|
||||
): mod is FreshFsMod<State> {
|
||||
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<State>(
|
||||
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<State>
|
||||
@@ -97,7 +101,7 @@ export function fsItemsToCommands<State>(
|
||||
continue;
|
||||
}
|
||||
case CommandType.Layout: {
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||
if (handlers !== null) {
|
||||
warnInvalidRoute("Layout does not support handlers");
|
||||
}
|
||||
@@ -107,7 +111,7 @@ export function fsItemsToCommands<State>(
|
||||
continue;
|
||||
}
|
||||
case CommandType.Error: {
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||
commands.push(newErrorCmd(
|
||||
pattern,
|
||||
{
|
||||
@@ -121,7 +125,7 @@ export function fsItemsToCommands<State>(
|
||||
continue;
|
||||
}
|
||||
case CommandType.NotFound: {
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||
commands.push(newNotFoundCmd({
|
||||
config: mod.config,
|
||||
component: mod.default,
|
||||
@@ -131,7 +135,7 @@ export function fsItemsToCommands<State>(
|
||||
continue;
|
||||
}
|
||||
case CommandType.App: {
|
||||
const { mod } = validateFsMod<State>(filePath, rawMod);
|
||||
const { mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||
if (mod.default === undefined) continue;
|
||||
|
||||
commands.push(newAppCmd(mod.default));
|
||||
@@ -148,7 +152,7 @@ export function fsItemsToCommands<State>(
|
||||
}, 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<State>(
|
||||
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<State>(
|
||||
filePath: string,
|
||||
mod: unknown,
|
||||
commandType: CommandType,
|
||||
): {
|
||||
handlers: RouteHandler<unknown, State> | HandlerFn<unknown, State>[] | null;
|
||||
mod: FreshFsMod<State>;
|
||||
} {
|
||||
if (!isFreshFile<State>(mod)) {
|
||||
if (!isFreshFile<State>(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<State>(
|
||||
filePath: string,
|
||||
rawMod: FreshFsMod<State>,
|
||||
routePattern: string,
|
||||
commandType: CommandType,
|
||||
): Route<State> {
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, commandType);
|
||||
|
||||
return {
|
||||
config: {
|
||||
|
||||
@@ -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];
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { HandlerFn } from "@fresh/core";
|
||||
|
||||
export const handler: HandlerFn<null, { text: string }> = (ctx) =>
|
||||
new Response(ctx.state.text);
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user