Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/build/virtual/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ${allHandlers

export const findRoute = ${nitro.routing.routes.compileToString({ serialize: (h) => serializeHandler(h, { tracing: traceH3 }) })}

export const findRoutedMiddleware = ${nitro.routing.routedMiddleware.compileToString({ serialize: serializeHandler, matchAll: true })};
export const findRoutedMiddleware = ${nitro.routing.routedMiddleware.compileToString({ serialize: serializeHandlerFn, matchAll: true })};

export const globalMiddleware = [
${nitro.routing.globalMiddleware.map((h) => (h.lazy ? h._importHash : `h3.toEventHandler(${h._importHash})`)).join(",")}
Expand Down
25 changes: 25 additions & 0 deletions test/unit/virtual-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,29 @@ describe("virtual/routing template", () => {
expect(template).toContain(`import { wrapHandlerWithTracing } from "h3/tracing"`);
expect(template).toContain("wrapHandlerWithTracing(h3.toEventHandler(_abc123))");
});

it("serializes routed middleware with serializeHandlerFn instead of route record", () => {
const handler: NitroEventHandler & { _importHash: string } = {
route: "/api/**",
handler: "/path/to/middleware.ts",
_importHash: "_mid123",
middleware: true,
};
const nitroStub = {
options: { baseURL: "/", routeRules: {} },
routing: {
routes: { routes: [], compileToString: () => "{}" },
routedMiddleware: {
routes: [{ route: "/api/**", data: handler }],
compileToString: ({ serialize }: { serialize: (h: unknown) => string }) =>
`{"/api/**":${serialize(handler)}}`,
},
globalMiddleware: [],
},
} as unknown as Nitro;
const template = routing(nitroStub).template();
expect(template).toContain(
'export const findRoutedMiddleware = {"/api/**":h3.toEventHandler(_mid123)};'
);
});
});