INestApplicationContext

Interface defining NestApplicationContext.


interface INestApplicationContext {
  select<T>(module: Type<T> | DynamicModule, options?: SelectOptions): INestApplicationContext
  get<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>): TResult
  resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>): Promise<TResult>
  registerRequestByContextId<T = any>(request: T, contextId: { id: number; }): void
  close(): Promise<void>
  useLogger(logger: false | LoggerService | LogLevel[]): void
  flushLogs(): void
  enableShutdownHooks(signals?: string[] | ShutdownSignal[]): this
  init(): Promise<this>
}

Methods

select()

Allows navigating through the modules tree, for example, to pull out a specific instance from the selected module.


select<T>(module: Type<T> | DynamicModule, options?: SelectOptions): INestApplicationContext

Parameters

Option Type Description
module Type | DynamicModule
options SelectOptions

Optional. Default is undefined.

Returns

INestApplicationContext

get()

Retrieves an instance of either injectable or controller, otherwise, throws exception.

Overload #1

get<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, options: { strict?: boolean; each?: false; }): TResult

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
options object

Returns

TResult


Overload #2

Retrieves a list of instances of either injectables or controllers, otherwise, throws exception.


get<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, options: { strict?: boolean; each: true; }): Array<TResult>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
options object

Returns

Array<TResult>


Overload #3

Retrieves an instance (or a list of instances) of either injectable or controller, otherwise, throws exception.


get<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, options?: GetOrResolveOptions): TResult | Array<TResult>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
options GetOrResolveOptions

Optional. Default is undefined.

Returns

TResult | Array<TResult>

resolve()

Resolves transient or request-scoped instance of either injectable or controller, otherwise, throws exception.

Overload #1

resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, contextId?: { id: number; }): Promise<TResult>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
contextId { id: number; }

Optional. Default is undefined.

Returns

Promise<TResult>


Overload #2

resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, contextId?: { id: number; }, options?: { strict?: boolean; each?: false; }): Promise<TResult>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
contextId { id: number; }

Optional. Default is undefined.

options object

Optional. Default is undefined.

Returns

Promise<TResult>


Overload #3

Resolves transient or request-scoped instances of either injectables or controllers, otherwise, throws exception.


resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, contextId?: { id: number; }, options?: { strict?: boolean; each: true; }): Promise<Array<TResult>>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
contextId { id: number; }

Optional. Default is undefined.

options object

Optional. Default is undefined.

Returns

Promise<Array<TResult>>


Overload #4

Resolves transient or request-scoped instance (or a list of instances) of either injectable or controller, otherwise, throws exception.


resolve<TInput = any, TResult = TInput>(typeOrToken: string | symbol | Function | Type<TInput>, contextId?: { id: number; }, options?: GetOrResolveOptions): Promise<TResult | Array<TResult>>

Parameters

Option Type Description
typeOrToken string | symbol | Function | Type
contextId { id: number; }

Optional. Default is undefined.

options GetOrResolveOptions

Optional. Default is undefined.

Returns

Promise<TResult | Array<TResult>>

registerRequestByContextId()

Registers the request/context object for a given context ID (DI container sub-tree).


registerRequestByContextId<T = any>(request: T, contextId: { id: number; }): void

Parameters

Option Type Description
request T
contextId { id: number; }

Returns

void

close()

Terminates the application


close(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>

useLogger()

Sets custom logger service. Flushes buffered logs if auto flush is on.


useLogger(logger: false | LoggerService | LogLevel[]): void

Parameters

Option Type Description
logger false | LoggerService | LogLevel[]

Returns

void

flushLogs()

Prints buffered logs and detaches buffer.


flushLogs(): void

Parameters

There are no parameters.

Returns

void

enableShutdownHooks()

Enables the usage of shutdown hooks. Will call the onApplicationShutdown function of a provider if the process receives a shutdown signal.


enableShutdownHooks(signals?: string[] | ShutdownSignal[]): this

Parameters

Option Type Description
signals string[] | ShutdownSignal[]

Optional. Default is undefined.

Returns

this The Nest application context instance

init()

Initializes the Nest application. Calls the Nest lifecycle events. It isn't mandatory to call this method directly.


init(): Promise<this>

Parameters

There are no parameters.

Returns

Promise<this> The NestApplicationContext instance as Promise