UNPKG

eventstore-interconnect

Version:
54 lines 2.86 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NextEventsValidatorService = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("../../constants"); const class_validator_1 = require("class-validator"); const invalid_event_error_1 = require("../errors/invalid-event.error"); const not_allowed_event_error_1 = require("../errors/not-allowed-event.error"); const safety_net_1 = require("../../safety-net"); const class_transformer_1 = require("class-transformer"); let NextEventsValidatorService = class NextEventsValidatorService { constructor(allowedEvents, safetyNet) { this.allowedEvents = allowedEvents; this.safetyNet = safetyNet; } async validate(event) { const eventInstance = this.tryToInstantiateEvent(event.event); const concatErrors = await (0, class_validator_1.validate)(eventInstance['data']); if (concatErrors.length > 0) { this.safetyNet.invalidEventHook(event); throw new invalid_event_error_1.InvalidEventError(event, JSON.stringify(concatErrors)); } } tryToInstantiateEvent(event) { try { const instance = new this.allowedEvents[event.type](event.data); return (0, class_transformer_1.plainToClassFromExist)(this.allowedEvents[event.type], instance); } catch (e) { this.safetyNet.invalidEventHook(event); throw new not_allowed_event_error_1.NotAllowedEventError(this.allowedEvents); } } }; NextEventsValidatorService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_1.ALLOWED_EVENTS)), __param(1, (0, common_1.Inject)(safety_net_1.SAFETY_NET)), __metadata("design:paramtypes", [Object, Object]) ], NextEventsValidatorService); exports.NextEventsValidatorService = NextEventsValidatorService; //# sourceMappingURL=next-events-validator.service.js.map