first-di 0.1.42 → 0.1.45

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.
@@ -1,3 +1,4 @@
1
+ import "reflect-metadata";
1
2
  import type { AutowiredOptions } from "../models/autowired-options";
2
3
  import type { ClassConstructor, OverrideConstructor } from "../typings/class-constructor";
3
4
  import type { OverrideOptions } from "../models/override-options";
@@ -1,13 +1,7 @@
1
1
  "use strict";
2
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
3
- /* eslint-disable @typescript-eslint/strict-boolean-expressions */
4
- /* eslint-disable max-statements */
5
- /* eslint-disable @typescript-eslint/ban-types */
6
- /* eslint-disable @typescript-eslint/no-explicit-any */
7
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
8
- /* eslint-disable @typescript-eslint/no-unsafe-call */
9
2
  Object.defineProperty(exports, "__esModule", { value: true });
10
3
  exports.DI = void 0;
4
+ require("reflect-metadata");
11
5
  const autowired_lifetimes_1 = require("../models/autowired-lifetimes");
12
6
  class DI {
13
7
  constructor() {
@@ -37,15 +31,16 @@ class DI {
37
31
  });
38
32
  };
39
33
  }
34
+ // eslint-disable-next-line max-statements
40
35
  makeResolve(inConstructor, inOptions, caller, propertyKey) {
41
- var _a, _b;
36
+ var _a, _b, _c;
42
37
  let constructor = inConstructor;
43
38
  let options = inOptions;
44
39
  if (this.overrideList.has(constructor)) {
45
40
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
46
- const overridOptions = this.overrideList.get(constructor);
47
- constructor = overridOptions.to;
48
- options = (_a = overridOptions.options) !== null && _a !== void 0 ? _a : options;
41
+ const overrideOptions = this.overrideList.get(constructor);
42
+ constructor = overrideOptions.to;
43
+ options = (_a = overrideOptions.options) !== null && _a !== void 0 ? _a : options;
49
44
  }
50
45
  const lifeTime = (_b = options === null || options === void 0 ? void 0 : options.lifeTime) !== null && _b !== void 0 ? _b : autowired_lifetimes_1.AutowiredLifetimes.Singleton;
51
46
  if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.Singleton) {
@@ -53,17 +48,18 @@ class DI {
53
48
  return this.singletonsList.get(constructor);
54
49
  }
55
50
  }
56
- else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerOwned && propertyKey) {
51
+ else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerOwned && Boolean(propertyKey)) {
57
52
  if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
58
53
  return Reflect.get(constructor, this.getDiKey(propertyKey));
59
54
  }
60
55
  }
61
- else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerInstance && caller && propertyKey) {
56
+ else if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.PerInstance && caller && Boolean(propertyKey)) {
62
57
  if (Reflect.has(caller, this.getDiKey(propertyKey))) {
63
58
  return Reflect.get(caller, this.getDiKey(propertyKey));
64
59
  }
65
60
  }
66
- const params = Reflect.getMetadata("design:paramtypes", constructor) || [];
61
+ const params = (_c = Reflect
62
+ .getMetadata("design:paramtypes", constructor)) !== null && _c !== void 0 ? _c : [];
67
63
  const object = new constructor(...params
68
64
  .map((paramConstructor) => this.makeResolve(paramConstructor, options)));
69
65
  if (lifeTime === autowired_lifetimes_1.AutowiredLifetimes.Singleton) {
@@ -82,8 +78,10 @@ class DI {
82
78
  this.overrideList = new Map();
83
79
  }
84
80
  makeOverride(from, to, options) {
85
- this.overrideList.set(from, { to,
86
- options });
81
+ this.overrideList.set(from, {
82
+ to,
83
+ options
84
+ });
87
85
  }
88
86
  getDiKey(propertyKey) {
89
87
  return `$_di_${String(propertyKey)}`; // Think about symbol
@@ -1,10 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
- /* eslint-disable @typescript-eslint/strict-boolean-expressions */
3
- /* eslint-disable max-statements */
4
- /* eslint-disable @typescript-eslint/ban-types */
5
- /* eslint-disable @typescript-eslint/no-explicit-any */
6
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
7
- /* eslint-disable @typescript-eslint/no-unsafe-call */
1
+ import "reflect-metadata";
8
2
  import { AutowiredLifetimes } from "../models/autowired-lifetimes";
9
3
  export class DI {
10
4
  constructor() {
@@ -34,15 +28,16 @@ export class DI {
34
28
  });
35
29
  };
36
30
  }
31
+ // eslint-disable-next-line max-statements
37
32
  makeResolve(inConstructor, inOptions, caller, propertyKey) {
38
- var _a, _b;
33
+ var _a, _b, _c;
39
34
  let constructor = inConstructor;
40
35
  let options = inOptions;
41
36
  if (this.overrideList.has(constructor)) {
42
37
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43
- const overridOptions = this.overrideList.get(constructor);
44
- constructor = overridOptions.to;
45
- options = (_a = overridOptions.options) !== null && _a !== void 0 ? _a : options;
38
+ const overrideOptions = this.overrideList.get(constructor);
39
+ constructor = overrideOptions.to;
40
+ options = (_a = overrideOptions.options) !== null && _a !== void 0 ? _a : options;
46
41
  }
47
42
  const lifeTime = (_b = options === null || options === void 0 ? void 0 : options.lifeTime) !== null && _b !== void 0 ? _b : AutowiredLifetimes.Singleton;
48
43
  if (lifeTime === AutowiredLifetimes.Singleton) {
@@ -50,17 +45,18 @@ export class DI {
50
45
  return this.singletonsList.get(constructor);
51
46
  }
52
47
  }
53
- else if (lifeTime === AutowiredLifetimes.PerOwned && propertyKey) {
48
+ else if (lifeTime === AutowiredLifetimes.PerOwned && Boolean(propertyKey)) {
54
49
  if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
55
50
  return Reflect.get(constructor, this.getDiKey(propertyKey));
56
51
  }
57
52
  }
58
- else if (lifeTime === AutowiredLifetimes.PerInstance && caller && propertyKey) {
53
+ else if (lifeTime === AutowiredLifetimes.PerInstance && caller && Boolean(propertyKey)) {
59
54
  if (Reflect.has(caller, this.getDiKey(propertyKey))) {
60
55
  return Reflect.get(caller, this.getDiKey(propertyKey));
61
56
  }
62
57
  }
63
- const params = Reflect.getMetadata("design:paramtypes", constructor) || [];
58
+ const params = (_c = Reflect
59
+ .getMetadata("design:paramtypes", constructor)) !== null && _c !== void 0 ? _c : [];
64
60
  const object = new constructor(...params
65
61
  .map((paramConstructor) => this.makeResolve(paramConstructor, options)));
66
62
  if (lifeTime === AutowiredLifetimes.Singleton) {
@@ -79,8 +75,10 @@ export class DI {
79
75
  this.overrideList = new Map();
80
76
  }
81
77
  makeOverride(from, to, options) {
82
- this.overrideList.set(from, { to,
83
- options });
78
+ this.overrideList.set(from, {
79
+ to,
80
+ options
81
+ });
84
82
  }
85
83
  getDiKey(propertyKey) {
86
84
  return `$_di_${String(propertyKey)}`; // Think about symbol
package/package.json CHANGED
@@ -1,20 +1,22 @@
1
1
  {
2
2
  "name": "first-di",
3
- "version": "0.1.42",
3
+ "version": "0.1.45",
4
4
  "description": "Easy dependency injection for typescript applications",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "typings": "./dist/index.d.ts",
8
+ "dependencies": {
9
+ "reflect-metadata": "^0.1.13"
10
+ },
8
11
  "devDependencies": {
9
- "@labeg/code-style": "^2.0.30",
10
- "@types/chai": "^4.3.0",
11
- "@types/mocha": "^9.1.0",
12
- "@types/node": "^17.0.21",
12
+ "@labeg/code-style": "^2.0.34",
13
+ "@types/chai": "^4.3.1",
14
+ "@types/mocha": "^9.1.1",
15
+ "@types/node": "^17.0.34",
13
16
  "chai": "^4.3.6",
14
- "mocha": "^9.2.2",
15
- "reflect-metadata": "^0.1.13",
17
+ "mocha": "^10.0.0",
16
18
  "ts-node": "^10.7.0",
17
- "typescript": "^4.6.2"
19
+ "typescript": "^4.6.4"
18
20
  },
19
21
  "scripts": {
20
22
  "cs:eslint": "eslint --fix -c .eslintrc.js --ext .tsx,.ts,.jsx,.js ./src/ ./tests/",
package/src/classes/di.ts CHANGED
@@ -1,11 +1,5 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
- /* eslint-disable @typescript-eslint/strict-boolean-expressions */
3
- /* eslint-disable max-statements */
4
- /* eslint-disable @typescript-eslint/ban-types */
5
- /* eslint-disable @typescript-eslint/no-explicit-any */
6
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
7
- /* eslint-disable @typescript-eslint/no-unsafe-call */
8
1
 
2
+ import "reflect-metadata";
9
3
  import type {AutowiredOptions} from "../models/autowired-options";
10
4
  import type {ClassConstructor, OverrideConstructor} from "../typings/class-constructor";
11
5
  import {AutowiredLifetimes} from "../models/autowired-lifetimes";
@@ -78,7 +72,7 @@ export class DI {
78
72
 
79
73
  protected makeAutowired (options?: AutowiredOptions): PropertyDecorator {
80
74
  return (target: object, propertyKey: string | symbol): void => {
81
- const type = (Reflect as any).getMetadata("design:type", target, propertyKey) as ClassConstructor<object>;
75
+ const type = Reflect.getMetadata("design:type", target, propertyKey) as ClassConstructor<object>;
82
76
  const {resolve} = this;
83
77
 
84
78
  Reflect.defineProperty(
@@ -95,6 +89,7 @@ export class DI {
95
89
  };
96
90
  }
97
91
 
92
+ // eslint-disable-next-line max-statements
98
93
  protected makeResolve<T extends object>(
99
94
  inConstructor: ClassConstructor<T>,
100
95
  inOptions?: AutowiredOptions,
@@ -106,9 +101,9 @@ export class DI {
106
101
 
107
102
  if (this.overrideList.has(constructor)) {
108
103
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109
- const overridOptions = this.overrideList.get(constructor)!;
110
- constructor = overridOptions.to as ClassConstructor<T>;
111
- options = overridOptions.options ?? options;
104
+ const overrideOptions = this.overrideList.get(constructor)!;
105
+ constructor = overrideOptions.to as ClassConstructor<T>;
106
+ options = overrideOptions.options ?? options;
112
107
  }
113
108
 
114
109
  const lifeTime = options?.lifeTime ?? AutowiredLifetimes.Singleton;
@@ -116,17 +111,18 @@ export class DI {
116
111
  if (this.singletonsList.has(constructor)) {
117
112
  return this.singletonsList.get(constructor) as T;
118
113
  }
119
- } else if (lifeTime === AutowiredLifetimes.PerOwned && propertyKey) {
114
+ } else if (lifeTime === AutowiredLifetimes.PerOwned && Boolean(propertyKey)) {
120
115
  if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
121
116
  return Reflect.get(constructor, this.getDiKey(propertyKey)) as T;
122
117
  }
123
- } else if (lifeTime === AutowiredLifetimes.PerInstance && caller && propertyKey) {
118
+ } else if (lifeTime === AutowiredLifetimes.PerInstance && caller && Boolean(propertyKey)) {
124
119
  if (Reflect.has(caller, this.getDiKey(propertyKey))) {
125
120
  return Reflect.get(caller, this.getDiKey(propertyKey)) as T;
126
121
  }
127
122
  }
128
123
 
129
- const params: ClassConstructor<object>[] = (Reflect as any).getMetadata("design:paramtypes", constructor) as [] || [];
124
+ const params: ClassConstructor<object>[] = Reflect
125
+ .getMetadata("design:paramtypes", constructor) as ([] | null) ?? [];
130
126
 
131
127
  const object = new constructor(...params
132
128
  .map((paramConstructor: ClassConstructor<object>) => this.makeResolve(paramConstructor, options)));
@@ -148,8 +144,13 @@ export class DI {
148
144
  }
149
145
 
150
146
  protected makeOverride<T extends object>(from: OverrideConstructor<T>, to: ClassConstructor<T>, options?: AutowiredOptions): void {
151
- this.overrideList.set(from, {to,
152
- options});
147
+ this.overrideList.set(
148
+ from,
149
+ {
150
+ to,
151
+ options
152
+ }
153
+ );
153
154
  }
154
155
 
155
156
  protected getDiKey (propertyKey?: string | symbol): string {