first-di 1.0.3 → 1.0.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "first-di",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Easy dependency injection for typescript applications",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -9,7 +9,7 @@
9
9
  "reflect-metadata": ">=0.1.0"
10
10
  },
11
11
  "devDependencies": {
12
- "@labeg/code-style": "^2.0.52",
12
+ "@labeg/code-style": "^2.0.59",
13
13
  "@types/chai": "^4.3.4",
14
14
  "@types/mocha": "^10.0.0",
15
15
  "@types/node": "^18.11.9",
package/src/classes/di.ts DELETED
@@ -1,159 +0,0 @@
1
-
2
- import type {AutowiredOptions} from "../models/autowired-options.js";
3
- import type {ClassConstructor, OverrideConstructor} from "../typings/class-constructor.js";
4
- import {AutowiredLifetimes} from "../models/autowired-lifetimes.js";
5
- import type {OverrideOptions} from "../models/override-options.js";
6
-
7
- export class DI {
8
-
9
- public static defaultOptions: AutowiredOptions = {
10
- lifeTime: AutowiredLifetimes.Singleton
11
- };
12
-
13
- public autowired: (options?: AutowiredOptions) => PropertyDecorator;
14
-
15
- public reset: () => void;
16
-
17
- public resolve: <T extends object>(
18
- constructor: ClassConstructor<T>,
19
- options?: AutowiredOptions,
20
- caller?: object,
21
- propertyKey?: string | symbol
22
- ) => T;
23
-
24
- public singleton: <T extends object>(constructor: ClassConstructor<T>, options?: AutowiredOptions) => T;
25
-
26
- public instance: <T extends object>(constructor: ClassConstructor<T>, options?: AutowiredOptions) => T;
27
-
28
- public override: <T extends object>(
29
- from: OverrideConstructor<object>, // Must be T, but typescript have bug with private property from implement class
30
- to: ClassConstructor<T>,
31
- options?: AutowiredOptions
32
- ) => void;
33
-
34
- protected singletonsList: Map<ClassConstructor<object>, object> = new Map<ClassConstructor<object>, object>();
35
-
36
- protected overrideList: Map<OverrideConstructor<object>, OverrideOptions> = new Map<ClassConstructor<object>, OverrideOptions>();
37
-
38
- public constructor () {
39
- this.autowired = (options?: AutowiredOptions): PropertyDecorator => this.makeAutowired(options);
40
- this.reset = (): void => {
41
- this.makeReset();
42
- };
43
-
44
- this.resolve = <T extends object>(
45
- constructor: ClassConstructor<T>,
46
- options?: AutowiredOptions,
47
- caller?: object,
48
- propertyKey?: string | symbol
49
- ): T => this.makeResolve(constructor, options, caller, propertyKey);
50
-
51
- this.singleton = <T extends object>(
52
- constructor: ClassConstructor<T>,
53
- options?: AutowiredOptions
54
- ): T => this.makeResolve(constructor, {...options,
55
- lifeTime: AutowiredLifetimes.Singleton});
56
-
57
- this.instance = <T extends object>(
58
- constructor: ClassConstructor<T>,
59
- options?: AutowiredOptions
60
- ): T => this.makeResolve(constructor, {...options,
61
- lifeTime: AutowiredLifetimes.PerInstance});
62
-
63
- this.override = <T extends object>(
64
- from: OverrideConstructor<T>,
65
- to: ClassConstructor<T>,
66
- options?: AutowiredOptions
67
- ): void => {
68
- this.makeOverride(from, to, options);
69
- };
70
- }
71
-
72
- protected makeAutowired (options?: AutowiredOptions): PropertyDecorator {
73
- return (target: object, propertyKey: string | symbol): void => {
74
- const type = Reflect.getMetadata("design:type", target, propertyKey) as ClassConstructor<object>;
75
- const {resolve} = this;
76
-
77
- Reflect.defineProperty(
78
- target,
79
- propertyKey,
80
- {
81
- configurable: false,
82
- enumerable: false,
83
- get () {
84
- return resolve(type, options, this as object, propertyKey);
85
- }
86
- }
87
- );
88
- };
89
- }
90
-
91
- // eslint-disable-next-line max-statements
92
- protected makeResolve<T extends object>(
93
- inConstructor: ClassConstructor<T>,
94
- inOptions?: AutowiredOptions,
95
- caller?: object,
96
- propertyKey?: string | symbol
97
- ): T {
98
- let constructor = inConstructor;
99
- let options = inOptions;
100
-
101
- if (this.overrideList.has(constructor)) {
102
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
103
- const overrideOptions = this.overrideList.get(constructor)!;
104
- constructor = overrideOptions.to as ClassConstructor<T>;
105
- options = overrideOptions.options ?? options;
106
- }
107
-
108
- const lifeTime = options?.lifeTime ?? AutowiredLifetimes.Singleton;
109
- if (lifeTime === AutowiredLifetimes.Singleton) {
110
- if (this.singletonsList.has(constructor)) {
111
- return this.singletonsList.get(constructor) as T;
112
- }
113
- } else if (lifeTime === AutowiredLifetimes.PerOwned && Boolean(propertyKey)) {
114
- if (Reflect.has(constructor, this.getDiKey(propertyKey))) {
115
- return Reflect.get(constructor, this.getDiKey(propertyKey)) as T;
116
- }
117
- } else if (lifeTime === AutowiredLifetimes.PerInstance && caller && Boolean(propertyKey)) {
118
- if (Reflect.has(caller, this.getDiKey(propertyKey))) {
119
- return Reflect.get(caller, this.getDiKey(propertyKey)) as T;
120
- }
121
- }
122
-
123
- const params: ClassConstructor<object>[] = Reflect
124
- .getMetadata("design:paramtypes", constructor) as ([] | null) ?? [];
125
-
126
- const object = new constructor(...params
127
- .map((paramConstructor: ClassConstructor<object>) => this.makeResolve(paramConstructor, options)));
128
-
129
- if (lifeTime === AutowiredLifetimes.Singleton) {
130
- this.singletonsList.set(constructor, object);
131
- } else if (lifeTime === AutowiredLifetimes.PerOwned) {
132
- Reflect.set(constructor, this.getDiKey(propertyKey), object);
133
- } else if (lifeTime === AutowiredLifetimes.PerInstance && caller) {
134
- Reflect.set(caller, this.getDiKey(propertyKey), object);
135
- }
136
-
137
- return object;
138
- }
139
-
140
- protected makeReset (): void {
141
- this.singletonsList = new Map<ClassConstructor<object>, object>();
142
- this.overrideList = new Map<ClassConstructor<object>, OverrideOptions>();
143
- }
144
-
145
- protected makeOverride<T extends object>(from: OverrideConstructor<T>, to: ClassConstructor<T>, options?: AutowiredOptions): void {
146
- this.overrideList.set(
147
- from,
148
- {
149
- to,
150
- options
151
- }
152
- );
153
- }
154
-
155
- protected getDiKey (propertyKey?: string | symbol): string {
156
- return `$_di_${String(propertyKey)}`; // Think about symbol
157
- }
158
-
159
- }
@@ -1,5 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
-
3
- export const reflection = (..._params: unknown[]): void => {
4
- // For generation reflection by typescript
5
- };
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export * from "./classes/di.js";
2
- export * from "./decorators/reflection.js";
3
- export * from "./models/autowired-lifetimes.js";
4
-
5
- import {DI} from "./classes/di.js";
6
- export const {
7
- autowired,
8
- override,
9
- resolve,
10
- singleton,
11
- instance,
12
- reset
13
- } = new DI(); // Export as singleton
@@ -1,24 +0,0 @@
1
- export enum AutowiredLifetimes {
2
-
3
- /**
4
- * Create one instance for all resolvers
5
- */
6
-
7
- Singleton = 0,
8
-
9
- /**
10
- * Create one instance for each resolver
11
- */
12
-
13
- PerInstance = 1,
14
-
15
- /**
16
- * Create one instance for each type of resolver
17
- */
18
- PerOwned = 2,
19
-
20
- /**
21
- * Recreate each dependency on each access to dependency
22
- */
23
- PerAccess = 3
24
- }
@@ -1,5 +0,0 @@
1
- import type {AutowiredLifetimes} from "./autowired-lifetimes.js";
2
-
3
- export interface AutowiredOptions {
4
- lifeTime: AutowiredLifetimes;
5
- }
@@ -1,8 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
- import type {ClassConstructor} from "../typings/class-constructor.js";
3
- import type {AutowiredOptions} from "./autowired-options.js";
4
-
5
- export interface OverrideOptions {
6
- to: ClassConstructor<object>;
7
- options?: AutowiredOptions;
8
- }
@@ -1,7 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- /* eslint-disable @typescript-eslint/no-type-alias */
3
- /* eslint-disable @typescript-eslint/ban-types */
4
-
5
- export type ClassConstructor<T extends object> = (new (...params: any) => T); // Use ConstructorParameters<T>, but how???
6
-
7
- export type OverrideConstructor<T extends object> = ClassConstructor<T> | (Function & {prototype: T});