@navios/core 0.1.15 → 0.2.0
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 +98 -422
- package/dist/_tsup-dts-rollup.d.ts +98 -422
- package/dist/index.d.mts +54 -44
- package/dist/index.d.ts +54 -44
- package/dist/index.js +111 -1023
- package/dist/index.mjs +113 -982
- package/package.json +3 -2
- package/src/adapters/endpoint-adapter.service.mts +3 -6
- package/src/adapters/handler-adapter.interface.mts +1 -1
- package/src/adapters/multipart-adapter.service.mts +2 -1
- package/src/adapters/stream-adapter.service.mts +3 -7
- package/src/attribute.factory.mts +1 -1
- package/src/config/config.provider.mts +8 -10
- package/src/decorators/controller.decorator.mts +3 -6
- package/src/decorators/module.decorator.mts +3 -6
- package/src/decorators/use-guards.decorator.mts +3 -2
- package/src/index.mts +1 -1
- package/src/logger/console-logger.service.mts +3 -2
- package/src/logger/logger.factory.mts +4 -5
- package/src/logger/logger.service.mts +2 -1
- package/src/metadata/controller.metadata.mts +3 -2
- package/src/metadata/handler.metadata.mts +1 -4
- package/src/metadata/index.mts +0 -1
- package/src/metadata/module.metadata.mts +3 -2
- package/src/navios.application.mts +9 -8
- package/src/navios.factory.mts +4 -2
- package/src/services/controller-adapter.service.mts +13 -12
- package/src/services/guard-runner.service.mts +4 -6
- package/src/services/module-loader.service.mts +4 -2
- package/src/tokens/application.token.mts +1 -1
- package/src/tokens/execution-context.token.mts +2 -1
- package/src/tokens/reply.token.mts +1 -1
- package/src/tokens/request.token.mts +1 -1
- package/src/metadata/injectable.metadata.mts +0 -11
- package/src/service-locator/__tests__/injectable.spec.mts +0 -171
- package/src/service-locator/__tests__/injection-token.spec.mts +0 -129
- package/src/service-locator/decorators/get-injectable-token.mts +0 -19
- package/src/service-locator/decorators/index.mts +0 -2
- package/src/service-locator/decorators/injectable.decorator.mts +0 -113
- package/src/service-locator/enums/index.mts +0 -1
- package/src/service-locator/enums/injectable-scope.enum.mts +0 -10
- package/src/service-locator/errors/errors.enum.mts +0 -8
- package/src/service-locator/errors/factory-not-found.mts +0 -8
- package/src/service-locator/errors/factory-token-not-resolved.mts +0 -10
- package/src/service-locator/errors/index.mts +0 -7
- package/src/service-locator/errors/instance-destroying.mts +0 -8
- package/src/service-locator/errors/instance-expired.mts +0 -8
- package/src/service-locator/errors/instance-not-found.mts +0 -8
- package/src/service-locator/errors/unknown-error.mts +0 -15
- package/src/service-locator/event-emitter.mts +0 -107
- package/src/service-locator/index.mts +0 -15
- package/src/service-locator/inject.mts +0 -42
- package/src/service-locator/injection-token.mts +0 -92
- package/src/service-locator/injector.mts +0 -18
- package/src/service-locator/interfaces/factory.interface.mts +0 -11
- package/src/service-locator/override.mts +0 -22
- package/src/service-locator/proxy-service-locator.mts +0 -99
- package/src/service-locator/resolve-service.mts +0 -46
- package/src/service-locator/service-locator-abstract-factory-context.mts +0 -23
- package/src/service-locator/service-locator-event-bus.mts +0 -96
- package/src/service-locator/service-locator-instance-holder.mts +0 -63
- package/src/service-locator/service-locator-manager.mts +0 -89
- package/src/service-locator/service-locator.mts +0 -535
- package/src/service-locator/sync-injector.mts +0 -66
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { z } from 'zod'
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
Factory,
|
|
6
|
-
FactoryWithArgs,
|
|
7
|
-
} from '../interfaces/factory.interface.mjs'
|
|
8
|
-
|
|
9
|
-
import { Injectable, InjectableType } from '../decorators/index.mjs'
|
|
10
|
-
import { inject } from '../inject.mjs'
|
|
11
|
-
import { InjectionToken } from '../injection-token.mjs'
|
|
12
|
-
|
|
13
|
-
describe('InjectToken', () => {
|
|
14
|
-
it('should work with class', async () => {
|
|
15
|
-
const token = InjectionToken.create('Test')
|
|
16
|
-
@Injectable({
|
|
17
|
-
token,
|
|
18
|
-
})
|
|
19
|
-
class Test {}
|
|
20
|
-
|
|
21
|
-
const value = await inject(Test)
|
|
22
|
-
expect(value).toBeInstanceOf(Test)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should work with class and schema', async () => {
|
|
26
|
-
const schema = z.object({
|
|
27
|
-
test: z.string(),
|
|
28
|
-
})
|
|
29
|
-
const token = InjectionToken.create('Test', schema)
|
|
30
|
-
|
|
31
|
-
@Injectable({
|
|
32
|
-
token,
|
|
33
|
-
})
|
|
34
|
-
class Test {
|
|
35
|
-
makeFoo() {
|
|
36
|
-
return 'foo'
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const value = await inject(token, {
|
|
40
|
-
test: 'test',
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
expect(value).toBeInstanceOf(Test)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('should work with factory', async () => {
|
|
47
|
-
const token = InjectionToken.create<string>('Test')
|
|
48
|
-
@Injectable({
|
|
49
|
-
token,
|
|
50
|
-
type: InjectableType.Factory,
|
|
51
|
-
})
|
|
52
|
-
class Test implements Factory<string> {
|
|
53
|
-
async create() {
|
|
54
|
-
return 'foo'
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const value = await inject(Test)
|
|
59
|
-
expect(value).toBe('foo')
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('should work with factory and schema', async () => {
|
|
63
|
-
const schema = z.object({
|
|
64
|
-
test: z.string(),
|
|
65
|
-
})
|
|
66
|
-
const token = InjectionToken.create<string, typeof schema>('Test', schema)
|
|
67
|
-
|
|
68
|
-
@Injectable({
|
|
69
|
-
token,
|
|
70
|
-
type: InjectableType.Factory,
|
|
71
|
-
})
|
|
72
|
-
class Test implements FactoryWithArgs<string, typeof schema> {
|
|
73
|
-
async create(ctx: any, args: { test: string }) {
|
|
74
|
-
return args.test
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const value = await inject(token, {
|
|
78
|
-
test: 'test',
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
expect(value).toBe('test')
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it('should work with bound token', async () => {
|
|
85
|
-
const schema = z.object({
|
|
86
|
-
test: z.string(),
|
|
87
|
-
})
|
|
88
|
-
const token = InjectionToken.create('Test', schema)
|
|
89
|
-
const boundToken = InjectionToken.bound(token, {
|
|
90
|
-
test: 'test',
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
@Injectable({
|
|
94
|
-
token,
|
|
95
|
-
})
|
|
96
|
-
class Test {
|
|
97
|
-
makeFoo() {
|
|
98
|
-
return 'foo'
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const value = await inject(boundToken)
|
|
102
|
-
|
|
103
|
-
expect(value).toBeInstanceOf(Test)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
it('should work with factory token', async () => {
|
|
107
|
-
const schema = z.object({
|
|
108
|
-
test: z.string(),
|
|
109
|
-
})
|
|
110
|
-
const token = InjectionToken.create('Test', schema)
|
|
111
|
-
const factoryInjectionToken = InjectionToken.factory(token, () =>
|
|
112
|
-
Promise.resolve({
|
|
113
|
-
test: 'test',
|
|
114
|
-
}),
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
@Injectable({
|
|
118
|
-
token,
|
|
119
|
-
})
|
|
120
|
-
class Test {
|
|
121
|
-
makeFoo() {
|
|
122
|
-
return 'foo'
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
const value = await inject(factoryInjectionToken)
|
|
126
|
-
|
|
127
|
-
expect(value).toBeInstanceOf(Test)
|
|
128
|
-
})
|
|
129
|
-
})
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { ClassType, InjectionToken } from '../injection-token.mjs'
|
|
2
|
-
|
|
3
|
-
import { InjectableTokenMeta } from './injectable.decorator.mjs'
|
|
4
|
-
|
|
5
|
-
export function getInjectableToken<R>(
|
|
6
|
-
target: ClassType,
|
|
7
|
-
): R extends { create(...args: any[]): infer V }
|
|
8
|
-
? InjectionToken<V>
|
|
9
|
-
: InjectionToken<R> {
|
|
10
|
-
// @ts-expect-error We inject the token into the class itself
|
|
11
|
-
const token = target[InjectableTokenMeta] as InjectionToken<any, any>
|
|
12
|
-
if (!token) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
`[ServiceLocator] Class ${target.name} is not decorated with @Injectable.`,
|
|
15
|
-
)
|
|
16
|
-
}
|
|
17
|
-
// @ts-expect-error We detect factory or class
|
|
18
|
-
return token
|
|
19
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { AnyZodObject } from 'zod'
|
|
2
|
-
|
|
3
|
-
import { NaviosException } from '@navios/common'
|
|
4
|
-
|
|
5
|
-
import type { ClassType, ClassTypeWithInstance } from '../injection-token.mjs'
|
|
6
|
-
import type {
|
|
7
|
-
Factory,
|
|
8
|
-
FactoryWithArgs,
|
|
9
|
-
} from '../interfaces/factory.interface.mjs'
|
|
10
|
-
|
|
11
|
-
import { InjectableScope } from '../enums/index.mjs'
|
|
12
|
-
import { InjectionToken } from '../injection-token.mjs'
|
|
13
|
-
import { getServiceLocator } from '../injector.mjs'
|
|
14
|
-
import { resolveService } from '../resolve-service.mjs'
|
|
15
|
-
|
|
16
|
-
export enum InjectableType {
|
|
17
|
-
Class = 'Class',
|
|
18
|
-
Factory = 'Factory',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface InjectableOptions {
|
|
22
|
-
scope?: InjectableScope
|
|
23
|
-
type?: InjectableType
|
|
24
|
-
token?: InjectionToken<any, any>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const InjectableTokenMeta = Symbol.for('InjectableTokenMeta')
|
|
28
|
-
|
|
29
|
-
export function Injectable(): <T extends ClassType>(
|
|
30
|
-
target: T,
|
|
31
|
-
context: ClassDecoratorContext,
|
|
32
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined> }
|
|
33
|
-
export function Injectable<T extends ClassType>(options: {
|
|
34
|
-
scope?: InjectableScope
|
|
35
|
-
token: InjectionToken<T, undefined>
|
|
36
|
-
}): (
|
|
37
|
-
target: T,
|
|
38
|
-
context: ClassDecoratorContext,
|
|
39
|
-
) => T & {
|
|
40
|
-
[InjectableTokenMeta]: InjectionToken<InstanceType<T>, undefined>
|
|
41
|
-
}
|
|
42
|
-
export function Injectable<R>(options: {
|
|
43
|
-
scope?: InjectableScope
|
|
44
|
-
type: InjectableType.Factory
|
|
45
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
46
|
-
target: T,
|
|
47
|
-
context: ClassDecoratorContext,
|
|
48
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
|
|
49
|
-
export function Injectable<R, S extends AnyZodObject>(options: {
|
|
50
|
-
scope?: InjectableScope
|
|
51
|
-
type: InjectableType.Factory
|
|
52
|
-
token: InjectionToken<R, S>
|
|
53
|
-
}): <T extends ClassTypeWithInstance<FactoryWithArgs<R, S>>>(
|
|
54
|
-
target: T,
|
|
55
|
-
context: ClassDecoratorContext,
|
|
56
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<R, S> }
|
|
57
|
-
export function Injectable<R>(options: {
|
|
58
|
-
scope?: InjectableScope
|
|
59
|
-
type: InjectableType.Factory
|
|
60
|
-
token: InjectionToken<R, undefined>
|
|
61
|
-
}): <T extends ClassTypeWithInstance<Factory<R>>>(
|
|
62
|
-
target: T,
|
|
63
|
-
context: ClassDecoratorContext,
|
|
64
|
-
) => T & { [InjectableTokenMeta]: InjectionToken<R, undefined> }
|
|
65
|
-
export function Injectable({
|
|
66
|
-
scope = InjectableScope.Singleton,
|
|
67
|
-
type = InjectableType.Class,
|
|
68
|
-
token,
|
|
69
|
-
}: InjectableOptions = {}) {
|
|
70
|
-
return <T extends ClassType>(
|
|
71
|
-
target: T,
|
|
72
|
-
context: ClassDecoratorContext,
|
|
73
|
-
): T & {
|
|
74
|
-
[InjectableTokenMeta]: InjectionToken<any, any>
|
|
75
|
-
} => {
|
|
76
|
-
if (context.kind !== 'class') {
|
|
77
|
-
throw new Error(
|
|
78
|
-
'[ServiceLocator] @Injectable decorator can only be used on classes.',
|
|
79
|
-
)
|
|
80
|
-
}
|
|
81
|
-
let injectableToken: InjectionToken<any, any> =
|
|
82
|
-
token ?? InjectionToken.create(target)
|
|
83
|
-
const locator = getServiceLocator()
|
|
84
|
-
if (type === InjectableType.Class) {
|
|
85
|
-
locator.registerAbstractFactory(
|
|
86
|
-
injectableToken,
|
|
87
|
-
async (ctx) => resolveService(ctx, target),
|
|
88
|
-
scope,
|
|
89
|
-
)
|
|
90
|
-
} else if (type === InjectableType.Factory) {
|
|
91
|
-
locator.registerAbstractFactory(
|
|
92
|
-
injectableToken,
|
|
93
|
-
async (ctx, args: any) => {
|
|
94
|
-
const builder = await resolveService(ctx, target)
|
|
95
|
-
if (typeof builder.create !== 'function') {
|
|
96
|
-
throw new NaviosException(
|
|
97
|
-
`[ServiceLocator] Factory ${target.name} does not implement the create method.`,
|
|
98
|
-
)
|
|
99
|
-
}
|
|
100
|
-
return builder.create(ctx, args)
|
|
101
|
-
},
|
|
102
|
-
scope,
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// @ts-expect-error
|
|
107
|
-
target[InjectableTokenMeta] = injectableToken
|
|
108
|
-
|
|
109
|
-
return target as T & {
|
|
110
|
-
[InjectableTokenMeta]: InjectionToken<any, any>
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './injectable-scope.enum.mjs'
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export enum ErrorsEnum {
|
|
2
|
-
InstanceExpired = 'InstanceExpired',
|
|
3
|
-
InstanceNotFound = 'InstanceNotFound',
|
|
4
|
-
InstanceDestroying = 'InstanceDestroying',
|
|
5
|
-
UnknownError = 'UnknownError',
|
|
6
|
-
FactoryNotFound = 'FactoryNotFound',
|
|
7
|
-
FactoryTokenNotResolved = 'FactoryTokenNotResolved',
|
|
8
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { ClassType } from '../injection-token.mjs'
|
|
2
|
-
|
|
3
|
-
import { ErrorsEnum } from './errors.enum.mjs'
|
|
4
|
-
|
|
5
|
-
export class FactoryTokenNotResolved extends Error {
|
|
6
|
-
code = ErrorsEnum.FactoryTokenNotResolved
|
|
7
|
-
constructor(name: string | symbol | ClassType) {
|
|
8
|
-
super(`Factory token not resolved: ${name.toString()}`)
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from './errors.enum.mjs'
|
|
2
|
-
export * from './factory-not-found.mjs'
|
|
3
|
-
export * from './factory-token-not-resolved.mjs'
|
|
4
|
-
export * from './instance-destroying.mjs'
|
|
5
|
-
export * from './instance-expired.mjs'
|
|
6
|
-
export * from './instance-not-found.mjs'
|
|
7
|
-
export * from './unknown-error.mjs'
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ErrorsEnum } from './errors.enum.mjs'
|
|
2
|
-
|
|
3
|
-
export class UnknownError extends Error {
|
|
4
|
-
code = ErrorsEnum.UnknownError
|
|
5
|
-
parent?: Error
|
|
6
|
-
|
|
7
|
-
constructor(message: string | Error) {
|
|
8
|
-
if (message instanceof Error) {
|
|
9
|
-
super(message.message)
|
|
10
|
-
this.parent = message
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
super(message)
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
-
export type EventsConfig = {
|
|
6
|
-
[event: string]: any[]
|
|
7
|
-
}
|
|
8
|
-
export type EventsNames<Events extends EventsConfig> = Exclude<keyof Events, symbol | number>
|
|
9
|
-
export type EventsArgs<
|
|
10
|
-
Events extends EventsConfig,
|
|
11
|
-
Name extends EventsNames<Events>,
|
|
12
|
-
> = Events[Name] extends any[] ? Events[Name] : []
|
|
13
|
-
|
|
14
|
-
export type ChannelEmitter<
|
|
15
|
-
Events extends EventsConfig,
|
|
16
|
-
Ns extends string,
|
|
17
|
-
E extends EventsNames<Events>,
|
|
18
|
-
> = {
|
|
19
|
-
emit<Args extends EventsArgs<Events, E>>(ns: Ns, event: E, ...args: Args): Promise<any>
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface EventEmitterInterface<Events extends EventsConfig> {
|
|
23
|
-
on<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
24
|
-
event: E,
|
|
25
|
-
listener: (...args: Args) => void,
|
|
26
|
-
): () => void
|
|
27
|
-
emit<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
28
|
-
event: E,
|
|
29
|
-
...args: Args
|
|
30
|
-
): void
|
|
31
|
-
addChannel<
|
|
32
|
-
E extends EventsNames<Events>,
|
|
33
|
-
Ns extends string,
|
|
34
|
-
Emmiter extends ChannelEmitter<Events, Ns, E>,
|
|
35
|
-
>(
|
|
36
|
-
ns: Ns,
|
|
37
|
-
event: E,
|
|
38
|
-
target: Emmiter,
|
|
39
|
-
): () => void
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class EventEmitter<Events extends EventsConfig = {}>
|
|
43
|
-
implements EventEmitterInterface<Events>
|
|
44
|
-
{
|
|
45
|
-
private listeners: Map<EventsNames<Events>, Set<Function>> = new Map()
|
|
46
|
-
|
|
47
|
-
on<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
48
|
-
event: E,
|
|
49
|
-
listener: (...args: Args) => void,
|
|
50
|
-
) {
|
|
51
|
-
if (!this.listeners.has(event)) {
|
|
52
|
-
this.listeners.set(event, new Set())
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
this.listeners.get(event)!.add(listener)
|
|
56
|
-
|
|
57
|
-
return () => {
|
|
58
|
-
this.off(event, listener)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
off<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
63
|
-
event: E,
|
|
64
|
-
listener: (...args: Args) => void,
|
|
65
|
-
) {
|
|
66
|
-
if (!this.listeners.has(event)) {
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
this.listeners.get(event)!.delete(listener)
|
|
71
|
-
if (this.listeners.get(event)!.size === 0) {
|
|
72
|
-
this.listeners.delete(event)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
once<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
77
|
-
event: E,
|
|
78
|
-
listener: (...args: Args) => void,
|
|
79
|
-
) {
|
|
80
|
-
const off = this.on(event, (...args) => {
|
|
81
|
-
off()
|
|
82
|
-
// @ts-expect-error - This is a valid call
|
|
83
|
-
listener(...args)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
return off
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async emit<E extends EventsNames<Events>, Args extends EventsArgs<Events, E>>(
|
|
90
|
-
event: E,
|
|
91
|
-
...args: Args
|
|
92
|
-
): Promise<any> {
|
|
93
|
-
if (!this.listeners.has(event)) {
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return Promise.all(Array.from(this.listeners.get(event)!).map(listener => listener(...args)))
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
addChannel<
|
|
101
|
-
E extends EventsNames<Events>,
|
|
102
|
-
Ns extends string,
|
|
103
|
-
Emitter extends ChannelEmitter<Events, Ns, E>,
|
|
104
|
-
>(ns: Ns, event: E, target: Emitter) {
|
|
105
|
-
return this.on(event, (...args) => target.emit(ns, event, ...args))
|
|
106
|
-
}
|
|
107
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export * from './decorators/index.mjs'
|
|
2
|
-
export * from './enums/index.mjs'
|
|
3
|
-
export * from './errors/index.mjs'
|
|
4
|
-
export * from './event-emitter.mjs'
|
|
5
|
-
export * from './inject.mjs'
|
|
6
|
-
export * from './injection-token.mjs'
|
|
7
|
-
export * from './injector.mjs'
|
|
8
|
-
export * from './override.mjs'
|
|
9
|
-
export * from './service-locator.mjs'
|
|
10
|
-
export * from './service-locator-abstract-factory-context.mjs'
|
|
11
|
-
export * from './service-locator-event-bus.mjs'
|
|
12
|
-
export * from './service-locator-instance-holder.mjs'
|
|
13
|
-
export * from './service-locator-manager.mjs'
|
|
14
|
-
export * from './sync-injector.mjs'
|
|
15
|
-
export * from './resolve-service.mjs'
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { AnyZodObject, z, ZodOptional } from 'zod'
|
|
2
|
-
|
|
3
|
-
import type { ClassType } from './injection-token.mjs'
|
|
4
|
-
|
|
5
|
-
import { getInjectableToken } from './decorators/index.mjs'
|
|
6
|
-
import { InjectionToken } from './injection-token.mjs'
|
|
7
|
-
import { getServiceLocator } from './injector.mjs'
|
|
8
|
-
|
|
9
|
-
export function inject<T extends ClassType>(token: T): Promise<InstanceType<T>>
|
|
10
|
-
// Not supported by TypeScript yet
|
|
11
|
-
// export function inject<
|
|
12
|
-
// R,
|
|
13
|
-
// T extends ClassType & {
|
|
14
|
-
// [InjectableTokenMeta]: InjectionToken<R, undefined>
|
|
15
|
-
// },
|
|
16
|
-
// >(token: T): Promise<R>
|
|
17
|
-
// export function inject<
|
|
18
|
-
// R,
|
|
19
|
-
// S extends AnyZodObject,
|
|
20
|
-
// T extends ClassType & {
|
|
21
|
-
// [InjectableTokenMeta]: InjectionToken<R, S>
|
|
22
|
-
// },
|
|
23
|
-
// >(token: T, args: z.input<S>): Promise<R>
|
|
24
|
-
export function inject<T, S extends AnyZodObject>(
|
|
25
|
-
token: InjectionToken<T, S>,
|
|
26
|
-
args: z.input<S>,
|
|
27
|
-
): Promise<T>
|
|
28
|
-
export function inject<T, S extends ZodOptional<AnyZodObject>>(
|
|
29
|
-
token: InjectionToken<T, S>,
|
|
30
|
-
args?: z.input<S>,
|
|
31
|
-
): Promise<T>
|
|
32
|
-
|
|
33
|
-
export function inject<T>(token: InjectionToken<T, undefined>): Promise<T>
|
|
34
|
-
export function inject(token: InjectionToken<any>, args?: unknown) {
|
|
35
|
-
let realToken = token
|
|
36
|
-
if (!(token instanceof InjectionToken)) {
|
|
37
|
-
realToken = getInjectableToken(token)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// @ts-expect-error We chek the type in overload
|
|
41
|
-
return getServiceLocator().getOrThrowInstance(realToken, args)
|
|
42
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import type { AnyZodObject } from 'zod'
|
|
2
|
-
|
|
3
|
-
import { randomUUID } from 'crypto'
|
|
4
|
-
|
|
5
|
-
import { z, ZodOptional } from 'zod'
|
|
6
|
-
|
|
7
|
-
export type ClassType = new (...args: any[]) => any
|
|
8
|
-
|
|
9
|
-
export type ClassTypeWithInstance<T> = new (...args: any[]) => T
|
|
10
|
-
|
|
11
|
-
export class InjectionToken<
|
|
12
|
-
T,
|
|
13
|
-
S extends AnyZodObject | ZodOptional<AnyZodObject> | unknown = unknown,
|
|
14
|
-
> {
|
|
15
|
-
public id = randomUUID()
|
|
16
|
-
constructor(
|
|
17
|
-
public readonly name: string | symbol | ClassType,
|
|
18
|
-
public readonly schema: AnyZodObject | undefined,
|
|
19
|
-
) {}
|
|
20
|
-
|
|
21
|
-
static create<T extends ClassType>(
|
|
22
|
-
name: T,
|
|
23
|
-
): InjectionToken<InstanceType<T>, undefined>
|
|
24
|
-
static create<
|
|
25
|
-
T extends ClassType,
|
|
26
|
-
Schema extends AnyZodObject | ZodOptional<AnyZodObject>,
|
|
27
|
-
>(name: T, schema: Schema): InjectionToken<InstanceType<T>, Schema>
|
|
28
|
-
static create<T>(name: string | symbol): InjectionToken<T, undefined>
|
|
29
|
-
static create<T, Schema extends AnyZodObject | ZodOptional<AnyZodObject>>(
|
|
30
|
-
name: string | any,
|
|
31
|
-
schema: Schema,
|
|
32
|
-
): InjectionToken<T, Schema>
|
|
33
|
-
static create(name: string | symbol, schema?: unknown) {
|
|
34
|
-
// @ts-expect-error
|
|
35
|
-
return new InjectionToken(name, schema)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
static bound<T, S extends AnyZodObject | ZodOptional<AnyZodObject>>(
|
|
39
|
-
token: InjectionToken<T, S>,
|
|
40
|
-
value: z.input<S>,
|
|
41
|
-
): BoundInjectionToken<T, S> {
|
|
42
|
-
return new BoundInjectionToken(token, value)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
static factory<T, S extends AnyZodObject | ZodOptional<AnyZodObject>>(
|
|
46
|
-
token: InjectionToken<T, S>,
|
|
47
|
-
factory: () => Promise<z.input<S>>,
|
|
48
|
-
): FactoryInjectionToken<T, S> {
|
|
49
|
-
return new FactoryInjectionToken(token, factory)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static refineType<T>(
|
|
53
|
-
token: BoundInjectionToken<any, any>,
|
|
54
|
-
): BoundInjectionToken<T, any> {
|
|
55
|
-
return token as BoundInjectionToken<T, any>
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class BoundInjectionToken<
|
|
60
|
-
T,
|
|
61
|
-
S extends AnyZodObject | ZodOptional<AnyZodObject>,
|
|
62
|
-
> extends InjectionToken<T, undefined> {
|
|
63
|
-
constructor(
|
|
64
|
-
public readonly token: InjectionToken<T, S>,
|
|
65
|
-
public readonly value: z.input<S>,
|
|
66
|
-
) {
|
|
67
|
-
super(token.name, token.schema)
|
|
68
|
-
this.id = token.id
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export class FactoryInjectionToken<
|
|
73
|
-
T,
|
|
74
|
-
S extends AnyZodObject | ZodOptional<AnyZodObject>,
|
|
75
|
-
> extends InjectionToken<T, S> {
|
|
76
|
-
public value?: z.input<S>
|
|
77
|
-
public resolved = false
|
|
78
|
-
constructor(
|
|
79
|
-
public readonly token: InjectionToken<T, S>,
|
|
80
|
-
public readonly factory: () => Promise<z.input<S>>,
|
|
81
|
-
) {
|
|
82
|
-
super(token.name, token.schema)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async resolve(): Promise<z.input<S>> {
|
|
86
|
-
if (!this.value) {
|
|
87
|
-
this.value = await this.factory()
|
|
88
|
-
this.resolved = true
|
|
89
|
-
}
|
|
90
|
-
return this.value
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ServiceLocator } from './service-locator.mjs'
|
|
2
|
-
|
|
3
|
-
let serviceLocator: ServiceLocator = new ServiceLocator()
|
|
4
|
-
|
|
5
|
-
export function provideServiceLocator(locator: ServiceLocator): ServiceLocator {
|
|
6
|
-
const original = serviceLocator
|
|
7
|
-
serviceLocator = locator
|
|
8
|
-
return original
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getServiceLocator(): ServiceLocator {
|
|
12
|
-
if (!serviceLocator) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
'[ServiceLocator] Service locator is not initialized. Please provide the service locator before using the @Injectable decorator.',
|
|
15
|
-
)
|
|
16
|
-
}
|
|
17
|
-
return serviceLocator
|
|
18
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { AnyZodObject } from 'zod'
|
|
2
|
-
|
|
3
|
-
import { z } from 'zod'
|
|
4
|
-
|
|
5
|
-
export interface Factory<T> {
|
|
6
|
-
create(ctx?: any): Promise<T> | T
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface FactoryWithArgs<T, A extends AnyZodObject> {
|
|
10
|
-
create(ctx: any, args: z.output<A>): Promise<T> | T
|
|
11
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ClassType, InjectionToken } from './injection-token.mjs'
|
|
2
|
-
|
|
3
|
-
import { getServiceLocator } from './injector.mjs'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Useful for tests or when you want to override a service
|
|
7
|
-
* with a different implementation.
|
|
8
|
-
*/
|
|
9
|
-
export function override<T>(token: InjectionToken<T>, target: ClassType) {
|
|
10
|
-
const serviceLocator = getServiceLocator()
|
|
11
|
-
const originalDefinition = serviceLocator['abstractFactories'].get(token)
|
|
12
|
-
serviceLocator.registerAbstractFactory(token, async (ctx, args: any) => {
|
|
13
|
-
const builder = new target()
|
|
14
|
-
return builder.create(ctx, args)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return () => {
|
|
18
|
-
if (originalDefinition) {
|
|
19
|
-
serviceLocator.registerAbstractFactory(token, originalDefinition)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|