first-di 3.2.2 → 3.3.1
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
First DI
|
|
2
2
|
=====
|
|
3
3
|
|
|
4
|
-
Easy dependency injection for
|
|
4
|
+
Easy dependency injection for TypeScript applications
|
|
5
5
|
|
|
6
6
|
Installation
|
|
7
7
|
------
|
|
@@ -15,16 +15,16 @@ npm i first-di
|
|
|
15
15
|
Features
|
|
16
16
|
------
|
|
17
17
|
|
|
18
|
-
- Easy and powerful dependency injection for any
|
|
18
|
+
- Easy and powerful dependency injection for any TypeScript application.
|
|
19
19
|
- 2 modes of work. Optional DI - for most apps. Classic DI - for advanced apps.
|
|
20
20
|
- Support for multiple scopes.
|
|
21
21
|
- Supports multiple life cycles.
|
|
22
|
-
- Dependency
|
|
22
|
+
- Dependency-free. Dependencies are used only for development.
|
|
23
23
|
|
|
24
24
|
Setup
|
|
25
25
|
------
|
|
26
26
|
|
|
27
|
-
Install [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) package and import in root
|
|
27
|
+
Install [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) package and import it in the root TypeScript file. This package is needed to support reflection and is a mandatory requirement of TypeScript.
|
|
28
28
|
|
|
29
29
|
In tsconfig.json enable compiler options:
|
|
30
30
|
|
|
@@ -48,7 +48,7 @@ Just write classes and inject dependencies through class constructors. When the
|
|
|
48
48
|
```typescript
|
|
49
49
|
import { resolve, override, reflection } from "first-di";
|
|
50
50
|
|
|
51
|
-
@reflection //
|
|
51
|
+
@reflection // TypeScript will generate reflection metadata
|
|
52
52
|
class ProdRepository { // Default implementation
|
|
53
53
|
|
|
54
54
|
public async getData (): Promise<string> {
|
|
@@ -99,7 +99,7 @@ if (process.env.NODE_ENV === "test") { // Override in test environment
|
|
|
99
99
|
override(ProdRepository, MockRepository);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
const store = resolve(ProdStore); // Create
|
|
102
|
+
const store = resolve(ProdStore); // Create instance by framework
|
|
103
103
|
const data = await store.getData();
|
|
104
104
|
|
|
105
105
|
if (process.env.NODE_ENV === "test") {
|
|
@@ -112,7 +112,7 @@ if (process.env.NODE_ENV === "test") {
|
|
|
112
112
|
Using in Classic DI mode
|
|
113
113
|
------
|
|
114
114
|
|
|
115
|
-
In professional mode
|
|
115
|
+
In professional mode Abstract classes are used instead of interfaces. TypeScript does not generate interfaces for working in runtime. Abstract classes serve as the abstract base class. So instead of interfaces, you need to write Abstract classes.
|
|
116
116
|
|
|
117
117
|
```typescript
|
|
118
118
|
import { resolve, override, reflection } from "first-di";
|
|
@@ -199,12 +199,12 @@ Options
|
|
|
199
199
|
First DI has several points for customizing dependency options:
|
|
200
200
|
|
|
201
201
|
- **Global** - `DI.defaultOptions: AutowiredOptions`. Sets global default behavior.
|
|
202
|
-
- **Override** - `override(fromClass, toClass, options?: AutowiredOptions)`. Sets behavior
|
|
202
|
+
- **Override** - `override(fromClass, toClass, options?: AutowiredOptions)`. Sets behavior overridden dependency.
|
|
203
203
|
- **Resolve** - `resolve(class, options?: AutowiredOptions)`. Sets behaviors for resolve dependencies.
|
|
204
204
|
|
|
205
|
-
Options
|
|
205
|
+
Options have the following properties:
|
|
206
206
|
|
|
207
|
-
- **lifeTime: AutowiredLifetimes** - Sets lifeTime of
|
|
207
|
+
- **lifeTime: AutowiredLifetimes** - Sets lifeTime of dependency.
|
|
208
208
|
|
|
209
209
|
SINGLETON - Create one instance for all resolvers.
|
|
210
210
|
|
|
@@ -239,10 +239,10 @@ API
|
|
|
239
239
|
First DI also has an API for extended use.
|
|
240
240
|
|
|
241
241
|
- override - Function. Override dependency and resolve options.
|
|
242
|
-
- resolve - Function. Resolves
|
|
242
|
+
- resolve - Function. Resolves dependency with default options or specified.
|
|
243
243
|
- singleton - Function. Resolve singleton.
|
|
244
244
|
- instance - Function. Resolve new instance.
|
|
245
|
-
- reset - Function. Reset all singleton list and override list, but
|
|
245
|
+
- reset - Function. Reset all singleton list and override list, but doesn't reset global options.
|
|
246
246
|
|
|
247
247
|
Resolve, singleton, instance - can be used to implement the Service Locator.
|
|
248
248
|
|
|
@@ -263,7 +263,7 @@ class ApiDemo {
|
|
|
263
263
|
Extension DI
|
|
264
264
|
------
|
|
265
265
|
|
|
266
|
-
First DI
|
|
266
|
+
First DI uses OOP and SOLID design principles. Each part of DI can be overridden or extended after inheritance from the base class.
|
|
267
267
|
|
|
268
268
|
```typescript
|
|
269
269
|
import { DI } from "first-di";
|
package/dist/classes/di.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/* eslint-disable max-params */
|
|
2
2
|
import { AutowiredLifetimes } from "../models/autowired-lifetimes.js";
|
|
3
|
+
/*
|
|
4
|
+
* Dependency Injection (DI) class for managing object creation and dependency resolution.
|
|
5
|
+
* Provides methods for resolving dependencies, managing singletons, and overriding implementations.
|
|
6
|
+
*/
|
|
3
7
|
export class DI {
|
|
4
8
|
constructor() {
|
|
5
9
|
this.singletonsList = new Map();
|
|
@@ -24,9 +28,9 @@ export class DI {
|
|
|
24
28
|
*/
|
|
25
29
|
makeAutowired(options) {
|
|
26
30
|
// eslint-disable-next-line no-console
|
|
27
|
-
console.warn("first-di: @autowired is
|
|
28
|
-
"new
|
|
29
|
-
"use 'resolve' functions
|
|
31
|
+
console.warn("first-di: @autowired is deprecated, " +
|
|
32
|
+
"new standard of ECMAScript decorators prohibits changing the classes, " +
|
|
33
|
+
"use 'resolve' functions instead of @autowired");
|
|
30
34
|
return (target, propertyKey) => {
|
|
31
35
|
const type = Reflect.getMetadata("design:type", target, propertyKey);
|
|
32
36
|
const { resolve } = this;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
/*
|
|
3
|
+
* A decorator function for enabling reflection metadata generation in TypeScript.
|
|
4
|
+
* This is used to support dependency injection and other reflection-based features.
|
|
5
|
+
*/
|
|
2
6
|
export const reflection = (..._params) => {
|
|
3
7
|
// For generation reflection by typescript
|
|
4
8
|
};
|
|
@@ -13,7 +13,7 @@ export var AutowiredLifetimes;
|
|
|
13
13
|
*/
|
|
14
14
|
AutowiredLifetimes[AutowiredLifetimes["PerOwned"] = 2] = "PerOwned";
|
|
15
15
|
/**
|
|
16
|
-
* Recreate each dependency on each access to dependency
|
|
16
|
+
* Recreate each dependency on each access to the dependency
|
|
17
17
|
*/
|
|
18
18
|
AutowiredLifetimes[AutowiredLifetimes["PerAccess"] = 3] = "PerAccess";
|
|
19
19
|
})(AutowiredLifetimes || (AutowiredLifetimes = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "first-di",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"author": "Eugene Labutin",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/LabEG/first-di#readme",
|
|
@@ -34,17 +34,17 @@
|
|
|
34
34
|
"reflect-metadata": ">=0.1.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@labeg/code-style": "^
|
|
38
|
-
"@swc-node/register": "^1.10.
|
|
39
|
-
"@types/chai": "^5.
|
|
40
|
-
"chai": "^5.
|
|
37
|
+
"@labeg/code-style": "^6.3.0",
|
|
38
|
+
"@swc-node/register": "^1.10.10",
|
|
39
|
+
"@types/chai": "^5.2.1",
|
|
40
|
+
"chai": "^5.2.0",
|
|
41
41
|
"reflect-metadata": "^0.2.2",
|
|
42
|
-
"typescript": "^5.
|
|
43
|
-
"@commitlint/cli": "^19.
|
|
44
|
-
"@commitlint/config-conventional": "^19.
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"@commitlint/cli": "^19.8.0",
|
|
44
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
45
45
|
"husky": "^9.1.7",
|
|
46
|
-
"lint-staged": "^15.
|
|
47
|
-
"@favware/cliff-jumper": "^
|
|
46
|
+
"lint-staged": "^15.5.0",
|
|
47
|
+
"@favware/cliff-jumper": "^6.0.0"
|
|
48
48
|
},
|
|
49
49
|
"keywords": [
|
|
50
50
|
"dependency injection",
|