UNPKG

@nestjs/common

Version:

Decorators, pipes, exceptions and utilities for NestJS applications: @Controller(), @Injectable(), @Body(), ValidationPipe, HttpException and more.

55 lines (54 loc) • 2.39 kB
import { uid } from 'uid'; import { INJECTABLE_WATERMARK, PARAMTYPES_METADATA, SCOPE_OPTIONS_METADATA, } from '../../constants.js'; /** * Decorator that marks a class as a [provider](https://docs.nestjs.com/providers). * Providers can be injected into other classes via constructor parameter injection * using Nest's built-in [Dependency Injection (DI)](https://docs.nestjs.com/providers#dependency-injection) * system. * * When injecting a provider, it must be visible within the module scope (loosely * speaking, the containing module) of the class it is being injected into. This * can be done by: * * - defining the provider in the same module scope * - exporting the provider from one module scope and importing that module into the * module scope of the class being injected into * - exporting the provider from a module that is marked as global using the * `@Global()` decorator * * Providers can also be defined in a more explicit and imperative form using * various [custom provider](https://docs.nestjs.com/fundamentals/custom-providers) techniques that expose * more capabilities of the DI system. * * @param options options specifying scope of injectable * * @see [Providers](https://docs.nestjs.com/providers) * @see [Custom Providers](https://docs.nestjs.com/fundamentals/custom-providers) * @see [Injection Scopes](https://docs.nestjs.com/fundamentals/injection-scopes) * * @publicApi */ export function Injectable(options) { return (target) => { Reflect.defineMetadata(INJECTABLE_WATERMARK, true, target); Reflect.defineMetadata(SCOPE_OPTIONS_METADATA, options, target); }; } /** * @publicApi */ export function mixin(mixinClass) { Object.defineProperty(mixinClass, 'name', { value: uid(21), }); // The class may declare a scope, on itself or on a class it extends, and // `@Injectable()` with no options would replace it with none Injectable(Reflect.getMetadata(SCOPE_OPTIONS_METADATA, mixinClass))(mixinClass); // Decorated here, at runtime, so a compiler that emits decorator metadata may // have had no decorator to emit parameter types for. The injector can't take // that for a compiler that emits none. if (!Reflect.hasMetadata(PARAMTYPES_METADATA, mixinClass)) { Reflect.defineMetadata(PARAMTYPES_METADATA, [], mixinClass); } return mixinClass; }