@travetto/di 7.0.0-rc.4 → 7.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 +9 -9
- package/package.json +4 -3
- package/src/decorator.ts +1 -0
- package/src/types.ts +0 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ yarn add @travetto/di
|
|
|
16
16
|
[Dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) is a framework primitive. When used in conjunction with automatic file scanning, it provides for handling of application dependency wiring. Due to the nature of [Typescript](https://typescriptlang.org) and type erasure of interfaces, dependency injection only supports `class`es as a type signifier. The primary goal of dependency injection is to allow for separation of concerns of object creation and it's usage.
|
|
17
17
|
|
|
18
18
|
## Declaration
|
|
19
|
-
The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
19
|
+
The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) and [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L48) decorators provide the registration of dependencies. Dependency declaration revolves around exposing `class`es and subtypes thereof to provide necessary functionality. Additionally, the framework will utilize dependencies to satisfy contracts with various implementation.
|
|
20
20
|
|
|
21
21
|
**Code: Example Injectable**
|
|
22
22
|
```typescript
|
|
@@ -77,7 +77,7 @@ class SpecificService extends BaseService {
|
|
|
77
77
|
}
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
In this scenario, `SpecificService` is a valid candidate for `BaseService` due to the abstract inheritance. Sometimes, you may want to provide a slight variation to a dependency without extending a class. To this end, the [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
80
|
+
In this scenario, `SpecificService` is a valid candidate for `BaseService` due to the abstract inheritance. Sometimes, you may want to provide a slight variation to a dependency without extending a class. To this end, the [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L48) decorator denotes a `static` class method that produces an [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16).
|
|
81
81
|
|
|
82
82
|
**Code: Example InjectableFactory**
|
|
83
83
|
```typescript
|
|
@@ -125,12 +125,12 @@ class RuntimeService {
|
|
|
125
125
|
|
|
126
126
|
In this example, the enabled flag is specified in relationship to the deployment environment. When coupled with optional properties, and optional chaining, allows for seamless inclusion of optional dependencies at runtime.
|
|
127
127
|
|
|
128
|
-
**Note**: Other modules are able to provide aliases to [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
128
|
+
**Note**: Other modules are able to provide aliases to [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) that also provide additional functionality. For example, the [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support") module @Config or the [Web API](https://github.com/travetto/travetto/tree/main/module/web#readme "Declarative support for creating Web Applications") module @Controller decorator registers the associated class as an injectable element.
|
|
129
129
|
|
|
130
130
|
## Injection
|
|
131
|
-
Once all of your necessary dependencies are defined, now is the time to provide those [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
131
|
+
Once all of your necessary dependencies are defined, now is the time to provide those [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) instances to your code. There are three primary methods for injection:
|
|
132
132
|
|
|
133
|
-
The [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
133
|
+
The [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) decorator, which denotes a desire to inject a value directly. These will be set post construction.
|
|
134
134
|
|
|
135
135
|
**Code: Example Injectable with dependencies as Inject fields**
|
|
136
136
|
```typescript
|
|
@@ -148,7 +148,7 @@ class CustomService {
|
|
|
148
148
|
}
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
-
The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
151
|
+
The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) constructor params, which will be provided as the instance is being constructed.
|
|
152
152
|
|
|
153
153
|
**Code: Example Injectable with dependencies in constructor**
|
|
154
154
|
```typescript
|
|
@@ -170,7 +170,7 @@ class CustomService {
|
|
|
170
170
|
}
|
|
171
171
|
```
|
|
172
172
|
|
|
173
|
-
Via [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
173
|
+
Via [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L48) params, which are comparable to constructor params
|
|
174
174
|
|
|
175
175
|
**Code: Example InjectableFactory with parameters as dependencies**
|
|
176
176
|
```typescript
|
|
@@ -228,9 +228,9 @@ class Config {
|
|
|
228
228
|
```
|
|
229
229
|
|
|
230
230
|
## Non-Framework Dependencies
|
|
231
|
-
The module is built around the framework's management of class registration, and being able to decorate the code with [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
231
|
+
The module is built around the framework's management of class registration, and being able to decorate the code with [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) decorators. There may also be a desire to leverage external code and pull it into the dependency injection framework. This could easily be achieved using a wrapper class that is owned by the framework.
|
|
232
232
|
|
|
233
|
-
It is also possible to directly reference external types, and they will be converted into unique symbols. These symbols cannot be used manually, but can be leveraged using [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#
|
|
233
|
+
It is also possible to directly reference external types, and they will be converted into unique symbols. These symbols cannot be used manually, but can be leveraged using [@Inject](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L16) decorators.
|
|
234
234
|
|
|
235
235
|
**Code: Example External Dependencies**
|
|
236
236
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/di",
|
|
3
|
-
"version": "7.0.0
|
|
3
|
+
"version": "7.0.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Dependency registration/management and injection support.",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"ast-transformations",
|
|
@@ -27,10 +28,10 @@
|
|
|
27
28
|
"directory": "module/di"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@travetto/registry": "^7.0.0
|
|
31
|
+
"@travetto/registry": "^7.0.0"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^7.0.0
|
|
34
|
+
"@travetto/transformer": "^7.0.0"
|
|
34
35
|
},
|
|
35
36
|
"peerDependenciesMeta": {
|
|
36
37
|
"@travetto/transformer": {
|
package/src/decorator.ts
CHANGED
|
@@ -10,6 +10,7 @@ const fromInput = <T extends { qualifier?: symbol }>(input?: T | symbol): T =>
|
|
|
10
10
|
/**
|
|
11
11
|
* Indicate that a class is able to be injected
|
|
12
12
|
* @augments `@travetto/schema:Schema`
|
|
13
|
+
* @example opt-in
|
|
13
14
|
* @kind decorator
|
|
14
15
|
*/
|
|
15
16
|
export function Injectable(input?: Partial<InjectableCandidate> | symbol) {
|