UNPKG

@nestjs/core

Version:

Nest - modern, fast, powerful node.js web framework (@core)

97 lines (96 loc) 3.51 kB
import { SetMetadata } from '@nestjs/common'; import { uid } from 'uid'; import { isEmptyArray, isObject } from '@nestjs/common/internal'; /** * Helper class providing Nest reflection capabilities. * * @see [Reflection](https://docs.nestjs.com/guards#putting-it-all-together) * * @publicApi */ export class Reflector { static createDecorator(options = {}) { const metadataKey = options.key ?? uid(21); const decoratorFn = (metadataValue) => (target, key, descriptor) => { const value = options.transform ? options.transform(metadataValue) : metadataValue; SetMetadata(metadataKey, value ?? {})(target, key, descriptor); }; decoratorFn.KEY = metadataKey; return decoratorFn; } /** * Retrieve metadata for a specified key or decorator for a specified target. * * @example * `const roles = this.reflector.get<string[]>('roles', context.getHandler());` * * @param metadataKey lookup key or decorator for metadata to retrieve * @param target context (decorated object) to retrieve metadata from * */ get(metadataKeyOrDecorator, target) { const metadataKey = metadataKeyOrDecorator.KEY ?? metadataKeyOrDecorator; return Reflect.getMetadata(metadataKey, target); } /** * Retrieve metadata for a specified key or decorator for a specified set of targets. * * @param metadataKeyOrDecorator lookup key or decorator for metadata to retrieve * @param targets context (decorated objects) to retrieve metadata from * */ getAll(metadataKeyOrDecorator, targets) { return (targets || []).map(target => this.get(metadataKeyOrDecorator, target)); } /** * Retrieve metadata for a specified key or decorator for a specified set of targets and merge results. * * @param metadataKeyOrDecorator lookup key for metadata to retrieve * @param targets context (decorated objects) to retrieve metadata from * */ getAllAndMerge(metadataKeyOrDecorator, targets) { const metadataCollection = this.getAll(metadataKeyOrDecorator, targets).filter(item => item !== undefined); if (isEmptyArray(metadataCollection)) { return metadataCollection; } if (metadataCollection.length === 1) { const value = metadataCollection[0]; if (isObject(value)) { return value; } return metadataCollection; } return metadataCollection.reduce((a, b) => { if (Array.isArray(a)) { return a.concat(b); } if (isObject(a) && isObject(b)) { return { ...a, ...b, }; } return [a, b]; }); } /** * Retrieve metadata for a specified key or decorator for a specified set of targets and return a first not undefined value. * * @param metadataKeyOrDecorator lookup key or metadata for metadata to retrieve * @param targets context (decorated objects) to retrieve metadata from * */ getAllAndOverride(metadataKeyOrDecorator, targets) { for (const target of targets) { const result = this.get(metadataKeyOrDecorator, target); if (result !== undefined) { return result; } } return undefined; } }