UNPKG

@kitstack/nest-powertools

Version:

A comprehensive collection of NestJS powertools, decorators, and utilities to supercharge your backend development

98 lines 3.44 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompositeGuardHelper = exports.CompositeGuard = void 0; const common_1 = require("@nestjs/common"); let CompositeGuard = class CompositeGuard { constructor(config) { this.config = config; } async canActivate(context) { const { guards, logic } = this.config; switch (logic) { case 'AND': return this.evaluateAnd(guards, context); case 'OR': return this.evaluateOr(guards, context); case 'NOT': return this.evaluateNot(guards, context); default: throw new Error(`Unsupported guard logic: ${logic}`); } } async evaluateAnd(guards, context) { for (const guard of guards) { const guardInstance = this.createGuardInstance(guard); const result = await guardInstance.canActivate(context); if (!result) { return false; } } return true; } async evaluateOr(guards, context) { for (const guard of guards) { const guardInstance = this.createGuardInstance(guard); const result = await guardInstance.canActivate(context); if (result) { return true; } } return false; } async evaluateNot(guards, context) { if (guards.length === 0) { return true; } const guardInstance = this.createGuardInstance(guards[0]); const result = await guardInstance.canActivate(context); return !result; } createGuardInstance(guard) { if (typeof guard === 'function') { return new guard(); } return guard; } }; exports.CompositeGuard = CompositeGuard; exports.CompositeGuard = CompositeGuard = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [Object]) ], CompositeGuard); class CompositeGuardHelper { static And(...guards) { return new CompositeGuard({ guards, logic: 'AND', name: 'AndGuard', }); } static Or(...guards) { return new CompositeGuard({ guards, logic: 'OR', name: 'OrGuard', }); } static Not(guard) { return new CompositeGuard({ guards: [guard], logic: 'NOT', name: 'NotGuard', }); } static Custom(config) { return new CompositeGuard(config); } } exports.CompositeGuardHelper = CompositeGuardHelper; //# sourceMappingURL=composite-guard.hook.js.map