@travetto/di 2.0.0 → 2.1.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 CHANGED
@@ -11,7 +11,7 @@ npm install @travetto/di
11
11
  [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 type signafiers. The primary goal of dependency injection is to allow for separation of concerns of object creation and it's usage.
12
12
 
13
13
  ## Declaration
14
- The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L30) and [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L72) 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 implementations (e.g. [MongoModelService](https://github.com/travetto/travetto/tree/main/module/model-mongo/src/service.ts#L42) provides itself as an injectable candidate for [ModelCrudSupport](https://github.com/travetto/travetto/tree/main/module/model/src/service/crud.ts).
14
+ The [@Injectable](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L30) and [@InjectableFactory](https://github.com/travetto/travetto/tree/main/module/di/src/decorator.ts#L72) 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 implementations (e.g. [MongoModelService](https://github.com/travetto/travetto/tree/main/module/model-mongo/src/service.ts#L44) provides itself as an injectable candidate for [ModelCrudSupport](https://github.com/travetto/travetto/tree/main/module/model/src/service/crud.ts).
15
15
 
16
16
  **Code: Example Injectable**
17
17
  ```typescript
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/di",
3
3
  "displayName": "Dependency Injection",
4
- "version": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "description": "Dependency registration/management and injection support.",
6
6
  "keywords": [
7
7
  "ast-transformations",
@@ -29,12 +29,12 @@
29
29
  "directory": "module/di"
30
30
  },
31
31
  "dependencies": {
32
- "@travetto/transformer": "^2.0.0",
33
- "@travetto/registry": "^2.0.0"
32
+ "@travetto/transformer": "^2.1.0",
33
+ "@travetto/registry": "^2.1.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@travetto/config": "^2.0.0",
37
- "@travetto/schema": "^2.0.0"
36
+ "@travetto/config": "^2.1.0",
37
+ "@travetto/schema": "^2.1.0"
38
38
  },
39
39
  "docDependencies": {
40
40
  "@travetto/model-mongo": true,
package/src/decorator.ts CHANGED
@@ -52,7 +52,7 @@ export function InjectArgs(configs?: InjectConfig[][]) {
52
52
  * @augments `@trv:di/Inject`
53
53
  */
54
54
  export function Inject(first?: InjectConfig | symbol, ...args: (InjectConfig | undefined)[]) {
55
- return (target: unknown, propertyKey: string | symbol, idx?: number) => {
55
+ return (target: unknown, propertyKey: string | symbol, idx?: number | PropertyDescriptor) => {
56
56
  if (typeof idx !== 'number') { // Only register if on property
57
57
  const config: InjectConfig = collapseConfig(first, ...args);
58
58
 
@@ -1,7 +1,7 @@
1
1
  import * as ts from 'typescript';
2
2
 
3
3
  import {
4
- TransformerState, DecoratorMeta, OnClass, OnProperty, OnStaticMethod, DecoratorUtil, LiteralUtil, TransformerId
4
+ TransformerState, DecoratorMeta, OnClass, OnProperty, OnStaticMethod, DecoratorUtil, LiteralUtil, TransformerId, OnSetter
5
5
  } from '@travetto/transformer';
6
6
 
7
7
  const INJECTABLE_MOD = '@travetto/di/src/decorator';
@@ -16,7 +16,7 @@ export class InjectableTransformer {
16
16
  /**
17
17
  * Handle a specific declaration param/property
18
18
  */
19
- static processDeclaration(state: TransformerState, param: ts.ParameterDeclaration | ts.PropertyDeclaration) {
19
+ static processDeclaration(state: TransformerState, param: ts.ParameterDeclaration | ts.SetAccessorDeclaration | ts.PropertyDeclaration) {
20
20
  const existing = state.findDecorator(this, param, 'Inject', INJECTABLE_MOD);
21
21
 
22
22
  if (!(existing || ts.isParameter(param))) {
@@ -32,7 +32,7 @@ export class InjectableTransformer {
32
32
  }
33
33
 
34
34
  args.unshift(state.fromLiteral({
35
- target: state.getOrImport(state.resolveExternalType(param)),
35
+ target: state.getOrImport(state.resolveExternalType(ts.isSetAccessorDeclaration(param) ? param.parameters[0] : param)),
36
36
  optional
37
37
  }));
38
38
 
@@ -102,6 +102,26 @@ export class InjectableTransformer {
102
102
  );
103
103
  }
104
104
 
105
+ /**
106
+ * Handle Inject annotations for fields/args
107
+ */
108
+ @OnSetter('Inject')
109
+ static registerInjectSetter(state: TransformerState, node: ts.SetAccessorDeclaration, dm?: DecoratorMeta) {
110
+ const decl = state.findDecorator(this, node, 'Inject', INJECTABLE_MOD);
111
+
112
+ // Doing decls
113
+ return state.factory.updateSetAccessorDeclaration(
114
+ node,
115
+ DecoratorUtil.spliceDecorators(node, decl, [
116
+ state.createDecorator(INJECTABLE_MOD, 'Inject', ...this.processDeclaration(state, node)),
117
+ ], 0),
118
+ node.modifiers,
119
+ node.name,
120
+ node.parameters,
121
+ node.body
122
+ );
123
+ }
124
+
105
125
  /**
106
126
  * Handle InjectableFactory creation
107
127
  */