@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.
27 lines (26 loc) • 1.13 kB
JavaScript
import { ParamsTokenFactory } from './params-token-factory.js';
const isThenable = (value) => value !== null &&
(typeof value === 'object' || typeof value === 'function') &&
typeof value.then === 'function';
export class PipesConsumer {
paramsTokenFactory = new ParamsTokenFactory();
async apply(value, metadata, pipes) {
const token = this.paramsTokenFactory.exchangeEnumForString(metadata.type);
return this.applyPipes(value, {
metatype: metadata.metatype,
type: token,
data: metadata.data,
schema: metadata.schema,
}, pipes);
}
async applyPipes(value, { metatype, type, data, schema }, transforms) {
// A custom param decorator with an async factory yields a promise, and
// the pipes must see the value it resolves to. Any other value goes to
// the first pipe right away.
let result = isThenable(value) ? await value : value;
for (const pipe of transforms) {
result = await pipe.transform(result, { metatype, type, data, schema });
}
return result;
}
}