@navios/core 1.0.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 +97 -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 -4409
- 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-C0Sg16Eb.d.cts +0 -3365
- package/lib/index-C0Sg16Eb.d.cts.map +0 -1
- package/lib/index-DySQ6Dpd.d.mts +0 -3365
- package/lib/index-DySQ6Dpd.d.mts.map +0 -1
- package/lib/navios.factory-CO5MB_OK.cjs +0 -4125
- package/lib/navios.factory-CO5MB_OK.cjs.map +0 -1
- package/lib/navios.factory-D6Y94P9B.mjs +0 -3828
- package/lib/navios.factory-D6Y94P9B.mjs.map +0 -1
- package/lib/tokens-4J9sredA.mjs +0 -100
- package/lib/tokens-4J9sredA.mjs.map +0 -1
- package/lib/tokens-CWw9kyeD.cjs +0 -196
- package/lib/tokens-CWw9kyeD.cjs.map +0 -1
- package/lib/use-guards.decorator-BecoQSmE.mjs +0 -339
- package/lib/use-guards.decorator-BecoQSmE.mjs.map +0 -1
- package/lib/use-guards.decorator-C4ml9XaT.cjs +0 -447
- package/lib/use-guards.decorator-C4ml9XaT.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 -11
- package/src/interfaces/navios-module.mts +0 -28
- package/src/interfaces/plugin.interface.mts +0 -139
- 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 -72
- package/src/metadata/handler.metadata.mts +0 -80
- package/src/metadata/index.mts +0 -4
- package/src/metadata/module.metadata.mts +0 -108
- package/src/metadata/navios-managed.metadata.mts +0 -42
- package/src/navios.application.mts +0 -484
- 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 -252
- 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/index.mts +0 -1
- 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,484 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ClassType,
|
|
3
|
-
ClassTypeWithArgument,
|
|
4
|
-
ClassTypeWithInstance,
|
|
5
|
-
InjectionTokenSchemaType,
|
|
6
|
-
Registry,
|
|
7
|
-
} from '@navios/di'
|
|
8
|
-
import type { z } from 'zod/v4'
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
BoundInjectionToken,
|
|
12
|
-
Container,
|
|
13
|
-
FactoryInjectionToken,
|
|
14
|
-
inject,
|
|
15
|
-
Injectable,
|
|
16
|
-
InjectionToken,
|
|
17
|
-
} from '@navios/di'
|
|
18
|
-
|
|
19
|
-
import type {
|
|
20
|
-
AbstractAdapterInterface,
|
|
21
|
-
AdapterEnvironment,
|
|
22
|
-
DefaultAdapterEnvironment,
|
|
23
|
-
HttpAdapterEnvironment,
|
|
24
|
-
NaviosModule,
|
|
25
|
-
PluginContext,
|
|
26
|
-
PluginDefinition,
|
|
27
|
-
} from './interfaces/index.mjs'
|
|
28
|
-
import type { LoggerService, LogLevel } from './logger/index.mjs'
|
|
29
|
-
import type { NaviosEnvironmentOptions } from './navios.environment.mjs'
|
|
30
|
-
|
|
31
|
-
import { Logger } from './logger/index.mjs'
|
|
32
|
-
import { NaviosEnvironment } from './navios.environment.mjs'
|
|
33
|
-
import { ModuleLoaderService } from './services/index.mjs'
|
|
34
|
-
import { AdapterToken } from './tokens/index.mjs'
|
|
35
|
-
import { assertAdapterSupports } from './utils/index.mjs'
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Options for configuring the Navios application context.
|
|
39
|
-
* These options control the application configuration.
|
|
40
|
-
*/
|
|
41
|
-
export interface NaviosApplicationOptions {
|
|
42
|
-
/**
|
|
43
|
-
* Specifies the logger to use. Pass `false` to turn off logging.
|
|
44
|
-
*
|
|
45
|
-
* - `LoggerService` instance: Use a custom logger implementation
|
|
46
|
-
* - `LogLevel[]`: Enable specific log levels (e.g., ['error', 'warn', 'log'])
|
|
47
|
-
* - `false`: Disable logging completely
|
|
48
|
-
*/
|
|
49
|
-
logger?: LoggerService | LogLevel[] | false
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Specifies a custom registry to use. Useful for testing.
|
|
53
|
-
* If not provided, a new Registry will be created.
|
|
54
|
-
*/
|
|
55
|
-
registry?: Registry
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Specifies a custom container to use. Useful for testing.
|
|
59
|
-
* If not provided, a new Container will be created.
|
|
60
|
-
*/
|
|
61
|
-
container?: Container
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Adapter environment(s) to use for the application.
|
|
65
|
-
* Can be a single adapter or an array of adapters.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* adapter: defineFastifyEnvironment()
|
|
70
|
-
* // or
|
|
71
|
-
* adapter: [defineFastifyEnvironment()]
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
adapter: NaviosEnvironmentOptions | NaviosEnvironmentOptions[]
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Whether to validate response schemas.
|
|
78
|
-
* When `false`, response schema validation is skipped for better performance.
|
|
79
|
-
* @default true
|
|
80
|
-
*/
|
|
81
|
-
validateResponses?: boolean
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Whether to enable request ID propagation via AsyncLocalStorage.
|
|
85
|
-
* When `true`, request IDs are available via `getRequestId()` throughout the request.
|
|
86
|
-
* @default false
|
|
87
|
-
*/
|
|
88
|
-
enableRequestId?: boolean
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Main application class for Navios.
|
|
93
|
-
*
|
|
94
|
-
* This class represents a Navios application instance and provides methods
|
|
95
|
-
* for initializing, configuring, and managing the application lifecycle.
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```typescript
|
|
99
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
100
|
-
* adapter: defineFastifyEnvironment(),
|
|
101
|
-
* })
|
|
102
|
-
*
|
|
103
|
-
* app.setGlobalPrefix('/api')
|
|
104
|
-
* app.enableCors({ origin: ['http://localhost:3000'] })
|
|
105
|
-
* await app.init()
|
|
106
|
-
* await app.listen({ port: 3000, host: '0.0.0.0' })
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
@Injectable()
|
|
110
|
-
export class NaviosApplication<
|
|
111
|
-
Environment extends AdapterEnvironment = DefaultAdapterEnvironment,
|
|
112
|
-
> {
|
|
113
|
-
private environment = inject(NaviosEnvironment)
|
|
114
|
-
private moduleLoader = inject(ModuleLoaderService)
|
|
115
|
-
private adapter: Environment['adapter'] | null = null
|
|
116
|
-
private logger = inject(Logger, {
|
|
117
|
-
context: NaviosApplication.name,
|
|
118
|
-
})
|
|
119
|
-
protected container = inject(Container)
|
|
120
|
-
|
|
121
|
-
private appModule: ClassTypeWithInstance<NaviosModule> | null = null
|
|
122
|
-
private options: NaviosApplicationOptions = {
|
|
123
|
-
adapter: [],
|
|
124
|
-
}
|
|
125
|
-
private plugins: PluginDefinition<any, any>[] = []
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Indicates whether the application has been initialized.
|
|
129
|
-
* Set to `true` after `init()` completes successfully.
|
|
130
|
-
*/
|
|
131
|
-
isInitialized = false
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Sets up the application with the provided module and options.
|
|
135
|
-
* This is called automatically by NaviosFactory.create().
|
|
136
|
-
*
|
|
137
|
-
* @param appModule - The root application module
|
|
138
|
-
* @param options - Application configuration options
|
|
139
|
-
* @internal
|
|
140
|
-
*/
|
|
141
|
-
async setup(
|
|
142
|
-
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
143
|
-
options: NaviosApplicationOptions = {
|
|
144
|
-
adapter: [],
|
|
145
|
-
},
|
|
146
|
-
) {
|
|
147
|
-
this.appModule = appModule
|
|
148
|
-
this.options = options
|
|
149
|
-
if (this.environment.hasAdapterSetup()) {
|
|
150
|
-
this.adapter = (await this.container.get(
|
|
151
|
-
AdapterToken,
|
|
152
|
-
)) as Environment['adapter']
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Gets the dependency injection container used by this application.
|
|
158
|
-
*
|
|
159
|
-
* @returns The Container instance
|
|
160
|
-
*/
|
|
161
|
-
getContainer() {
|
|
162
|
-
return this.container
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Returns the current adapter instance.
|
|
167
|
-
*
|
|
168
|
-
* @returns The adapter instance
|
|
169
|
-
* @throws Error if adapter is not initialized
|
|
170
|
-
*/
|
|
171
|
-
getAdapter(): Environment['adapter'] {
|
|
172
|
-
if (!this.adapter) {
|
|
173
|
-
throw new Error('Adapter not initialized')
|
|
174
|
-
}
|
|
175
|
-
return this.adapter
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Registers a plugin to be initialized after modules are loaded.
|
|
180
|
-
*
|
|
181
|
-
* Plugins are initialized in the order they are registered,
|
|
182
|
-
* after all modules are loaded but before the server starts listening.
|
|
183
|
-
*
|
|
184
|
-
* @param definition - Plugin definition with options
|
|
185
|
-
* @returns this for method chaining
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* ```typescript
|
|
189
|
-
* import { defineOpenApiPlugin } from '@navios/openapi-fastify'
|
|
190
|
-
*
|
|
191
|
-
* app.usePlugin(defineOpenApiPlugin({
|
|
192
|
-
* info: { title: 'My API', version: '1.0.0' },
|
|
193
|
-
* }))
|
|
194
|
-
* ```
|
|
195
|
-
*/
|
|
196
|
-
usePlugin<TOptions, TAdapter extends AbstractAdapterInterface>(
|
|
197
|
-
definition: PluginDefinition<TOptions, TAdapter>,
|
|
198
|
-
): this {
|
|
199
|
-
this.plugins.push(definition)
|
|
200
|
-
return this
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Initializes the application.
|
|
205
|
-
*
|
|
206
|
-
* This method:
|
|
207
|
-
* - Loads all modules and their dependencies
|
|
208
|
-
* - Sets up the adapter if one is configured
|
|
209
|
-
* - Calls onModuleInit hooks on all modules
|
|
210
|
-
* - Initializes registered plugins
|
|
211
|
-
* - Marks the application as initialized
|
|
212
|
-
*
|
|
213
|
-
* Must be called before `listen()`.
|
|
214
|
-
*
|
|
215
|
-
* @throws Error if app module is not set
|
|
216
|
-
*
|
|
217
|
-
* @example
|
|
218
|
-
* ```typescript
|
|
219
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
220
|
-
* adapter: defineFastifyEnvironment(),
|
|
221
|
-
* })
|
|
222
|
-
* await app.init()
|
|
223
|
-
* await app.listen({ port: 3000 })
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
async init() {
|
|
227
|
-
if (!this.appModule) {
|
|
228
|
-
throw new Error('App module is not set. Call setAppModule() first.')
|
|
229
|
-
}
|
|
230
|
-
await this.moduleLoader.loadModules(this.appModule)
|
|
231
|
-
|
|
232
|
-
if (this.environment.hasAdapterSetup() && this.adapter) {
|
|
233
|
-
await this.adapter.setupAdapter(this.options)
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
await this.initPlugins()
|
|
237
|
-
await this.initModules()
|
|
238
|
-
|
|
239
|
-
if (this.adapter) {
|
|
240
|
-
await this.adapter.ready()
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
this.isInitialized = true
|
|
244
|
-
this.logger.debug('Navios application initialized')
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
private async initModules() {
|
|
248
|
-
const modules = this.moduleLoader.getAllModules()
|
|
249
|
-
if (this.adapter) {
|
|
250
|
-
await this.adapter.onModulesInit(modules)
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
private async initPlugins() {
|
|
255
|
-
if (this.plugins.length === 0) return
|
|
256
|
-
|
|
257
|
-
if (!this.adapter) {
|
|
258
|
-
throw new Error('Cannot initialize plugins without an adapter')
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const context: PluginContext = {
|
|
262
|
-
modules: this.moduleLoader.getAllModules(),
|
|
263
|
-
adapter: this.adapter,
|
|
264
|
-
container: this.container,
|
|
265
|
-
moduleLoader: this.moduleLoader,
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
for (const { plugin, options } of this.plugins) {
|
|
269
|
-
this.logger.debug(`Initializing plugin: ${plugin.name}`)
|
|
270
|
-
await plugin.register(context, options)
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Gets a service instance from the dependency injection container.
|
|
276
|
-
*
|
|
277
|
-
* This is a shorthand for `app.getContainer().get(token)`.
|
|
278
|
-
*
|
|
279
|
-
* @param token - The injection token or class to resolve
|
|
280
|
-
* @returns Promise resolving to the service instance
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```typescript
|
|
284
|
-
* const userService = await app.get(UserService)
|
|
285
|
-
* const config = await app.get(ConfigToken)
|
|
286
|
-
* ```
|
|
287
|
-
*/
|
|
288
|
-
get<T extends ClassType>(token: T): Promise<InstanceType<T>>
|
|
289
|
-
get<T extends ClassTypeWithArgument<R>, R>(
|
|
290
|
-
token: T,
|
|
291
|
-
args: R,
|
|
292
|
-
): Promise<InstanceType<T>>
|
|
293
|
-
get<T, S extends InjectionTokenSchemaType>(
|
|
294
|
-
token: InjectionToken<T, S>,
|
|
295
|
-
args: z.input<S>,
|
|
296
|
-
): Promise<T>
|
|
297
|
-
get<T, S extends InjectionTokenSchemaType, R extends boolean>(
|
|
298
|
-
token: InjectionToken<T, S, R>,
|
|
299
|
-
): Promise<T>
|
|
300
|
-
get<T>(token: InjectionToken<T, undefined>): Promise<T>
|
|
301
|
-
get<T>(token: BoundInjectionToken<T, any>): Promise<T>
|
|
302
|
-
get<T>(token: FactoryInjectionToken<T, any>): Promise<T>
|
|
303
|
-
async get(
|
|
304
|
-
token:
|
|
305
|
-
| ClassType
|
|
306
|
-
| InjectionToken<any>
|
|
307
|
-
| BoundInjectionToken<any, any>
|
|
308
|
-
| FactoryInjectionToken<any, any>,
|
|
309
|
-
args?: unknown,
|
|
310
|
-
): Promise<any> {
|
|
311
|
-
return this.container.get(token as any, args)
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Configures the adapter with additional options before initialization.
|
|
316
|
-
*
|
|
317
|
-
* This method allows setting adapter-specific configuration options
|
|
318
|
-
* before the adapter is initialized. Must be called before `init()`.
|
|
319
|
-
*
|
|
320
|
-
* @param options - Adapter-specific configuration options
|
|
321
|
-
* @returns this for method chaining
|
|
322
|
-
* @throws Error if called after init() or if adapter doesn't support configure
|
|
323
|
-
*
|
|
324
|
-
* @example
|
|
325
|
-
* ```typescript
|
|
326
|
-
* // With Fastify adapter
|
|
327
|
-
* app.configure({ trustProxy: true, logger: true })
|
|
328
|
-
*
|
|
329
|
-
* // With Bun adapter
|
|
330
|
-
* app.configure({ development: true })
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
configure(options: Partial<Environment['options']>): this {
|
|
334
|
-
if (this.isInitialized) {
|
|
335
|
-
throw new Error('configure() must be called before init()')
|
|
336
|
-
}
|
|
337
|
-
assertAdapterSupports(this.adapter, 'configure')
|
|
338
|
-
this.adapter.configure(options)
|
|
339
|
-
return this
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Enables CORS (Cross-Origin Resource Sharing) for the application.
|
|
344
|
-
*
|
|
345
|
-
* @param options - CORS configuration options (adapter-specific)
|
|
346
|
-
* @throws Error if adapter doesn't support enableCors
|
|
347
|
-
*
|
|
348
|
-
* @example
|
|
349
|
-
* ```typescript
|
|
350
|
-
* app.enableCors({
|
|
351
|
-
* origin: ['http://localhost:3000', 'https://example.com'],
|
|
352
|
-
* methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
|
353
|
-
* credentials: true,
|
|
354
|
-
* })
|
|
355
|
-
* ```
|
|
356
|
-
*/
|
|
357
|
-
enableCors(
|
|
358
|
-
options: Environment extends HttpAdapterEnvironment
|
|
359
|
-
? Environment['corsOptions']
|
|
360
|
-
: never,
|
|
361
|
-
): void {
|
|
362
|
-
assertAdapterSupports(this.adapter, 'enableCors')
|
|
363
|
-
this.adapter.enableCors(options)
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Enables multipart/form-data support for file uploads.
|
|
368
|
-
*
|
|
369
|
-
* @param options - Multipart configuration options (adapter-specific)
|
|
370
|
-
* @throws Error if adapter doesn't support enableMultipart
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* ```typescript
|
|
374
|
-
* app.enableMultipart({
|
|
375
|
-
* limits: {
|
|
376
|
-
* fileSize: 1024 * 1024 * 10, // 10MB
|
|
377
|
-
* },
|
|
378
|
-
* })
|
|
379
|
-
* ```
|
|
380
|
-
*/
|
|
381
|
-
enableMultipart(
|
|
382
|
-
options: Environment extends HttpAdapterEnvironment
|
|
383
|
-
? Environment['multipartOptions']
|
|
384
|
-
: never,
|
|
385
|
-
): void {
|
|
386
|
-
assertAdapterSupports(this.adapter, 'enableMultipart')
|
|
387
|
-
this.adapter.enableMultipart(options)
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Sets a global prefix for all routes.
|
|
392
|
-
*
|
|
393
|
-
* @param prefix - The prefix to prepend to all route URLs (e.g., '/api')
|
|
394
|
-
* @throws Error if adapter doesn't support setGlobalPrefix
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```typescript
|
|
398
|
-
* app.setGlobalPrefix('/api/v1')
|
|
399
|
-
* // All routes will be prefixed with /api/v1
|
|
400
|
-
* ```
|
|
401
|
-
*/
|
|
402
|
-
setGlobalPrefix(prefix: string): void {
|
|
403
|
-
assertAdapterSupports(this.adapter, 'setGlobalPrefix')
|
|
404
|
-
this.adapter.setGlobalPrefix(prefix)
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Gets the underlying HTTP server instance.
|
|
409
|
-
*
|
|
410
|
-
* The type of the returned server depends on the adapter used:
|
|
411
|
-
* - Fastify adapter: Returns FastifyInstance
|
|
412
|
-
* - Bun adapter: Returns Bun.Server
|
|
413
|
-
*
|
|
414
|
-
* @returns The HTTP server instance
|
|
415
|
-
* @throws Error if adapter doesn't support getServer
|
|
416
|
-
*
|
|
417
|
-
* @example
|
|
418
|
-
* ```typescript
|
|
419
|
-
* const server = app.getServer()
|
|
420
|
-
* // Use adapter-specific server methods
|
|
421
|
-
* ```
|
|
422
|
-
*/
|
|
423
|
-
getServer(): Environment extends HttpAdapterEnvironment
|
|
424
|
-
? Environment['server']
|
|
425
|
-
: never {
|
|
426
|
-
assertAdapterSupports(this.adapter, 'getServer')
|
|
427
|
-
return this.adapter.getServer() as Environment extends HttpAdapterEnvironment
|
|
428
|
-
? Environment['server']
|
|
429
|
-
: never
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Starts the HTTP server and begins listening for requests.
|
|
434
|
-
*
|
|
435
|
-
* @param options - Listen options (port, host, etc.)
|
|
436
|
-
* @throws Error if adapter doesn't support listen
|
|
437
|
-
*
|
|
438
|
-
* @example
|
|
439
|
-
* ```typescript
|
|
440
|
-
* await app.listen({ port: 3000, host: '0.0.0.0' })
|
|
441
|
-
* ```
|
|
442
|
-
*/
|
|
443
|
-
async listen(
|
|
444
|
-
options: Environment extends HttpAdapterEnvironment
|
|
445
|
-
? Environment['listenOptions']
|
|
446
|
-
: never,
|
|
447
|
-
): Promise<string> {
|
|
448
|
-
assertAdapterSupports(this.adapter, 'listen')
|
|
449
|
-
return this.adapter.listen(options) as Promise<string>
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* Disposes of application resources.
|
|
454
|
-
*
|
|
455
|
-
* Cleans up the adapter and module loader.
|
|
456
|
-
* This method is called automatically by `close()`.
|
|
457
|
-
*/
|
|
458
|
-
async dispose() {
|
|
459
|
-
if (this.adapter) {
|
|
460
|
-
await this.adapter.dispose()
|
|
461
|
-
}
|
|
462
|
-
if (this.moduleLoader) {
|
|
463
|
-
this.moduleLoader.dispose()
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Closes the application and cleans up all resources.
|
|
469
|
-
*
|
|
470
|
-
* This is an alias for `dispose()`.
|
|
471
|
-
*
|
|
472
|
-
* @example
|
|
473
|
-
* ```typescript
|
|
474
|
-
* // Graceful shutdown
|
|
475
|
-
* process.on('SIGTERM', async () => {
|
|
476
|
-
* await app.close()
|
|
477
|
-
* process.exit(0)
|
|
478
|
-
* })
|
|
479
|
-
* ```
|
|
480
|
-
*/
|
|
481
|
-
async close() {
|
|
482
|
-
await this.dispose()
|
|
483
|
-
}
|
|
484
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { AnyInjectableType, InjectionToken } from '@navios/di'
|
|
2
|
-
|
|
3
|
-
import { Injectable } from '@navios/di'
|
|
4
|
-
|
|
5
|
-
import { AdapterToken } from './tokens/index.mjs'
|
|
6
|
-
|
|
7
|
-
export interface NaviosEnvironmentOptions {
|
|
8
|
-
tokens?: Map<InjectionToken<any, undefined>, AnyInjectableType>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
@Injectable()
|
|
12
|
-
export class NaviosEnvironment {
|
|
13
|
-
private adapterConfigured = false
|
|
14
|
-
private tokens = new Map<InjectionToken<any, undefined>, AnyInjectableType>()
|
|
15
|
-
|
|
16
|
-
setupEnvironment(
|
|
17
|
-
tokens: Map<InjectionToken<any, undefined>, AnyInjectableType>,
|
|
18
|
-
) {
|
|
19
|
-
const hasAdapterToken = tokens.has(AdapterToken)
|
|
20
|
-
if (hasAdapterToken && this.adapterConfigured) {
|
|
21
|
-
throw new Error(
|
|
22
|
-
'Adapter already configured. Only one adapter per application.',
|
|
23
|
-
)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
for (const [token, value] of tokens) {
|
|
27
|
-
this.tokens.set(token, value)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (hasAdapterToken) {
|
|
31
|
-
this.adapterConfigured = true
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
getToken(token: InjectionToken<any, undefined>) {
|
|
36
|
-
return this.tokens.get(token)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
hasAdapterSetup() {
|
|
40
|
-
return this.adapterConfigured
|
|
41
|
-
}
|
|
42
|
-
}
|
package/src/navios.factory.mts
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyInjectableType,
|
|
3
|
-
ClassTypeWithInstance,
|
|
4
|
-
InjectionToken,
|
|
5
|
-
} from '@navios/di'
|
|
6
|
-
|
|
7
|
-
import { Container } from '@navios/di'
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
AdapterEnvironment,
|
|
11
|
-
DefaultAdapterEnvironment,
|
|
12
|
-
NaviosModule,
|
|
13
|
-
} from './interfaces/index.mjs'
|
|
14
|
-
import type { NaviosApplicationOptions } from './navios.application.mjs'
|
|
15
|
-
|
|
16
|
-
import { ConsoleLogger, isNil, LoggerOutput } from './logger/index.mjs'
|
|
17
|
-
import { NaviosApplication } from './navios.application.mjs'
|
|
18
|
-
import { NaviosEnvironment } from './navios.environment.mjs'
|
|
19
|
-
import { setRequestIdEnabled } from './stores/index.mjs'
|
|
20
|
-
import { NaviosOptionsToken } from './tokens/index.mjs'
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Factory class for creating and configuring Navios applications.
|
|
24
|
-
*
|
|
25
|
-
* This is the main entry point for bootstrapping a Navios application.
|
|
26
|
-
* It handles dependency injection container setup, adapter registration,
|
|
27
|
-
* and logger configuration.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* import { NaviosFactory } from '@navios/core'
|
|
32
|
-
* import { defineFastifyEnvironment } from '@navios/adapter-fastify'
|
|
33
|
-
*
|
|
34
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
35
|
-
* adapter: defineFastifyEnvironment(),
|
|
36
|
-
* logger: ['log', 'error', 'warn'],
|
|
37
|
-
* })
|
|
38
|
-
*
|
|
39
|
-
* await app.init()
|
|
40
|
-
* await app.listen({ port: 3000 })
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export class NaviosFactory {
|
|
44
|
-
/**
|
|
45
|
-
* Creates a new Navios application instance.
|
|
46
|
-
*
|
|
47
|
-
* This method sets up the dependency injection container, registers the adapter,
|
|
48
|
-
* configures logging, and initializes the application with the provided module.
|
|
49
|
-
*
|
|
50
|
-
* @typeParam Environment - Adapter environment interface for type-safe access
|
|
51
|
-
* to adapter-specific features. When specified, methods like `getServer()`,
|
|
52
|
-
* `enableCors()`, `listen()`, and `configure()` will have proper types.
|
|
53
|
-
*
|
|
54
|
-
* @param appModule - The root application module class decorated with @Module()
|
|
55
|
-
* @param options - Configuration options for the application
|
|
56
|
-
* @param options.adapter - Adapter environment (required for server functionality)
|
|
57
|
-
* @param options.logger - Logger configuration. Can be:
|
|
58
|
-
* - A LoggerService instance for custom logging
|
|
59
|
-
* - An array of LogLevel strings to enable specific log levels
|
|
60
|
-
* - `false` to disable logging
|
|
61
|
-
* @param options.container - Optional custom dependency injection container (useful for testing)
|
|
62
|
-
* @returns A configured NaviosApplication instance ready to be initialized
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* // Basic setup with Fastify adapter
|
|
67
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
68
|
-
* adapter: defineFastifyEnvironment(),
|
|
69
|
-
* })
|
|
70
|
-
*
|
|
71
|
-
* // Type-safe setup with Fastify environment
|
|
72
|
-
* import { FastifyEnvironment } from '@navios/adapter-fastify'
|
|
73
|
-
*
|
|
74
|
-
* const app = await NaviosFactory.create<FastifyEnvironment>(AppModule, {
|
|
75
|
-
* adapter: defineFastifyEnvironment(),
|
|
76
|
-
* })
|
|
77
|
-
* app.configure({ trustProxy: true })
|
|
78
|
-
* const server = app.getServer() // FastifyInstance
|
|
79
|
-
*
|
|
80
|
-
* // With custom logger configuration
|
|
81
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
82
|
-
* adapter: defineFastifyEnvironment(),
|
|
83
|
-
* logger: ['error', 'warn', 'log'],
|
|
84
|
-
* })
|
|
85
|
-
*
|
|
86
|
-
* // With custom container for testing
|
|
87
|
-
* const container = new Container()
|
|
88
|
-
* const app = await NaviosFactory.create(AppModule, {
|
|
89
|
-
* adapter: defineFastifyEnvironment(),
|
|
90
|
-
* container,
|
|
91
|
-
* })
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
static async create<
|
|
95
|
-
Environment extends AdapterEnvironment = DefaultAdapterEnvironment,
|
|
96
|
-
>(
|
|
97
|
-
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
98
|
-
options: NaviosApplicationOptions = {
|
|
99
|
-
adapter: [],
|
|
100
|
-
},
|
|
101
|
-
): Promise<NaviosApplication<Environment>> {
|
|
102
|
-
const container = options.container ?? new Container(options.registry)
|
|
103
|
-
|
|
104
|
-
// Set request ID flag early, before any adapters are registered
|
|
105
|
-
if (options.enableRequestId === true) {
|
|
106
|
-
setRequestIdEnabled(true)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Store options in container for DI access by adapters
|
|
110
|
-
container.addInstance(NaviosOptionsToken, options)
|
|
111
|
-
|
|
112
|
-
await this.registerLoggerConfiguration(container, options)
|
|
113
|
-
const adapters = Array.isArray(options.adapter)
|
|
114
|
-
? options.adapter
|
|
115
|
-
: [options.adapter]
|
|
116
|
-
for (const adapter of adapters) {
|
|
117
|
-
await this.registerEnvironment(container, adapter)
|
|
118
|
-
}
|
|
119
|
-
const app = (await container.get(
|
|
120
|
-
NaviosApplication,
|
|
121
|
-
)) as NaviosApplication<Environment>
|
|
122
|
-
await app.setup(appModule, options)
|
|
123
|
-
return app
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private static async registerEnvironment(
|
|
127
|
-
container: Container,
|
|
128
|
-
environment: {
|
|
129
|
-
tokens?: Map<InjectionToken<any, undefined>, AnyInjectableType>
|
|
130
|
-
} = {},
|
|
131
|
-
) {
|
|
132
|
-
const naviosEnvironment = await container.get(NaviosEnvironment)
|
|
133
|
-
const { tokens } = environment
|
|
134
|
-
if (tokens) {
|
|
135
|
-
naviosEnvironment.setupEnvironment(tokens)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private static async registerLoggerConfiguration(
|
|
140
|
-
container: Container,
|
|
141
|
-
options: NaviosApplicationOptions,
|
|
142
|
-
) {
|
|
143
|
-
const { logger } = options
|
|
144
|
-
if (Array.isArray(logger) || isNil(logger) || options.enableRequestId) {
|
|
145
|
-
const loggerInstance = (await container.get(
|
|
146
|
-
LoggerOutput,
|
|
147
|
-
)) as ConsoleLogger
|
|
148
|
-
loggerInstance?.setup({
|
|
149
|
-
logLevels: Array.isArray(logger) ? logger : undefined,
|
|
150
|
-
requestId: options.enableRequestId ?? false,
|
|
151
|
-
})
|
|
152
|
-
return
|
|
153
|
-
}
|
|
154
|
-
if ((logger as boolean) !== true && !isNil(logger)) {
|
|
155
|
-
container.addInstance(LoggerOutput, logger)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|