UNPKG

2.41 kBMarkdownView Raw
1# Description
2
3[![npm version](https://badge.fury.io/js/nestjs-extractor.svg)](https://badge.fury.io/js/nestjs-extractor)
4[![install size](https://packagephobia.now.sh/badge?p=nestjs-extractor)](https://packagephobia.now.sh/result?p=nestjs-extractor)
5
6NestJS module for extract providers from parent non-global modules
7
8## Installation
9
10```bash
11npm install --save nestjs-extractor
12```
13
14## Usage
15
16Create **ZooConfigService** and **ZooConfigModule**
17
18```ts
19@Injectable()
20class ZooConfigService {
21 public get zooName(): string {
22 return process.env.ZOO_NAME ?? 'Central Park Zoo';
23 }
24}
25
26@Module({
27 providers: [ZooConfigService],
28 exports: [ZooConfigService],
29})
30class ZooConfigModule {}
31```
32
33Provide **ZooConfigService** through **ExtractorModule** in **CatModule**...
34
35```ts
36import { ExtractorModule } from 'nestjs-extractor';
37
38@Module({
39 imports: [ExtractorModule.forFeature([ZooConfigService])],
40 providers: [CatService],
41 controllers: [CatController],
42})
43class CatModule {}
44```
45
46...and use **ZooConfigService** in **CatController**...
47
48```ts
49@Controller()
50class CatController {
51 public constructor(
52 private readonly catService: CatService,
53 private readonly zooConfigService: ZooConfigService,
54 ) {}
55
56 @Get('/cat/:catId/info')
57 public getCatInfo(
58 @Param('catId', ParseIntPipe) catId: number,
59 ): string {
60 const catName = this.catService.getCatById(catId);
61 const zooName = this.zooConfigService.zooName;
62
63 return `Cat ${catName} from ${zooName}`;
64 }
65}
66```
67
68Import **CatModule** in **ZooModule**
69
70```ts
71@Module({
72 imports: [CatModule, DogModule],
73})
74class ZooModule {}
75```
76
77Import **ZooConfigModule** and **ZooModule** in **AppModule**
78
79```ts
80@Module({
81 imports: [ZooConfigModule, ZooModule],
82})
83class AppModule {}
84```
85
86## Options
87
88You can provide the options for the providers search as the second argument of the `ExtractorModule.forFeature()` function
89
90```ts
91type ExtractorModuleOptions = {
92 optional?: boolean;
93};
94```
95
961. `optional` is needed for providers marked as `@Optional()`
97
98```ts
99@Injectable()
100class Service {
101 public constructor(
102 @Optional()
103 public readonly nestedService: NestedService,
104 ) {}
105}
106
107@Module({
108 imports: [
109 ExtractorModule.forFeature([NestedService], { optional: true }),
110 ],
111
112 providers: [Service],
113})
114class ServiceModule {}
115```
116
117## License
118
119MIT