@navios/di 0.1.3 → 0.1.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/dist/_tsup-dts-rollup.d.mts +36 -31
- package/dist/_tsup-dts-rollup.d.ts +36 -31
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +11 -4
- package/dist/index.mjs +10 -3
- package/package.json +1 -1
- package/src/__tests__/injectable.spec.mts +19 -1
- package/src/decorators/injectable.decorator.mts +34 -11
- package/src/index.mts +1 -0
- package/src/injection-token.mts +2 -0
- package/src/injector.mts +13 -5
- package/src/symbols/index.mts +1 -0
- package/src/symbols/injectable-token.mts +1 -0
- package/src/utils/get-injectable-token.mts +1 -1
- package/src/utils/get-injectors.mts +5 -5
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { AnyZodObject } from 'zod';
|
|
2
|
-
import { BoundInjectionToken as BoundInjectionToken_2 } from './injection-token.mjs';
|
|
3
|
-
import { ClassType as ClassType_2 } from './injection-token.mjs';
|
|
4
|
-
import { FactoryInjectionToken as FactoryInjectionToken_2 } from './injection-token.mjs';
|
|
5
|
-
import { InjectionToken as InjectionToken_2 } from './injection-token.mjs';
|
|
6
|
-
import { input } from 'zod';
|
|
1
|
+
import type { AnyZodObject } from 'zod';
|
|
7
2
|
import { z } from 'zod';
|
|
8
3
|
import { ZodOptional } from 'zod';
|
|
9
4
|
|
|
@@ -29,10 +24,18 @@ declare type ClassType = new (...args: any[]) => any;
|
|
|
29
24
|
export { ClassType }
|
|
30
25
|
export { ClassType as ClassType_alias_1 }
|
|
31
26
|
|
|
27
|
+
declare type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
|
|
28
|
+
export { ClassTypeWithArgument }
|
|
29
|
+
export { ClassTypeWithArgument as ClassTypeWithArgument_alias_1 }
|
|
30
|
+
|
|
32
31
|
declare type ClassTypeWithInstance<T> = new (...args: any[]) => T;
|
|
33
32
|
export { ClassTypeWithInstance }
|
|
34
33
|
export { ClassTypeWithInstance as ClassTypeWithInstance_alias_1 }
|
|
35
34
|
|
|
35
|
+
declare type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
|
|
36
|
+
export { ClassTypeWithInstanceAndArgument }
|
|
37
|
+
export { ClassTypeWithInstanceAndArgument as ClassTypeWithInstanceAndArgument_alias_1 }
|
|
38
|
+
|
|
36
39
|
declare interface CreateInjectorsOptions {
|
|
37
40
|
baseLocator: ServiceLocator;
|
|
38
41
|
}
|
|
@@ -173,14 +176,7 @@ declare const globalRegistry: Registry;
|
|
|
173
176
|
export { globalRegistry }
|
|
174
177
|
export { globalRegistry as globalRegistry_alias_1 }
|
|
175
178
|
|
|
176
|
-
declare const inject:
|
|
177
|
-
<T extends ClassType_2>(token: T): Promise<InstanceType<T>>;
|
|
178
|
-
<T, S extends AnyZodObject>(token: InjectionToken_2<T, S>, args: input<S>): Promise<T>;
|
|
179
|
-
<T, S extends ZodOptional<AnyZodObject>>(token: InjectionToken_2<T, S>, args?: input<S>): Promise<T>;
|
|
180
|
-
<T>(token: InjectionToken_2<T, undefined>): Promise<T>;
|
|
181
|
-
<T>(token: BoundInjectionToken_2<T, any>): Promise<T>;
|
|
182
|
-
<T>(token: FactoryInjectionToken_2<T, any>): Promise<T>;
|
|
183
|
-
};
|
|
179
|
+
declare const inject: Injectors['inject'];
|
|
184
180
|
export { inject }
|
|
185
181
|
export { inject as inject_alias_1 }
|
|
186
182
|
|
|
@@ -188,13 +184,6 @@ declare function Injectable(): <T extends ClassType>(target: T, context: ClassDe
|
|
|
188
184
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
189
185
|
};
|
|
190
186
|
|
|
191
|
-
declare function Injectable<T extends ClassType>(options: {
|
|
192
|
-
scope?: InjectableScope;
|
|
193
|
-
token: InjectionToken<T, undefined>;
|
|
194
|
-
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
195
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
196
|
-
};
|
|
197
|
-
|
|
198
187
|
declare function Injectable<R>(options: {
|
|
199
188
|
scope?: InjectableScope;
|
|
200
189
|
type: InjectableType.Factory;
|
|
@@ -202,6 +191,29 @@ declare function Injectable<R>(options: {
|
|
|
202
191
|
[InjectableTokenMeta]: InjectionToken<R, undefined>;
|
|
203
192
|
};
|
|
204
193
|
|
|
194
|
+
declare function Injectable<S extends AnyZodObject>(options: {
|
|
195
|
+
scope?: InjectableScope;
|
|
196
|
+
type?: InjectableType.Class;
|
|
197
|
+
token: InjectionToken<undefined, S>;
|
|
198
|
+
}): <T extends ClassTypeWithArgument<z.output<S>>>(target: T, context: ClassDecoratorContext) => T & {
|
|
199
|
+
[InjectableTokenMeta]: InjectionToken<T, S>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
203
|
+
scope?: InjectableScope;
|
|
204
|
+
type?: InjectableType.Class;
|
|
205
|
+
token: InjectionToken<R, S>;
|
|
206
|
+
}): <T extends ClassTypeWithInstanceAndArgument<R, z.output<S>>>(target: T, context: ClassDecoratorContext) => T & {
|
|
207
|
+
[InjectableTokenMeta]: InjectionToken<R, S>;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
declare function Injectable<T extends ClassType>(options: {
|
|
211
|
+
scope?: InjectableScope;
|
|
212
|
+
token: InjectionToken<T, undefined>;
|
|
213
|
+
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
214
|
+
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
215
|
+
};
|
|
216
|
+
|
|
205
217
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
206
218
|
scope?: InjectableScope;
|
|
207
219
|
type: InjectableType.Factory;
|
|
@@ -336,7 +348,7 @@ declare function makeProxyServiceLocator(serviceLocator: ServiceLocator, ctx: Fa
|
|
|
336
348
|
export { makeProxyServiceLocator }
|
|
337
349
|
export { makeProxyServiceLocator as makeProxyServiceLocator_alias_1 }
|
|
338
350
|
|
|
339
|
-
declare const provideServiceLocator:
|
|
351
|
+
declare const provideServiceLocator: Injectors['provideServiceLocator'];
|
|
340
352
|
export { provideServiceLocator }
|
|
341
353
|
export { provideServiceLocator as provideServiceLocator_alias_1 }
|
|
342
354
|
|
|
@@ -509,14 +521,7 @@ declare class ServiceLocatorManager {
|
|
|
509
521
|
export { ServiceLocatorManager }
|
|
510
522
|
export { ServiceLocatorManager as ServiceLocatorManager_alias_1 }
|
|
511
523
|
|
|
512
|
-
declare const syncInject:
|
|
513
|
-
<T extends ClassType_2>(token: T): InstanceType<T>;
|
|
514
|
-
<T, S extends AnyZodObject>(token: InjectionToken_2<T, S>, args: input<S>): T;
|
|
515
|
-
<T, S extends ZodOptional<AnyZodObject>>(token: InjectionToken_2<T, S>, args?: input<S>): T;
|
|
516
|
-
<T>(token: InjectionToken_2<T, undefined>): T;
|
|
517
|
-
<T>(token: BoundInjectionToken_2<T, any>): T;
|
|
518
|
-
<T>(token: FactoryInjectionToken_2<T, any>): T;
|
|
519
|
-
};
|
|
524
|
+
declare const syncInject: Injectors['syncInject'];
|
|
520
525
|
export { syncInject }
|
|
521
526
|
export { syncInject as syncInject_alias_1 }
|
|
522
527
|
|
|
@@ -529,7 +534,7 @@ export { UnknownError }
|
|
|
529
534
|
export { UnknownError as UnknownError_alias_1 }
|
|
530
535
|
export { UnknownError as UnknownError_alias_2 }
|
|
531
536
|
|
|
532
|
-
declare const wrapSyncInit:
|
|
537
|
+
declare const wrapSyncInit: Injectors['wrapSyncInit'];
|
|
533
538
|
export { wrapSyncInit }
|
|
534
539
|
export { wrapSyncInit as wrapSyncInit_alias_1 }
|
|
535
540
|
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { AnyZodObject } from 'zod';
|
|
2
|
-
import { BoundInjectionToken as BoundInjectionToken_2 } from './injection-token.mjs';
|
|
3
|
-
import { ClassType as ClassType_2 } from './injection-token.mjs';
|
|
4
|
-
import { FactoryInjectionToken as FactoryInjectionToken_2 } from './injection-token.mjs';
|
|
5
|
-
import { InjectionToken as InjectionToken_2 } from './injection-token.mjs';
|
|
6
|
-
import { input } from 'zod';
|
|
1
|
+
import type { AnyZodObject } from 'zod';
|
|
7
2
|
import { z } from 'zod';
|
|
8
3
|
import { ZodOptional } from 'zod';
|
|
9
4
|
|
|
@@ -29,10 +24,18 @@ declare type ClassType = new (...args: any[]) => any;
|
|
|
29
24
|
export { ClassType }
|
|
30
25
|
export { ClassType as ClassType_alias_1 }
|
|
31
26
|
|
|
27
|
+
declare type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
|
|
28
|
+
export { ClassTypeWithArgument }
|
|
29
|
+
export { ClassTypeWithArgument as ClassTypeWithArgument_alias_1 }
|
|
30
|
+
|
|
32
31
|
declare type ClassTypeWithInstance<T> = new (...args: any[]) => T;
|
|
33
32
|
export { ClassTypeWithInstance }
|
|
34
33
|
export { ClassTypeWithInstance as ClassTypeWithInstance_alias_1 }
|
|
35
34
|
|
|
35
|
+
declare type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
|
|
36
|
+
export { ClassTypeWithInstanceAndArgument }
|
|
37
|
+
export { ClassTypeWithInstanceAndArgument as ClassTypeWithInstanceAndArgument_alias_1 }
|
|
38
|
+
|
|
36
39
|
declare interface CreateInjectorsOptions {
|
|
37
40
|
baseLocator: ServiceLocator;
|
|
38
41
|
}
|
|
@@ -173,14 +176,7 @@ declare const globalRegistry: Registry;
|
|
|
173
176
|
export { globalRegistry }
|
|
174
177
|
export { globalRegistry as globalRegistry_alias_1 }
|
|
175
178
|
|
|
176
|
-
declare const inject:
|
|
177
|
-
<T extends ClassType_2>(token: T): Promise<InstanceType<T>>;
|
|
178
|
-
<T, S extends AnyZodObject>(token: InjectionToken_2<T, S>, args: input<S>): Promise<T>;
|
|
179
|
-
<T, S extends ZodOptional<AnyZodObject>>(token: InjectionToken_2<T, S>, args?: input<S>): Promise<T>;
|
|
180
|
-
<T>(token: InjectionToken_2<T, undefined>): Promise<T>;
|
|
181
|
-
<T>(token: BoundInjectionToken_2<T, any>): Promise<T>;
|
|
182
|
-
<T>(token: FactoryInjectionToken_2<T, any>): Promise<T>;
|
|
183
|
-
};
|
|
179
|
+
declare const inject: Injectors['inject'];
|
|
184
180
|
export { inject }
|
|
185
181
|
export { inject as inject_alias_1 }
|
|
186
182
|
|
|
@@ -188,13 +184,6 @@ declare function Injectable(): <T extends ClassType>(target: T, context: ClassDe
|
|
|
188
184
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
189
185
|
};
|
|
190
186
|
|
|
191
|
-
declare function Injectable<T extends ClassType>(options: {
|
|
192
|
-
scope?: InjectableScope;
|
|
193
|
-
token: InjectionToken<T, undefined>;
|
|
194
|
-
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
195
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
196
|
-
};
|
|
197
|
-
|
|
198
187
|
declare function Injectable<R>(options: {
|
|
199
188
|
scope?: InjectableScope;
|
|
200
189
|
type: InjectableType.Factory;
|
|
@@ -202,6 +191,29 @@ declare function Injectable<R>(options: {
|
|
|
202
191
|
[InjectableTokenMeta]: InjectionToken<R, undefined>;
|
|
203
192
|
};
|
|
204
193
|
|
|
194
|
+
declare function Injectable<S extends AnyZodObject>(options: {
|
|
195
|
+
scope?: InjectableScope;
|
|
196
|
+
type?: InjectableType.Class;
|
|
197
|
+
token: InjectionToken<undefined, S>;
|
|
198
|
+
}): <T extends ClassTypeWithArgument<z.output<S>>>(target: T, context: ClassDecoratorContext) => T & {
|
|
199
|
+
[InjectableTokenMeta]: InjectionToken<T, S>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
203
|
+
scope?: InjectableScope;
|
|
204
|
+
type?: InjectableType.Class;
|
|
205
|
+
token: InjectionToken<R, S>;
|
|
206
|
+
}): <T extends ClassTypeWithInstanceAndArgument<R, z.output<S>>>(target: T, context: ClassDecoratorContext) => T & {
|
|
207
|
+
[InjectableTokenMeta]: InjectionToken<R, S>;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
declare function Injectable<T extends ClassType>(options: {
|
|
211
|
+
scope?: InjectableScope;
|
|
212
|
+
token: InjectionToken<T, undefined>;
|
|
213
|
+
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
214
|
+
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
215
|
+
};
|
|
216
|
+
|
|
205
217
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
206
218
|
scope?: InjectableScope;
|
|
207
219
|
type: InjectableType.Factory;
|
|
@@ -336,7 +348,7 @@ declare function makeProxyServiceLocator(serviceLocator: ServiceLocator, ctx: Fa
|
|
|
336
348
|
export { makeProxyServiceLocator }
|
|
337
349
|
export { makeProxyServiceLocator as makeProxyServiceLocator_alias_1 }
|
|
338
350
|
|
|
339
|
-
declare const provideServiceLocator:
|
|
351
|
+
declare const provideServiceLocator: Injectors['provideServiceLocator'];
|
|
340
352
|
export { provideServiceLocator }
|
|
341
353
|
export { provideServiceLocator as provideServiceLocator_alias_1 }
|
|
342
354
|
|
|
@@ -509,14 +521,7 @@ declare class ServiceLocatorManager {
|
|
|
509
521
|
export { ServiceLocatorManager }
|
|
510
522
|
export { ServiceLocatorManager as ServiceLocatorManager_alias_1 }
|
|
511
523
|
|
|
512
|
-
declare const syncInject:
|
|
513
|
-
<T extends ClassType_2>(token: T): InstanceType<T>;
|
|
514
|
-
<T, S extends AnyZodObject>(token: InjectionToken_2<T, S>, args: input<S>): T;
|
|
515
|
-
<T, S extends ZodOptional<AnyZodObject>>(token: InjectionToken_2<T, S>, args?: input<S>): T;
|
|
516
|
-
<T>(token: InjectionToken_2<T, undefined>): T;
|
|
517
|
-
<T>(token: BoundInjectionToken_2<T, any>): T;
|
|
518
|
-
<T>(token: FactoryInjectionToken_2<T, any>): T;
|
|
519
|
-
};
|
|
524
|
+
declare const syncInject: Injectors['syncInject'];
|
|
520
525
|
export { syncInject }
|
|
521
526
|
export { syncInject as syncInject_alias_1 }
|
|
522
527
|
|
|
@@ -529,7 +534,7 @@ export { UnknownError }
|
|
|
529
534
|
export { UnknownError as UnknownError_alias_1 }
|
|
530
535
|
export { UnknownError as UnknownError_alias_2 }
|
|
531
536
|
|
|
532
|
-
declare const wrapSyncInit:
|
|
537
|
+
declare const wrapSyncInit: Injectors['wrapSyncInit'];
|
|
533
538
|
export { wrapSyncInit }
|
|
534
539
|
export { wrapSyncInit as wrapSyncInit_alias_1 }
|
|
535
540
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { Injectable } from './_tsup-dts-rollup.mjs';
|
|
2
2
|
export { InjectableOptions } from './_tsup-dts-rollup.mjs';
|
|
3
|
-
export { InjectableTokenMeta } from './_tsup-dts-rollup.mjs';
|
|
4
3
|
export { InjectableScope } from './_tsup-dts-rollup.mjs';
|
|
5
4
|
export { InjectableType } from './_tsup-dts-rollup.mjs';
|
|
6
5
|
export { ErrorsEnum } from './_tsup-dts-rollup.mjs';
|
|
@@ -25,7 +24,9 @@ export { EventEmitterInterface_alias_1 as EventEmitterInterface } from './_tsup-
|
|
|
25
24
|
export { EventEmitter_alias_1 as EventEmitter } from './_tsup-dts-rollup.mjs';
|
|
26
25
|
export { FactoryContext_alias_1 as FactoryContext } from './_tsup-dts-rollup.mjs';
|
|
27
26
|
export { ClassType } from './_tsup-dts-rollup.mjs';
|
|
27
|
+
export { ClassTypeWithArgument } from './_tsup-dts-rollup.mjs';
|
|
28
28
|
export { ClassTypeWithInstance } from './_tsup-dts-rollup.mjs';
|
|
29
|
+
export { ClassTypeWithInstanceAndArgument } from './_tsup-dts-rollup.mjs';
|
|
29
30
|
export { InjectionToken } from './_tsup-dts-rollup.mjs';
|
|
30
31
|
export { BoundInjectionToken } from './_tsup-dts-rollup.mjs';
|
|
31
32
|
export { FactoryInjectionToken } from './_tsup-dts-rollup.mjs';
|
|
@@ -52,3 +53,4 @@ export { ServiceLocatorInstanceHolderCreated } from './_tsup-dts-rollup.mjs';
|
|
|
52
53
|
export { ServiceLocatorInstanceHolderDestroying } from './_tsup-dts-rollup.mjs';
|
|
53
54
|
export { ServiceLocatorInstanceHolder } from './_tsup-dts-rollup.mjs';
|
|
54
55
|
export { ServiceLocatorManager } from './_tsup-dts-rollup.mjs';
|
|
56
|
+
export { InjectableTokenMeta } from './_tsup-dts-rollup.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { Injectable } from './_tsup-dts-rollup.js';
|
|
2
2
|
export { InjectableOptions } from './_tsup-dts-rollup.js';
|
|
3
|
-
export { InjectableTokenMeta } from './_tsup-dts-rollup.js';
|
|
4
3
|
export { InjectableScope } from './_tsup-dts-rollup.js';
|
|
5
4
|
export { InjectableType } from './_tsup-dts-rollup.js';
|
|
6
5
|
export { ErrorsEnum } from './_tsup-dts-rollup.js';
|
|
@@ -25,7 +24,9 @@ export { EventEmitterInterface_alias_1 as EventEmitterInterface } from './_tsup-
|
|
|
25
24
|
export { EventEmitter_alias_1 as EventEmitter } from './_tsup-dts-rollup.js';
|
|
26
25
|
export { FactoryContext_alias_1 as FactoryContext } from './_tsup-dts-rollup.js';
|
|
27
26
|
export { ClassType } from './_tsup-dts-rollup.js';
|
|
27
|
+
export { ClassTypeWithArgument } from './_tsup-dts-rollup.js';
|
|
28
28
|
export { ClassTypeWithInstance } from './_tsup-dts-rollup.js';
|
|
29
|
+
export { ClassTypeWithInstanceAndArgument } from './_tsup-dts-rollup.js';
|
|
29
30
|
export { InjectionToken } from './_tsup-dts-rollup.js';
|
|
30
31
|
export { BoundInjectionToken } from './_tsup-dts-rollup.js';
|
|
31
32
|
export { FactoryInjectionToken } from './_tsup-dts-rollup.js';
|
|
@@ -52,3 +53,4 @@ export { ServiceLocatorInstanceHolderCreated } from './_tsup-dts-rollup.js';
|
|
|
52
53
|
export { ServiceLocatorInstanceHolderDestroying } from './_tsup-dts-rollup.js';
|
|
53
54
|
export { ServiceLocatorInstanceHolder } from './_tsup-dts-rollup.js';
|
|
54
55
|
export { ServiceLocatorManager } from './_tsup-dts-rollup.js';
|
|
56
|
+
export { InjectableTokenMeta } from './_tsup-dts-rollup.js';
|
package/dist/index.js
CHANGED
|
@@ -58,6 +58,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
58
58
|
|
|
59
59
|
// packages/di/src/decorators/injectable.decorator.mts
|
|
60
60
|
var import_common2 = require("@navios/common");
|
|
61
|
+
var import_zod3 = require("zod");
|
|
61
62
|
|
|
62
63
|
// packages/di/src/enums/injectable-scope.enum.mts
|
|
63
64
|
var InjectableScope = /* @__PURE__ */ ((InjectableScope3) => {
|
|
@@ -229,6 +230,9 @@ function makeProxyServiceLocator(serviceLocator, ctx) {
|
|
|
229
230
|
return new ProxyServiceLocator(serviceLocator, ctx);
|
|
230
231
|
}
|
|
231
232
|
|
|
233
|
+
// packages/di/src/symbols/injectable-token.mts
|
|
234
|
+
var InjectableTokenMeta = Symbol.for("InjectableTokenMeta");
|
|
235
|
+
|
|
232
236
|
// packages/di/src/utils/get-injectors.mts
|
|
233
237
|
var InjectorsBase = /* @__PURE__ */ new Map();
|
|
234
238
|
function getInjectors({ baseLocator }) {
|
|
@@ -333,7 +337,6 @@ async function resolveService(ctx, target, args = []) {
|
|
|
333
337
|
}
|
|
334
338
|
|
|
335
339
|
// packages/di/src/decorators/injectable.decorator.mts
|
|
336
|
-
var InjectableTokenMeta = Symbol.for("InjectableTokenMeta");
|
|
337
340
|
function Injectable({
|
|
338
341
|
scope = "Singleton" /* Singleton */,
|
|
339
342
|
type = "Class" /* Class */,
|
|
@@ -350,7 +353,7 @@ function Injectable({
|
|
|
350
353
|
if (type === "Class" /* Class */) {
|
|
351
354
|
registry.set(
|
|
352
355
|
injectableToken,
|
|
353
|
-
async (ctx) => resolveService(ctx, target),
|
|
356
|
+
async (ctx, args) => resolveService(ctx, target, [args]),
|
|
354
357
|
scope
|
|
355
358
|
);
|
|
356
359
|
} else if (type === "Factory" /* Factory */) {
|
|
@@ -443,7 +446,7 @@ var UnknownError = class extends Error {
|
|
|
443
446
|
};
|
|
444
447
|
|
|
445
448
|
// packages/di/src/interfaces/factory.interface.mts
|
|
446
|
-
var
|
|
449
|
+
var import_zod4 = require("zod");
|
|
447
450
|
|
|
448
451
|
// packages/di/src/event-emitter.mts
|
|
449
452
|
var EventEmitter = class {
|
|
@@ -978,9 +981,13 @@ function getGlobalServiceLocator() {
|
|
|
978
981
|
}
|
|
979
982
|
return globalServiceLocator;
|
|
980
983
|
}
|
|
981
|
-
var
|
|
984
|
+
var values = getInjectors({
|
|
982
985
|
baseLocator: globalServiceLocator
|
|
983
986
|
});
|
|
987
|
+
var inject = values.inject;
|
|
988
|
+
var syncInject = values.syncInject;
|
|
989
|
+
var wrapSyncInit = values.wrapSyncInit;
|
|
990
|
+
var provideServiceLocator = values.provideServiceLocator;
|
|
984
991
|
// Annotate the CommonJS export names for ESM import in node:
|
|
985
992
|
0 && (module.exports = {
|
|
986
993
|
BoundInjectionToken,
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// packages/di/src/decorators/injectable.decorator.mts
|
|
2
2
|
import { NaviosException as NaviosException2 } from "@navios/common";
|
|
3
|
+
import "zod";
|
|
3
4
|
|
|
4
5
|
// packages/di/src/enums/injectable-scope.enum.mts
|
|
5
6
|
var InjectableScope = /* @__PURE__ */ ((InjectableScope3) => {
|
|
@@ -171,6 +172,9 @@ function makeProxyServiceLocator(serviceLocator, ctx) {
|
|
|
171
172
|
return new ProxyServiceLocator(serviceLocator, ctx);
|
|
172
173
|
}
|
|
173
174
|
|
|
175
|
+
// packages/di/src/symbols/injectable-token.mts
|
|
176
|
+
var InjectableTokenMeta = Symbol.for("InjectableTokenMeta");
|
|
177
|
+
|
|
174
178
|
// packages/di/src/utils/get-injectors.mts
|
|
175
179
|
var InjectorsBase = /* @__PURE__ */ new Map();
|
|
176
180
|
function getInjectors({ baseLocator }) {
|
|
@@ -275,7 +279,6 @@ async function resolveService(ctx, target, args = []) {
|
|
|
275
279
|
}
|
|
276
280
|
|
|
277
281
|
// packages/di/src/decorators/injectable.decorator.mts
|
|
278
|
-
var InjectableTokenMeta = Symbol.for("InjectableTokenMeta");
|
|
279
282
|
function Injectable({
|
|
280
283
|
scope = "Singleton" /* Singleton */,
|
|
281
284
|
type = "Class" /* Class */,
|
|
@@ -292,7 +295,7 @@ function Injectable({
|
|
|
292
295
|
if (type === "Class" /* Class */) {
|
|
293
296
|
registry.set(
|
|
294
297
|
injectableToken,
|
|
295
|
-
async (ctx) => resolveService(ctx, target),
|
|
298
|
+
async (ctx, args) => resolveService(ctx, target, [args]),
|
|
296
299
|
scope
|
|
297
300
|
);
|
|
298
301
|
} else if (type === "Factory" /* Factory */) {
|
|
@@ -920,9 +923,13 @@ function getGlobalServiceLocator() {
|
|
|
920
923
|
}
|
|
921
924
|
return globalServiceLocator;
|
|
922
925
|
}
|
|
923
|
-
var
|
|
926
|
+
var values = getInjectors({
|
|
924
927
|
baseLocator: globalServiceLocator
|
|
925
928
|
});
|
|
929
|
+
var inject = values.inject;
|
|
930
|
+
var syncInject = values.syncInject;
|
|
931
|
+
var wrapSyncInit = values.wrapSyncInit;
|
|
932
|
+
var provideServiceLocator = values.provideServiceLocator;
|
|
926
933
|
export {
|
|
927
934
|
BoundInjectionToken,
|
|
928
935
|
ErrorsEnum,
|
package/package.json
CHANGED
|
@@ -79,7 +79,7 @@ describe('Injectable decorator', () => {
|
|
|
79
79
|
expect(value).toBeInstanceOf(Test)
|
|
80
80
|
})
|
|
81
81
|
|
|
82
|
-
it('should work with injection token and schema', async () => {
|
|
82
|
+
it('should work with factory injection token and schema', async () => {
|
|
83
83
|
class TestFoo {
|
|
84
84
|
constructor(public readonly foo: string) {}
|
|
85
85
|
}
|
|
@@ -164,4 +164,22 @@ describe('Injectable decorator', () => {
|
|
|
164
164
|
const result2 = await inst2.makeFoo()
|
|
165
165
|
expect(result1).toBe(result2)
|
|
166
166
|
})
|
|
167
|
+
|
|
168
|
+
it('should work with constructor argument', async () => {
|
|
169
|
+
const schema = z.object({
|
|
170
|
+
foo: z.string(),
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const token = InjectionToken.create('Test', schema)
|
|
174
|
+
|
|
175
|
+
@Injectable({ token })
|
|
176
|
+
class Test {
|
|
177
|
+
constructor(public readonly arg: z.output<typeof schema>) {}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const value = await inject(token, { foo: 'bar' })
|
|
181
|
+
expect(value).toBeInstanceOf(Test)
|
|
182
|
+
// @ts-expect-error It's a test
|
|
183
|
+
expect(value.arg).toEqual({ foo: 'bar' })
|
|
184
|
+
})
|
|
167
185
|
})
|
|
@@ -2,7 +2,14 @@ import type { AnyZodObject } from 'zod'
|
|
|
2
2
|
|
|
3
3
|
import { NaviosException } from '@navios/common'
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
ClassType,
|
|
9
|
+
ClassTypeWithArgument,
|
|
10
|
+
ClassTypeWithInstance,
|
|
11
|
+
ClassTypeWithInstanceAndArgument,
|
|
12
|
+
} from '../injection-token.mjs'
|
|
6
13
|
import type { Factory, FactoryWithArgs } from '../interfaces/index.mjs'
|
|
7
14
|
import type { Registry } from '../registry.mjs'
|
|
8
15
|
|
|
@@ -10,6 +17,7 @@ import { InjectableScope, InjectableType } from '../enums/index.mjs'
|
|
|
10
17
|
import { InjectionToken } from '../injection-token.mjs'
|
|
11
18
|
import { globalRegistry } from '../registry.mjs'
|
|
12
19
|
import { resolveService } from '../resolve-service.mjs'
|
|
20
|
+
import { InjectableTokenMeta } from '../symbols/index.mjs'
|
|
13
21
|
|
|
14
22
|
export interface InjectableOptions {
|
|
15
23
|
scope?: InjectableScope
|
|
@@ -18,12 +26,33 @@ export interface InjectableOptions {
|
|
|
18
26
|
registry?: Registry
|
|
19
27
|
}
|
|
20
28
|
|
|
21
|
-
export const InjectableTokenMeta = Symbol.for('InjectableTokenMeta')
|
|
22
|
-
|
|
23
29
|
export function Injectable(): <T extends ClassType>(
|
|
24
30
|
target: T,
|
|
25
31
|
context: ClassDecoratorContext,
|
|
26
32
|
) => T & { [InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined> }
|
|
33
|
+
export function Injectable<R>(options: {
|
|
34
|
+
scope?: InjectableScope
|
|
35
|
+
type: InjectableType.Factory
|
|
36
|
+
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
37
|
+
target: T,
|
|
38
|
+
context: ClassDecoratorContext,
|
|
39
|
+
) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
|
|
40
|
+
export function Injectable<S extends AnyZodObject>(options: {
|
|
41
|
+
scope?: InjectableScope
|
|
42
|
+
type?: InjectableType.Class
|
|
43
|
+
token: InjectionToken<undefined, S>
|
|
44
|
+
}): <T extends ClassTypeWithArgument<z.output<S>>>(
|
|
45
|
+
target: T,
|
|
46
|
+
context: ClassDecoratorContext,
|
|
47
|
+
) => T & { [InjectableTokenMeta]: InjectionToken<T, S> }
|
|
48
|
+
export function Injectable<R, S extends AnyZodObject>(options: {
|
|
49
|
+
scope?: InjectableScope
|
|
50
|
+
type?: InjectableType.Class
|
|
51
|
+
token: InjectionToken<R, S>
|
|
52
|
+
}): <T extends ClassTypeWithInstanceAndArgument<R, z.output<S>>>(
|
|
53
|
+
target: T,
|
|
54
|
+
context: ClassDecoratorContext,
|
|
55
|
+
) => T & { [InjectableTokenMeta]: InjectionToken<R, S> }
|
|
27
56
|
export function Injectable<T extends ClassType>(options: {
|
|
28
57
|
scope?: InjectableScope
|
|
29
58
|
token: InjectionToken<T, undefined>
|
|
@@ -33,13 +62,7 @@ export function Injectable<T extends ClassType>(options: {
|
|
|
33
62
|
) => T & {
|
|
34
63
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>
|
|
35
64
|
}
|
|
36
|
-
|
|
37
|
-
scope?: InjectableScope
|
|
38
|
-
type: InjectableType.Factory
|
|
39
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
40
|
-
target: T,
|
|
41
|
-
context: ClassDecoratorContext,
|
|
42
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
|
|
65
|
+
|
|
43
66
|
export function Injectable<R, S extends AnyZodObject>(options: {
|
|
44
67
|
scope?: InjectableScope
|
|
45
68
|
type: InjectableType.Factory
|
|
@@ -78,7 +101,7 @@ export function Injectable({
|
|
|
78
101
|
if (type === InjectableType.Class) {
|
|
79
102
|
registry.set(
|
|
80
103
|
injectableToken,
|
|
81
|
-
async (ctx) => resolveService(ctx, target),
|
|
104
|
+
async (ctx, args: any) => resolveService(ctx, target, [args]),
|
|
82
105
|
scope,
|
|
83
106
|
)
|
|
84
107
|
} else if (type === InjectableType.Factory) {
|
package/src/index.mts
CHANGED
package/src/injection-token.mts
CHANGED
|
@@ -5,8 +5,10 @@ import { randomUUID } from 'crypto'
|
|
|
5
5
|
import { z, ZodOptional } from 'zod'
|
|
6
6
|
|
|
7
7
|
export type ClassType = new (...args: any[]) => any
|
|
8
|
+
export type ClassTypeWithArgument<Arg> = new (arg: Arg) => any
|
|
8
9
|
|
|
9
10
|
export type ClassTypeWithInstance<T> = new (...args: any[]) => T
|
|
11
|
+
export type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T
|
|
10
12
|
|
|
11
13
|
export class InjectionToken<
|
|
12
14
|
T,
|
package/src/injector.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Injectors } from './utils/index.mjs'
|
|
2
|
+
|
|
1
3
|
import { ServiceLocator } from './service-locator.mjs'
|
|
2
4
|
import { getInjectors } from './utils/index.mjs'
|
|
3
5
|
|
|
@@ -11,9 +13,15 @@ export function getGlobalServiceLocator(): ServiceLocator {
|
|
|
11
13
|
}
|
|
12
14
|
return globalServiceLocator
|
|
13
15
|
}
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const values = getInjectors({
|
|
17
|
+
baseLocator: globalServiceLocator,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const inject: Injectors['inject'] = values.inject
|
|
21
|
+
|
|
22
|
+
export const syncInject: Injectors['syncInject'] = values.syncInject
|
|
23
|
+
|
|
24
|
+
export const wrapSyncInit: Injectors['wrapSyncInit'] = values.wrapSyncInit
|
|
18
25
|
|
|
19
|
-
export
|
|
26
|
+
export const provideServiceLocator: Injectors['provideServiceLocator'] =
|
|
27
|
+
values.provideServiceLocator
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './injectable-token.mjs'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const InjectableTokenMeta = Symbol.for('InjectableTokenMeta')
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { AnyZodObject, z, ZodOptional } from 'zod'
|
|
2
2
|
|
|
3
|
-
import type {
|
|
4
|
-
import type { ServiceLocator } from '../service-locator.mjs'
|
|
5
|
-
|
|
6
|
-
import { InjectableTokenMeta } from '../decorators/index.mjs'
|
|
7
|
-
import {
|
|
3
|
+
import type {
|
|
8
4
|
BoundInjectionToken,
|
|
5
|
+
ClassType,
|
|
9
6
|
FactoryInjectionToken,
|
|
10
7
|
InjectionToken,
|
|
11
8
|
} from '../injection-token.mjs'
|
|
9
|
+
import type { ServiceLocator } from '../service-locator.mjs'
|
|
10
|
+
|
|
11
|
+
import { InjectableTokenMeta } from '../symbols/index.mjs'
|
|
12
12
|
|
|
13
13
|
export interface CreateInjectorsOptions {
|
|
14
14
|
baseLocator: ServiceLocator
|