nestjs-extractor
v1.2.0
Published
NestJS module for extract providers from parent non-global modules
Maintainers
Readme
Description
NestJS module for extract providers from parent non-global modules
Installation
npm install --save nestjs-extractorUsage
Create ZooConfigService and ZooConfigModule
@Injectable()
class ZooConfigService {
public get zooName(): string {
return process.env.ZOO_NAME ?? 'Central Park Zoo';
}
}
@Module({
providers: [ZooConfigService],
exports: [ZooConfigService],
})
class ZooConfigModule {}Provide ZooConfigService through ExtractorModule in CatModule...
import { ExtractorModule } from 'nestjs-extractor';
@Module({
imports: [ExtractorModule.forFeature([ZooConfigService])],
providers: [CatService],
controllers: [CatController],
})
class CatModule {}...and use ZooConfigService in CatController...
@Controller()
class CatController {
public constructor(
private readonly catService: CatService,
private readonly zooConfigService: ZooConfigService,
) {}
@Get('/cat/:catId/info')
public getCatInfo(
@Param('catId', ParseIntPipe) catId: number,
): string {
const catName = this.catService.getCatById(catId);
const zooName = this.zooConfigService.zooName;
return `Cat ${catName} from ${zooName}`;
}
}Import CatModule in ZooModule
@Module({
imports: [CatModule, DogModule],
})
class ZooModule {}Import ZooConfigModule and ZooModule in AppModule
@Module({
imports: [ZooConfigModule, ZooModule],
})
class AppModule {}Options
You can provide the options for the providers search as the second argument of the ExtractorModule.forFeature() function
type ExtractorModuleOptions = {
optional?: boolean;
};optionalis needed for providers marked as@Optional()
@Injectable()
class Service {
public constructor(
@Inject(NestedService)
@Optional()
public readonly nestedService: NestedService | undefind,
) {}
}
@Module({
imports: [
ExtractorModule.forFeature([NestedService], { optional: true }),
],
providers: [Service],
})
class ServiceModule {}License
MIT
