UNPKG

@nestia/core

Version:

Super-fast validation decorators of NestJS

144 lines 7.98 kB
"use strict"; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) { if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : ""; return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EncryptedModule = EncryptedModule; const common_1 = require("@nestjs/common"); const EncryptedConstant_1 = require("./internal/EncryptedConstant"); const load_controller_1 = require("./internal/load_controller"); /** * Encrypted module. * * `EncryptedModule` is an extension of the {@link Module} class decorator * function who configures encryption password of the AES-128/256 algorithm. The * encryption algorithm and password would be used by {@link EncryptedRoute} and * {@link EncryptedBody} to encrypt the request and response bod of the HTTP * protocol. * * By using this `EncryptedModule` decorator function, all of the * {@link Controller controllers} configured in the _metadata_ would be * automatically changed to the {@link EncryptedController} with the _password_. * If there're some original {@link EncryptedController} decorated classes in the * _metadata_, their encryption password would be kept. * * Therefore, if you're planning to place original {@link EncryptedController} * decorated classes in the _metadata_, I hope them to have different encryption * password from the module level. If not, I recommend you use the * {@link Controller} decorator function instead. * * In addition, the `EncryptedModule` supports a convenient dynamic controller * importing function, {@link EncryptedModule.dynamic}. If you utilize the * function with directory path of the controller classes, it imports and * configures the controller classes into the `Module`, automatically. * * @author Jeongho Nam - https://github.com/samchon * @param metadata Module configuration metadata * @param password Encryption password or its getter function * @returns Class decorator */ function EncryptedModule(metadata, password) { return function (target) { (0, common_1.Module)(metadata)(target); iterate(password)(target); }; } (function (EncryptedModule) { /** * Dynamic encrypted module. * * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} * function who configures controller classes by the dynamic importing. By * specifying directory path of the controller classes, those controllers * would be automatically imported and configured. * * @param path Directory path of the controller classes * @param password Encryption password or its getter function * @param options Additional options except controller * @returns Class decorated module instance */ function dynamic(path_1, password_1) { return __awaiter(this, arguments, void 0, function* (path, password, options = {}, isTsNode) { // LOAD CONTROLLERS const controllers = yield (0, load_controller_1.load_controllers)(path, isTsNode); // RETURNS WITH DECORATING let NestiaModule = (() => { let _classDecorators = [EncryptedModule(Object.assign(Object.assign({}, options), { controllers }), typeof password === "object" ? () => password : password)]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; var NestiaModule = _classThis = class { }; __setFunctionName(_classThis, "NestiaModule"); (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); NestiaModule = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); __runInitializers(_classThis, _classExtraInitializers); })(); return NestiaModule = _classThis; })(); return NestiaModule; }); } EncryptedModule.dynamic = dynamic; })(EncryptedModule || (exports.EncryptedModule = EncryptedModule = {})); /** @internal */ const iterate = (password) => (modulo) => { const imports = Reflect.getMetadata("imports", modulo); if (Array.isArray(imports)) for (const imp of imports) if (typeof imp === "function") iterate(password)(imp); const controllers = Reflect.getMetadata("controllers", modulo); if (Array.isArray(controllers)) for (const c of controllers) if (typeof c === "function") Reflect.defineMetadata(EncryptedConstant_1.ENCRYPTION_METADATA_KEY, password, c); }; //# sourceMappingURL=EncryptedModule.js.map