@nestia/core
Version:
Super-fast validation decorators of NestJS
100 lines • 5.19 kB
JavaScript
;
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 __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());
});
};
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 = class NestiaModule {
};
NestiaModule = __decorate([
EncryptedModule(Object.assign(Object.assign({}, options), { controllers }), typeof password === "object" ? () => password : password)
], NestiaModule);
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