@chubbyts/chubbyts-framework
Version:
A minimal, highly performant middleware PSR-15 inspired function based micro framework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.
19 lines (18 loc) • 805 B
JavaScript
import { ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';
/**
* ```ts
* import type { Middleware } from '@chubbyts/chubbyts-undici-server/dist/server';
* import type { Match } from '@chubbyts/chubbyts-framework/dist/router/route-matcher';
* import { createRouteMatcherMiddleware } from '@chubbyts/chubbyts-framework/dist/middleware/route-matcher-middleware';
*
* const match: Match = ...;
*
* const routeMatcherMiddleware: Middleware = createRouteMatcherMiddleware(match);
* ```
*/
export const createRouteMatcherMiddleware = (match) => {
return async (serverRequest, handler) => {
const route = match(serverRequest);
return handler(new ServerRequest(serverRequest, { attributes: { ...serverRequest.attributes, route, ...route.attributes } }));
};
};