UNPKG

@universal-middleware/webroute

Version:
45 lines (44 loc) 1.21 kB
// src/common.ts import { bindUniversal, getAdapterRuntime, universalSymbol } from "@universal-middleware/core"; function createHandler(handlerFactory) { return (...args) => { const handler = handlerFactory(...args); return bindUniversal(handler, async function universalHandlerWebroute(request, ctx) { const context = initContext(ctx); return this[universalSymbol](request, context, await getRuntime(ctx)); }); }; } function createMiddleware(middlewareFactory) { return (...args) => { const middleware = middlewareFactory(...args); return bindUniversal( middleware, async function universalMiddlewareWebroute(request, ctx) { const context = initContext(ctx); return this[universalSymbol](request, context, await getRuntime(ctx)); } ); }; } function initContext(ctx) { ctx.state ??= {}; return ctx.state; } function getContext(ctx) { return ctx.state; } async function getRuntime(ctx) { const parsed = await ctx?.parse(); const params = parsed?.params ?? void 0; return getAdapterRuntime("webroute", { params, webroute: ctx }); } export { createHandler, createMiddleware, getContext, getRuntime };