@navios/core 1.1.0 → 2.0.0-alpha.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/CHANGELOG.md +53 -11
- package/README.md +67 -220
- package/lib/async-context-6pdlnNLA.d.mts +237 -0
- package/lib/async-context-6pdlnNLA.d.mts.map +1 -0
- package/lib/async-context-Dqk7pE-D.d.cts +237 -0
- package/lib/async-context-Dqk7pE-D.d.cts.map +1 -0
- package/lib/attribute-factory-B3VuYnp7.d.cts +70 -0
- package/lib/attribute-factory-B3VuYnp7.d.cts.map +1 -0
- package/lib/attribute-factory-mKOXoVnV.d.mts +70 -0
- package/lib/attribute-factory-mKOXoVnV.d.mts.map +1 -0
- package/lib/attribute-value-CjX9ZkLq.cjs +172 -0
- package/lib/attribute-value-CjX9ZkLq.cjs.map +1 -0
- package/lib/attribute-value-DMEA255r.mjs +119 -0
- package/lib/attribute-value-DMEA255r.mjs.map +1 -0
- package/lib/basic-execution-context-CeGuhMOz.mjs +246 -0
- package/lib/basic-execution-context-CeGuhMOz.mjs.map +1 -0
- package/lib/basic-execution-context-DSk5MNtc.cjs +281 -0
- package/lib/basic-execution-context-DSk5MNtc.cjs.map +1 -0
- package/lib/execution-context-BPD3FeFb.d.mts +71 -0
- package/lib/execution-context-BPD3FeFb.d.mts.map +1 -0
- package/lib/execution-context-BTJdHhbf.d.cts +71 -0
- package/lib/execution-context-BTJdHhbf.d.cts.map +1 -0
- package/lib/execution-context-token-BXUKB2hf.cjs +112 -0
- package/lib/execution-context-token-BXUKB2hf.cjs.map +1 -0
- package/lib/execution-context-token-CrXmTvk2.mjs +77 -0
- package/lib/execution-context-token-CrXmTvk2.mjs.map +1 -0
- package/lib/index.cjs +1687 -4424
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +838 -3
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +838 -3
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +1573 -4219
- package/lib/index.mjs.map +1 -1
- package/lib/legacy-compat/index.cjs +72 -371
- package/lib/legacy-compat/index.cjs.map +1 -1
- package/lib/legacy-compat/index.d.cts +42 -382
- package/lib/legacy-compat/index.d.cts.map +1 -1
- package/lib/legacy-compat/index.d.mts +41 -382
- package/lib/legacy-compat/index.d.mts.map +1 -1
- package/lib/legacy-compat/index.mjs +51 -345
- package/lib/legacy-compat/index.mjs.map +1 -1
- package/lib/module-Bha7GER_.mjs +135 -0
- package/lib/module-Bha7GER_.mjs.map +1 -0
- package/lib/module-DZewk_2r.cjs +164 -0
- package/lib/module-DZewk_2r.cjs.map +1 -0
- package/lib/module-graph-DCP9MHG0.cjs +147 -0
- package/lib/module-graph-DCP9MHG0.cjs.map +1 -0
- package/lib/module-graph-xvVEykFM.mjs +142 -0
- package/lib/module-graph-xvVEykFM.mjs.map +1 -0
- package/lib/module-metadata-C8S92seK.cjs +95 -0
- package/lib/module-metadata-C8S92seK.cjs.map +1 -0
- package/lib/module-metadata-k3RTt20x.mjs +66 -0
- package/lib/module-metadata-k3RTt20x.mjs.map +1 -0
- package/lib/module-options-Dmse0gii.d.mts +51 -0
- package/lib/module-options-Dmse0gii.d.mts.map +1 -0
- package/lib/module-options-IuDO-p8u.d.cts +51 -0
- package/lib/module-options-IuDO-p8u.d.cts.map +1 -0
- package/lib/testing/index.cjs +96 -414
- package/lib/testing/index.cjs.map +1 -1
- package/lib/testing/index.d.cts +25 -385
- package/lib/testing/index.d.cts.map +1 -1
- package/lib/testing/index.d.mts +24 -385
- package/lib/testing/index.d.mts.map +1 -1
- package/lib/testing/index.mjs +94 -408
- package/lib/testing/index.mjs.map +1 -1
- package/lib/transport.cjs +25 -0
- package/lib/transport.d.cts +190 -0
- package/lib/transport.d.cts.map +1 -0
- package/lib/transport.d.mts +190 -0
- package/lib/transport.d.mts.map +1 -0
- package/lib/transport.mjs +7 -0
- package/package.json +37 -17
- package/docs/README.md +0 -316
- package/docs/adapters.md +0 -308
- package/docs/application-setup.md +0 -524
- package/docs/attributes.md +0 -689
- package/docs/controllers.md +0 -373
- package/docs/endpoints.md +0 -444
- package/docs/exceptions.md +0 -352
- package/docs/guards.md +0 -550
- package/docs/legacy-compat.md +0 -356
- package/docs/modules.md +0 -377
- package/docs/quick-start.md +0 -290
- package/docs/services.md +0 -427
- package/docs/testing.md +0 -827
- package/lib/index-3LcTrbxz.d.mts +0 -3804
- package/lib/index-3LcTrbxz.d.mts.map +0 -1
- package/lib/index-B14SekVb.d.cts +0 -3804
- package/lib/index-B14SekVb.d.cts.map +0 -1
- package/lib/navios.factory-CUrO_p6K.cjs +0 -4538
- package/lib/navios.factory-CUrO_p6K.cjs.map +0 -1
- package/lib/navios.factory-DjUOOVVL.mjs +0 -4151
- package/lib/navios.factory-DjUOOVVL.mjs.map +0 -1
- package/lib/tokens-BEuBMGGX.cjs +0 -193
- package/lib/tokens-BEuBMGGX.cjs.map +0 -1
- package/lib/tokens-COyNGV1I.mjs +0 -97
- package/lib/tokens-COyNGV1I.mjs.map +0 -1
- package/lib/use-guards.decorator-DLmRl2CV.mjs +0 -336
- package/lib/use-guards.decorator-DLmRl2CV.mjs.map +0 -1
- package/lib/use-guards.decorator-DhumFTk3.cjs +0 -444
- package/lib/use-guards.decorator-DhumFTk3.cjs.map +0 -1
- package/project.json +0 -66
- package/src/__tests__/attribute.factory.spec.mts +0 -300
- package/src/__tests__/config.service.spec.mts +0 -43
- package/src/__tests__/console-logger.service.spec.mts +0 -312
- package/src/__tests__/controller-resolver.spec.mts +0 -229
- package/src/__tests__/controller.spec.mts +0 -58
- package/src/__tests__/guard-runner.service.spec.mts +0 -399
- package/src/__tests__/logger.service.spec.mts +0 -147
- package/src/__tests__/responders.spec.mts +0 -340
- package/src/__tests__/testing-module.spec.mts +0 -459
- package/src/__tests__/unit-testing-module.spec.mts +0 -424
- package/src/attribute.factory.mts +0 -325
- package/src/config/config-service.interface.mts +0 -47
- package/src/config/config.provider.mts +0 -69
- package/src/config/config.service.mts +0 -176
- package/src/config/index.mts +0 -5
- package/src/config/types.mts +0 -58
- package/src/config/utils/helpers.mts +0 -23
- package/src/config/utils/index.mts +0 -1
- package/src/decorators/controller.decorator.mts +0 -81
- package/src/decorators/endpoint.decorator.mts +0 -181
- package/src/decorators/header.decorator.mts +0 -38
- package/src/decorators/http-code.decorator.mts +0 -38
- package/src/decorators/index.mts +0 -8
- package/src/decorators/module.decorator.mts +0 -106
- package/src/decorators/multipart.decorator.mts +0 -169
- package/src/decorators/stream.decorator.mts +0 -155
- package/src/decorators/use-guards.decorator.mts +0 -73
- package/src/exceptions/bad-request.exception.mts +0 -28
- package/src/exceptions/conflict.exception.mts +0 -31
- package/src/exceptions/forbidden.exception.mts +0 -30
- package/src/exceptions/http.exception.mts +0 -33
- package/src/exceptions/index.mts +0 -7
- package/src/exceptions/internal-server-error.exception.mts +0 -33
- package/src/exceptions/not-found.exception.mts +0 -33
- package/src/exceptions/unauthorized.exception.mts +0 -30
- package/src/factories/adapter.factory.mts +0 -20
- package/src/factories/endpoint-adapter.factory.mts +0 -21
- package/src/factories/http-adapter.factory.mts +0 -20
- package/src/factories/index.mts +0 -8
- package/src/factories/multipart-adapter.factory.mts +0 -21
- package/src/factories/reply.factory.mts +0 -21
- package/src/factories/request.factory.mts +0 -21
- package/src/factories/stream-adapter.factory.mts +0 -20
- package/src/factories/xml-stream-adapter.factory.mts +0 -20
- package/src/index.mts +0 -16
- package/src/interfaces/abstract-adapter.interface.mts +0 -32
- package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
- package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
- package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
- package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
- package/src/interfaces/adapter-environment.interface.mts +0 -74
- package/src/interfaces/can-activate.mts +0 -38
- package/src/interfaces/http-header.mts +0 -18
- package/src/interfaces/index.mts +0 -14
- package/src/interfaces/navios-module.mts +0 -28
- package/src/interfaces/plugin-context.mts +0 -104
- package/src/interfaces/plugin-stage.mts +0 -62
- package/src/interfaces/plugin.interface.mts +0 -119
- package/src/interfaces/staged-plugin.interface.mts +0 -209
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
- package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
- package/src/legacy-compat/attribute.factory.mts +0 -351
- package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
- package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
- package/src/legacy-compat/decorators/header.decorator.mts +0 -43
- package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
- package/src/legacy-compat/decorators/index.mts +0 -11
- package/src/legacy-compat/decorators/module.decorator.mts +0 -37
- package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
- package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
- package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
- package/src/legacy-compat/index.mts +0 -57
- package/src/logger/README.md +0 -3
- package/src/logger/console-logger.service.mts +0 -714
- package/src/logger/index.mts +0 -6
- package/src/logger/log-levels.mts +0 -21
- package/src/logger/logger-service.interface.mts +0 -42
- package/src/logger/logger.service.mts +0 -113
- package/src/logger/logger.tokens.mts +0 -44
- package/src/logger/utils/cli-colors.util.mts +0 -17
- package/src/logger/utils/filter-log-levelts.util.mts +0 -29
- package/src/logger/utils/index.mts +0 -5
- package/src/logger/utils/is-log-level-enabled.mts +0 -33
- package/src/logger/utils/is-log-level.util.mts +0 -10
- package/src/logger/utils/shared.utils.mts +0 -51
- package/src/metadata/controller.metadata.mts +0 -79
- package/src/metadata/handler.metadata.mts +0 -80
- package/src/metadata/index.mts +0 -4
- package/src/metadata/module.metadata.mts +0 -116
- package/src/metadata/navios-managed.metadata.mts +0 -42
- package/src/navios.application.mts +0 -678
- package/src/navios.environment.mts +0 -42
- package/src/navios.factory.mts +0 -158
- package/src/responders/enums/framework-error.enum.mts +0 -38
- package/src/responders/enums/index.mts +0 -1
- package/src/responders/index.mts +0 -4
- package/src/responders/interfaces/error-responder.interface.mts +0 -42
- package/src/responders/interfaces/error-response.interface.mts +0 -22
- package/src/responders/interfaces/index.mts +0 -3
- package/src/responders/interfaces/problem-details.interface.mts +0 -34
- package/src/responders/services/error-response-producer.service.mts +0 -143
- package/src/responders/services/forbidden-responder.service.mts +0 -77
- package/src/responders/services/index.mts +0 -5
- package/src/responders/services/internal-server-error-responder.service.mts +0 -57
- package/src/responders/services/not-found-responder.service.mts +0 -76
- package/src/responders/services/validation-error-responder.service.mts +0 -78
- package/src/responders/tokens/index.mts +0 -1
- package/src/responders/tokens/responder.tokens.mts +0 -84
- package/src/services/abstract-handler-adapter.service.mts +0 -366
- package/src/services/guard-runner.service.mts +0 -152
- package/src/services/index.mts +0 -4
- package/src/services/instance-resolver.service.mts +0 -177
- package/src/services/module-loader.service.mts +0 -255
- package/src/stores/index.mts +0 -1
- package/src/stores/request-id.store.mts +0 -85
- package/src/testing/index.mts +0 -3
- package/src/testing/testing-module.mts +0 -393
- package/src/testing/unit-testing-module.mts +0 -298
- package/src/tokens/adapter.token.mts +0 -6
- package/src/tokens/endpoint-adapter.token.mts +0 -8
- package/src/tokens/execution-context.token.mts +0 -9
- package/src/tokens/http-adapter.token.mts +0 -8
- package/src/tokens/index.mts +0 -10
- package/src/tokens/multipart-adapter.token.mts +0 -8
- package/src/tokens/navios-options.token.mts +0 -6
- package/src/tokens/reply.token.mts +0 -3
- package/src/tokens/request.token.mts +0 -3
- package/src/tokens/stream-adapter.token.mts +0 -8
- package/src/tokens/xml-stream-adapter.token.mts +0 -8
- package/src/utils/adapter-supports.util.mts +0 -47
- package/src/utils/define-plugin.mts +0 -251
- package/src/utils/index.mts +0 -2
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -8
- package/tsconfig.spec.json +0 -15
- package/tsdown.config.mts +0 -35
- package/vitest.config.mts +0 -15
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Container,
|
|
3
|
-
getInjectableToken,
|
|
4
|
-
globalRegistry,
|
|
5
|
-
inject,
|
|
6
|
-
Injectable,
|
|
7
|
-
InjectableScope,
|
|
8
|
-
Registry,
|
|
9
|
-
} from '@navios/di'
|
|
10
|
-
|
|
11
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
12
|
-
|
|
13
|
-
import { InstanceResolverService } from '../services/instance-resolver.service.mjs'
|
|
14
|
-
|
|
15
|
-
function createTestSetup() {
|
|
16
|
-
const registry = new Registry(globalRegistry)
|
|
17
|
-
const container = new Container(registry)
|
|
18
|
-
|
|
19
|
-
return { registry, container }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
describe('InstanceResolverService', () => {
|
|
23
|
-
let container: Container
|
|
24
|
-
let registry: Registry
|
|
25
|
-
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
const setup = createTestSetup()
|
|
28
|
-
registry = setup.registry
|
|
29
|
-
container = setup.container
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
afterEach(async () => {
|
|
33
|
-
await container.dispose()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
describe('resolve', () => {
|
|
37
|
-
it('should cache singleton controller without request-scoped dependencies', async () => {
|
|
38
|
-
@Injectable({ registry })
|
|
39
|
-
class SimpleService {
|
|
40
|
-
value = 'simple'
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
@Injectable({ registry })
|
|
44
|
-
class SingletonController {
|
|
45
|
-
private service = inject(SimpleService)
|
|
46
|
-
|
|
47
|
-
getValue() {
|
|
48
|
-
return this.service.value
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const resolver = await container.get(InstanceResolverService)
|
|
53
|
-
const resolution = await resolver.resolve(SingletonController)
|
|
54
|
-
|
|
55
|
-
expect(resolution.cached).toBe(true)
|
|
56
|
-
expect(resolution.instance).toBeInstanceOf(SingletonController)
|
|
57
|
-
expect((resolution.instance as SingletonController).getValue()).toBe(
|
|
58
|
-
'simple',
|
|
59
|
-
)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('should not cache controller with request-scoped dependencies', async () => {
|
|
63
|
-
@Injectable({ scope: InjectableScope.Request, registry })
|
|
64
|
-
class RequestScopedService {
|
|
65
|
-
id = Math.random().toString(36).substring(7)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
@Injectable({ registry })
|
|
69
|
-
class ControllerWithRequestDep {
|
|
70
|
-
private service = inject(RequestScopedService)
|
|
71
|
-
|
|
72
|
-
getServiceId() {
|
|
73
|
-
return this.service.id
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const resolver = await container.get(InstanceResolverService)
|
|
78
|
-
const resolution = await resolver.resolve(ControllerWithRequestDep)
|
|
79
|
-
|
|
80
|
-
expect(resolution.cached).toBe(false)
|
|
81
|
-
expect(resolution.instance).toBeNull()
|
|
82
|
-
expect(typeof resolution.resolve).toBe('function')
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
it('should update controller scope to Request when it has request-scoped dependencies', async () => {
|
|
86
|
-
@Injectable({ scope: InjectableScope.Request, registry })
|
|
87
|
-
class RequestScopedService {
|
|
88
|
-
id = Math.random().toString(36).substring(7)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
@Injectable({ registry })
|
|
92
|
-
class ControllerWithRequestDep {
|
|
93
|
-
private service = inject(RequestScopedService)
|
|
94
|
-
|
|
95
|
-
getServiceId() {
|
|
96
|
-
return this.service.id
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const resolver = await container.get(InstanceResolverService)
|
|
101
|
-
await resolver.resolve(ControllerWithRequestDep)
|
|
102
|
-
|
|
103
|
-
// Check that the controller's scope was updated
|
|
104
|
-
const token = container
|
|
105
|
-
.getRegistry()
|
|
106
|
-
.get(getInjectableToken(ControllerWithRequestDep))
|
|
107
|
-
expect(token.scope).toBe(InjectableScope.Request)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
it('should resolve different instances per request when controller has request-scoped deps', async () => {
|
|
111
|
-
let serviceInstanceCount = 0
|
|
112
|
-
|
|
113
|
-
@Injectable({ scope: InjectableScope.Request, registry })
|
|
114
|
-
class RequestScopedService {
|
|
115
|
-
id = ++serviceInstanceCount
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
@Injectable({ registry })
|
|
119
|
-
class ControllerWithRequestDep {
|
|
120
|
-
private service = inject(RequestScopedService)
|
|
121
|
-
|
|
122
|
-
getServiceId() {
|
|
123
|
-
return this.service.id
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const resolver = await container.get(InstanceResolverService)
|
|
128
|
-
const resolution = await resolver.resolve(ControllerWithRequestDep)
|
|
129
|
-
|
|
130
|
-
expect(resolution.cached).toBe(false)
|
|
131
|
-
|
|
132
|
-
// Request 1
|
|
133
|
-
const scoped1 = container.beginRequest('request-1')
|
|
134
|
-
const controller1 = await resolution.resolve(scoped1)
|
|
135
|
-
const id1 = (controller1 as ControllerWithRequestDep).getServiceId()
|
|
136
|
-
console.log('id1', id1)
|
|
137
|
-
|
|
138
|
-
// Request 2
|
|
139
|
-
const scoped2 = container.beginRequest('request-2')
|
|
140
|
-
const controller2 = await resolution.resolve(scoped2)
|
|
141
|
-
const id2 = (controller2 as ControllerWithRequestDep).getServiceId()
|
|
142
|
-
|
|
143
|
-
expect(id1).toBe(1)
|
|
144
|
-
expect(id2).toBe(2)
|
|
145
|
-
expect(controller1).not.toBe(controller2)
|
|
146
|
-
|
|
147
|
-
await scoped1.endRequest()
|
|
148
|
-
await scoped2.endRequest()
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
it('should handle parallel requests with isolated instances', async () => {
|
|
152
|
-
let serviceInstanceCount = 0
|
|
153
|
-
|
|
154
|
-
@Injectable({ scope: InjectableScope.Request, registry })
|
|
155
|
-
class RequestTrackerService {
|
|
156
|
-
id = ++serviceInstanceCount
|
|
157
|
-
data: Record<string, any> = {}
|
|
158
|
-
|
|
159
|
-
addData(key: string, value: any) {
|
|
160
|
-
this.data[key] = value
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
getData(key: string) {
|
|
164
|
-
return this.data[key]
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
@Injectable({ registry })
|
|
169
|
-
class ControllerWithTracker {
|
|
170
|
-
private tracker = inject(RequestTrackerService)
|
|
171
|
-
|
|
172
|
-
async handleRequest(data: string) {
|
|
173
|
-
this.tracker.addData('input', data)
|
|
174
|
-
// Simulate async work
|
|
175
|
-
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
176
|
-
return {
|
|
177
|
-
id: this.tracker.id,
|
|
178
|
-
data: this.tracker.getData('input'),
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const resolver = await container.get(InstanceResolverService)
|
|
184
|
-
const resolution = await resolver.resolve(ControllerWithTracker)
|
|
185
|
-
|
|
186
|
-
expect(resolution.cached).toBe(false)
|
|
187
|
-
|
|
188
|
-
// Create 5 parallel requests
|
|
189
|
-
const requests = ['req1', 'req2', 'req3', 'req4', 'req5'].map(
|
|
190
|
-
async (data, i) => {
|
|
191
|
-
const scoped = container.beginRequest(`request-${i}`)
|
|
192
|
-
const controller = (await resolution.resolve(
|
|
193
|
-
scoped,
|
|
194
|
-
)) as ControllerWithTracker
|
|
195
|
-
const result = await controller.handleRequest(data)
|
|
196
|
-
await scoped.endRequest()
|
|
197
|
-
return result
|
|
198
|
-
},
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
const results = await Promise.all(requests)
|
|
202
|
-
|
|
203
|
-
// Verify each request got its own unique ID
|
|
204
|
-
const ids = results.map((r) => r.id)
|
|
205
|
-
const uniqueIds = new Set(ids)
|
|
206
|
-
expect(uniqueIds.size).toBe(5)
|
|
207
|
-
|
|
208
|
-
// Verify each request returned its own data
|
|
209
|
-
const expectedData = ['req1', 'req2', 'req3', 'req4', 'req5']
|
|
210
|
-
const actualData = results.map((r) => r.data)
|
|
211
|
-
expect(actualData).toEqual(expect.arrayContaining(expectedData))
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
it('should return same cached instance for singleton controllers', async () => {
|
|
215
|
-
@Injectable({ registry })
|
|
216
|
-
class SingletonController {
|
|
217
|
-
id = Math.random()
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const resolver = await container.get(InstanceResolverService)
|
|
221
|
-
const resolution1 = await resolver.resolve(SingletonController)
|
|
222
|
-
const resolution2 = await resolver.resolve(SingletonController)
|
|
223
|
-
|
|
224
|
-
expect(resolution1.cached).toBe(true)
|
|
225
|
-
expect(resolution2.cached).toBe(true)
|
|
226
|
-
expect(resolution1.instance).toBe(resolution2.instance)
|
|
227
|
-
})
|
|
228
|
-
})
|
|
229
|
-
})
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { builder } from '@navios/builder'
|
|
2
|
-
|
|
3
|
-
import { describe, expect, it } from 'vitest'
|
|
4
|
-
import { z } from 'zod/v4'
|
|
5
|
-
|
|
6
|
-
import type { EndpointParams } from '../decorators/index.mjs'
|
|
7
|
-
|
|
8
|
-
import { Controller, Endpoint } from '../decorators/index.mjs'
|
|
9
|
-
import { extractControllerMetadata } from '../index.mjs'
|
|
10
|
-
|
|
11
|
-
describe('Controller decorator', () => {
|
|
12
|
-
const api = builder({
|
|
13
|
-
useDiscriminatorResponse: true,
|
|
14
|
-
})
|
|
15
|
-
const endpoint = api.declareEndpoint({
|
|
16
|
-
url: 'test/$test/foo' as const,
|
|
17
|
-
method: 'GET',
|
|
18
|
-
querySchema: z.object({
|
|
19
|
-
test: z.string(),
|
|
20
|
-
}),
|
|
21
|
-
responseSchema: z.object({
|
|
22
|
-
foo: z.string(),
|
|
23
|
-
}),
|
|
24
|
-
})
|
|
25
|
-
const endpoint2 = api.declareEndpoint({
|
|
26
|
-
url: 'test/$test/foo',
|
|
27
|
-
method: 'POST',
|
|
28
|
-
requestSchema: z.object({
|
|
29
|
-
test: z.string(),
|
|
30
|
-
}),
|
|
31
|
-
responseSchema: z.object({
|
|
32
|
-
foo: z.string(),
|
|
33
|
-
}),
|
|
34
|
-
})
|
|
35
|
-
it('should work with class', async () => {
|
|
36
|
-
@Controller()
|
|
37
|
-
class Test {
|
|
38
|
-
@Endpoint(endpoint)
|
|
39
|
-
async getBar() {
|
|
40
|
-
return {
|
|
41
|
-
foo: 'bar',
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
@Endpoint(endpoint2)
|
|
46
|
-
async postBar(params: EndpointParams<typeof endpoint2>) {
|
|
47
|
-
return {
|
|
48
|
-
foo: params.data.test,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const metadata = extractControllerMetadata(Test)
|
|
54
|
-
expect(metadata).toBeDefined()
|
|
55
|
-
expect(metadata.endpoints).toBeInstanceOf(Set)
|
|
56
|
-
expect(metadata.endpoints.size).toBe(2)
|
|
57
|
-
})
|
|
58
|
-
})
|
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
import type { ScopedContainer } from '@navios/di'
|
|
2
|
-
|
|
3
|
-
import { Container, Injectable, InjectionToken } from '@navios/di'
|
|
4
|
-
|
|
5
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import type {
|
|
8
|
-
AbstractExecutionContext,
|
|
9
|
-
CanActivate,
|
|
10
|
-
} from '../interfaces/index.mjs'
|
|
11
|
-
import type {
|
|
12
|
-
ControllerMetadata,
|
|
13
|
-
HandlerMetadata,
|
|
14
|
-
ModuleMetadata,
|
|
15
|
-
} from '../metadata/index.mjs'
|
|
16
|
-
|
|
17
|
-
import { HttpException } from '../exceptions/index.mjs'
|
|
18
|
-
import { LoggerOutput } from '../logger/logger.tokens.mjs'
|
|
19
|
-
import {
|
|
20
|
-
ForbiddenResponderToken,
|
|
21
|
-
InternalServerErrorResponderToken,
|
|
22
|
-
NotFoundResponderToken,
|
|
23
|
-
ValidationErrorResponderToken,
|
|
24
|
-
} from '../responders/tokens/responder.tokens.mjs'
|
|
25
|
-
import { GuardRunnerService } from '../services/guard-runner.service.mjs'
|
|
26
|
-
|
|
27
|
-
// Mock responders
|
|
28
|
-
const createMockResponder = (statusCode: number, message: string) => ({
|
|
29
|
-
getResponse: vi.fn().mockReturnValue({
|
|
30
|
-
statusCode,
|
|
31
|
-
payload: { message },
|
|
32
|
-
headers: { 'content-type': 'application/problem+json' },
|
|
33
|
-
}),
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
// Mock logger output
|
|
37
|
-
const mockLoggerOutput = {
|
|
38
|
-
log: vi.fn(),
|
|
39
|
-
error: vi.fn(),
|
|
40
|
-
warn: vi.fn(),
|
|
41
|
-
debug: vi.fn(),
|
|
42
|
-
verbose: vi.fn(),
|
|
43
|
-
fatal: vi.fn(),
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Mock execution context
|
|
47
|
-
const createMockExecutionContext = (): AbstractExecutionContext => {
|
|
48
|
-
const mockReply = {
|
|
49
|
-
status: vi.fn().mockReturnThis(),
|
|
50
|
-
send: vi.fn().mockReturnThis(),
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
getRequest: vi.fn().mockReturnValue({}),
|
|
54
|
-
getReply: vi.fn().mockReturnValue(mockReply),
|
|
55
|
-
getHandler: vi.fn(),
|
|
56
|
-
getModule: vi.fn(),
|
|
57
|
-
getController: vi.fn(),
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Mock guard
|
|
62
|
-
const createMockGuard = (
|
|
63
|
-
canActivateResult: boolean | Promise<boolean>,
|
|
64
|
-
): CanActivate => ({
|
|
65
|
-
canActivate: vi.fn().mockImplementation(() => canActivateResult),
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
describe('GuardRunnerService', () => {
|
|
69
|
-
let container: Container
|
|
70
|
-
let mockForbiddenResponder: ReturnType<typeof createMockResponder>
|
|
71
|
-
let mockInternalErrorResponder: ReturnType<typeof createMockResponder>
|
|
72
|
-
let mockNotFoundResponder: ReturnType<typeof createMockResponder>
|
|
73
|
-
let mockValidationErrorResponder: ReturnType<typeof createMockResponder>
|
|
74
|
-
|
|
75
|
-
beforeEach(() => {
|
|
76
|
-
container = new Container()
|
|
77
|
-
vi.clearAllMocks()
|
|
78
|
-
|
|
79
|
-
// Create mock responders
|
|
80
|
-
mockForbiddenResponder = createMockResponder(403, 'Forbidden')
|
|
81
|
-
mockInternalErrorResponder = createMockResponder(
|
|
82
|
-
500,
|
|
83
|
-
'Internal Server Error',
|
|
84
|
-
)
|
|
85
|
-
mockNotFoundResponder = createMockResponder(404, 'Not Found')
|
|
86
|
-
mockValidationErrorResponder = createMockResponder(400, 'Validation Error')
|
|
87
|
-
|
|
88
|
-
// Register all required dependencies
|
|
89
|
-
container.addInstance(ForbiddenResponderToken, mockForbiddenResponder)
|
|
90
|
-
container.addInstance(
|
|
91
|
-
InternalServerErrorResponderToken,
|
|
92
|
-
mockInternalErrorResponder,
|
|
93
|
-
)
|
|
94
|
-
container.addInstance(NotFoundResponderToken, mockNotFoundResponder)
|
|
95
|
-
container.addInstance(
|
|
96
|
-
ValidationErrorResponderToken,
|
|
97
|
-
mockValidationErrorResponder,
|
|
98
|
-
)
|
|
99
|
-
container.addInstance(LoggerOutput, mockLoggerOutput)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
afterEach(async () => {
|
|
103
|
-
await container.dispose()
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
describe('runGuardsStatic', () => {
|
|
107
|
-
it('should return true when all guards pass', async () => {
|
|
108
|
-
const service = await container.get(GuardRunnerService)
|
|
109
|
-
const guards = [createMockGuard(true), createMockGuard(true)]
|
|
110
|
-
const context = createMockExecutionContext()
|
|
111
|
-
|
|
112
|
-
const result = await service.runGuardsStatic(guards, context)
|
|
113
|
-
|
|
114
|
-
expect(result).toBe(true)
|
|
115
|
-
expect(guards[0].canActivate).toHaveBeenCalledWith(context)
|
|
116
|
-
expect(guards[1].canActivate).toHaveBeenCalledWith(context)
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
it('should return false when a guard returns false', async () => {
|
|
120
|
-
const service = await container.get(GuardRunnerService)
|
|
121
|
-
const guards = [createMockGuard(true), createMockGuard(false)]
|
|
122
|
-
const context = createMockExecutionContext()
|
|
123
|
-
|
|
124
|
-
const result = await service.runGuardsStatic(guards, context)
|
|
125
|
-
|
|
126
|
-
expect(result).toBe(false)
|
|
127
|
-
expect(context.getReply().status).toHaveBeenCalledWith(403)
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
it('should stop execution when a guard fails', async () => {
|
|
131
|
-
const service = await container.get(GuardRunnerService)
|
|
132
|
-
const firstGuard = createMockGuard(false)
|
|
133
|
-
const secondGuard = createMockGuard(true)
|
|
134
|
-
const guards = [firstGuard, secondGuard]
|
|
135
|
-
const context = createMockExecutionContext()
|
|
136
|
-
|
|
137
|
-
await service.runGuardsStatic(guards, context)
|
|
138
|
-
|
|
139
|
-
expect(firstGuard.canActivate).toHaveBeenCalled()
|
|
140
|
-
expect(secondGuard.canActivate).not.toHaveBeenCalled()
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
it('should handle HttpException from guard', async () => {
|
|
144
|
-
const service = await container.get(GuardRunnerService)
|
|
145
|
-
const httpException = new HttpException(401, 'Unauthorized')
|
|
146
|
-
const guard = {
|
|
147
|
-
canActivate: vi.fn().mockRejectedValue(httpException),
|
|
148
|
-
}
|
|
149
|
-
const context = createMockExecutionContext()
|
|
150
|
-
|
|
151
|
-
const result = await service.runGuardsStatic([guard], context)
|
|
152
|
-
|
|
153
|
-
expect(result).toBe(false)
|
|
154
|
-
expect(context.getReply().status).toHaveBeenCalledWith(401)
|
|
155
|
-
expect(context.getReply().send).toHaveBeenCalledWith('Unauthorized')
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('should handle unknown errors from guard', async () => {
|
|
159
|
-
const service = await container.get(GuardRunnerService)
|
|
160
|
-
const guard = {
|
|
161
|
-
canActivate: vi.fn().mockRejectedValue(new Error('Unknown error')),
|
|
162
|
-
}
|
|
163
|
-
const context = createMockExecutionContext()
|
|
164
|
-
|
|
165
|
-
const result = await service.runGuardsStatic([guard], context)
|
|
166
|
-
|
|
167
|
-
expect(result).toBe(false)
|
|
168
|
-
expect(mockLoggerOutput.error).toHaveBeenCalled()
|
|
169
|
-
expect(context.getReply().status).toHaveBeenCalledWith(500)
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('should handle async guards', async () => {
|
|
173
|
-
const service = await container.get(GuardRunnerService)
|
|
174
|
-
const asyncGuard = createMockGuard(Promise.resolve(true))
|
|
175
|
-
const context = createMockExecutionContext()
|
|
176
|
-
|
|
177
|
-
const result = await service.runGuardsStatic([asyncGuard], context)
|
|
178
|
-
|
|
179
|
-
expect(result).toBe(true)
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
it('should work with empty guard array', async () => {
|
|
183
|
-
const service = await container.get(GuardRunnerService)
|
|
184
|
-
const context = createMockExecutionContext()
|
|
185
|
-
|
|
186
|
-
const result = await service.runGuardsStatic([], context)
|
|
187
|
-
|
|
188
|
-
expect(result).toBe(true)
|
|
189
|
-
})
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
describe('runGuards', () => {
|
|
193
|
-
it('should resolve guards from scoped container', async () => {
|
|
194
|
-
@Injectable()
|
|
195
|
-
class TestGuard implements CanActivate {
|
|
196
|
-
canActivate = vi.fn().mockReturnValue(true)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const service = await container.get(GuardRunnerService)
|
|
200
|
-
const testGuardInstance = new TestGuard()
|
|
201
|
-
|
|
202
|
-
const mockScopedContainer = {
|
|
203
|
-
get: vi.fn().mockResolvedValue(testGuardInstance),
|
|
204
|
-
} as unknown as ScopedContainer
|
|
205
|
-
|
|
206
|
-
const guards = new Set([TestGuard])
|
|
207
|
-
const context = createMockExecutionContext()
|
|
208
|
-
|
|
209
|
-
const result = await service.runGuards(
|
|
210
|
-
guards,
|
|
211
|
-
context,
|
|
212
|
-
mockScopedContainer,
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
expect(result).toBe(true)
|
|
216
|
-
expect(mockScopedContainer.get).toHaveBeenCalled()
|
|
217
|
-
expect(testGuardInstance.canActivate).toHaveBeenCalledWith(context)
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
it('should throw error for guard without canActivate', async () => {
|
|
221
|
-
@Injectable()
|
|
222
|
-
class InvalidGuard {
|
|
223
|
-
// Missing canActivate
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const service = await container.get(GuardRunnerService)
|
|
227
|
-
const mockScopedContainer = {
|
|
228
|
-
get: vi.fn().mockResolvedValue(new InvalidGuard()),
|
|
229
|
-
} as unknown as ScopedContainer
|
|
230
|
-
|
|
231
|
-
const guards = new Set([InvalidGuard as any])
|
|
232
|
-
const context = createMockExecutionContext()
|
|
233
|
-
|
|
234
|
-
await expect(
|
|
235
|
-
service.runGuards(guards, context, mockScopedContainer),
|
|
236
|
-
).rejects.toThrow('does not implement canActivate')
|
|
237
|
-
})
|
|
238
|
-
|
|
239
|
-
it('should reverse guard order (module -> controller -> endpoint)', async () => {
|
|
240
|
-
const callOrder: string[] = []
|
|
241
|
-
|
|
242
|
-
@Injectable()
|
|
243
|
-
class Guard1 implements CanActivate {
|
|
244
|
-
canActivate() {
|
|
245
|
-
callOrder.push('guard1')
|
|
246
|
-
return true
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
@Injectable()
|
|
251
|
-
class Guard2 implements CanActivate {
|
|
252
|
-
canActivate() {
|
|
253
|
-
callOrder.push('guard2')
|
|
254
|
-
return true
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
@Injectable()
|
|
259
|
-
class Guard3 implements CanActivate {
|
|
260
|
-
canActivate() {
|
|
261
|
-
callOrder.push('guard3')
|
|
262
|
-
return true
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const service = await container.get(GuardRunnerService)
|
|
267
|
-
|
|
268
|
-
const mockScopedContainer = {
|
|
269
|
-
get: vi.fn().mockImplementation((token) => {
|
|
270
|
-
if (token === Guard1) return new Guard1()
|
|
271
|
-
if (token === Guard2) return new Guard2()
|
|
272
|
-
if (token === Guard3) return new Guard3()
|
|
273
|
-
}),
|
|
274
|
-
} as unknown as ScopedContainer
|
|
275
|
-
|
|
276
|
-
// Order in Set: Guard1, Guard2, Guard3
|
|
277
|
-
// Should execute in reverse: Guard3, Guard2, Guard1
|
|
278
|
-
const guards = new Set([Guard1, Guard2, Guard3])
|
|
279
|
-
const context = createMockExecutionContext()
|
|
280
|
-
|
|
281
|
-
await service.runGuards(guards, context, mockScopedContainer)
|
|
282
|
-
|
|
283
|
-
expect(callOrder).toEqual(['guard3', 'guard2', 'guard1'])
|
|
284
|
-
})
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
describe('makeContext', () => {
|
|
288
|
-
it('should merge guards from module, controller, and endpoint', async () => {
|
|
289
|
-
const service = await container.get(GuardRunnerService)
|
|
290
|
-
|
|
291
|
-
class ModuleGuard {}
|
|
292
|
-
class ControllerGuard {}
|
|
293
|
-
class EndpointGuard {}
|
|
294
|
-
|
|
295
|
-
const moduleMetadata = {
|
|
296
|
-
guards: new Set([ModuleGuard]),
|
|
297
|
-
} as unknown as ModuleMetadata
|
|
298
|
-
|
|
299
|
-
const controllerMetadata = {
|
|
300
|
-
guards: new Set([ControllerGuard]),
|
|
301
|
-
} as unknown as ControllerMetadata
|
|
302
|
-
|
|
303
|
-
const endpointMetadata = {
|
|
304
|
-
guards: new Set([EndpointGuard]),
|
|
305
|
-
} as unknown as HandlerMetadata<any>
|
|
306
|
-
|
|
307
|
-
const result = service.makeContext(
|
|
308
|
-
moduleMetadata,
|
|
309
|
-
controllerMetadata,
|
|
310
|
-
endpointMetadata,
|
|
311
|
-
)
|
|
312
|
-
|
|
313
|
-
expect(result.size).toBe(3)
|
|
314
|
-
expect(result.has(ModuleGuard as any)).toBe(true)
|
|
315
|
-
expect(result.has(ControllerGuard as any)).toBe(true)
|
|
316
|
-
expect(result.has(EndpointGuard as any)).toBe(true)
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
it('should handle empty guards at each level', async () => {
|
|
320
|
-
const service = await container.get(GuardRunnerService)
|
|
321
|
-
|
|
322
|
-
const moduleMetadata = {
|
|
323
|
-
guards: new Set(),
|
|
324
|
-
} as unknown as ModuleMetadata
|
|
325
|
-
|
|
326
|
-
const controllerMetadata = {
|
|
327
|
-
guards: new Set(),
|
|
328
|
-
} as unknown as ControllerMetadata
|
|
329
|
-
|
|
330
|
-
const endpointMetadata = {
|
|
331
|
-
guards: new Set(),
|
|
332
|
-
} as unknown as HandlerMetadata<any>
|
|
333
|
-
|
|
334
|
-
const result = service.makeContext(
|
|
335
|
-
moduleMetadata,
|
|
336
|
-
controllerMetadata,
|
|
337
|
-
endpointMetadata,
|
|
338
|
-
)
|
|
339
|
-
|
|
340
|
-
expect(result.size).toBe(0)
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
it('should deduplicate guards', async () => {
|
|
344
|
-
const service = await container.get(GuardRunnerService)
|
|
345
|
-
|
|
346
|
-
class SharedGuard {}
|
|
347
|
-
|
|
348
|
-
const moduleMetadata = {
|
|
349
|
-
guards: new Set([SharedGuard]),
|
|
350
|
-
} as unknown as ModuleMetadata
|
|
351
|
-
|
|
352
|
-
const controllerMetadata = {
|
|
353
|
-
guards: new Set([SharedGuard]), // Same guard
|
|
354
|
-
} as unknown as ControllerMetadata
|
|
355
|
-
|
|
356
|
-
const endpointMetadata = {
|
|
357
|
-
guards: new Set([SharedGuard]), // Same guard
|
|
358
|
-
} as unknown as HandlerMetadata<any>
|
|
359
|
-
|
|
360
|
-
const result = service.makeContext(
|
|
361
|
-
moduleMetadata,
|
|
362
|
-
controllerMetadata,
|
|
363
|
-
endpointMetadata,
|
|
364
|
-
)
|
|
365
|
-
|
|
366
|
-
// Set deduplicates automatically
|
|
367
|
-
expect(result.size).toBe(1)
|
|
368
|
-
})
|
|
369
|
-
|
|
370
|
-
it('should handle injection tokens as guards', async () => {
|
|
371
|
-
const service = await container.get(GuardRunnerService)
|
|
372
|
-
|
|
373
|
-
const GuardToken = InjectionToken.create<CanActivate>(
|
|
374
|
-
Symbol.for('GuardToken'),
|
|
375
|
-
)
|
|
376
|
-
|
|
377
|
-
const moduleMetadata = {
|
|
378
|
-
guards: new Set([GuardToken]),
|
|
379
|
-
} as unknown as ModuleMetadata
|
|
380
|
-
|
|
381
|
-
const controllerMetadata = {
|
|
382
|
-
guards: new Set(),
|
|
383
|
-
} as unknown as ControllerMetadata
|
|
384
|
-
|
|
385
|
-
const endpointMetadata = {
|
|
386
|
-
guards: new Set(),
|
|
387
|
-
} as unknown as HandlerMetadata<any>
|
|
388
|
-
|
|
389
|
-
const result = service.makeContext(
|
|
390
|
-
moduleMetadata,
|
|
391
|
-
controllerMetadata,
|
|
392
|
-
endpointMetadata,
|
|
393
|
-
)
|
|
394
|
-
|
|
395
|
-
expect(result.size).toBe(1)
|
|
396
|
-
expect(result.has(GuardToken)).toBe(true)
|
|
397
|
-
})
|
|
398
|
-
})
|
|
399
|
-
})
|