forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nestjs#1163 from nestjs/feature/dynamic-create
feature(core) instantiate class dynamically (ModuleRef)
- Loading branch information
Showing
8 changed files
with
154 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { INVALID_CLASS_MESSAGE } from '../messages'; | ||
import { RuntimeException } from './runtime.exception'; | ||
|
||
export class InvalidClassException extends RuntimeException { | ||
constructor(value: any) { | ||
super(INVALID_CLASS_MESSAGE`${value}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { isFunction } from '@nestjs/common/utils/shared.utils'; | ||
import { UnknownElementException } from '../errors/exceptions/unknown-element.exception'; | ||
import { InstanceWrapper, NestContainer } from './container'; | ||
import { Module } from './module'; | ||
|
||
export class ContainerScanner { | ||
private flatContainer: Partial<Module>; | ||
|
||
constructor(private readonly container: NestContainer) {} | ||
|
||
public find<TInput = any, TResult = TInput>( | ||
typeOrToken: Type<TInput> | string | symbol, | ||
): TResult { | ||
this.initFlatContainer(); | ||
return this.findInstanceByPrototypeOrToken<TInput, TResult>( | ||
typeOrToken, | ||
this.flatContainer, | ||
); | ||
} | ||
|
||
public findInstanceByPrototypeOrToken<TInput = any, TResult = TInput>( | ||
metatypeOrToken: Type<TInput> | string | symbol, | ||
contextModule: Partial<Module>, | ||
): TResult { | ||
const dependencies = new Map([ | ||
...contextModule.components, | ||
...contextModule.routes, | ||
...contextModule.injectables, | ||
]); | ||
const name = isFunction(metatypeOrToken) | ||
? (metatypeOrToken as Function).name | ||
: metatypeOrToken; | ||
const instanceWrapper = dependencies.get(name as string); | ||
if (!instanceWrapper) { | ||
throw new UnknownElementException(); | ||
} | ||
return (instanceWrapper as InstanceWrapper<any>).instance; | ||
} | ||
|
||
private initFlatContainer() { | ||
if (this.flatContainer) { | ||
return undefined; | ||
} | ||
const modules = this.container.getModules(); | ||
const initialValue = { | ||
components: [], | ||
routes: [], | ||
injectables: [], | ||
}; | ||
const merge = <T = any>( | ||
initial: Map<string, T> | T[], | ||
arr: Map<string, T>, | ||
) => [...initial, ...arr]; | ||
|
||
this.flatContainer = ([...modules.values()].reduce( | ||
(current, next) => ({ | ||
components: merge(current.components, next.components), | ||
routes: merge(current.routes, next.routes), | ||
injectables: merge(current.injectables, next.injectables), | ||
}), | ||
initialValue, | ||
) as any) as Partial<Module>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,61 @@ | ||
import { Type } from '@nestjs/common'; | ||
import { isFunction } from '@nestjs/common/utils/shared.utils'; | ||
import { UnknownElementException } from '../errors/exceptions/unknown-element.exception'; | ||
import { InstanceWrapper, NestContainer } from './container'; | ||
import { NestContainer } from './container'; | ||
import { ContainerScanner } from './container-scanner'; | ||
import { Injector } from './injector'; | ||
import { Module } from './module'; | ||
|
||
export abstract class ModuleRef { | ||
private flattenModuleFixture: Partial<Module>; | ||
private readonly injector = new Injector(); | ||
private readonly containerScanner: ContainerScanner; | ||
|
||
constructor(protected readonly container: NestContainer) {} | ||
constructor(protected readonly container: NestContainer) { | ||
this.containerScanner = new ContainerScanner(container); | ||
} | ||
|
||
public abstract get<TInput = any, TResult = TInput>( | ||
typeOrToken: Type<TInput> | string | symbol, | ||
options?: { strict: boolean }, | ||
): TResult; | ||
|
||
public abstract create<T = any>(type: Type<T>): Promise<T>; | ||
|
||
protected find<TInput = any, TResult = TInput>( | ||
typeOrToken: Type<TInput> | string | symbol, | ||
): TResult { | ||
this.initFlattenModule(); | ||
return this.findInstanceByPrototypeOrToken<TInput, TResult>( | ||
typeOrToken, | ||
this.flattenModuleFixture, | ||
); | ||
return this.containerScanner.find<TInput, TResult>(typeOrToken); | ||
} | ||
|
||
protected async instantiateClass<T = any>( | ||
type: Type<T>, | ||
module: Module, | ||
): Promise<T> { | ||
const wrapper = { | ||
name: type.name, | ||
metatype: type, | ||
instance: undefined, | ||
isResolved: false, | ||
}; | ||
return new Promise<T>(async (resolve, reject) => { | ||
try { | ||
await this.injector.resolveConstructorParams<T>( | ||
wrapper, | ||
module, | ||
undefined, | ||
async instances => resolve(new type(...instances)), | ||
); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
|
||
protected findInstanceByPrototypeOrToken<TInput = any, TResult = TInput>( | ||
metatypeOrToken: Type<TInput> | string | symbol, | ||
contextModule: Partial<Module>, | ||
): TResult { | ||
const dependencies = new Map([ | ||
...contextModule.components, | ||
...contextModule.routes, | ||
...contextModule.injectables, | ||
]); | ||
const name = isFunction(metatypeOrToken) | ||
? (metatypeOrToken as any).name | ||
: metatypeOrToken; | ||
const instanceWrapper = dependencies.get(name); | ||
if (!instanceWrapper) { | ||
throw new UnknownElementException(); | ||
} | ||
return (instanceWrapper as InstanceWrapper<any>).instance; | ||
} | ||
|
||
private initFlattenModule() { | ||
if (this.flattenModuleFixture) { | ||
return void 0; | ||
} | ||
const modules = this.container.getModules(); | ||
const initialValue = { | ||
components: [], | ||
routes: [], | ||
injectables: [], | ||
}; | ||
this.flattenModuleFixture = [...modules.values()].reduce( | ||
(flatten, curr) => ({ | ||
components: [...flatten.components, ...curr.components], | ||
routes: [...flatten.routes, ...curr.routes], | ||
injectables: [...flatten.injectables, ...curr.injectables], | ||
}), | ||
initialValue, | ||
) as any; | ||
return this.containerScanner.findInstanceByPrototypeOrToken< | ||
TInput, | ||
TResult | ||
>(metatypeOrToken, contextModule); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters