@rsdk/nest-tools 5.15.0-next.9 → 5.15.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/CHANGELOG.md +4 -0
- package/README.md +42 -0
- package/dist/create-di.d.ts +65 -0
- package/dist/create-di.js +10 -0
- package/dist/create-di.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/jest.config.js +1 -0
- package/jest.config.unit.js +1 -0
- package/package.json +2 -2
- package/src/create-di.spec.ts +168 -0
- package/src/create-di.ts +83 -0
- package/src/index.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.15.0](https://github.com/R-Vision/rsdk/compare/v5.15.0-next.14...v5.15.0) (2026-10-30)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @rsdk/nest-tools
|
|
9
|
+
|
|
6
10
|
## [5.14.1](https://github.com/R-Vision/rsdk/compare/v5.14.1-next.0...v5.14.1) (2026-09-16)
|
|
7
11
|
|
|
8
12
|
**Note:** Version bump only for package @rsdk/nest-tools
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @rsdk/nest-tools
|
|
2
|
+
|
|
3
|
+
Хелперы для работы с DI и объявлением модулей NestJS.
|
|
4
|
+
|
|
5
|
+
## `createDi`
|
|
6
|
+
|
|
7
|
+
Создаёт декоратор `@InjectSome()` вместе с методом `token()`, который возвращает тот же токен, что внедряет декоратор. Токен описывается в одном месте, поэтому декоратор и регистрация провайдера не разойдутся. Заменяет обычную пару `getSomeToken()` + `InjectSome()`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Injectable, Module } from '@nestjs/common';
|
|
11
|
+
import { createDi } from '@rsdk/nest-tools';
|
|
12
|
+
|
|
13
|
+
const InjectBar = createDi((foo: string) => 'bar:' + foo);
|
|
14
|
+
|
|
15
|
+
@Injectable()
|
|
16
|
+
class Service {
|
|
17
|
+
constructor(@InjectBar('zoo') zoo: string) {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Module({
|
|
21
|
+
providers: [
|
|
22
|
+
Service,
|
|
23
|
+
{ provide: InjectBar.token('zoo'), useFactory: () => 'my zoo' },
|
|
24
|
+
],
|
|
25
|
+
})
|
|
26
|
+
class MyModule {}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Со статическим токеном (строкой или символом) декоратор вызывается без аргументов:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const InjectConfig = createDi(Symbol('CONFIG'));
|
|
33
|
+
|
|
34
|
+
@Injectable()
|
|
35
|
+
class Service {
|
|
36
|
+
constructor(@InjectConfig() config: Config) {}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// providers: [{ provide: InjectConfig.token(), useValue: config }]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Декоратор ставится на параметр конструктора и на свойство класса, как `@Inject()` из `@nestjs/common`.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injection token produced by {@link createDi}.
|
|
3
|
+
*/
|
|
4
|
+
export type DiToken = string | symbol;
|
|
5
|
+
/**
|
|
6
|
+
* Decorator returned by {@link createDi}. Works both as a constructor
|
|
7
|
+
* parameter decorator and as a class property decorator, exactly like
|
|
8
|
+
* `@Inject()` from `@nestjs/common`.
|
|
9
|
+
*/
|
|
10
|
+
export type DiDecorator = ParameterDecorator & PropertyDecorator;
|
|
11
|
+
/**
|
|
12
|
+
* Result of {@link createDi}: a decorator factory with a `token()` method
|
|
13
|
+
* that builds the very same token the decorator injects.
|
|
14
|
+
*/
|
|
15
|
+
export interface DiInjector<TArgs extends unknown[]> {
|
|
16
|
+
(...args: TArgs): DiDecorator;
|
|
17
|
+
/**
|
|
18
|
+
* Builds the injection token for the given arguments. Use it in
|
|
19
|
+
* `provide: InjectSome.token(...)` when registering the provider.
|
|
20
|
+
*/
|
|
21
|
+
token(...args: TArgs): DiToken;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates an `@InjectSome()` decorator together with a matching `token()`
|
|
25
|
+
* helper from a single source, so the token and the decorator can no longer
|
|
26
|
+
* drift apart. Replaces the usual pair of `getSomeToken()` + `InjectSome()`.
|
|
27
|
+
*
|
|
28
|
+
* A static token (string or symbol) yields a decorator without arguments.
|
|
29
|
+
* A token factory yields a decorator whose arguments are inferred from
|
|
30
|
+
* the factory signature.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const InjectBar = createDi((foo: string) => 'bar:' + foo);
|
|
35
|
+
*
|
|
36
|
+
* @Injectable()
|
|
37
|
+
* class Service {
|
|
38
|
+
* constructor(@InjectBar('zoo') zoo: string) {}
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* @Module({
|
|
42
|
+
* providers: [
|
|
43
|
+
* Service,
|
|
44
|
+
* { provide: InjectBar.token('zoo'), useFactory: () => 'my zoo' },
|
|
45
|
+
* ],
|
|
46
|
+
* })
|
|
47
|
+
* class MyModule {}
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const InjectConfig = createDi(Symbol('CONFIG'));
|
|
53
|
+
*
|
|
54
|
+
* @Injectable()
|
|
55
|
+
* class Service {
|
|
56
|
+
* constructor(@InjectConfig() config: Config) {}
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* // providers: [{ provide: InjectConfig.token(), useValue: config }]
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @param source static token or a factory building the token from arguments
|
|
63
|
+
*/
|
|
64
|
+
export declare function createDi(source: DiToken): DiInjector<[]>;
|
|
65
|
+
export declare function createDi<TArgs extends unknown[]>(source: (...args: TArgs) => DiToken): DiInjector<TArgs>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDi = createDi;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
function createDi(source) {
|
|
6
|
+
const token = (...args) => typeof source === 'function' ? source(...args) : source;
|
|
7
|
+
const decorator = (...args) => (0, common_1.Inject)(token(...args));
|
|
8
|
+
return Object.assign(decorator, { token });
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=create-di.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-di.js","sourceRoot":"","sources":["../src/create-di.ts"],"names":[],"mappings":";;AAyEA,4BASC;AAlFD,2CAAwC;AAyExC,SAAgB,QAAQ,CACtB,MAA+C;IAE/C,MAAM,KAAK,GAAG,CAAC,GAAG,IAAW,EAAW,EAAE,CACxC,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAE1D,MAAM,SAAS,GAAG,CAAC,GAAG,IAAW,EAAe,EAAE,CAAC,IAAA,eAAM,EAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IAE1E,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AAC7C,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export { NestDefinitionType } from './constants';
|
|
|
3
3
|
export { NestDefinitionIterator } from './nest-definition.iterator';
|
|
4
4
|
export { NestDefinition } from './nest-definition';
|
|
5
5
|
export { Assert } from './assert';
|
|
6
|
+
export { createDi } from './create-di';
|
|
7
|
+
export type { DiDecorator, DiInjector, DiToken } from './create-di';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Assert = exports.NestDefinition = exports.NestDefinitionIterator = exports.NestDefinitionType = void 0;
|
|
3
|
+
exports.createDi = exports.Assert = exports.NestDefinition = exports.NestDefinitionIterator = exports.NestDefinitionType = void 0;
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
var constants_1 = require("./constants");
|
|
6
6
|
Object.defineProperty(exports, "NestDefinitionType", { enumerable: true, get: function () { return constants_1.NestDefinitionType; } });
|
|
@@ -10,4 +10,6 @@ var nest_definition_1 = require("./nest-definition");
|
|
|
10
10
|
Object.defineProperty(exports, "NestDefinition", { enumerable: true, get: function () { return nest_definition_1.NestDefinition; } });
|
|
11
11
|
var assert_1 = require("./assert");
|
|
12
12
|
Object.defineProperty(exports, "Assert", { enumerable: true, get: function () { return assert_1.Assert; } });
|
|
13
|
+
var create_di_1 = require("./create-di");
|
|
14
|
+
Object.defineProperty(exports, "createDi", { enumerable: true, get: function () { return create_di_1.createDi; } });
|
|
13
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,yCAAiD;AAAxC,+GAAA,kBAAkB,OAAA;AAE3B,uEAAoE;AAA3D,kIAAA,sBAAsB,OAAA;AAE/B,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,mCAAkC;AAAzB,gGAAA,MAAM,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,yCAAiD;AAAxC,+GAAA,kBAAkB,OAAA;AAE3B,uEAAoE;AAA3D,kIAAA,sBAAsB,OAAA;AAE/B,qDAAmD;AAA1C,iHAAA,cAAc,OAAA;AACvB,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,yCAAuC;AAA9B,qGAAA,QAAQ,OAAA"}
|
package/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('@rsdk/jest/jest.config');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('@rsdk/jest/jest.config.unit');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/nest-tools",
|
|
3
|
-
"version": "5.15.0
|
|
3
|
+
"version": "5.15.0",
|
|
4
4
|
"license": "Apache License 2.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -12,5 +12,5 @@
|
|
|
12
12
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
13
13
|
"rxjs": "^7.1.0"
|
|
14
14
|
},
|
|
15
|
-
"gitHead": "
|
|
15
|
+
"gitHead": "1b5584a7b75980e56ac9a2e0a92996aab80510fd"
|
|
16
16
|
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { Injectable, Module } from '@nestjs/common';
|
|
2
|
+
import { Test } from '@nestjs/testing';
|
|
3
|
+
|
|
4
|
+
import { createDi } from './create-di';
|
|
5
|
+
|
|
6
|
+
describe('createDi', () => {
|
|
7
|
+
describe('static string token', () => {
|
|
8
|
+
const InjectFoo = createDi('foo');
|
|
9
|
+
|
|
10
|
+
@Injectable()
|
|
11
|
+
class Service {
|
|
12
|
+
constructor(@InjectFoo() readonly foo: string) {}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
it('exposes the source string as token()', () => {
|
|
16
|
+
expect(InjectFoo.token()).toBe('foo');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('injects the provider registered under token()', async () => {
|
|
20
|
+
const module = await Test.createTestingModule({
|
|
21
|
+
providers: [
|
|
22
|
+
Service,
|
|
23
|
+
{ provide: InjectFoo.token(), useValue: 'foo value' },
|
|
24
|
+
],
|
|
25
|
+
}).compile();
|
|
26
|
+
|
|
27
|
+
expect(module.get(Service).foo).toBe('foo value');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('static symbol token', () => {
|
|
32
|
+
const symbol = Symbol('bar');
|
|
33
|
+
const InjectBar = createDi(symbol);
|
|
34
|
+
|
|
35
|
+
@Injectable()
|
|
36
|
+
class Service {
|
|
37
|
+
constructor(@InjectBar() readonly bar: number) {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
it('exposes the source symbol as token()', () => {
|
|
41
|
+
expect(InjectBar.token()).toBe(symbol);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('injects the provider registered under the symbol', async () => {
|
|
45
|
+
const module = await Test.createTestingModule({
|
|
46
|
+
providers: [Service, { provide: symbol, useValue: 42 }],
|
|
47
|
+
}).compile();
|
|
48
|
+
|
|
49
|
+
expect(module.get(Service).bar).toBe(42);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('token factory', () => {
|
|
54
|
+
const InjectZoo = createDi(
|
|
55
|
+
(name: string, index: number) => `zoo:${name}:${index}`,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
@Injectable()
|
|
59
|
+
class Service {
|
|
60
|
+
constructor(
|
|
61
|
+
@InjectZoo('cat', 1) readonly cat: string,
|
|
62
|
+
@InjectZoo('dog', 2) readonly dog: string,
|
|
63
|
+
) {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
it('builds the token from the factory arguments', () => {
|
|
67
|
+
expect(InjectZoo.token('cat', 1)).toBe('zoo:cat:1');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('injects the provider registered under token(...args)', async () => {
|
|
71
|
+
const module = await Test.createTestingModule({
|
|
72
|
+
providers: [
|
|
73
|
+
Service,
|
|
74
|
+
{ provide: InjectZoo.token('cat', 1), useValue: 'my cat' },
|
|
75
|
+
{
|
|
76
|
+
provide: InjectZoo.token('dog', 2),
|
|
77
|
+
useFactory: (): string => 'my dog',
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
}).compile();
|
|
81
|
+
|
|
82
|
+
const service = module.get(Service);
|
|
83
|
+
|
|
84
|
+
expect(service.cat).toBe('my cat');
|
|
85
|
+
expect(service.dog).toBe('my dog');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('supports the example from the issue', async () => {
|
|
89
|
+
const InjectBar = createDi((foo: string) => 'bar:' + foo);
|
|
90
|
+
|
|
91
|
+
@Injectable()
|
|
92
|
+
class IssueService {
|
|
93
|
+
constructor(@InjectBar('zoo') readonly zoo: string) {}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Module({
|
|
97
|
+
providers: [
|
|
98
|
+
IssueService,
|
|
99
|
+
{
|
|
100
|
+
provide: InjectBar.token('zoo'),
|
|
101
|
+
useFactory: (): string => 'my zoo',
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
})
|
|
105
|
+
class MyModule {}
|
|
106
|
+
|
|
107
|
+
const module = await Test.createTestingModule({
|
|
108
|
+
imports: [MyModule],
|
|
109
|
+
}).compile();
|
|
110
|
+
|
|
111
|
+
expect(module.get(IssueService).zoo).toBe('my zoo');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('property injection', () => {
|
|
116
|
+
const InjectFoo = createDi('foo');
|
|
117
|
+
|
|
118
|
+
@Injectable()
|
|
119
|
+
class Service {
|
|
120
|
+
@InjectFoo()
|
|
121
|
+
readonly foo!: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
it('injects the provider into a class property', async () => {
|
|
125
|
+
const module = await Test.createTestingModule({
|
|
126
|
+
providers: [Service, { provide: InjectFoo.token(), useValue: 'prop' }],
|
|
127
|
+
}).compile();
|
|
128
|
+
|
|
129
|
+
expect(module.get(Service).foo).toBe('prop');
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('missing provider', () => {
|
|
134
|
+
const InjectMissing = createDi((id: string) => `missing:${id}`);
|
|
135
|
+
|
|
136
|
+
@Injectable()
|
|
137
|
+
class Service {
|
|
138
|
+
constructor(@InjectMissing('x') readonly value: string) {}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
it('fails module compilation with the Nest dependency error', async () => {
|
|
142
|
+
await expect(
|
|
143
|
+
Test.createTestingModule({ providers: [Service] }).compile(),
|
|
144
|
+
).rejects.toThrow(/Nest can't resolve dependencies of the Service/);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('typing', () => {
|
|
149
|
+
it('rejects arguments for a static token and requires them for a factory', () => {
|
|
150
|
+
const InjectStatic = createDi('static');
|
|
151
|
+
const InjectFactory = createDi((a: string, b: number) => `${a}:${b}`);
|
|
152
|
+
|
|
153
|
+
// @ts-expect-error static token takes no arguments
|
|
154
|
+
InjectStatic.token('extra');
|
|
155
|
+
|
|
156
|
+
// @ts-expect-error static decorator takes no arguments
|
|
157
|
+
InjectStatic('extra');
|
|
158
|
+
|
|
159
|
+
// @ts-expect-error factory requires both arguments
|
|
160
|
+
InjectFactory.token('a');
|
|
161
|
+
|
|
162
|
+
// @ts-expect-error factory argument types are checked
|
|
163
|
+
InjectFactory('a', 'not a number');
|
|
164
|
+
|
|
165
|
+
expect(InjectFactory.token('a', 1)).toBe('a:1');
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
package/src/create-di.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Inject } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Injection token produced by {@link createDi}.
|
|
5
|
+
*/
|
|
6
|
+
export type DiToken = string | symbol;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Decorator returned by {@link createDi}. Works both as a constructor
|
|
10
|
+
* parameter decorator and as a class property decorator, exactly like
|
|
11
|
+
* `@Inject()` from `@nestjs/common`.
|
|
12
|
+
*/
|
|
13
|
+
export type DiDecorator = ParameterDecorator & PropertyDecorator;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Result of {@link createDi}: a decorator factory with a `token()` method
|
|
17
|
+
* that builds the very same token the decorator injects.
|
|
18
|
+
*/
|
|
19
|
+
export interface DiInjector<TArgs extends unknown[]> {
|
|
20
|
+
(...args: TArgs): DiDecorator;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Builds the injection token for the given arguments. Use it in
|
|
24
|
+
* `provide: InjectSome.token(...)` when registering the provider.
|
|
25
|
+
*/
|
|
26
|
+
token(...args: TArgs): DiToken;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Creates an `@InjectSome()` decorator together with a matching `token()`
|
|
31
|
+
* helper from a single source, so the token and the decorator can no longer
|
|
32
|
+
* drift apart. Replaces the usual pair of `getSomeToken()` + `InjectSome()`.
|
|
33
|
+
*
|
|
34
|
+
* A static token (string or symbol) yields a decorator without arguments.
|
|
35
|
+
* A token factory yields a decorator whose arguments are inferred from
|
|
36
|
+
* the factory signature.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const InjectBar = createDi((foo: string) => 'bar:' + foo);
|
|
41
|
+
*
|
|
42
|
+
* @Injectable()
|
|
43
|
+
* class Service {
|
|
44
|
+
* constructor(@InjectBar('zoo') zoo: string) {}
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* @Module({
|
|
48
|
+
* providers: [
|
|
49
|
+
* Service,
|
|
50
|
+
* { provide: InjectBar.token('zoo'), useFactory: () => 'my zoo' },
|
|
51
|
+
* ],
|
|
52
|
+
* })
|
|
53
|
+
* class MyModule {}
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* const InjectConfig = createDi(Symbol('CONFIG'));
|
|
59
|
+
*
|
|
60
|
+
* @Injectable()
|
|
61
|
+
* class Service {
|
|
62
|
+
* constructor(@InjectConfig() config: Config) {}
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* // providers: [{ provide: InjectConfig.token(), useValue: config }]
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @param source static token or a factory building the token from arguments
|
|
69
|
+
*/
|
|
70
|
+
export function createDi(source: DiToken): DiInjector<[]>;
|
|
71
|
+
export function createDi<TArgs extends unknown[]>(
|
|
72
|
+
source: (...args: TArgs) => DiToken,
|
|
73
|
+
): DiInjector<TArgs>;
|
|
74
|
+
export function createDi<TArgs extends unknown[]>(
|
|
75
|
+
source: DiToken | ((...args: TArgs) => DiToken),
|
|
76
|
+
): DiInjector<TArgs> {
|
|
77
|
+
const token = (...args: TArgs): DiToken =>
|
|
78
|
+
typeof source === 'function' ? source(...args) : source;
|
|
79
|
+
|
|
80
|
+
const decorator = (...args: TArgs): DiDecorator => Inject(token(...args));
|
|
81
|
+
|
|
82
|
+
return Object.assign(decorator, { token });
|
|
83
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,3 +6,5 @@ export { NestDefinitionIterator } from './nest-definition.iterator';
|
|
|
6
6
|
|
|
7
7
|
export { NestDefinition } from './nest-definition';
|
|
8
8
|
export { Assert } from './assert';
|
|
9
|
+
export { createDi } from './create-di';
|
|
10
|
+
export type { DiDecorator, DiInjector, DiToken } from './create-di';
|