{"version":3,"sources":["../src/compose.ts"],"names":["compose","utilities","handler","options","onError","defaultErrorHandler","composedHandler","next","utility","request","context","error","createHandler","builder"],"mappings":"mEAmBO,SAASA,KACTC,CAAAA,CACuG,CAC1G,OAAO,CAACC,CAAAA,CAAiCC,IAA6B,CAClE,IAAMC,EAAUD,CAAAA,EAAS,OAAA,EAAWE,oBAG9BC,CAAAA,CAAkBL,CAAAA,CAAU,YAC9B,CAACM,CAAAA,CAAMC,IAAYA,CAAAA,CAAQD,CAAoB,CAAA,CAC/CL,CACJ,EAEA,OAAO,MAAOO,GAA4C,CACtD,IAAMC,EAA0B,CAAE,OAAA,CAAAD,CAAQ,CAAA,CAE1C,GAAI,CACA,OAAO,MAAMH,EAAgBI,CAAO,CACxC,OAASC,CAAAA,CAAO,CACZ,OAAO,MAAMP,EAAQO,CAAAA,CAAOD,CAAO,CACvC,CACJ,CACJ,CACJ,CAiBO,SAASE,GAAgB,CAC5B,IAAMX,EAA8B,EAAC,CAE/BY,EAAU,CAIZ,GAAA,CACIL,EACF,CACE,OAAAP,CAAAA,CAAU,IAAA,CAAKO,CAAoC,CAAA,CAC5CK,CACX,EAKA,MAAA,CACIX,CAAAA,CACAC,EAC2C,CAC3C,OAAOH,EAAQ,GAAGC,CAAS,EAAEC,CAAAA,CAAyBC,CAAO,CACjE,CACJ,CAAA,CAEA,OAAOU,CACX","file":"index.cjs","sourcesContent":["import { defaultErrorHandler } from \"./errors.js\";\nimport type { ComposeOptions, HandlerContext, HandlerUtility, NextRequest, RouteHandler } from \"./types.js\";\n\n/**\n * Compose multiple utilities into a single route handler.\n *\n * @example\n * ```ts\n * import { compose, withCors, withAuth, withRateLimit } from \"next-route-compose\";\n *\n * export const GET = compose(\n *   withCors({ origin: \"*\" }),\n *   withAuth({ verify: (ctx) => verifyToken(ctx.request) }),\n *   withRateLimit({ limit: 100, window: 60 })\n * )(async (ctx) => {\n *   return Response.json({ user: ctx.user });\n * });\n * ```\n */\nexport function compose<TContext extends HandlerContext = HandlerContext>(\n    ...utilities: HandlerUtility[]\n): (handler: RouteHandler<TContext>, options?: ComposeOptions) => (request: NextRequest) => Promise<Response> {\n    return (handler: RouteHandler<TContext>, options?: ComposeOptions) => {\n        const onError = options?.onError ?? defaultErrorHandler;\n\n        // Compose utilities from right to left\n        const composedHandler = utilities.reduceRight(\n            (next, utility) => utility(next as RouteHandler),\n            handler as RouteHandler\n        );\n\n        return async (request: NextRequest): Promise<Response> => {\n            const context: HandlerContext = { request };\n\n            try {\n                return await composedHandler(context);\n            } catch (error) {\n                return await onError(error, context);\n            }\n        };\n    };\n}\n\n/**\n * Create a handler with a fluent builder API.\n *\n * @example\n * ```ts\n * import { createHandler, withCors, withAuth } from \"next-route-compose\";\n *\n * export const GET = createHandler()\n *   .use(withCors({ origin: \"*\" }))\n *   .use(withAuth({ verify: verifyToken }))\n *   .handle(async (ctx) => {\n *     return Response.json({ user: ctx.user });\n *   });\n * ```\n */\nexport function createHandler() {\n    const utilities: HandlerUtility[] = [];\n\n    const builder = {\n        /**\n         * Add a utility to the handler chain.\n         */\n        use<TContextIn extends HandlerContext, TContextOut extends HandlerContext>(\n            utility: HandlerUtility<TContextIn, TContextOut>\n        ) {\n            utilities.push(utility as unknown as HandlerUtility);\n            return builder;\n        },\n\n        /**\n         * Set the final handler and return the composed route handler.\n         */\n        handle<TContext extends HandlerContext>(\n            handler: RouteHandler<TContext>,\n            options?: ComposeOptions\n        ): (request: NextRequest) => Promise<Response> {\n            return compose(...utilities)(handler as RouteHandler, options);\n        },\n    };\n\n    return builder;\n}\n"]}