MCPcopy
hub / github.com/nestjs/nest / Inject

Function Inject

packages/common/decorators/core/inject.decorator.ts:38–68  ·  view source on GitHub ↗
(
  token?: InjectionToken | ForwardReference,
)

Source from the content-addressed store, hash-verified

36 * @publicApi
37 */
38export function Inject(
39 token?: InjectionToken | ForwardReference,
40): PropertyDecorator & ParameterDecorator {
41 const injectCallHasArguments = arguments.length > 0;
42
43 return (target: object, key: string | symbol | undefined, index?: number) => {
44 let type = token || Reflect.getMetadata('design:type', target, key!);
45 // Try to infer the token in a constructor-based injection
46 if (!type && !injectCallHasArguments) {
47 type = Reflect.getMetadata(PARAMTYPES_METADATA, target, key!)?.[index!];
48 }
49
50 if (!isUndefined(index)) {
51 let dependencies =
52 Reflect.getMetadata(SELF_DECLARED_DEPS_METADATA, target) || [];
53
54 dependencies = [...dependencies, { index, param: type }];
55 Reflect.defineMetadata(SELF_DECLARED_DEPS_METADATA, dependencies, target);
56 return;
57 }
58 let properties =
59 Reflect.getMetadata(PROPERTY_DEPS_METADATA, target.constructor) || [];
60
61 properties = [...properties, { key, type }];
62 Reflect.defineMetadata(
63 PROPERTY_DEPS_METADATA,
64 properties,
65 target.constructor,
66 );
67 };
68}

Callers 15

BaseExceptionFilterClass · 0.90
MainTestClass · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90

Calls 2

isUndefinedFunction · 0.90
getMetadataMethod · 0.45

Tested by 14

constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72