feat(server): middleware params (#1314)

This commit is contained in:
Reed von Redwitz
2023-07-10 15:06:43 +02:00
committed by GitHub
parent 27622a190e
commit 724c81f2c7
9 changed files with 142 additions and 39 deletions
+12
View File
@@ -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;
+30 -9
View File
@@ -10,6 +10,8 @@ export type Handler<T = unknown> = (
export type FinalHandler<T = unknown> = (
req: Request,
ctx: HandlerContext<T>,
params: Record<string, string>,
route?: InternalRoute<T>,
) => {
destination: DestinationKind;
handler: () => Response | Promise<Response>;
@@ -125,25 +127,22 @@ function processRoutes<T>(
}
}
export function router<T = unknown>(
export function getParamsAndRoute<T>(
{
internalRoutes,
staticRoutes,
routes,
otherHandler,
unknownMethodHandler,
}: RouterOptions<T>,
): FinalHandler<T> {
unknownMethodHandler ??= defaultUnknownMethodHandler;
): (
url: string,
) => { route: InternalRoute<T> | undefined; params: Record<string, string> } {
const processedRoutes: InternalRoute<T>[] = [];
processRoutes(processedRoutes, internalRoutes, "internal");
processRoutes(processedRoutes, staticRoutes, "static");
processRoutes(processedRoutes, routes, "route");
return (req, ctx) => {
return (url: string) => {
for (const route of processedRoutes) {
const res = route.pattern.exec(req.url);
const res = route.pattern.exec(url);
if (res !== null) {
const groups: Record<string, string> = {};
@@ -156,7 +155,29 @@ export function router<T = unknown>(
groups[key] = decodeURIComponent(value);
}
}
return { route: route, params: groups };
}
}
return {
route: undefined,
params: {},
};
};
}
export function router<T = unknown>(
{
otherHandler,
unknownMethodHandler,
}: RouterOptions<T>,
): FinalHandler<T> {
unknownMethodHandler ??= defaultUnknownMethodHandler;
return (req, ctx, groups, route) => {
if (route) {
const res = route.pattern.exec(req.url);
if (res !== null) {
// If not overridden, HEAD requests should be handled as GET requests but without the body.
if (req.method === "HEAD" && !route.methods["HEAD"]) {
req = new Request(req.url, { method: "GET", headers: req.headers });
+1
View File
@@ -231,6 +231,7 @@ export interface MiddlewareHandlerContext<State = Record<string, unknown>>
next: () => Promise<Response>;
state: State;
destination: router.DestinationKind;
params: Record<string, string>;
}
export interface MiddlewareRoute extends Middleware {