UNPKG

@nestjs/core

Version:

The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.

54 lines (53 loc) • 1.93 kB
import { getClassScope } from '../helpers/get-class-scope.js'; import { isDurable } from '../helpers/is-durable.js'; import { InstanceWrapper } from '../injector/instance-wrapper.js'; export class MiddlewareContainer { container; middleware = new Map(); configurationSets = new Map(); constructor(container) { this.container = container; } getMiddlewareCollection(moduleKey) { if (!this.middleware.has(moduleKey)) { const moduleRef = this.container.getModuleByKey(moduleKey); this.middleware.set(moduleKey, moduleRef.middlewares); } return this.middleware.get(moduleKey); } getConfigurations() { return this.configurationSets; } /** * Gives the module its place among the configurations, which are kept in * the order of insertion, before its configurations are known. */ reserveConfig(moduleKey) { this.getTargetConfig(moduleKey); } insertConfig(configList, moduleKey) { const middleware = this.getMiddlewareCollection(moduleKey); const targetConfig = this.getTargetConfig(moduleKey); const configurations = configList || []; const insertMiddleware = (metatype) => { const token = metatype; middleware.set(token, new InstanceWrapper({ scope: getClassScope(metatype), durable: isDurable(metatype), name: token?.name ?? token, metatype, token, })); }; configurations.forEach(config => { [].concat(config.middleware).map(insertMiddleware); targetConfig.add(config); }); } getTargetConfig(moduleName) { if (!this.configurationSets.has(moduleName)) { this.configurationSets.set(moduleName, new Set()); } return this.configurationSets.get(moduleName); } }