@navios/di 0.1.4 → 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 +31 -7
- package/dist/_tsup-dts-rollup.d.ts +31 -7
- 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 +33 -9
- 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
|
}
|
|
@@ -176,13 +184,6 @@ declare function Injectable(): <T extends ClassType>(target: T, context: ClassDe
|
|
|
176
184
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
177
185
|
};
|
|
178
186
|
|
|
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
|
-
};
|
|
185
|
-
|
|
186
187
|
declare function Injectable<R>(options: {
|
|
187
188
|
scope?: InjectableScope;
|
|
188
189
|
type: InjectableType.Factory;
|
|
@@ -190,6 +191,29 @@ declare function Injectable<R>(options: {
|
|
|
190
191
|
[InjectableTokenMeta]: InjectionToken<R, undefined>;
|
|
191
192
|
};
|
|
192
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
|
+
|
|
193
217
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
194
218
|
scope?: InjectableScope;
|
|
195
219
|
type: InjectableType.Factory;
|
|
@@ -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
|
}
|
|
@@ -176,13 +184,6 @@ declare function Injectable(): <T extends ClassType>(target: T, context: ClassDe
|
|
|
176
184
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>;
|
|
177
185
|
};
|
|
178
186
|
|
|
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
|
-
};
|
|
185
|
-
|
|
186
187
|
declare function Injectable<R>(options: {
|
|
187
188
|
scope?: InjectableScope;
|
|
188
189
|
type: InjectableType.Factory;
|
|
@@ -190,6 +191,29 @@ declare function Injectable<R>(options: {
|
|
|
190
191
|
[InjectableTokenMeta]: InjectionToken<R, undefined>;
|
|
191
192
|
};
|
|
192
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
|
+
|
|
193
217
|
declare function Injectable<R, S extends AnyZodObject>(options: {
|
|
194
218
|
scope?: InjectableScope;
|
|
195
219
|
type: InjectableType.Factory;
|
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
|
|
|
@@ -23,6 +30,29 @@ export function Injectable(): <T extends ClassType>(
|
|
|
23
30
|
target: T,
|
|
24
31
|
context: ClassDecoratorContext,
|
|
25
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> }
|
|
26
56
|
export function Injectable<T extends ClassType>(options: {
|
|
27
57
|
scope?: InjectableScope
|
|
28
58
|
token: InjectionToken<T, undefined>
|
|
@@ -32,13 +62,7 @@ export function Injectable<T extends ClassType>(options: {
|
|
|
32
62
|
) => T & {
|
|
33
63
|
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>
|
|
34
64
|
}
|
|
35
|
-
|
|
36
|
-
scope?: InjectableScope
|
|
37
|
-
type: InjectableType.Factory
|
|
38
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
39
|
-
target: T,
|
|
40
|
-
context: ClassDecoratorContext,
|
|
41
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
|
|
65
|
+
|
|
42
66
|
export function Injectable<R, S extends AnyZodObject>(options: {
|
|
43
67
|
scope?: InjectableScope
|
|
44
68
|
type: InjectableType.Factory
|
|
@@ -77,7 +101,7 @@ export function Injectable({
|
|
|
77
101
|
if (type === InjectableType.Class) {
|
|
78
102
|
registry.set(
|
|
79
103
|
injectableToken,
|
|
80
|
-
async (ctx) => resolveService(ctx, target),
|
|
104
|
+
async (ctx, args: any) => resolveService(ctx, target, [args]),
|
|
81
105
|
scope,
|
|
82
106
|
)
|
|
83
107
|
} else if (type === InjectableType.Factory) {
|
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,
|