nestjs-array-provider 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +36 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# nestjs-array-provider
|
|
2
|
+
|
|
3
|
+
A tiny utility to collect multiple NestJS providers into an injectable array. Useful for plugin, strategy, and registry patterns.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install nestjs-array-provider
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { arrayProvider } from 'nestjs-array-provider';
|
|
15
|
+
|
|
16
|
+
// 1. Define a token
|
|
17
|
+
const PAYMENT_PROVIDERS = Symbol('PAYMENT_PROVIDERS');
|
|
18
|
+
|
|
19
|
+
// 2. Register in your module
|
|
20
|
+
@Module({
|
|
21
|
+
providers: [
|
|
22
|
+
DirectProvider,
|
|
23
|
+
HitPayProvider,
|
|
24
|
+
arrayProvider<PaymentProvider>(PAYMENT_PROVIDERS, [
|
|
25
|
+
DirectProvider,
|
|
26
|
+
HitPayProvider,
|
|
27
|
+
]),
|
|
28
|
+
PaymentService,
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
export class PaymentModule {}
|
|
32
|
+
|
|
33
|
+
// 3. Inject the array
|
|
34
|
+
@Injectable()
|
|
35
|
+
export class PaymentService {
|
|
36
|
+
constructor(
|
|
37
|
+
@Inject(PAYMENT_PROVIDERS)
|
|
38
|
+
private readonly providers: PaymentProvider[],
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
charge(amount: number) {
|
|
42
|
+
// Use all registered providers
|
|
43
|
+
return this.providers.map((p) => p.charge(amount));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### `arrayProvider<T>(token, providers): Provider`
|
|
51
|
+
|
|
52
|
+
| Parameter | Type | Description |
|
|
53
|
+
| ----------- | ----------------- | ------------------------------------------------ |
|
|
54
|
+
| `token` | `InjectionToken` | The injection token to provide the array under |
|
|
55
|
+
| `providers` | `Type<T>[]` | Array of classes to collect into the injected array |
|
|
56
|
+
|
|
57
|
+
Returns a NestJS `Provider` that resolves each class and provides them as a `T[]`.
|
|
58
|
+
|
|
59
|
+
## Why?
|
|
60
|
+
|
|
61
|
+
NestJS doesn't have a built-in way to inject all implementations of an interface as an array. The common workaround involves verbose boilerplate with `useFactory` and manual `inject` lists. `arrayProvider` wraps that pattern into a single, type-safe call.
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { InjectionToken, Provider, Type } from '@nestjs/common';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a NestJS provider that injects multiple services and provides them as an array.
|
|
4
|
+
*
|
|
5
|
+
* This is useful for implementing plugin/strategy patterns where you want to collect
|
|
6
|
+
* all implementations of an interface and inject them as an array.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // Define a token:
|
|
10
|
+
* const PAYMENT_PROVIDERS = Symbol('PAYMENT_PROVIDERS');
|
|
11
|
+
*
|
|
12
|
+
* // In your module:
|
|
13
|
+
* @Module({
|
|
14
|
+
* providers: [
|
|
15
|
+
* DirectProvider,
|
|
16
|
+
* HitPayProvider,
|
|
17
|
+
* arrayProvider<PaymentProvider>(PAYMENT_PROVIDERS, [DirectProvider, HitPayProvider]),
|
|
18
|
+
* PaymentProviderRegistry,
|
|
19
|
+
* ],
|
|
20
|
+
* })
|
|
21
|
+
*
|
|
22
|
+
* // In your service:
|
|
23
|
+
* constructor(
|
|
24
|
+
* @Inject(PAYMENT_PROVIDERS)
|
|
25
|
+
* private readonly providers: PaymentProvider[],
|
|
26
|
+
* ) {}
|
|
27
|
+
*/
|
|
28
|
+
export declare function arrayProvider<T>(token: InjectionToken, providers: Type<T>[]): Provider;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayProvider = arrayProvider;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a NestJS provider that injects multiple services and provides them as an array.
|
|
6
|
+
*
|
|
7
|
+
* This is useful for implementing plugin/strategy patterns where you want to collect
|
|
8
|
+
* all implementations of an interface and inject them as an array.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Define a token:
|
|
12
|
+
* const PAYMENT_PROVIDERS = Symbol('PAYMENT_PROVIDERS');
|
|
13
|
+
*
|
|
14
|
+
* // In your module:
|
|
15
|
+
* @Module({
|
|
16
|
+
* providers: [
|
|
17
|
+
* DirectProvider,
|
|
18
|
+
* HitPayProvider,
|
|
19
|
+
* arrayProvider<PaymentProvider>(PAYMENT_PROVIDERS, [DirectProvider, HitPayProvider]),
|
|
20
|
+
* PaymentProviderRegistry,
|
|
21
|
+
* ],
|
|
22
|
+
* })
|
|
23
|
+
*
|
|
24
|
+
* // In your service:
|
|
25
|
+
* constructor(
|
|
26
|
+
* @Inject(PAYMENT_PROVIDERS)
|
|
27
|
+
* private readonly providers: PaymentProvider[],
|
|
28
|
+
* ) {}
|
|
29
|
+
*/
|
|
30
|
+
function arrayProvider(token, providers) {
|
|
31
|
+
return {
|
|
32
|
+
provide: token,
|
|
33
|
+
useFactory: (...instances) => instances,
|
|
34
|
+
inject: providers,
|
|
35
|
+
};
|
|
36
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nestjs-array-provider",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Create NestJS providers that inject multiple services as an array. Useful for plugin/strategy patterns.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
13
|
+
"test": "jest"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"nestjs",
|
|
17
|
+
"array",
|
|
18
|
+
"provider",
|
|
19
|
+
"dependency-injection",
|
|
20
|
+
"plugin-pattern",
|
|
21
|
+
"strategy-pattern"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@nestjs/common": ">=8.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@nestjs/common": "^10.0.0",
|
|
29
|
+
"@nestjs/core": "^10.0.0",
|
|
30
|
+
"@nestjs/testing": "^10.0.0",
|
|
31
|
+
"@types/jest": "^29.5.0",
|
|
32
|
+
"jest": "^29.7.0",
|
|
33
|
+
"reflect-metadata": "^0.2.0",
|
|
34
|
+
"ts-jest": "^29.1.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|