Skip to content

Commit

Permalink
docs(core): fix type def of ModuleRef#get and #resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Jan 7, 2023
1 parent a545127 commit 3c669cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
39 changes: 34 additions & 5 deletions packages/core/injector/module-ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IntrospectionResult, Scope, Type } from '@nestjs/common';
import { GetOrResolveOptions } from '@nestjs/common/interfaces';
import { getClassScope } from '../helpers/get-class-scope';
import { isDurable } from '../helpers/is-durable';
import { AbstractInstanceResolver } from './abstract-instance-resolver';
Expand All @@ -9,6 +8,20 @@ import { InstanceLinksHost } from './instance-links-host';
import { ContextId, InstanceWrapper } from './instance-wrapper';
import { Module } from './module';

export interface ModuleRefGetOrResolveOpts {
/**
* If enabled, lookup will only be performed in the host module.
* @default true
*/
strict?: boolean;
/**
* If enabled, instead of returning a first instance registered under a given token,
* a list of instances will be returned.
* @default false
*/
each?: boolean;
}

export abstract class ModuleRef extends AbstractInstanceResolver {
protected readonly injector = new Injector();
private _instanceLinksHost: InstanceLinksHost;
Expand Down Expand Up @@ -37,23 +50,39 @@ export abstract class ModuleRef extends AbstractInstanceResolver {
*/
abstract get<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
options: { strict?: boolean; each?: undefined | false },
options: {
/**
* If enabled, lookup will only be performed in the host module.
* @default true
*/
strict?: boolean;
/** This indicates that only the first instance registered will be returned. */
each?: undefined | false;
},
): TResult;
/**
* Retrieves a list of instances of either injectables or controllers, otherwise, throws exception.
* @returns {Array<TResult>}
*/
abstract get<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
options: { strict?: boolean; each: true },
options: {
/**
* If enabled, lookup will only be performed in the host module.
* @default true
*/
strict?: boolean;
/** This indicates that a list of instances will be returned. */
each: true;
},
): Array<TResult>;
/**
* Retrieves an instance (or a list of instances) of either injectable or controller, otherwise, throws exception.
* @returns {TResult | Array<TResult>}
*/
abstract get<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
options?: GetOrResolveOptions,
options?: ModuleRefGetOrResolveOpts,
): TResult | Array<TResult>;

/**
Expand Down Expand Up @@ -96,7 +125,7 @@ export abstract class ModuleRef extends AbstractInstanceResolver {
abstract resolve<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | Function | string | symbol,
contextId?: { id: number },
options?: GetOrResolveOptions,
options?: ModuleRefGetOrResolveOpts,
): Promise<TResult | Array<TResult>>;

public abstract create<T = any>(type: Type<T>): Promise<T>;
Expand Down
7 changes: 3 additions & 4 deletions packages/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
DynamicModule,
ExistingProvider,
FactoryProvider,
GetOrResolveOptions,
Injectable,
InjectionToken,
NestModule,
Expand Down Expand Up @@ -33,7 +32,7 @@ import { isDurable } from '../helpers/is-durable';
import { CONTROLLER_ID_KEY } from './constants';
import { NestContainer } from './container';
import { InstanceWrapper } from './instance-wrapper';
import { ModuleRef } from './module-ref';
import { ModuleRefGetOrResolveOpts, ModuleRef } from './module-ref';

/**
* @note
Expand Down Expand Up @@ -521,7 +520,7 @@ export class Module {

public get<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | string | symbol,
options: GetOrResolveOptions = {},
options: ModuleRefGetOrResolveOpts = {},
): TResult | Array<TResult> {
options.strict ??= true;
options.each ??= false;
Expand All @@ -540,7 +539,7 @@ export class Module {
public resolve<TInput = any, TResult = TInput>(
typeOrToken: Type<TInput> | string | symbol,
contextId = createContextId(),
options: GetOrResolveOptions = {},
options: ModuleRefGetOrResolveOpts = {},
): Promise<TResult | Array<TResult>> {
options.strict ??= true;
options.each ??= false;
Expand Down

0 comments on commit 3c669cc

Please sign in to comment.