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.

86 lines (85 loc) • 3.58 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 { PIPES_METADATA, isEmpty, isFunction, } from '@nestjs/common/internal'; export class PipesContextCreator extends ContextCreator { container; config; moduleContext; constructor(container, config) { super(); this.container = container; this.config = config; } create(instance, callback, moduleKey, contextId = STATIC_CONTEXT, inquirerId) { this.moduleContext = moduleKey; return this.createContext(instance, callback, PIPES_METADATA, contextId, inquirerId); } createConcreteContext(metadata, contextId = STATIC_CONTEXT, inquirerId) { if (isEmpty(metadata)) { return []; } return iterate(metadata) .filter((pipe) => pipe && (isFunction(pipe) || isFunction(pipe.transform))) .map(pipe => this.getPipeInstance(pipe, contextId, inquirerId)) .filter(pipe => !!pipe && pipe.transform && isFunction(pipe.transform)) .toArray(); } getPipeInstance(pipe, contextId = STATIC_CONTEXT, inquirerId) { if (!isFunction(pipe)) { return pipe; } const instanceWrapper = this.getInstanceByMetatype(pipe); if (!instanceWrapper) { return this.pickEnhancer(pipe, null, 'transform'); } const instanceHost = instanceWrapper.getInstanceByContextId(this.getContextId(contextId, instanceWrapper), inquirerId); const enhancer = this.pickEnhancer(pipe, instanceHost && instanceHost.instance, 'transform'); // Nest constructed the bound class, and neither the instance nor the // class can run as a pipe. A request-scoped or transient pipe 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, 'pipe', describeEnhancer(pipe)); } 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 globalPipes = this.config.getGlobalPipes(); if (contextId === STATIC_CONTEXT && !inquirerId) { return globalPipes; } const scopedPipeWrappers = this.config.getGlobalRequestPipes(); const scopedPipes = iterate(scopedPipeWrappers) .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, 'pipe'); } return host.instance; }) .toArray(); return globalPipes.concat(scopedPipes); } setModuleContext(context) { this.moduleContext = context; } }