@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.
107 lines (106 loc) • 4.71 kB
JavaScript
import { Logger } from '@nestjs/common';
import { ValidationPipe } from '@nestjs/common/pipes/validation.pipe.js';
import { MISSING_HANDLER_METADATA_MESSAGE } from '../errors/messages.js';
import { getClassRef, getParamsHost } from './class-ref-registry.js';
import { ExecutionContextHost } from './execution-context-host.js';
import { PARAMTYPES_METADATA, RESPONSE_PASSTHROUGH_METADATA, isFunction, isString, } from '@nestjs/common/internal';
// Handlers already warned about (see `warnOnMissingParamtypes()`). Shared, as
// each context creator has its own `ContextUtils`.
const handlersWithoutParamtypes = new WeakSet();
export class ContextUtils {
mapParamType(key) {
const keyPair = key.split(':');
return keyPair[0];
}
reflectCallbackParamtypes(instance, methodName) {
// The types are stored on the prototype that declared the method, which a
// method copied by a mixin helper does not share with the instance.
const paramsHost = this.getParamsHost(instance, methodName);
return ((paramsHost?.prototype &&
Reflect.getMetadata(PARAMTYPES_METADATA, paramsHost.prototype, methodName)) ??
Reflect.getMetadata(PARAMTYPES_METADATA, instance, methodName));
}
reflectCallbackMetadata(instance, methodName, metadataKey) {
return Reflect.getMetadata(metadataKey, this.getParamsHost(instance, methodName), methodName);
}
reflectPassthrough(instance, methodName) {
return Reflect.getMetadata(RESPONSE_PASSTHROUGH_METADATA, this.getParamsHost(instance, methodName), methodName);
}
/**
* Returns the class that the parameter decorators of the given handler
* stored their metadata on (see `getParamsHost`).
*/
getParamsHost(instance, methodName) {
return getParamsHost(getClassRef(instance), methodName);
}
getArgumentsLength(keys, metadata) {
return keys.length
? Math.max(...keys.map(key => metadata[key].index)) + 1
: 0;
}
createNullArray(length) {
const a = new Array(length);
for (let i = 0; i < length; ++i)
a[i] = undefined;
return a;
}
mergeParamsMetatypes(paramsProperties, paramtypes) {
if (!paramtypes) {
return paramsProperties;
}
return paramsProperties.map(param => ({
...param,
metatype: paramtypes[param.index],
}));
}
/**
* A handler has parameter types only if its compiler emits decorator
* metadata, and tsx and esbuild don't. Without them a `ValidationPipe` has
* nothing to validate a parameter against and passes it on as it came, so
* this says so once per handler, when the handler is set up. It doesn't
* throw, as the handler may not rely on the pipe. A parameter whose
* `ValidationPipe` was given an `expectedType` needs no metadata, so it isn't
* reported, and neither is a custom decorator's, which `ValidationPipe`
* skips by default.
*
* @param pipes pipes bound to the handler (global, class and method ones)
* @param paramsProperties parameters the pipes are applied to
*/
warnOnMissingParamtypes(instance, methodName, paramtypes, pipes, paramsProperties) {
const handler = instance[methodName];
if (paramtypes ||
!isFunction(handler) ||
handlersWithoutParamtypes.has(handler)) {
return;
}
const isLeftUnvalidated = ({ type, pipes: paramPipes, }) => {
if (isString(type)) {
return false;
}
const validationPipes = pipes
.concat(paramPipes ?? [])
.filter((pipe) => pipe instanceof ValidationPipe);
// `expectedType` is protected: read it the way `transform()` does
return (validationPipes.length > 0 &&
!validationPipes.some(pipe => pipe['expectedType']));
};
if (!paramsProperties.some(isLeftUnvalidated)) {
return;
}
handlersWithoutParamtypes.add(handler);
new Logger(ContextUtils.name).warn(MISSING_HANDLER_METADATA_MESSAGE(getClassRef(instance).name, methodName));
}
getCustomFactory(factory, data, contextFactory) {
return isFunction(factory)
? (...args) => factory(data, contextFactory(args))
: () => null;
}
getContextFactory(contextType, instance, callback) {
const type = instance && getClassRef(instance);
return (args) => {
const ctx = new ExecutionContextHost(args, type, callback);
ctx.setType(contextType);
return ctx;
};
}
}