@navios/di 0.2.0 → 0.3.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/README.md +301 -39
- package/docs/README.md +122 -49
- package/docs/api-reference.md +763 -0
- package/docs/container.md +274 -0
- package/docs/examples/basic-usage.mts +97 -0
- package/docs/examples/factory-pattern.mts +318 -0
- package/docs/examples/injection-tokens.mts +225 -0
- package/docs/examples/request-scope-example.mts +254 -0
- package/docs/examples/service-lifecycle.mts +359 -0
- package/docs/factory.md +584 -0
- package/docs/getting-started.md +308 -0
- package/docs/injectable.md +496 -0
- package/docs/injection-tokens.md +400 -0
- package/docs/lifecycle.md +539 -0
- package/docs/scopes.md +749 -0
- package/lib/_tsup-dts-rollup.d.mts +495 -150
- package/lib/_tsup-dts-rollup.d.ts +495 -150
- package/lib/index.d.mts +26 -12
- package/lib/index.d.ts +26 -12
- package/lib/index.js +993 -462
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +983 -453
- package/lib/index.mjs.map +1 -1
- package/package.json +2 -2
- package/project.json +10 -2
- package/src/__tests__/container.spec.mts +1301 -0
- package/src/__tests__/factory.spec.mts +137 -0
- package/src/__tests__/injectable.spec.mts +32 -88
- package/src/__tests__/injection-token.spec.mts +333 -17
- package/src/__tests__/request-scope.spec.mts +263 -0
- package/src/__type-tests__/factory.spec-d.mts +65 -0
- package/src/__type-tests__/inject.spec-d.mts +27 -28
- package/src/__type-tests__/injectable.spec-d.mts +42 -206
- package/src/container.mts +167 -0
- package/src/decorators/factory.decorator.mts +79 -0
- package/src/decorators/index.mts +1 -0
- package/src/decorators/injectable.decorator.mts +6 -56
- package/src/enums/injectable-scope.enum.mts +5 -1
- package/src/event-emitter.mts +18 -20
- package/src/factory-context.mts +2 -10
- package/src/index.mts +3 -2
- package/src/injection-token.mts +24 -9
- package/src/injector.mts +8 -20
- package/src/interfaces/factory.interface.mts +3 -3
- package/src/interfaces/index.mts +2 -0
- package/src/interfaces/on-service-destroy.interface.mts +3 -0
- package/src/interfaces/on-service-init.interface.mts +3 -0
- package/src/registry.mts +7 -16
- package/src/request-context-holder.mts +145 -0
- package/src/service-instantiator.mts +158 -0
- package/src/service-locator-event-bus.mts +0 -28
- package/src/service-locator-instance-holder.mts +27 -16
- package/src/service-locator-manager.mts +84 -0
- package/src/service-locator.mts +550 -395
- package/src/utils/defer.mts +73 -0
- package/src/utils/get-injectors.mts +93 -80
- package/src/utils/index.mts +2 -0
- package/src/utils/types.mts +52 -0
- package/docs/concepts/injectable.md +0 -182
- package/docs/concepts/injection-token.md +0 -145
- package/src/proxy-service-locator.mts +0 -83
- package/src/resolve-service.mts +0 -41
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { z } from 'zod/v4'
|
|
2
|
+
|
|
3
|
+
import type { Factorable, FactorableWithArgs } from '../interfaces/index.mjs'
|
|
4
|
+
|
|
5
|
+
import { Factory } from '../decorators/index.mjs'
|
|
6
|
+
import { InjectableScope } from '../enums/index.mjs'
|
|
7
|
+
import { InjectionToken } from '../injection-token.mjs'
|
|
8
|
+
import { Registry } from '../registry.mjs'
|
|
9
|
+
|
|
10
|
+
// Test factory without arguments
|
|
11
|
+
@Factory()
|
|
12
|
+
class TestFactory1 implements Factorable<string> {
|
|
13
|
+
create() {
|
|
14
|
+
return 'test'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Test factory with scope
|
|
19
|
+
@Factory({ scope: InjectableScope.Transient })
|
|
20
|
+
class TestFactory2 implements Factorable<number> {
|
|
21
|
+
create() {
|
|
22
|
+
return 42
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Test factory with token
|
|
27
|
+
const token = InjectionToken.create('TestToken')
|
|
28
|
+
@Factory({ token })
|
|
29
|
+
class TestFactory3 implements Factorable<boolean> {
|
|
30
|
+
create() {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Test factory with token and schema
|
|
36
|
+
const schema = z.object({ name: z.string() })
|
|
37
|
+
const tokenWithSchema = InjectionToken.create('TestTokenWithSchema', schema)
|
|
38
|
+
@Factory({ token: tokenWithSchema })
|
|
39
|
+
class TestFactory4
|
|
40
|
+
implements FactorableWithArgs<{ name: string }, typeof schema>
|
|
41
|
+
{
|
|
42
|
+
create(ctx: any, args: z.output<typeof schema>) {
|
|
43
|
+
return args
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Test factory with custom registry
|
|
48
|
+
const registry = new Registry()
|
|
49
|
+
@Factory({ registry })
|
|
50
|
+
class TestFactory5 implements Factorable<object> {
|
|
51
|
+
create() {
|
|
52
|
+
return {}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Type tests
|
|
57
|
+
const test1: string = new TestFactory1().create()
|
|
58
|
+
const test2: number = new TestFactory2().create()
|
|
59
|
+
const test3: boolean = new TestFactory3().create()
|
|
60
|
+
const test4: { name: string } = new TestFactory4().create(undefined, {
|
|
61
|
+
name: 'test',
|
|
62
|
+
})
|
|
63
|
+
const test5: object = new TestFactory5().create()
|
|
64
|
+
|
|
65
|
+
export { test1, test2, test3, test4, test5 }
|
|
@@ -2,9 +2,8 @@ import { assertType, describe, test } from 'vitest'
|
|
|
2
2
|
import { z } from 'zod/v4'
|
|
3
3
|
|
|
4
4
|
import { Injectable } from '../decorators/index.mjs'
|
|
5
|
-
import { InjectableType } from '../enums/index.mjs'
|
|
6
5
|
import { InjectionToken } from '../injection-token.mjs'
|
|
7
|
-
import {
|
|
6
|
+
import { asyncInject } from '../injector.mjs'
|
|
8
7
|
|
|
9
8
|
interface FooService {
|
|
10
9
|
makeFoo(): string
|
|
@@ -18,8 +17,8 @@ const simpleOptionalObjectSchema = z
|
|
|
18
17
|
foo: z.string(),
|
|
19
18
|
})
|
|
20
19
|
.optional()
|
|
21
|
-
const simpleRecordSchema = z.record(z.string(), z.string())
|
|
22
|
-
const simpleOptionalRecordSchema = z.record(z.string(), z.string()).optional()
|
|
20
|
+
// const simpleRecordSchema = z.record(z.string(), z.string())
|
|
21
|
+
// const simpleOptionalRecordSchema = z.record(z.string(), z.string()).optional()
|
|
23
22
|
|
|
24
23
|
const typelessObjectToken = InjectionToken.create(
|
|
25
24
|
Symbol.for('Typeless object token'),
|
|
@@ -29,14 +28,14 @@ const typelessOptionalObjectToken = InjectionToken.create(
|
|
|
29
28
|
Symbol.for('Typeless optional object token'),
|
|
30
29
|
simpleOptionalObjectSchema,
|
|
31
30
|
)
|
|
32
|
-
const typelessRecordToken = InjectionToken.create(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
)
|
|
36
|
-
const typelessOptionalRecordToken = InjectionToken.create(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
31
|
+
// const typelessRecordToken = InjectionToken.create(
|
|
32
|
+
// Symbol.for('Typeless record token'),
|
|
33
|
+
// simpleRecordSchema,
|
|
34
|
+
// )
|
|
35
|
+
// const typelessOptionalRecordToken = InjectionToken.create(
|
|
36
|
+
// Symbol.for('Typeless optional record token'),
|
|
37
|
+
// simpleOptionalRecordSchema,
|
|
38
|
+
// )
|
|
40
39
|
|
|
41
40
|
const typedObjectToken = InjectionToken.create<
|
|
42
41
|
FooService,
|
|
@@ -46,14 +45,14 @@ const typedOptionalObjectToken = InjectionToken.create<
|
|
|
46
45
|
FooService,
|
|
47
46
|
typeof simpleOptionalObjectSchema
|
|
48
47
|
>(Symbol.for('Typed optional object token'), simpleOptionalObjectSchema)
|
|
49
|
-
const typedRecordToken = InjectionToken.create<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
>(Symbol.for('Typed record token'), simpleRecordSchema)
|
|
53
|
-
const typedOptionalRecordToken = InjectionToken.create<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
>(Symbol.for('Typed optional record token'), simpleOptionalRecordSchema)
|
|
48
|
+
// const typedRecordToken = InjectionToken.create<
|
|
49
|
+
// FooService,
|
|
50
|
+
// typeof simpleRecordSchema
|
|
51
|
+
// >(Symbol.for('Typed record token'), simpleRecordSchema)
|
|
52
|
+
// const typedOptionalRecordToken = InjectionToken.create<
|
|
53
|
+
// FooService,
|
|
54
|
+
// typeof simpleOptionalRecordSchema
|
|
55
|
+
// >(Symbol.for('Typed optional record token'), simpleOptionalRecordSchema)
|
|
57
56
|
|
|
58
57
|
const typedToken = InjectionToken.create<FooService>(Symbol.for('Typed token'))
|
|
59
58
|
|
|
@@ -66,34 +65,34 @@ describe('inject', () => {
|
|
|
66
65
|
}
|
|
67
66
|
}
|
|
68
67
|
|
|
69
|
-
assertType<Foo>(await
|
|
68
|
+
assertType<Foo>(await asyncInject(Foo))
|
|
70
69
|
})
|
|
71
70
|
test('#2 Token with required Schema', async () => {
|
|
72
|
-
const result = await
|
|
71
|
+
const result = await asyncInject(typelessObjectToken, { foo: 'bar' })
|
|
73
72
|
assertType<unknown>(result)
|
|
74
73
|
|
|
75
|
-
const result2 = await
|
|
74
|
+
const result2 = await asyncInject(typedObjectToken, { foo: 'bar' })
|
|
76
75
|
assertType<FooService>(result2)
|
|
77
76
|
|
|
78
77
|
// @ts-expect-error We show error when we pass the wrong type
|
|
79
|
-
await
|
|
78
|
+
await asyncInject(typedObjectToken, undefined)
|
|
80
79
|
})
|
|
81
80
|
|
|
82
81
|
test('#3 Token with optional Schema', async () => {
|
|
83
|
-
const result = await
|
|
82
|
+
const result = await asyncInject(typelessOptionalObjectToken)
|
|
84
83
|
assertType<unknown>(result)
|
|
85
84
|
|
|
86
|
-
const result2 = await
|
|
85
|
+
const result2 = await asyncInject(typedOptionalObjectToken)
|
|
87
86
|
assertType<FooService>(result2)
|
|
88
87
|
|
|
89
|
-
const result3 = await
|
|
88
|
+
const result3 = await asyncInject(typedObjectToken)
|
|
90
89
|
// Special case when we pass the token without args
|
|
91
90
|
// We can only return an error string
|
|
92
91
|
assertType<'Error: Your token requires args: foo'>(result3)
|
|
93
92
|
})
|
|
94
93
|
|
|
95
94
|
test('#4 Token with no Schema', async () => {
|
|
96
|
-
const result = await
|
|
95
|
+
const result = await asyncInject(typedToken)
|
|
97
96
|
assertType<FooService>(result)
|
|
98
97
|
})
|
|
99
98
|
})
|
|
@@ -2,7 +2,6 @@ import { expectTypeOf, test } from 'vitest'
|
|
|
2
2
|
import { z } from 'zod/v4'
|
|
3
3
|
|
|
4
4
|
import { Injectable } from '../decorators/index.mjs'
|
|
5
|
-
import { InjectableType } from '../enums/index.mjs'
|
|
6
5
|
import { InjectionToken } from '../injection-token.mjs'
|
|
7
6
|
|
|
8
7
|
interface FooService {
|
|
@@ -17,8 +16,8 @@ const simpleOptionalObjectSchema = z
|
|
|
17
16
|
foo: z.string(),
|
|
18
17
|
})
|
|
19
18
|
.optional()
|
|
20
|
-
const simpleRecordSchema = z.record(z.string(), z.string())
|
|
21
|
-
const simpleOptionalRecordSchema = z.record(z.string(), z.string()).optional()
|
|
19
|
+
// const simpleRecordSchema = z.record(z.string(), z.string())
|
|
20
|
+
// const simpleOptionalRecordSchema = z.record(z.string(), z.string()).optional()
|
|
22
21
|
|
|
23
22
|
const typelessObjectToken = InjectionToken.create(
|
|
24
23
|
Symbol.for('Typeless object token'),
|
|
@@ -28,14 +27,14 @@ const typelessOptionalObjectToken = InjectionToken.create(
|
|
|
28
27
|
Symbol.for('Typeless optional object token'),
|
|
29
28
|
simpleOptionalObjectSchema,
|
|
30
29
|
)
|
|
31
|
-
const typelessRecordToken = InjectionToken.create(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
)
|
|
35
|
-
const typelessOptionalRecordToken = InjectionToken.create(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
)
|
|
30
|
+
// const typelessRecordToken = InjectionToken.create(
|
|
31
|
+
// Symbol.for('Typeless record token'),
|
|
32
|
+
// simpleRecordSchema,
|
|
33
|
+
// )
|
|
34
|
+
// const typelessOptionalRecordToken = InjectionToken.create(
|
|
35
|
+
// Symbol.for('Typeless optional record token'),
|
|
36
|
+
// simpleOptionalRecordSchema,
|
|
37
|
+
// )
|
|
39
38
|
|
|
40
39
|
const typedObjectToken = InjectionToken.create<
|
|
41
40
|
FooService,
|
|
@@ -45,14 +44,14 @@ const typedOptionalObjectToken = InjectionToken.create<
|
|
|
45
44
|
FooService,
|
|
46
45
|
typeof simpleOptionalObjectSchema
|
|
47
46
|
>(Symbol.for('Typed optional object token'), simpleOptionalObjectSchema)
|
|
48
|
-
const typedRecordToken = InjectionToken.create<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
>(Symbol.for('Typed record token'), simpleRecordSchema)
|
|
52
|
-
const typedOptionalRecordToken = InjectionToken.create<
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
>(Symbol.for('Typed optional record token'), simpleOptionalRecordSchema)
|
|
47
|
+
// const typedRecordToken = InjectionToken.create<
|
|
48
|
+
// FooService,
|
|
49
|
+
// typeof simpleRecordSchema
|
|
50
|
+
// >(Symbol.for('Typed record token'), simpleRecordSchema)
|
|
51
|
+
// const typedOptionalRecordToken = InjectionToken.create<
|
|
52
|
+
// FooService,
|
|
53
|
+
// typeof simpleOptionalRecordSchema
|
|
54
|
+
// >(Symbol.for('Typed optional record token'), simpleOptionalRecordSchema)
|
|
56
55
|
|
|
57
56
|
const typedToken = InjectionToken.create<FooService>(Symbol.for('Typed token'))
|
|
58
57
|
|
|
@@ -62,24 +61,8 @@ test('Injectable types', () => {
|
|
|
62
61
|
@Injectable()
|
|
63
62
|
class {},
|
|
64
63
|
).toBeConstructibleWith()
|
|
65
|
-
// #2
|
|
66
|
-
expectTypeOf(
|
|
67
|
-
@Injectable({
|
|
68
|
-
type: InjectableType.Factory,
|
|
69
|
-
})
|
|
70
|
-
class {
|
|
71
|
-
create() {}
|
|
72
|
-
},
|
|
73
|
-
).toBeConstructibleWith()
|
|
74
|
-
expectTypeOf(
|
|
75
|
-
// @ts-expect-error should check that the class implements the factory
|
|
76
|
-
@Injectable({
|
|
77
|
-
type: InjectableType.Factory,
|
|
78
|
-
})
|
|
79
|
-
class {},
|
|
80
|
-
).toBeConstructibleWith()
|
|
81
64
|
|
|
82
|
-
// #
|
|
65
|
+
// #2 required argument
|
|
83
66
|
expectTypeOf(
|
|
84
67
|
@Injectable({
|
|
85
68
|
token: typelessObjectToken,
|
|
@@ -90,7 +73,7 @@ test('Injectable types', () => {
|
|
|
90
73
|
).toBeConstructibleWith({
|
|
91
74
|
foo: 'something',
|
|
92
75
|
})
|
|
93
|
-
// #
|
|
76
|
+
// #2 it's required in token but optional in class allowed
|
|
94
77
|
expectTypeOf(
|
|
95
78
|
@Injectable({
|
|
96
79
|
token: typelessObjectToken,
|
|
@@ -101,7 +84,7 @@ test('Injectable types', () => {
|
|
|
101
84
|
).toBeConstructibleWith({
|
|
102
85
|
foo: 'something',
|
|
103
86
|
})
|
|
104
|
-
// #
|
|
87
|
+
// #2 optional value but class accepts it
|
|
105
88
|
expectTypeOf(
|
|
106
89
|
@Injectable({
|
|
107
90
|
token: typelessOptionalObjectToken,
|
|
@@ -112,7 +95,7 @@ test('Injectable types', () => {
|
|
|
112
95
|
).toBeConstructibleWith({
|
|
113
96
|
foo: 'something',
|
|
114
97
|
})
|
|
115
|
-
// #
|
|
98
|
+
// #2 optional value and class accepts it
|
|
116
99
|
expectTypeOf(
|
|
117
100
|
@Injectable({
|
|
118
101
|
token: typelessOptionalObjectToken,
|
|
@@ -121,7 +104,7 @@ test('Injectable types', () => {
|
|
|
121
104
|
constructor(public arg: z.infer<typeof simpleOptionalObjectSchema>) {}
|
|
122
105
|
},
|
|
123
106
|
).toBeConstructibleWith(undefined)
|
|
124
|
-
// #
|
|
107
|
+
// #2 compatible schemas
|
|
125
108
|
expectTypeOf(
|
|
126
109
|
@Injectable({
|
|
127
110
|
token: typelessOptionalObjectToken,
|
|
@@ -130,7 +113,7 @@ test('Injectable types', () => {
|
|
|
130
113
|
constructor(public arg?: z.infer<typeof simpleObjectSchema>) {}
|
|
131
114
|
},
|
|
132
115
|
).toBeConstructibleWith(undefined)
|
|
133
|
-
// #
|
|
116
|
+
// #2 compatible schemas
|
|
134
117
|
expectTypeOf(
|
|
135
118
|
// @ts-expect-error token has optional schema, but Class has required, should fail
|
|
136
119
|
@Injectable({
|
|
@@ -143,7 +126,7 @@ test('Injectable types', () => {
|
|
|
143
126
|
foo: 'something',
|
|
144
127
|
})
|
|
145
128
|
|
|
146
|
-
// #
|
|
129
|
+
// #2 typed token and required argument
|
|
147
130
|
expectTypeOf(
|
|
148
131
|
@Injectable({
|
|
149
132
|
token: typedObjectToken,
|
|
@@ -158,7 +141,7 @@ test('Injectable types', () => {
|
|
|
158
141
|
).toBeConstructibleWith({
|
|
159
142
|
foo: 'something',
|
|
160
143
|
})
|
|
161
|
-
// #
|
|
144
|
+
// #2 typed token and required argument
|
|
162
145
|
expectTypeOf(
|
|
163
146
|
@Injectable({
|
|
164
147
|
token: typedOptionalObjectToken,
|
|
@@ -173,7 +156,7 @@ test('Injectable types', () => {
|
|
|
173
156
|
).toBeConstructibleWith({
|
|
174
157
|
foo: 'something',
|
|
175
158
|
})
|
|
176
|
-
// #
|
|
159
|
+
// #2 should fail if not compatible
|
|
177
160
|
expectTypeOf(
|
|
178
161
|
// @ts-expect-error class doesn't implement the token type
|
|
179
162
|
@Injectable({
|
|
@@ -185,7 +168,7 @@ test('Injectable types', () => {
|
|
|
185
168
|
).toBeConstructibleWith({
|
|
186
169
|
foo: 'something',
|
|
187
170
|
})
|
|
188
|
-
// #
|
|
171
|
+
// #2 should fail if not compatible
|
|
189
172
|
expectTypeOf(
|
|
190
173
|
// @ts-expect-error class doesn't implement the token type
|
|
191
174
|
@Injectable({
|
|
@@ -201,7 +184,7 @@ test('Injectable types', () => {
|
|
|
201
184
|
).toBeConstructibleWith({
|
|
202
185
|
foo: 'something',
|
|
203
186
|
})
|
|
204
|
-
// #
|
|
187
|
+
// #2 typed token without schema
|
|
205
188
|
expectTypeOf(
|
|
206
189
|
@Injectable({
|
|
207
190
|
token: typedToken,
|
|
@@ -213,7 +196,7 @@ test('Injectable types', () => {
|
|
|
213
196
|
}
|
|
214
197
|
},
|
|
215
198
|
).toBeConstructibleWith()
|
|
216
|
-
// #
|
|
199
|
+
// #2 typed token without schema fail if not compatible
|
|
217
200
|
expectTypeOf(
|
|
218
201
|
// @ts-expect-error class doesn't implement the token type
|
|
219
202
|
@Injectable({
|
|
@@ -224,69 +207,6 @@ test('Injectable types', () => {
|
|
|
224
207
|
},
|
|
225
208
|
).toBeConstructibleWith()
|
|
226
209
|
|
|
227
|
-
// #4 factory with typed token
|
|
228
|
-
expectTypeOf(
|
|
229
|
-
@Injectable({
|
|
230
|
-
type: InjectableType.Factory,
|
|
231
|
-
token: typedToken,
|
|
232
|
-
})
|
|
233
|
-
class {
|
|
234
|
-
constructor() {}
|
|
235
|
-
create() {
|
|
236
|
-
return {
|
|
237
|
-
makeFoo: () => 'foo',
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
).toBeConstructibleWith()
|
|
242
|
-
// #4 factory with typed token without schema should fail if not compatible
|
|
243
|
-
expectTypeOf(
|
|
244
|
-
// @ts-expect-error factory doesn't implement the token type
|
|
245
|
-
@Injectable({
|
|
246
|
-
type: InjectableType.Factory,
|
|
247
|
-
token: typedToken,
|
|
248
|
-
})
|
|
249
|
-
class {
|
|
250
|
-
constructor() {}
|
|
251
|
-
create(ctx: any, arg: z.infer<typeof simpleObjectSchema>) {
|
|
252
|
-
return {
|
|
253
|
-
makeFoo: () => 'foo',
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
},
|
|
257
|
-
).toBeConstructibleWith()
|
|
258
|
-
// #4 factory with typed token fail if not compatible
|
|
259
|
-
expectTypeOf(
|
|
260
|
-
// @ts-expect-error class doesn't implement the token type
|
|
261
|
-
@Injectable({
|
|
262
|
-
type: InjectableType.Factory,
|
|
263
|
-
token: typedToken,
|
|
264
|
-
})
|
|
265
|
-
class {
|
|
266
|
-
constructor() {}
|
|
267
|
-
create() {
|
|
268
|
-
return {
|
|
269
|
-
// makeFoo: () => 'foo',
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
},
|
|
273
|
-
).toBeConstructibleWith()
|
|
274
|
-
// #4 factory with typed token and schema
|
|
275
|
-
expectTypeOf(
|
|
276
|
-
@Injectable({
|
|
277
|
-
type: InjectableType.Factory,
|
|
278
|
-
token: typedObjectToken,
|
|
279
|
-
})
|
|
280
|
-
class {
|
|
281
|
-
constructor() {}
|
|
282
|
-
create(ctx: any, arg: z.infer<typeof simpleObjectSchema>) {
|
|
283
|
-
return {
|
|
284
|
-
makeFoo: () => 'foo',
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
},
|
|
288
|
-
)
|
|
289
|
-
|
|
290
210
|
// #1 Injectable w/o decorators enabled in project
|
|
291
211
|
expectTypeOf(
|
|
292
212
|
Injectable({
|
|
@@ -300,25 +220,8 @@ test('Injectable types', () => {
|
|
|
300
220
|
},
|
|
301
221
|
),
|
|
302
222
|
).toBeConstructibleWith()
|
|
303
|
-
expectTypeOf(
|
|
304
|
-
Injectable({
|
|
305
|
-
type: InjectableType.Factory,
|
|
306
|
-
})(
|
|
307
|
-
class {
|
|
308
|
-
create() {}
|
|
309
|
-
},
|
|
310
|
-
),
|
|
311
|
-
).toBeConstructibleWith()
|
|
312
|
-
expectTypeOf(
|
|
313
|
-
Injectable({
|
|
314
|
-
type: InjectableType.Factory,
|
|
315
|
-
})(
|
|
316
|
-
// @ts-expect-error should check that the class implements the factory
|
|
317
|
-
class {},
|
|
318
|
-
),
|
|
319
|
-
).toBeConstructibleWith()
|
|
320
223
|
|
|
321
|
-
// #
|
|
224
|
+
// #2 required argument
|
|
322
225
|
expectTypeOf(
|
|
323
226
|
Injectable({
|
|
324
227
|
token: typelessObjectToken,
|
|
@@ -330,7 +233,7 @@ test('Injectable types', () => {
|
|
|
330
233
|
).toBeConstructibleWith({
|
|
331
234
|
foo: 'something',
|
|
332
235
|
})
|
|
333
|
-
// #
|
|
236
|
+
// #2 it's required in token but optional in class allowed
|
|
334
237
|
expectTypeOf(
|
|
335
238
|
Injectable({
|
|
336
239
|
token: typelessObjectToken,
|
|
@@ -342,7 +245,7 @@ test('Injectable types', () => {
|
|
|
342
245
|
).toBeConstructibleWith({
|
|
343
246
|
foo: 'something',
|
|
344
247
|
})
|
|
345
|
-
// #
|
|
248
|
+
// #2 optional value but class accepts it
|
|
346
249
|
expectTypeOf(
|
|
347
250
|
Injectable({
|
|
348
251
|
token: typelessOptionalObjectToken,
|
|
@@ -354,7 +257,7 @@ test('Injectable types', () => {
|
|
|
354
257
|
).toBeConstructibleWith({
|
|
355
258
|
foo: 'something',
|
|
356
259
|
})
|
|
357
|
-
// #
|
|
260
|
+
// #2 optional value and class accepts it
|
|
358
261
|
expectTypeOf(
|
|
359
262
|
Injectable({
|
|
360
263
|
token: typelessOptionalObjectToken,
|
|
@@ -364,7 +267,7 @@ test('Injectable types', () => {
|
|
|
364
267
|
},
|
|
365
268
|
),
|
|
366
269
|
).toBeConstructibleWith(undefined)
|
|
367
|
-
// #
|
|
270
|
+
// #2 compatible schemas
|
|
368
271
|
expectTypeOf(
|
|
369
272
|
Injectable({
|
|
370
273
|
token: typelessOptionalObjectToken,
|
|
@@ -374,7 +277,7 @@ test('Injectable types', () => {
|
|
|
374
277
|
},
|
|
375
278
|
),
|
|
376
279
|
).toBeConstructibleWith(undefined)
|
|
377
|
-
// #
|
|
280
|
+
// #2 compatible schemas
|
|
378
281
|
expectTypeOf(
|
|
379
282
|
Injectable({
|
|
380
283
|
token: typelessOptionalObjectToken,
|
|
@@ -388,7 +291,7 @@ test('Injectable types', () => {
|
|
|
388
291
|
foo: 'something',
|
|
389
292
|
})
|
|
390
293
|
|
|
391
|
-
// #
|
|
294
|
+
// #2 typed token and required argument
|
|
392
295
|
expectTypeOf(
|
|
393
296
|
Injectable({
|
|
394
297
|
token: typedObjectToken,
|
|
@@ -404,7 +307,7 @@ test('Injectable types', () => {
|
|
|
404
307
|
).toBeConstructibleWith({
|
|
405
308
|
foo: 'something',
|
|
406
309
|
})
|
|
407
|
-
// #
|
|
310
|
+
// #2 typed token and required argument
|
|
408
311
|
expectTypeOf(
|
|
409
312
|
Injectable({
|
|
410
313
|
token: typedOptionalObjectToken,
|
|
@@ -420,7 +323,7 @@ test('Injectable types', () => {
|
|
|
420
323
|
).toBeConstructibleWith({
|
|
421
324
|
foo: 'something',
|
|
422
325
|
})
|
|
423
|
-
// #
|
|
326
|
+
// #2 should fail if not compatible
|
|
424
327
|
expectTypeOf(
|
|
425
328
|
Injectable({
|
|
426
329
|
token: typedOptionalObjectToken,
|
|
@@ -433,7 +336,7 @@ test('Injectable types', () => {
|
|
|
433
336
|
).toBeConstructibleWith({
|
|
434
337
|
foo: 'something',
|
|
435
338
|
})
|
|
436
|
-
// #
|
|
339
|
+
// #2 should fail if not compatible
|
|
437
340
|
expectTypeOf(
|
|
438
341
|
Injectable({
|
|
439
342
|
token: typedOptionalObjectToken,
|
|
@@ -450,7 +353,7 @@ test('Injectable types', () => {
|
|
|
450
353
|
).toBeConstructibleWith({
|
|
451
354
|
foo: 'something',
|
|
452
355
|
})
|
|
453
|
-
// #
|
|
356
|
+
// #2 typed token without schema
|
|
454
357
|
expectTypeOf(
|
|
455
358
|
Injectable({
|
|
456
359
|
token: typedToken,
|
|
@@ -463,82 +366,15 @@ test('Injectable types', () => {
|
|
|
463
366
|
},
|
|
464
367
|
),
|
|
465
368
|
).toBeConstructibleWith()
|
|
466
|
-
// #
|
|
467
|
-
expectTypeOf(
|
|
468
|
-
Injectable({
|
|
469
|
-
token: typedToken,
|
|
470
|
-
})(
|
|
471
|
-
// @ts-expect-error class doesn't implement the token type
|
|
472
|
-
class {
|
|
473
|
-
constructor() {}
|
|
474
|
-
},
|
|
475
|
-
),
|
|
476
|
-
).toBeConstructibleWith()
|
|
477
|
-
|
|
478
|
-
// #4 factory with typed token
|
|
479
|
-
expectTypeOf(
|
|
480
|
-
Injectable({
|
|
481
|
-
type: InjectableType.Factory,
|
|
482
|
-
token: typedToken,
|
|
483
|
-
})(
|
|
484
|
-
class {
|
|
485
|
-
constructor() {}
|
|
486
|
-
create() {
|
|
487
|
-
return {
|
|
488
|
-
makeFoo: () => 'foo',
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
},
|
|
492
|
-
),
|
|
493
|
-
).toBeConstructibleWith()
|
|
494
|
-
// #4 factory with typed token without schema should fail if not compatible
|
|
369
|
+
// #2 typed token without schema fail if not compatible
|
|
495
370
|
expectTypeOf(
|
|
496
371
|
Injectable({
|
|
497
|
-
type: InjectableType.Factory,
|
|
498
|
-
token: typedToken,
|
|
499
|
-
})(
|
|
500
|
-
// @ts-expect-error factory doesn't implement the token type
|
|
501
|
-
class {
|
|
502
|
-
constructor() {}
|
|
503
|
-
create(ctx: any, arg: z.infer<typeof simpleObjectSchema>) {
|
|
504
|
-
return {
|
|
505
|
-
makeFoo: () => 'foo',
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
},
|
|
509
|
-
),
|
|
510
|
-
).toBeConstructibleWith()
|
|
511
|
-
// #4 factory with typed token fail if not compatible
|
|
512
|
-
expectTypeOf(
|
|
513
|
-
Injectable({
|
|
514
|
-
type: InjectableType.Factory,
|
|
515
372
|
token: typedToken,
|
|
516
373
|
})(
|
|
517
374
|
// @ts-expect-error class doesn't implement the token type
|
|
518
375
|
class {
|
|
519
376
|
constructor() {}
|
|
520
|
-
create() {
|
|
521
|
-
return {
|
|
522
|
-
// makeFoo: () => 'foo',
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
377
|
},
|
|
526
378
|
),
|
|
527
379
|
).toBeConstructibleWith()
|
|
528
|
-
// #4 factory with typed token and schema
|
|
529
|
-
expectTypeOf(
|
|
530
|
-
Injectable({
|
|
531
|
-
type: InjectableType.Factory,
|
|
532
|
-
token: typedObjectToken,
|
|
533
|
-
})(
|
|
534
|
-
class {
|
|
535
|
-
constructor() {}
|
|
536
|
-
create(ctx: any, arg: z.infer<typeof simpleObjectSchema>) {
|
|
537
|
-
return {
|
|
538
|
-
makeFoo: () => 'foo',
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
},
|
|
542
|
-
),
|
|
543
|
-
)
|
|
544
380
|
})
|