@navios/core 0.1.15 → 0.2.1
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 +96 -425
- package/dist/_tsup-dts-rollup.d.ts +96 -425
- 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 +5 -4
- 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 +13 -11
- package/src/decorators/controller.decorator.mts +3 -6
- package/src/decorators/endpoint.decorator.mts +17 -10
- package/src/decorators/module.decorator.mts +3 -6
- package/src/decorators/multipart.decorator.mts +17 -10
- package/src/decorators/stream.decorator.mts +17 -10
- 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,171 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { z } from 'zod'
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
getInjectableToken,
|
|
6
|
-
Injectable,
|
|
7
|
-
InjectableType,
|
|
8
|
-
} from '../decorators/index.mjs'
|
|
9
|
-
import { InjectableScope } from '../enums/index.mjs'
|
|
10
|
-
import { syncInject } from '../index.mjs'
|
|
11
|
-
import { inject } from '../inject.mjs'
|
|
12
|
-
import { InjectionToken } from '../injection-token.mjs'
|
|
13
|
-
import { getServiceLocator } from '../injector.mjs'
|
|
14
|
-
|
|
15
|
-
describe('Injectable decorator', () => {
|
|
16
|
-
it('should work with class', async () => {
|
|
17
|
-
@Injectable()
|
|
18
|
-
class Test {}
|
|
19
|
-
|
|
20
|
-
const value = await inject(Test)
|
|
21
|
-
expect(value).toBeInstanceOf(Test)
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should work with inner inject', async () => {
|
|
25
|
-
@Injectable()
|
|
26
|
-
class Test {
|
|
27
|
-
makeFoo() {
|
|
28
|
-
return 'foo'
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@Injectable()
|
|
33
|
-
class Test2 {
|
|
34
|
-
fooMaker = inject(Test)
|
|
35
|
-
|
|
36
|
-
async makeFoo() {
|
|
37
|
-
const fooMaker = await this.fooMaker
|
|
38
|
-
return fooMaker.makeFoo()
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const value = await inject(Test2)
|
|
43
|
-
expect(value).toBeInstanceOf(Test2)
|
|
44
|
-
const result = await value.makeFoo()
|
|
45
|
-
expect(result).toBe('foo')
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
it('should work with factory', async () => {
|
|
49
|
-
@Injectable({ type: InjectableType.Factory })
|
|
50
|
-
class Test {
|
|
51
|
-
create() {
|
|
52
|
-
return 'foo'
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const value = await inject(Test)
|
|
57
|
-
expect(value).toBe('foo')
|
|
58
|
-
})
|
|
59
|
-
it('should work with request scope', async () => {
|
|
60
|
-
@Injectable({
|
|
61
|
-
scope: InjectableScope.Instance,
|
|
62
|
-
type: InjectableType.Factory,
|
|
63
|
-
})
|
|
64
|
-
class Test {
|
|
65
|
-
create() {
|
|
66
|
-
return Date.now()
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const value = await inject(Test)
|
|
71
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
72
|
-
const value2 = await inject(Test)
|
|
73
|
-
expect(value).not.toBe(value2)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('should work with injection token', async () => {
|
|
77
|
-
const token = InjectionToken.create('Test')
|
|
78
|
-
|
|
79
|
-
@Injectable({ token })
|
|
80
|
-
class Test {}
|
|
81
|
-
|
|
82
|
-
const value = await inject(token)
|
|
83
|
-
expect(value).toBeInstanceOf(Test)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it('should work with injection token and schema', async () => {
|
|
87
|
-
class TestFoo {
|
|
88
|
-
constructor(public readonly foo: string) {}
|
|
89
|
-
}
|
|
90
|
-
const token = InjectionToken.create(
|
|
91
|
-
TestFoo,
|
|
92
|
-
z.object({
|
|
93
|
-
foo: z.string(),
|
|
94
|
-
}),
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
@Injectable({ token, type: InjectableType.Factory })
|
|
98
|
-
class Test {
|
|
99
|
-
create(ctx: any, args: { foo: string }) {
|
|
100
|
-
return new TestFoo(args.foo)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const value = await inject(token, { foo: 'bar' })
|
|
105
|
-
const differentValue = await inject(token, { foo: 'baz' })
|
|
106
|
-
const sameValue = await inject(token, { foo: 'bar' })
|
|
107
|
-
expect(value).toBeInstanceOf(TestFoo)
|
|
108
|
-
expect(value.foo).toBe('bar')
|
|
109
|
-
expect(differentValue).toBeInstanceOf(TestFoo)
|
|
110
|
-
expect(differentValue.foo).toBe('baz')
|
|
111
|
-
expect(value).not.toBe(differentValue)
|
|
112
|
-
expect(value).toBe(sameValue)
|
|
113
|
-
})
|
|
114
|
-
it('should work with invalidation', async () => {
|
|
115
|
-
@Injectable()
|
|
116
|
-
class Test {
|
|
117
|
-
value = Date.now()
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
@Injectable()
|
|
121
|
-
class Test2 {
|
|
122
|
-
test = inject(Test)
|
|
123
|
-
|
|
124
|
-
async makeFoo() {
|
|
125
|
-
const test = await this.test
|
|
126
|
-
return test.value
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
const identifier = getServiceLocator().getInstanceIdentifier(
|
|
130
|
-
getInjectableToken(Test),
|
|
131
|
-
undefined,
|
|
132
|
-
)
|
|
133
|
-
const inst1 = await inject(Test2)
|
|
134
|
-
expect(inst1).toBeInstanceOf(Test2)
|
|
135
|
-
const result1 = await inst1.makeFoo()
|
|
136
|
-
const inst2 = await inject(Test2)
|
|
137
|
-
expect(inst1).toBe(inst2)
|
|
138
|
-
const result2 = await inst2.makeFoo()
|
|
139
|
-
await getServiceLocator().invalidate(identifier)
|
|
140
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
141
|
-
const inst3 = await inject(Test2)
|
|
142
|
-
expect(inst1).not.toBe(inst3)
|
|
143
|
-
const result3 = await inst3.makeFoo()
|
|
144
|
-
expect(result1).not.toBe(result3)
|
|
145
|
-
expect(result2).not.toBe(result3)
|
|
146
|
-
expect(result1).toBe(result2)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('should work with syncInject', async () => {
|
|
150
|
-
@Injectable()
|
|
151
|
-
class Test {
|
|
152
|
-
value = Date.now()
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
@Injectable()
|
|
156
|
-
class Test2 {
|
|
157
|
-
test = syncInject(Test)
|
|
158
|
-
|
|
159
|
-
makeFoo() {
|
|
160
|
-
return this.test.value
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const inst1 = await inject(Test2)
|
|
164
|
-
expect(inst1).toBeInstanceOf(Test2)
|
|
165
|
-
const result1 = inst1.makeFoo()
|
|
166
|
-
const inst2 = await inject(Test2)
|
|
167
|
-
expect(inst1).toBe(inst2)
|
|
168
|
-
const result2 = await inst2.makeFoo()
|
|
169
|
-
expect(result1).toBe(result2)
|
|
170
|
-
})
|
|
171
|
-
})
|
|
@@ -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'
|