{"version":3,"sources":["../../src/Router.mts"],"sourcesContent":["import path from 'path';\nimport urlJoin from 'url-join';\nimport { CompiledRoute } from './CompiledRoute.mjs';\nimport type { BaseController } from './Controller.mjs';\nimport { MiddlewareFactory } from './MiddlewareFactory.mjs';\nimport { Route } from './Route.mjs';\nimport type {\n  HandlerType,\n  HttpMethod,\n  MiddlewareProvider,\n  Request,\n  Response,\n  RouteCheck,\n} from './types.mjs';\n\nexport class Router {\n  private middlewares: MiddlewareProvider[] = [];\n  private checks: RouteCheck[] = [];\n  routes: Route[] = [];\n  addRoute(methods: HttpMethod[], path: string, handler: HandlerType) {\n    const route: Route = new Route(methods, path, handler);\n    this.routes.push(route);\n    return route;\n  }\n\n  getMiddlewares() {\n    return [...this.middlewares];\n  }\n\n  addCheck(checks: RouteCheck | RouteCheck[]) {\n    this.checks = this.checks.concat(checks);\n    return this;\n  }\n\n  getChecks() {\n    return [...this.checks];\n  }\n\n  addController(controller: typeof BaseController) {\n    const basePath = controller.basePath || '';\n    for (const route of controller.routes) {\n      const urlPath = path.join(basePath, route.path);\n      this.addRoute(route.methods, urlPath, async (req: Request, res: Response) => {\n        const controllerInstance = controller.getInstance();\n        // @ts-expect-error\n        return await controllerInstance[route.handler]();\n      }).addMiddleware([...controller.baseMiddlewares, ...route.middlewares]);\n    }\n  }\n\n  addRouter(path: string, router: Router) {\n    for (const route of router.routes) {\n      const path2 = urlJoin('/', path, route.path);\n      this.addRoute(route.methods, path2, route.handler)\n        .addMiddleware(router.getMiddlewares())\n        .addMiddleware(route.getMiddlewares())\n        .addCheck(router.getChecks())\n        .addCheck(route.getChecks());\n    }\n  }\n\n  addGlobalMiddleware(middlewares: MiddlewareProvider | MiddlewareProvider[]) {\n    this.middlewares = this.middlewares.concat(middlewares);\n  }\n\n  resolve(request: Request): Route | undefined {\n    for (const route of this.routes) {\n      if (route.test(request)) {\n        return route;\n      }\n    }\n    return undefined;\n  }\n\n  resolveMultiple(request: Request): Route[] {\n    const rc: Route[] = [];\n    const url = new URL(request.url || '/', 'http://localhost');\n    for (const route of this.routes) {\n      if (route.testPath(url.pathname)) {\n        rc.push(route);\n      }\n    }\n    return rc;\n  }\n\n  getCompiledRoute(request: Request, response: Response) {\n    for (const check of this.checks) {\n      if (!check(request, response)) {\n        return undefined;\n      }\n    }\n\n    for (const route of this.routes) {\n      if (!route.test(request)) {\n        continue;\n      }\n      const match = route.match(request);\n      if (!match) {\n        continue;\n      }\n\n      let allChecksPass = true;\n      for (const check of route.getChecks()) {\n        if (!check(request, response)) {\n          allChecksPass = false;\n          break;\n        }\n      }\n      if (!allChecksPass) {\n        continue;\n      }\n\n      request.params = match.params;\n      return new CompiledRoute(route, request, response, this.middlewares);\n    }\n\n    return undefined;\n  }\n}\n"],"mappings":"AAAA,OAAO,UAAU;AACjB,OAAO,aAAa;AACpB,SAAS,qBAAqB;AAG9B,SAAS,aAAa;AAUf,MAAM,OAAO;AAAA,EACV,cAAoC,CAAC;AAAA,EACrC,SAAuB,CAAC;AAAA,EAChC,SAAkB,CAAC;AAAA,EACnB,SAAS,SAAuBA,OAAc,SAAsB;AAClE,UAAM,QAAe,IAAI,MAAM,SAASA,OAAM,OAAO;AACrD,SAAK,OAAO,KAAK,KAAK;AACtB,WAAO;AAAA,EACT;AAAA,EAEA,iBAAiB;AACf,WAAO,CAAC,GAAG,KAAK,WAAW;AAAA,EAC7B;AAAA,EAEA,SAAS,QAAmC;AAC1C,SAAK,SAAS,KAAK,OAAO,OAAO,MAAM;AACvC,WAAO;AAAA,EACT;AAAA,EAEA,YAAY;AACV,WAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EACxB;AAAA,EAEA,cAAc,YAAmC;AAC/C,UAAM,WAAW,WAAW,YAAY;AACxC,eAAW,SAAS,WAAW,QAAQ;AACrC,YAAM,UAAU,KAAK,KAAK,UAAU,MAAM,IAAI;AAC9C,WAAK,SAAS,MAAM,SAAS,SAAS,OAAO,KAAc,QAAkB;AAC3E,cAAM,qBAAqB,WAAW,YAAY;AAEl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