{"version":3,"file":"index-node18.mjs","sources":["../src/index.ts"],"sourcesContent":["import type { Context, NodeApplication } from 'alp-types';\nimport type {\n  Router,\n  RouteMatch,\n  LocaleType,\n  RouterBuilder,\n} from 'router-segments';\nimport { createRouterBuilder } from 'router-segments';\n\nexport type AlpRouteRef = (ctx: Context) => Promise<void> | void;\ntype ReturnType = (app: NodeApplication) => AlpRouteRef;\n\nexport const createAlpRouterBuilder = <\n  Locales extends LocaleType,\n>(): RouterBuilder<Locales, AlpRouteRef> =>\n  createRouterBuilder<Locales, AlpRouteRef>();\n\nexport type UrlGenerator = <P extends Record<string, unknown> | undefined>(\n  routeKey: string,\n  params?: P,\n) => string;\n\ndeclare module 'alp-types' {\n  // eslint-disable-next-line @typescript-eslint/no-shadow\n  interface NodeApplication {\n    router?: Router<any, AlpRouteRef>;\n  }\n\n  interface BaseContext {\n    urlGenerator: UrlGenerator;\n    redirectTo: <P extends Record<string, unknown>>(\n      to: string,\n      params?: P,\n    ) => Promise<void>;\n  }\n\n  // eslint-disable-next-line @typescript-eslint/no-shadow\n  interface Context {\n    route: RouteMatch<any, AlpRouteRef>;\n  }\n}\n\nexport default function alpRouter<Locales extends string>(\n  router: Router<Locales, AlpRouteRef>,\n): ReturnType {\n  return (app: NodeApplication) => {\n    app.router = router;\n\n    app.context.urlGenerator = function <\n      P extends Record<string, unknown> | undefined,\n    >(this: Context, routeKey: string, params?: P): string {\n      return router.toLocalizedPath(this.language as Locales, routeKey, params);\n    };\n\n    app.context.redirectTo = function <\n      P extends Record<string, unknown> | undefined,\n    >(this: Context, to: string, params?: P): Promise<void> {\n      return this.redirect(\n        router.toLocalizedPath(this.language as Locales, to, params),\n      );\n    };\n\n    return async (ctx: Context): Promise<void> => {\n      // eslint-disable-next-line unicorn/no-array-method-this-argument\n      const routeMatch = router.find(ctx.request.path, ctx.language as Locales);\n\n      if (!routeMatch) {\n        ctx.status = 404;\n        throw new Error(`Route not found: ${ctx.request.path}`);\n      }\n\n      ctx.route = routeMatch;\n\n      await routeMatch.ref(ctx);\n    };\n  };\n}\n"],"names":["createAlpRouterBuilder","createRouterBuilder","alpRouter","router","app","context","urlGenerator","routeKey","params","toLocalizedPath","language","redirectTo","to","redirect","ctx","routeMatch","find","request","path","status","Error","route","ref"],"mappings":";;MAYaA,sBAAsB,GAAGA,MAGpCC,mBAAmB,GAAwB;AA2B9B,SAASC,SAASA,CAC/BC,MAAoC,EACxB;AACZ,EAAA,OAAQC,GAAoB,IAAK;IAC/BA,GAAG,CAACD,MAAM,GAAGA,MAAM,CAAA;IAEnBC,GAAG,CAACC,OAAO,CAACC,YAAY,GAAG,UAEVC,QAAgB,EAAEC,MAAU,EAAU;MACrD,OAAOL,MAAM,CAACM,eAAe,CAAC,IAAI,CAACC,QAAQ,EAAaH,QAAQ,EAAEC,MAAM,CAAC,CAAA;KAC1E,CAAA;IAEDJ,GAAG,CAACC,OAAO,CAACM,UAAU,GAAG,UAERC,EAAU,EAAEJ,MAAU,EAAiB;AACtD,MAAA,OAAO,IAAI,CAACK,QAAQ,CAClBV,MAAM,CAACM,eAAe,CAAC,IAAI,CAACC,QAAQ,EAAaE,EAAE,EAAEJ,MAAM,CAC7D,CAAC,CAAA;KACF,CAAA;IAED,OAAO,MAAOM,GAAY,IAAoB;AAC5C;AACA,MAAA,MAAMC,UAAU,GAAGZ,MAAM,CAACa,IAAI,CAACF,GAAG,CAACG,OAAO,CAACC,IAAI,EAAEJ,GAAG,CAACJ,QAAmB,CAAC,CAAA;MAEzE,IAAI,CAACK,UAAU,EAAE;QACfD,GAAG,CAACK,MAAM,GAAG,GAAG,CAAA;QAChB,MAAM,IAAIC,KAAK,CAAE,CAAmBN,iBAAAA,EAAAA,GAAG,CAACG,OAAO,CAACC,IAAK,CAAA,CAAC,CAAC,CAAA;AACzD,OAAA;MAEAJ,GAAG,CAACO,KAAK,GAAGN,UAAU,CAAA;AAEtB,MAAA,MAAMA,UAAU,CAACO,GAAG,CAACR,GAAG,CAAC,CAAA;KAC1B,CAAA;GACF,CAAA;AACH;;;;"}