UNPKG

@duongtrungnguyen/nestro

Version:
101 lines 3.82 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index); import { Injectable, Inject, RequestMethod, HttpStatus } from "@nestjs/common"; import { lastValueFrom, Observable } from "rxjs"; import { ModuleRef } from "@nestjs/core"; import { match } from "path-to-regexp"; import { GLOBAL_GUARDS, PROXY_ROUTES_CONFIG } from "../constants"; let RouteHandleService = class { constructor(routesConfig, globalGuards, moduleRef) { this.routesConfig = routesConfig; this.globalGuards = globalGuards; this.moduleRef = moduleRef; } findMatchingRoute(path) { return this.routesConfig.find((config) => match(config.route)(path)); } async checkGuards(guards = [], req, res) { const allGuards = [...this.globalGuards, ...guards]; for (let i = 0; i < allGuards.length; i++) { const guard = allGuards[i]; const isGlobalGuard = i < this.globalGuards.length; const shouldRun = typeof guard === "function" ? true : isGlobalGuard || this.shouldApplyHook(req, guard.includes, guard.excludes); if (!shouldRun) continue; const instance = typeof guard === "function" ? guard : guard.instance; const context = this.createExecutionContext(req, res); const result = await this.executeGuard(instance, context); if (!result) { if ("switchToWs" in context) { const socket = context.switchToWs().getClient(); socket.emit("error", { message: "Unauthorized", timestamp: (/* @__PURE__ */ new Date()).toISOString() }); socket.disconnect(); } else { res.status(401).json({ message: "Unauthorized", timestamp: (/* @__PURE__ */ new Date()).toISOString(), statusCode: HttpStatus.UNAUTHORIZED }); } return false; } } return true; } shouldApplyHook(req, includes, excludes) { const isSocket = "handshake" in req; const method = isSocket ? "WS" : RequestMethod[req.method]; const url = isSocket ? req.handshake.url : req.url; const isMatch = (routes, defaultV = true) => routes?.some((route) => (route.method === method || route.method === RequestMethod.ALL || method === "WS") && match(route.path)(url)) ?? defaultV; return isMatch(includes) && !isMatch(excludes, false); } async executeGuard(Guard, context) { const instance = typeof Guard === "function" ? Guard : await this.moduleRef.resolve(Guard); try { const result = await instance.canActivate(context); if (result instanceof Observable) { return await lastValueFrom(result); } return result; } catch { return false; } } createExecutionContext(req, res) { const isSocket = "handshake" in req; if (isSocket) { return { switchToWs: () => ({ getClient: () => req }) }; } return { switchToHttp: () => ({ getRequest: () => req, getResponse: () => res }) }; } }; RouteHandleService = __decorateClass([ Injectable(), __decorateParam(0, Inject(PROXY_ROUTES_CONFIG)), __decorateParam(1, Inject(GLOBAL_GUARDS)), __decorateParam(2, Inject(ModuleRef)) ], RouteHandleService); export { RouteHandleService }; //# sourceMappingURL=route-handle.service.js.map