UNPKG

nestjs-cls

Version:

A continuation-local storage module compatible with NestJS's dependency injection.

64 lines (63 loc) 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 ClsPluginsHooksHost_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClsPluginsHooksHost = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const is_non_nullable_1 = require("../../utils/is-non-nullable"); const cls_service_manager_1 = require("../cls-service-manager"); const cls_plugin_base_1 = require("./cls-plugin-base"); /** * This class gathers all the plugin hooks registered in the DI * and makes them available to the CLS-initializers (Cls-Middleware, -Guard, -Interceptor) * via a singleton instance. */ let ClsPluginsHooksHost = ClsPluginsHooksHost_1 = class ClsPluginsHooksHost { static getInstance() { if (!this._instance) { throw new Error('ClsPluginsHooksHost not initialized'); } return this._instance; } constructor(discoveryService) { this.discoveryService = discoveryService; this.cls = cls_service_manager_1.ClsServiceManager.getClsService(); ClsPluginsHooksHost_1._instance = this; } onModuleInit() { const hooks = this.discoveryService .getProviders() .filter((p) => (0, cls_plugin_base_1.isPluginHooksToken)(p.name)) .map((p) => p.instance); this.beforeSetupHooks = hooks .map((hook) => hook.beforeSetup) .filter(is_non_nullable_1.isNonNullable); this.afterSetupHooks = hooks .map((hook) => hook.afterSetup) .filter(is_non_nullable_1.isNonNullable); } async beforeSetup(context) { for (const hook of this.beforeSetupHooks) { await hook(this.cls, context); } } async afterSetup(context) { for (const hook of this.afterSetupHooks) { await hook(this.cls, context); } } }; exports.ClsPluginsHooksHost = ClsPluginsHooksHost; exports.ClsPluginsHooksHost = ClsPluginsHooksHost = ClsPluginsHooksHost_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [core_1.DiscoveryService]) ], ClsPluginsHooksHost);