Skip to content

Instantly share code, notes, and snippets.

@gpproton
Forked from evolkmann/importing.module.ts
Created August 13, 2022 00:49
Show Gist options
  • Save gpproton/39e3cc67c2c65dce5d2f838d1d66327d to your computer and use it in GitHub Desktop.
Save gpproton/39e3cc67c2c65dce5d2f838d1d66327d 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