You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: 'Enforces a styleguide for provider types'
3
+
---
4
+
5
+
Large teams can have the desire to limit or enforce a particular style of creating [custom providers](https://docs.nestjs.com/fundamentals/custom-providers); e.g. banning request-scoped providers to avoid potential circular dependencies, or [preferring factory providers over value providers to significantly increase performance](https://github.com/nestjs/nest/pull/12753). This rule enforces a particular type of provider to be used.
6
+
7
+
## Options
8
+
9
+
This rule accepts an object with the "prefer" property, which is an array containing one or more of the following values:
10
+
11
+
-`value`: Enforces the use of value providers.
12
+
-`factory`: Enforces the use of factory providers.
13
+
-`class`: Enforces the use of class providers.
14
+
-`existing`: Enforces the use of existing providers.
15
+
16
+
17
+
### Example of Options
18
+
19
+
```json
20
+
"rules": {
21
+
"@trilon/enforce-custom-provider-type": [
22
+
"warn", {
23
+
"prefer": ["factory", "value"]
24
+
}
25
+
]
26
+
}
27
+
```
28
+
29
+
## Examples
30
+
Considering the options above, the following examples will show how the rule behaves when the `prefer` option is set to `factory`.
31
+
32
+
### ❌ Incorrect
33
+
34
+
```ts
35
+
const customValueProvider:Provider= {
36
+
provide: 'TOKEN',
37
+
useExisting: 'some-value'// ⚠️ provider is not of type ["factory", "value"]
38
+
}
39
+
40
+
const customClassProvider:Provider= {
41
+
provide: AbstractClass,
42
+
useClass: SomeClass// ⚠️ provider is not of type ["factory", "value"]
43
+
}
44
+
```
45
+
46
+
### ✅ Correct
47
+
48
+
const factoryProvider: Provider = {
49
+
provide: 'TOKEN',
50
+
useFactory: () => 'some-value'
51
+
}
52
+
53
+
## When Not To Use It
54
+
55
+
If you don't want to enforce a particular style of provider, you can disable this rule.
0 commit comments