feat(server): middleware params (#1314)
This commit is contained in:
@@ -45,6 +45,7 @@ import {
|
||||
EsbuildBuilder,
|
||||
JSXConfig,
|
||||
} from "../build/mod.ts";
|
||||
import { InternalRoute } from "./router.ts";
|
||||
|
||||
const DEFAULT_CONN_INFO: ConnInfo = {
|
||||
localAddr: { transport: "tcp", hostname: "localhost", port: 8080 },
|
||||
@@ -379,6 +380,7 @@ export class ServerContext {
|
||||
const withMiddlewares = this.#composeMiddlewares(
|
||||
this.#middlewares,
|
||||
handlers.errorHandler,
|
||||
router.getParamsAndRoute<RouterState>(handlers),
|
||||
);
|
||||
const trailingSlashEnabled = this.#routerOptions?.trailingSlash;
|
||||
return async function handler(
|
||||
@@ -437,6 +439,12 @@ export class ServerContext {
|
||||
#composeMiddlewares(
|
||||
middlewares: MiddlewareRoute[],
|
||||
errorHandler: router.ErrorHandler<RouterState>,
|
||||
paramsAndRoute: (
|
||||
url: string,
|
||||
) => {
|
||||
route: InternalRoute<RouterState> | undefined;
|
||||
params: Record<string, string>;
|
||||
},
|
||||
) {
|
||||
return (
|
||||
req: Request,
|
||||
@@ -448,6 +456,7 @@ export class ServerContext {
|
||||
const mws = selectMiddlewares(req.url, middlewares);
|
||||
|
||||
const handlers: (() => Response | Promise<Response>)[] = [];
|
||||
const paramsAndRouteResult = paramsAndRoute(req.url);
|
||||
|
||||
const middlewareCtx: MiddlewareHandlerContext = {
|
||||
next() {
|
||||
@@ -470,6 +479,7 @@ export class ServerContext {
|
||||
...connInfo,
|
||||
state: {},
|
||||
destination: "route",
|
||||
params: paramsAndRouteResult.params,
|
||||
};
|
||||
|
||||
for (const mw of mws) {
|
||||
@@ -490,6 +500,8 @@ export class ServerContext {
|
||||
const { destination, handler } = inner(
|
||||
req,
|
||||
ctx,
|
||||
paramsAndRouteResult.params,
|
||||
paramsAndRouteResult.route,
|
||||
);
|
||||
handlers.push(handler);
|
||||
middlewareCtx.destination = destination;
|
||||
|
||||
Reference in New Issue
Block a user