@art-ws/di 2.0.6 → 2.0.8

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.
@@ -7,7 +7,7 @@ export declare class Injector {
7
7
  parent: Injector | null;
8
8
  addFactory<T>(token: Token<T>, factory?: Func<T>): void;
9
9
  addValue<T>(token: Token<T>, value: T): void;
10
- addProviders(providers?: Provider[]): this;
10
+ addProviders(...providers: Provider[]): this;
11
11
  get<T>(token: Token<T>): T;
12
12
  runScope<T>(fn: Func<T>, options?: Partial<{
13
13
  providers: Provider[];
@@ -23,10 +23,36 @@ export class Injector {
23
23
  addValue(token, value) {
24
24
  this.addFactory(token, () => value);
25
25
  }
26
- addProviders(providers) {
26
+ addProviders(...providers) {
27
27
  if (Array.isArray(providers)) {
28
- for (const { token, factory } of providers) {
29
- this.addFactory(token, factory);
28
+ for (const p of providers) {
29
+ if (p.token && p.factory) {
30
+ this.addFactory(p.token, p.factory);
31
+ }
32
+ else if (p.provide || p.useClass) {
33
+ const provide = p.provide || p.useClass;
34
+ if (!provide)
35
+ throw new Error(`provide|useClass is not defined`);
36
+ const factory = () => {
37
+ if (p.useFactory || p.useClass) {
38
+ const deps = (p.deps || []).map((dep) => inject(dep));
39
+ if (p.useClass)
40
+ return new p.useClass(...deps);
41
+ else
42
+ return p.useFactory(...deps);
43
+ }
44
+ else if (p.useValue) {
45
+ return p.useValue;
46
+ }
47
+ else if (p.useExisting) {
48
+ return inject(p.useExisting);
49
+ }
50
+ else {
51
+ throw new Error(`useClass|useFactory|useValue|useExisting is not defined`);
52
+ }
53
+ };
54
+ this.addFactory(provide, factory);
55
+ }
30
56
  }
31
57
  }
32
58
  return this;
@@ -34,13 +60,10 @@ export class Injector {
34
60
  get(token) {
35
61
  if (!token)
36
62
  throw new Error(`Token not defined`);
37
- let instance = undefined;
38
- if (this.parent) {
63
+ let instance = this.#instances.get(token);
64
+ if (typeof instance === "undefined" && this.parent) {
39
65
  instance = this.parent.get(token);
40
66
  }
41
- if (typeof instance === "undefined") {
42
- instance = this.#instances.get(token);
43
- }
44
67
  if (typeof instance === "undefined") {
45
68
  const factory = this.#factories.get(token);
46
69
  if (typeof factory === "function") {
@@ -53,7 +76,7 @@ export class Injector {
53
76
  async runScope(fn, options) {
54
77
  const injector = new Injector();
55
78
  injector.parent = this;
56
- injector.addProviders(options?.providers);
79
+ injector.addProviders(...(options?.providers || []));
57
80
  Injector.onScope?.(injector);
58
81
  return asyncLocalStorage.run({ injector }, async () => {
59
82
  try {
@@ -1,4 +1,5 @@
1
1
  export type Func<T = unknown> = () => T;
2
+ export type FuncArgs<T = unknown> = (...args: any[]) => T;
2
3
  export declare class InjectionToken<T = unknown> {
3
4
  readonly name: string;
4
5
  factory?: (() => T) | undefined;
@@ -8,10 +9,17 @@ export interface AbstractType<T> extends Function {
8
9
  prototype: T;
9
10
  }
10
11
  export interface Type<T> extends Function {
11
- new (...args: unknown[]): T;
12
+ new (...args: any[]): T;
12
13
  }
13
14
  export type Token<T = unknown> = string | Symbol | Type<T> | AbstractType<T> | InjectionToken<T>;
14
15
  export type Provider<T = unknown> = {
15
- token: Token<T>;
16
- factory: Func<T>;
16
+ token?: Token<T>;
17
+ factory?: Func<T>;
18
+ provide?: Token<T>;
19
+ multi?: boolean;
20
+ deps?: Token[];
21
+ useClass?: Type<T>;
22
+ useExisting?: Token<T>;
23
+ useValue?: T;
24
+ useFactory?: FuncArgs<T>;
17
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@art-ws/di",
3
- "version": "2.0.6",
3
+ "version": "2.0.8",
4
4
  "description": "Dependency injection for TypeScript",
5
5
  "license": "UNLICENSED",
6
6
  "author": "art-ws Team",