Skip to content

Instantly share code, notes, and snippets.

@joebowbeer
Forked from evolkmann/importing.module.ts
Created February 8, 2022 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebowbeer/ae8ff61e5f5c1c45e778e993dd4c65c8 to your computer and use it in GitHub Desktop.
Save joebowbeer/ae8ff61e5f5c1c45e778e993dd4c65c8 to your computer and use it in GitHub Desktop.
Create Nest.js modules with custom config
import { Module } from '@nestjs/common';
import { MyLibModule } from './my-lib.module';
@Module({
imports: [
MyLibModule.register({ name: 'Enzo' }),
]
})
export class ImportingModule {}
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
]
};
}
}
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