-
-
Save joebowbeer/ae8ff61e5f5c1c45e778e993dd4c65c8 to your computer and use it in GitHub Desktop.
Create Nest.js modules with custom config
This file contains hidden or 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
import { Module } from '@nestjs/common'; | |
import { MyLibModule } from './my-lib.module'; | |
@Module({ | |
imports: [ | |
MyLibModule.register({ name: 'Enzo' }), | |
] | |
}) | |
export class ImportingModule {} |
This file contains hidden or 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
import { Module, DynamicModule } from '@nestjs/common'; | |
import { MyLibService } from './my-lib.service'; | |
export interface Config { | |
name: string; | |
} | |
@Module({}) | |
export class MyLibModule { | |
static register(options: Config): DynamicModule { | |
return { | |
module: MyLibModule, | |
providers: [ | |
{ | |
provide: MyLibService, | |
useValue: new MyLibService(options) | |
} | |
], | |
exports: [ | |
MyLibService | |
] | |
}; | |
} | |
} |
This file contains hidden or 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
import { Injectable } from '@nestjs/common'; | |
import { Config } from './my-lib.module'; | |
@Injectable() | |
export class MyLibService { | |
private readonly config: Config; | |
constructor(config: Config) { | |
this.config = config; | |
} | |
greet() { | |
console.log(`Hello ${this.config.name}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment