NestApplicationContext

  
class NestApplicationContext<TOptions extends NestApplicationContextOptions = NestApplicationContextOptions> extends AbstractInstanceResolver implements INestApplicationContext {
  constructor(container: NestContainer, appOptions: TOptions = {} as TOptions, contextModule: Module = null, scope: Type<any>[] = new Array<Type<any>>())
  protected isInitialized: false
  protected injector: Injector
  protected logger: Logger
  protected instanceLinksHost
  protected container: NestContainer
  protected appOptions: TOptions
  selectContextModule()
  select<T>(moduleType: any): INestApplicationContext
  get<TInput = any, TResult = TInput>(typeOrToken: any, options: GetOrResolveOptions = { strict: false }): TResult | Array<TResult>
  resolve<TInput = any, TResult = TInput>(typeOrToken: any, contextId: ContextId = createContextId(), options: GetOrResolveOptions = { strict: false }): Promise<TResult | Array<TResult>>
  registerRequestByContextId<T = any>(request: T, contextId: ContextId)
  init(): Promise<this>
  close(signal?: string): Promise<void>
  useLogger(logger: any)
  flushLogs()
  flushLogsOnOverride()
  enableShutdownHooks(signals: any[] = []): this
  protected dispose(): Promise<void>
  protected listenToShutdownSignals(signals: string[])
  protected unsubscribeFromProcessSignals()
  protected callInitHook(): Promise<void>
  protected callDestroyHook(): Promise<void>
  protected callBootstrapHook(): Promise<void>
  protected callShutdownHook(signal?: string): Promise<void>
  protected callBeforeShutdownHook(signal?: string): Promise<void>
  protected assertNotInPreviewMode(methodName: string)
}

Subclasses

Constructor


constructor(container: NestContainer, appOptions: TOptions = {} as TOptions, contextModule: Module = null, scope: Type<any>[] = new Array<Type<any>>())

Parameters

Option Type Description
container NestContainer
appOptions TOptions

Optional. Default is {} as TOptions.

contextModule Module

Optional. Default is null.

scope Type[]

Optional. Default is new Array<Type<any>>().

Properties

Property Description
protected isInitialized: false
protected injector: Injector
protected logger: Logger Read-only.
protected instanceLinksHost Read-only.
protected container: NestContainer Read-only. Declared in constructor.
protected appOptions: TOptions Read-only. Declared in constructor.

Methods

selectContextModule()


selectContextModule()

Parameters

There are no parameters.

select()

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


select<T>(moduleType: any): INestApplicationContext

Parameters

Option Type Description
moduleType any

Returns

INestApplicationContext

get()

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

Overload #1

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


get<TInput = any, TResult = TInput>(typeOrToken: any): TResult

Parameters

Option Type Description
typeOrToken any

Returns

TResult


Overload #2

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


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

Parameters

Option Type Description
typeOrToken any
options object

Returns

TResult


Overload #3

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


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

Parameters

Option Type Description
typeOrToken any
options object

Returns

Array<TResult>

resolve()

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

Overload #1

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


resolve<TInput = any, TResult = TInput>(typeOrToken: any): Promise<TResult>

Parameters

Option Type Description
typeOrToken any

Returns

Promise<TResult>


Overload #2

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


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

Parameters

Option Type Description
typeOrToken any
contextId { id: number; }

Optional. Default is undefined.

Returns

Promise<TResult>


Overload #3

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


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

Parameters

Option Type Description
typeOrToken any
contextId { id: number; }

Optional. Default is undefined.

options object

Optional. Default is undefined.

Returns

Promise<TResult>


Overload #4

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


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

Parameters

Option Type Description
typeOrToken any
contextId { id: number; }

Optional. Default is undefined.

options object

Optional. Default is undefined.

Returns

Promise<Array<TResult>>

registerRequestByContextId()

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


registerRequestByContextId<T = any>(request: T, contextId: ContextId)

Parameters

Option Type Description
request T
contextId ContextId

Returns

[object Object]

init()

Initializes the Nest application. Calls the Nest lifecycle events.


init(): Promise<this>

Parameters

There are no parameters.

Returns

Promise<this> The NestApplicationContext instance as Promise

close()

Terminates the application


close(signal?: string): Promise<void>

Parameters

Option Type Description
signal string

Optional. Default is undefined.

Returns

Promise<void>

useLogger()

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


useLogger(logger: any)

Parameters

Option Type Description
logger any

Returns

[object Object]

flushLogs()

Prints buffered logs and detaches buffer.


flushLogs()

Parameters

There are no parameters.

Returns

[object Object]

flushLogsOnOverride()

Define that it must flush logs right after defining a custom logger.


flushLogsOnOverride()

Parameters

There are no parameters.

enableShutdownHooks()

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


enableShutdownHooks(signals: any[] = []): this

Parameters

Option Type Description
signals any[]

Optional. Default is [].

Returns

this The Nest application context instance

dispose()


protected dispose(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>

listenToShutdownSignals()

Listens to shutdown signals by listening to process events


protected listenToShutdownSignals(signals: string[])

Parameters

Option Type Description
signals string[]

The system signals it should listen to

unsubscribeFromProcessSignals()

Unsubscribes from shutdown signals (process events)


protected unsubscribeFromProcessSignals()

Parameters

There are no parameters.

callInitHook()

Calls the onModuleInit function on the registered modules and its children.


protected callInitHook(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>

callDestroyHook()

Calls the onModuleDestroy function on the registered modules and its children.


protected callDestroyHook(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>

callBootstrapHook()

Calls the onApplicationBootstrap function on the registered modules and its children.


protected callBootstrapHook(): Promise<void>

Parameters

There are no parameters.

Returns

Promise<void>

callShutdownHook()

Calls the onApplicationShutdown function on the registered modules and children.


protected callShutdownHook(signal?: string): Promise<void>

Parameters

Option Type Description
signal string

Optional. Default is undefined.

Returns

Promise<void>

callBeforeShutdownHook()

Calls the beforeApplicationShutdown function on the registered modules and children.


protected callBeforeShutdownHook(signal?: string): Promise<void>

Parameters

Option Type Description
signal string

Optional. Default is undefined.

Returns

Promise<void>

assertNotInPreviewMode()


protected assertNotInPreviewMode(methodName: string)

Parameters

Option Type Description
methodName string