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 |
|
6 | NestJS module for extract providers from parent non-global modules
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ```bash
|
11 | npm install --save nestjs-extractor
|
12 | ```
|
13 |
|
14 | ## Usage
|
15 |
|
16 | Create **ZooConfigService** and **ZooConfigModule**
|
17 |
|
18 | ```ts
|
19 | @Injectable()
|
20 | class 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 | })
|
30 | class ZooConfigModule {}
|
31 | ```
|
32 |
|
33 | Provide **ZooConfigService** through **ExtractorModule** in **CatModule**...
|
34 |
|
35 | ```ts
|
36 | import { ExtractorModule } from 'nestjs-extractor';
|
37 |
|
38 | @Module({
|
39 | imports: [ExtractorModule.forFeature([ZooConfigService])],
|
40 | providers: [CatService],
|
41 | controllers: [CatController],
|
42 | })
|
43 | class CatModule {}
|
44 | ```
|
45 |
|
46 | ...and use **ZooConfigService** in **CatController**...
|
47 |
|
48 | ```ts
|
49 | @Controller()
|
50 | class 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 |
|
68 | Import **CatModule** in **ZooModule**
|
69 |
|
70 | ```ts
|
71 | @Module({
|
72 | imports: [CatModule, DogModule],
|
73 | })
|
74 | class ZooModule {}
|
75 | ```
|
76 |
|
77 | Import **ZooConfigModule** and **ZooModule** in **AppModule**
|
78 |
|
79 | ```ts
|
80 | @Module({
|
81 | imports: [ZooConfigModule, ZooModule],
|
82 | })
|
83 | class AppModule {}
|
84 | ```
|
85 |
|
86 | ## Options
|
87 |
|
88 | You can provide the options for the providers search as the second argument of the `ExtractorModule.forFeature()` function
|
89 |
|
90 | ```ts
|
91 | type ExtractorModuleOptions = {
|
92 | optional?: boolean;
|
93 | };
|
94 | ```
|
95 |
|
96 | 1. `optional` is needed for providers marked as `@Optional()`
|
97 |
|
98 | ```ts
|
99 | @Injectable()
|
100 | class 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 | })
|
114 | class ServiceModule {}
|
115 | ```
|
116 |
|
117 | ## License
|
118 |
|
119 | MIT
|