-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add docs for new rule and update README
- Loading branch information
1 parent
b03a987
commit 58647c7
Showing
3 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
description: 'Enforces a styleguide for provider types' | ||
--- | ||
|
||
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. | ||
|
||
## Options | ||
|
||
This rule accepts an object with the "prefer" property, which might be one of the following values: | ||
|
||
- `value`: Enforces the use of value providers. | ||
- `factory`: Enforces the use of factory providers. | ||
- `class`: Enforces the use of class providers. | ||
- `existing`: Enforces the use of existing providers. | ||
|
||
|
||
### Example of Options | ||
|
||
```json | ||
"rules": { | ||
"@trilon/enforce-custom-provider-type": [ | ||
"warn", { | ||
"prefer": "factory" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Examples | ||
Considering the options above, the following examples will show how the rule behaves when the `prefer` option is set to `factory`. | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
const customValueProvider: Provider = { | ||
provide: 'TOKEN', | ||
useValue: 'some-value' // ⚠️ provider is not of type "factory" | ||
} | ||
|
||
const customClassProvider: Provider = { | ||
provide: AbstractClass, | ||
useClass: SomeClass // ⚠️ provider is not of type "factory" | ||
} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
const factoryProvider: Provider = { | ||
provide: 'TOKEN', | ||
useFactory: () => 'some-value' | ||
} | ||
|
||
## When Not To Use It | ||
|
||
If you don't want to enforce a particular style of provider, you can disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters