-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dynamic modules): helpers to reduce dynamic module boilerplate
- Loading branch information
1 parent
2bb6404
commit 80a2b2c
Showing
9 changed files
with
286 additions
and
88 deletions.
There are no files selected for viewing
Empty file.
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,137 @@ | ||
import { DynamicModule, Provider, Type } from '@nestjs/common'; | ||
import { ModuleMetadata } from '@nestjs/common/interfaces'; | ||
import { get } from 'lodash'; | ||
import { interval, race, Subject } from 'rxjs'; | ||
import { first, map } from 'rxjs/operators'; | ||
|
||
type InjectionToken = string | symbol | Type<any>; | ||
|
||
export interface ModuleConfigFactory<T> { | ||
createModuleConfig(): Promise<T> | T; | ||
} | ||
|
||
export interface AsyncModuleConfig<T> | ||
extends Pick<ModuleMetadata, 'imports' | 'exports'> { | ||
useExisting?: { | ||
value: ModuleConfigFactory<T>; | ||
provide?: InjectionToken; | ||
}; | ||
useClass?: Type<ModuleConfigFactory<T>>; | ||
useFactory?: (...args: any[]) => Promise<T> | T; | ||
inject?: any[]; | ||
} | ||
|
||
export function createModuleConfigProvider<T>( | ||
provide: InjectionToken, | ||
options: AsyncModuleConfig<T> | ||
): Provider { | ||
if (options.useFactory) { | ||
return { | ||
provide, | ||
useFactory: options.useFactory, | ||
inject: options.inject || [] | ||
}; | ||
} | ||
|
||
return { | ||
provide, | ||
useFactory: async (moduleConfigFactory: ModuleConfigFactory<T>) => { | ||
const options = await moduleConfigFactory.createModuleConfig(); | ||
return options; | ||
}, | ||
inject: [ | ||
options.useClass || | ||
get( | ||
options, | ||
'useExisting.provide', | ||
(options.useExisting as any).value.constructor.name | ||
) | ||
] | ||
}; | ||
} | ||
|
||
export interface IConfigurableDynamicRootModule<T, U> { | ||
new (): Type<T>; | ||
|
||
moduleSubject: Subject<DynamicModule>; | ||
|
||
forRoot(moduleCtor: Type<T>, moduleConfig: U): DynamicModule; | ||
|
||
forRootAsync( | ||
moduleCtor: Type<T>, | ||
asyncModuleConfig: AsyncModuleConfig<U> | ||
): DynamicModule; | ||
|
||
externallyConfigured( | ||
moduleCtor: Type<T>, | ||
wait: number | ||
): Promise<DynamicModule>; | ||
} | ||
|
||
export function MakeConfigurableDynamicRootModule<T, U>( | ||
moduleConfigToken: InjectionToken, | ||
additionalProviders: Provider[] = [] | ||
) { | ||
abstract class DynamicRootModule { | ||
static moduleSubject = new Subject<DynamicModule>(); | ||
|
||
static forRootAsync( | ||
moduleCtor: Type<T>, | ||
asyncModuleConfig: AsyncModuleConfig<U> | ||
): DynamicModule { | ||
const dynamicModule = { | ||
module: moduleCtor, | ||
imports: asyncModuleConfig.imports, | ||
exports: asyncModuleConfig.exports, | ||
providers: [ | ||
createModuleConfigProvider(moduleConfigToken, asyncModuleConfig), | ||
...additionalProviders | ||
] | ||
}; | ||
|
||
DynamicRootModule.moduleSubject.next(dynamicModule); | ||
|
||
return dynamicModule; | ||
} | ||
|
||
static forRoot(moduleCtor: Type<T>, moduleConfig: U): DynamicModule { | ||
const dynamicModule = { | ||
module: moduleCtor, | ||
providers: [ | ||
{ | ||
provide: moduleConfigToken, | ||
useValue: moduleConfig | ||
}, | ||
...additionalProviders | ||
] | ||
}; | ||
|
||
DynamicRootModule.moduleSubject.next(dynamicModule); | ||
|
||
return dynamicModule; | ||
} | ||
|
||
static async externallyConfigured( | ||
moduleCtor: Type<T>, | ||
wait: number | ||
): Promise<DynamicModule> { | ||
const timeout$ = interval(wait).pipe( | ||
first(), | ||
map(x => { | ||
throw new Error( | ||
`Expected ${ | ||
moduleCtor.name | ||
} to be configured by at last one Module but it was not configured within ${wait}ms` | ||
); | ||
}) | ||
); | ||
|
||
return race( | ||
timeout$, | ||
DynamicRootModule.moduleSubject.pipe(first()) | ||
).toPromise(); | ||
} | ||
} | ||
|
||
return DynamicRootModule as IConfigurableDynamicRootModule<T, U>; | ||
} |
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,2 +1,3 @@ | ||
export * from './mixins'; | ||
export * from './options'; | ||
export * from './dynamicModules'; |
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,11 @@ | ||
# `@nestjs-plus/modules` | ||
|
||
> TODO: description | ||
## Usage | ||
|
||
``` | ||
const modules = require('@nestjs-plus/modules'); | ||
// TODO: DEMONSTRATE API | ||
``` |
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,32 @@ | ||
{ | ||
"name": "@nestjs-plus/modules", | ||
"version": "0.0.1", | ||
"description": "Badass building blocks for creating NestJS modules", | ||
"keywords": [ | ||
"nestjs" | ||
], | ||
"author": "Jesse Carter <[email protected]>", | ||
"homepage": "https://github.com/WonderPanda/nestjs-plus#readme", | ||
"license": "MIT", | ||
"main": "lib/modules.js", | ||
"directories": { | ||
"lib": "lib", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/WonderPanda/nestjs-plus.git" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: run tests from root\" && exit 1" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WonderPanda/nestjs-plus/issues" | ||
} | ||
} |
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,2 +1,2 @@ | ||
export const RABBIT_HANDLER = Symbol('RABBIT_HANDLER'); | ||
export const RABBIT_CONFIG = Symbol('RABBIT_CONFIG'); | ||
export const RABBIT_CONFIG_TOKEN = Symbol('RABBIT_CONFIG'); |
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
Oops, something went wrong.