fix(server): merge competing PluginMiddleware handlers together (#1545)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Plugin } from "$fresh/server.ts";
|
||||
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
||||
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
||||
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
||||
export type { Options };
|
||||
@@ -6,15 +6,41 @@ export type { Options };
|
||||
interface Options {
|
||||
title: string;
|
||||
}
|
||||
export type PluginMiddlewareState = {
|
||||
num: number;
|
||||
test: string;
|
||||
};
|
||||
|
||||
const twoPointlessMiddlewares = [
|
||||
async (
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||
) => {
|
||||
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||
return await ctx.next();
|
||||
},
|
||||
async (
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||
) => {
|
||||
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||
return await ctx.next();
|
||||
},
|
||||
];
|
||||
|
||||
export default function routePlugin(
|
||||
options: Options,
|
||||
): Plugin<{ test: string }> {
|
||||
): Plugin<PluginMiddlewareState> {
|
||||
return {
|
||||
name: "routePlugin",
|
||||
middlewares: [{
|
||||
middleware: { handler: testMiddleware },
|
||||
path: "/",
|
||||
}, {
|
||||
middleware: {
|
||||
handler: twoPointlessMiddlewares,
|
||||
},
|
||||
path: "lots-of-middleware",
|
||||
}],
|
||||
routes: [{
|
||||
path: "/_app",
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import { PluginMiddlewareState } from "../../utils/route-plugin.ts";
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<{ test: string }>,
|
||||
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||
) {
|
||||
ctx.state.test = "look, i'm set from a plugin!";
|
||||
const resp = await ctx.next();
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
||||
import { PluginMiddlewareState } from "./route-plugin.ts";
|
||||
|
||||
export default function secondMiddlewarePlugin(): Plugin<
|
||||
PluginMiddlewareState
|
||||
> {
|
||||
return {
|
||||
name: "secondMiddlewarePlugin",
|
||||
middlewares: [{
|
||||
middleware: {
|
||||
handler: async (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||
return await ctx.next();
|
||||
},
|
||||
},
|
||||
path: "/", // this is the root route
|
||||
}, {
|
||||
middleware: {
|
||||
handler: async (
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||
) => {
|
||||
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||
return await ctx.next();
|
||||
},
|
||||
},
|
||||
path: "lots-of-middleware/",
|
||||
}, {
|
||||
middleware: {
|
||||
handler: async (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||
return await ctx.next();
|
||||
},
|
||||
},
|
||||
path: "", // this also goes to the root route, as of 1.4
|
||||
}],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user