UNPKG

@chubbyts/chubbyts-framework

Version:

A minimal, highly performant middleware PSR-15 inspired function based microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

22 lines (21 loc) 921 B
import { isRoute } from '../router/route.js'; /** * ```ts * import type { Handler } from '@chubbyts/chubbyts-http-types/dist/handler'; * import { createRouteHandler } from '@chubbyts/chubbyts-framework/dist/handler/route-handler'; * import type { MiddlewareDispatcher } from '@chubbyts/chubbyts-framework/dist/middleware/middleware-dispatcher'; * * const middlewareDispatcher: MiddlewareDispatcher = ...; * * const routeHandler: Handler = createRouteHandler(middlewareDispatcher); * ``` */ export const createRouteHandler = (middlewareDispatcher) => { return (request) => { const route = request.attributes.route; if (isRoute(route)) { return middlewareDispatcher(route.middlewares, route.handler, request); } throw new Error(`Request attribute "route" missing or wrong type "${typeof route}", please add the "${createRouteHandler.name}" middleware.`); }; };