@navios/di 0.1.4 → 0.1.6
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 +27 -13
- package/dist/_tsup-dts-rollup.d.ts +27 -13
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -2
- package/dist/index.mjs +2 -1
- package/package.json +1 -1
- package/src/__tests__/injectable.spec.mts +19 -1
- package/src/decorators/injectable.decorator.mts +34 -19
- package/src/injection-token.mts +2 -0
- package/src/utils/get-injectable-token.mts +1 -1
|
@@ -24,10 +24,18 @@ declare type ClassType = new (...args: any[]) => any;
|
|
|
24
24
|
export { ClassType }
|
|
25
25
|
export { ClassType as ClassType_alias_1 }
|
|
26
26
|
|
|
27
|
+
declare type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
|
|
28
|
+
export { ClassTypeWithArgument }
|
|
29
|
+
export { ClassTypeWithArgument as ClassTypeWithArgument_alias_1 }
|
|
30
|
+
|
|
27
31
|
declare type ClassTypeWithInstance<T> = new (...args: any[]) => T;
|
|
28
32
|
export { ClassTypeWithInstance }
|
|
29
33
|
export { ClassTypeWithInstance as ClassTypeWithInstance_alias_1 }
|
|
30
34
|
|
|
35
|
+
declare type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
|
|
36
|
+
export { ClassTypeWithInstanceAndArgument }
|
|
37
|
+
export { ClassTypeWithInstanceAndArgument as ClassTypeWithInstanceAndArgument_alias_1 }
|
|
38
|
+
|
|
31
39
|
declare interface CreateInjectorsOptions {
|
|
32
40
|
baseLocator: ServiceLocator;
|
|
33
41
|
}
|
|
@@ -172,23 +180,29 @@ declare const inject: Injectors['inject'];
|
|
|
172
180
|
export { inject }
|
|
173
181
|
export { inject as inject_alias_1 }
|
|
174
182
|
|
|
175
|
-
declare function Injectable(): <T extends ClassType>(target: T, context: ClassDecoratorContext) => T
|
|
176
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
declare function Injectable<T extends ClassType>(options: {
|
|
180
|
-
scope?: InjectableScope;
|
|
181
|
-
token: InjectionToken<T, undefined>;
|
|
182
|
-
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
183
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
184
|
-
};
|
|
183
|
+
declare function Injectable(): <T extends ClassType>(target: T, context: ClassDecoratorContext) => T;
|
|
185
184
|
|
|
186
185
|
declare function Injectable<R>(options: {
|
|
187
186
|
scope?: InjectableScope;
|
|
188
187
|
type: InjectableType.Factory;
|
|
189
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(target: T, context: ClassDecoratorContext) => T
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
}): <T extends ClassTypeWithInstance<Factory<R>>>(target: T, context: ClassDecoratorContext) => T;
|
|
189
|
+
|
|
190
|
+
declare function Injectable<S extends AnyZodObject>(options: {
|
|
191
|
+
scope?: InjectableScope;
|
|
192
|
+
type?: InjectableType.Class;
|
|
193
|
+
token: InjectionToken<undefined, S>;
|
|
194
|
+
}): <T extends ClassTypeWithArgument<z.output<S>>>(target: T, context: ClassDecoratorContext) => T;
|
|
195
|
+
|
|
196
|
+
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
197
|
+
scope?: InjectableScope;
|
|
198
|
+
type?: InjectableType.Class;
|
|
199
|
+
token: InjectionToken<R, S>;
|
|
200
|
+
}): <T extends ClassTypeWithInstanceAndArgument<R, z.output<S>>>(target: T, context: ClassDecoratorContext) => T;
|
|
201
|
+
|
|
202
|
+
declare function Injectable<T extends ClassType>(options: {
|
|
203
|
+
scope?: InjectableScope;
|
|
204
|
+
token: InjectionToken<T, undefined>;
|
|
205
|
+
}): (target: T, context: ClassDecoratorContext) => T;
|
|
192
206
|
|
|
193
207
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
194
208
|
scope?: InjectableScope;
|
|
@@ -24,10 +24,18 @@ declare type ClassType = new (...args: any[]) => any;
|
|
|
24
24
|
export { ClassType }
|
|
25
25
|
export { ClassType as ClassType_alias_1 }
|
|
26
26
|
|
|
27
|
+
declare type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
|
|
28
|
+
export { ClassTypeWithArgument }
|
|
29
|
+
export { ClassTypeWithArgument as ClassTypeWithArgument_alias_1 }
|
|
30
|
+
|
|
27
31
|
declare type ClassTypeWithInstance<T> = new (...args: any[]) => T;
|
|
28
32
|
export { ClassTypeWithInstance }
|
|
29
33
|
export { ClassTypeWithInstance as ClassTypeWithInstance_alias_1 }
|
|
30
34
|
|
|
35
|
+
declare type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
|
|
36
|
+
export { ClassTypeWithInstanceAndArgument }
|
|
37
|
+
export { ClassTypeWithInstanceAndArgument as ClassTypeWithInstanceAndArgument_alias_1 }
|
|
38
|
+
|
|
31
39
|
declare interface CreateInjectorsOptions {
|
|
32
40
|
baseLocator: ServiceLocator;
|
|
33
41
|
}
|
|
@@ -172,23 +180,29 @@ declare const inject: Injectors['inject'];
|
|
|
172
180
|
export { inject }
|
|
173
181
|
export { inject as inject_alias_1 }
|
|
174
182
|
|
|
175
|
-
declare function Injectable(): <T extends ClassType>(target: T, context: ClassDecoratorContext) => T
|
|
176
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
declare function Injectable<T extends ClassType>(options: {
|
|
180
|
-
scope?: InjectableScope;
|
|
181
|
-
token: InjectionToken<T, undefined>;
|
|
182
|
-
}): (target: T, context: ClassDecoratorContext) => T & {
|
|
183
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
184
|
-
};
|
|
183
|
+
declare function Injectable(): <T extends ClassType>(target: T, context: ClassDecoratorContext) => T;
|
|
185
184
|
|
|
186
185
|
declare function Injectable<R>(options: {
|
|
187
186
|
scope?: InjectableScope;
|
|
188
187
|
type: InjectableType.Factory;
|
|
189
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(target: T, context: ClassDecoratorContext) => T
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
}): <T extends ClassTypeWithInstance<Factory<R>>>(target: T, context: ClassDecoratorContext) => T;
|
|
189
|
+
|
|
190
|
+
declare function Injectable<S extends AnyZodObject>(options: {
|
|
191
|
+
scope?: InjectableScope;
|
|
192
|
+
type?: InjectableType.Class;
|
|
193
|
+
token: InjectionToken<undefined, S>;
|
|
194
|
+
}): <T extends ClassTypeWithArgument<z.output<S>>>(target: T, context: ClassDecoratorContext) => T;
|
|
195
|
+
|
|
196
|
+
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
197
|
+
scope?: InjectableScope;
|
|
198
|
+
type?: InjectableType.Class;
|
|
199
|
+
token: InjectionToken<R, S>;
|
|
200
|
+
}): <T extends ClassTypeWithInstanceAndArgument<R, z.output<S>>>(target: T, context: ClassDecoratorContext) => T;
|
|
201
|
+
|
|
202
|
+
declare function Injectable<T extends ClassType>(options: {
|
|
203
|
+
scope?: InjectableScope;
|
|
204
|
+
token: InjectionToken<T, undefined>;
|
|
205
|
+
}): (target: T, context: ClassDecoratorContext) => T;
|
|
192
206
|
|
|
193
207
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
194
208
|
scope?: InjectableScope;
|
package/dist/index.d.mts
CHANGED
|
@@ -24,7 +24,9 @@ export { EventEmitterInterface_alias_1 as EventEmitterInterface } from './_tsup-
|
|
|
24
24
|
export { EventEmitter_alias_1 as EventEmitter } from './_tsup-dts-rollup.mjs';
|
|
25
25
|
export { FactoryContext_alias_1 as FactoryContext } from './_tsup-dts-rollup.mjs';
|
|
26
26
|
export { ClassType } from './_tsup-dts-rollup.mjs';
|
|
27
|
+
export { ClassTypeWithArgument } from './_tsup-dts-rollup.mjs';
|
|
27
28
|
export { ClassTypeWithInstance } from './_tsup-dts-rollup.mjs';
|
|
29
|
+
export { ClassTypeWithInstanceAndArgument } from './_tsup-dts-rollup.mjs';
|
|
28
30
|
export { InjectionToken } from './_tsup-dts-rollup.mjs';
|
|
29
31
|
export { BoundInjectionToken } from './_tsup-dts-rollup.mjs';
|
|
30
32
|
export { FactoryInjectionToken } from './_tsup-dts-rollup.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,9 @@ export { EventEmitterInterface_alias_1 as EventEmitterInterface } from './_tsup-
|
|
|
24
24
|
export { EventEmitter_alias_1 as EventEmitter } from './_tsup-dts-rollup.js';
|
|
25
25
|
export { FactoryContext_alias_1 as FactoryContext } from './_tsup-dts-rollup.js';
|
|
26
26
|
export { ClassType } from './_tsup-dts-rollup.js';
|
|
27
|
+
export { ClassTypeWithArgument } from './_tsup-dts-rollup.js';
|
|
27
28
|
export { ClassTypeWithInstance } from './_tsup-dts-rollup.js';
|
|
29
|
+
export { ClassTypeWithInstanceAndArgument } from './_tsup-dts-rollup.js';
|
|
28
30
|
export { InjectionToken } from './_tsup-dts-rollup.js';
|
|
29
31
|
export { BoundInjectionToken } from './_tsup-dts-rollup.js';
|
|
30
32
|
export { FactoryInjectionToken } 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) => {
|
|
@@ -352,7 +353,7 @@ function Injectable({
|
|
|
352
353
|
if (type === "Class" /* Class */) {
|
|
353
354
|
registry.set(
|
|
354
355
|
injectableToken,
|
|
355
|
-
async (ctx) => resolveService(ctx, target),
|
|
356
|
+
async (ctx, args) => resolveService(ctx, target, [args]),
|
|
356
357
|
scope
|
|
357
358
|
);
|
|
358
359
|
} else if (type === "Factory" /* Factory */) {
|
|
@@ -445,7 +446,7 @@ var UnknownError = class extends Error {
|
|
|
445
446
|
};
|
|
446
447
|
|
|
447
448
|
// packages/di/src/interfaces/factory.interface.mts
|
|
448
|
-
var
|
|
449
|
+
var import_zod4 = require("zod");
|
|
449
450
|
|
|
450
451
|
// packages/di/src/event-emitter.mts
|
|
451
452
|
var EventEmitter = class {
|
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) => {
|
|
@@ -294,7 +295,7 @@ function Injectable({
|
|
|
294
295
|
if (type === "Class" /* Class */) {
|
|
295
296
|
registry.set(
|
|
296
297
|
injectableToken,
|
|
297
|
-
async (ctx) => resolveService(ctx, target),
|
|
298
|
+
async (ctx, args) => resolveService(ctx, target, [args]),
|
|
298
299
|
scope
|
|
299
300
|
);
|
|
300
301
|
} else if (type === "Factory" /* Factory */) {
|
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
|
|
|
@@ -22,23 +29,35 @@ export interface InjectableOptions {
|
|
|
22
29
|
export function Injectable(): <T extends ClassType>(
|
|
23
30
|
target: T,
|
|
24
31
|
context: ClassDecoratorContext,
|
|
25
|
-
) => T
|
|
26
|
-
export function Injectable<T extends ClassType>(options: {
|
|
27
|
-
scope?: InjectableScope
|
|
28
|
-
token: InjectionToken<T, undefined>
|
|
29
|
-
}): (
|
|
30
|
-
target: T,
|
|
31
|
-
context: ClassDecoratorContext,
|
|
32
|
-
) => T & {
|
|
33
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>
|
|
34
|
-
}
|
|
32
|
+
) => T
|
|
35
33
|
export function Injectable<R>(options: {
|
|
36
34
|
scope?: InjectableScope
|
|
37
35
|
type: InjectableType.Factory
|
|
38
36
|
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
39
37
|
target: T,
|
|
40
38
|
context: ClassDecoratorContext,
|
|
41
|
-
) => T
|
|
39
|
+
) => T
|
|
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
|
|
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
|
|
56
|
+
export function Injectable<T extends ClassType>(options: {
|
|
57
|
+
scope?: InjectableScope
|
|
58
|
+
token: InjectionToken<T, undefined>
|
|
59
|
+
}): (target: T, context: ClassDecoratorContext) => T
|
|
60
|
+
|
|
42
61
|
export function Injectable<R, S extends AnyZodObject>(options: {
|
|
43
62
|
scope?: InjectableScope
|
|
44
63
|
type: InjectableType.Factory
|
|
@@ -64,9 +83,7 @@ export function Injectable({
|
|
|
64
83
|
return <T extends ClassType>(
|
|
65
84
|
target: T,
|
|
66
85
|
context: ClassDecoratorContext,
|
|
67
|
-
): T
|
|
68
|
-
[InjectableTokenMeta]: InjectionToken<any, any>
|
|
69
|
-
} => {
|
|
86
|
+
): T => {
|
|
70
87
|
if (context.kind !== 'class') {
|
|
71
88
|
throw new Error(
|
|
72
89
|
'[ServiceLocator] @Injectable decorator can only be used on classes.',
|
|
@@ -77,7 +94,7 @@ export function Injectable({
|
|
|
77
94
|
if (type === InjectableType.Class) {
|
|
78
95
|
registry.set(
|
|
79
96
|
injectableToken,
|
|
80
|
-
async (ctx) => resolveService(ctx, target),
|
|
97
|
+
async (ctx, args: any) => resolveService(ctx, target, [args]),
|
|
81
98
|
scope,
|
|
82
99
|
)
|
|
83
100
|
} else if (type === InjectableType.Factory) {
|
|
@@ -99,8 +116,6 @@ export function Injectable({
|
|
|
99
116
|
// @ts-expect-error
|
|
100
117
|
target[InjectableTokenMeta] = injectableToken
|
|
101
118
|
|
|
102
|
-
return target
|
|
103
|
-
[InjectableTokenMeta]: InjectionToken<any, any>
|
|
104
|
-
}
|
|
119
|
+
return target
|
|
105
120
|
}
|
|
106
121
|
}
|
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,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ClassType, InjectionToken } from '../injection-token.mjs'
|
|
2
2
|
|
|
3
|
-
import { InjectableTokenMeta } from '../symbols/
|
|
3
|
+
import { InjectableTokenMeta } from '../symbols/index.mjs'
|
|
4
4
|
|
|
5
5
|
export function getInjectableToken<R>(
|
|
6
6
|
target: ClassType,
|