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.

84 lines (83 loc) • 3.72 kB
import { iterate } from 'iterare'; import { assertEnhancer, describeEnhancer, } from '../helpers/assert-enhancer.js'; import { ContextCreator } from '../helpers/context-creator.js'; import { STATIC_CONTEXT } from '../injector/constants.js'; import { INTERCEPTORS_METADATA, isEmpty, isFunction, } from '@nestjs/common/internal'; export class InterceptorsContextCreator extends ContextCreator { container; config; moduleContext; constructor(container, config) { super(); this.container = container; this.config = config; } create(instance, callback, module, contextId = STATIC_CONTEXT, inquirerId) { this.moduleContext = module; return this.createContext(instance, callback, INTERCEPTORS_METADATA, contextId, inquirerId); } createConcreteContext(metadata, contextId = STATIC_CONTEXT, inquirerId) { if (isEmpty(metadata)) { return []; } return iterate(metadata) .filter(interceptor => interceptor && (isFunction(interceptor) || isFunction(interceptor.intercept))) .map(interceptor => this.getInterceptorInstance(interceptor, contextId, inquirerId)) .filter((interceptor) => interceptor ? isFunction(interceptor.intercept) : false) .toArray(); } getInterceptorInstance(metatype, contextId = STATIC_CONTEXT, inquirerId) { if (!isFunction(metatype)) { return metatype; } const instanceWrapper = this.getInstanceByMetatype(metatype); if (!instanceWrapper) { return this.pickEnhancer(metatype, null, 'intercept'); } const instanceHost = instanceWrapper.getInstanceByContextId(this.getContextId(contextId, instanceWrapper), inquirerId); const enhancer = this.pickEnhancer(metatype, instanceHost && instanceHost.instance, 'intercept'); // Nest constructed the bound class, and neither the instance nor the // class can run as a interceptor. A request-scoped or transient interceptor outside of // its context is only a placeholder whose constructor never ran, and a // class that resolves to no provider (a host outside of every module) // is skipped as before. if (instanceHost?.isConstructorCalled) { assertEnhancer(enhancer, 'interceptor', describeEnhancer(metatype)); } return enhancer; } getInstanceByMetatype(metatype) { if (!this.moduleContext) { return; } const collection = this.container.getModules(); const moduleRef = collection.get(this.moduleContext); if (!moduleRef) { return; } return moduleRef.injectables.get(metatype); } getGlobalMetadata(contextId = STATIC_CONTEXT, inquirerId) { if (!this.config) { return []; } const globalInterceptors = this.config.getGlobalInterceptors(); if (contextId === STATIC_CONTEXT && !inquirerId) { return globalInterceptors; } const scopedInterceptorWrappers = this.config.getGlobalRequestInterceptors(); const scopedInterceptors = iterate(scopedInterceptorWrappers) .map(wrapper => wrapper.getInstanceByContextId(this.getContextId(contextId, wrapper), inquirerId)) .filter(host => !!host) .map(host => { // A scoped instance only exists here, once per context if (host.isConstructorCalled) { assertEnhancer(host.instance, 'interceptor'); } return host.instance; }) .toArray(); return globalInterceptors.concat(scopedInterceptors); } }