@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.
Files changed (237) hide show
  1. package/CHANGELOG.md +97 -11
  2. package/README.md +67 -220
  3. package/lib/async-context-6pdlnNLA.d.mts +237 -0
  4. package/lib/async-context-6pdlnNLA.d.mts.map +1 -0
  5. package/lib/async-context-Dqk7pE-D.d.cts +237 -0
  6. package/lib/async-context-Dqk7pE-D.d.cts.map +1 -0
  7. package/lib/attribute-factory-B3VuYnp7.d.cts +70 -0
  8. package/lib/attribute-factory-B3VuYnp7.d.cts.map +1 -0
  9. package/lib/attribute-factory-mKOXoVnV.d.mts +70 -0
  10. package/lib/attribute-factory-mKOXoVnV.d.mts.map +1 -0
  11. package/lib/attribute-value-CjX9ZkLq.cjs +172 -0
  12. package/lib/attribute-value-CjX9ZkLq.cjs.map +1 -0
  13. package/lib/attribute-value-DMEA255r.mjs +119 -0
  14. package/lib/attribute-value-DMEA255r.mjs.map +1 -0
  15. package/lib/basic-execution-context-CeGuhMOz.mjs +246 -0
  16. package/lib/basic-execution-context-CeGuhMOz.mjs.map +1 -0
  17. package/lib/basic-execution-context-DSk5MNtc.cjs +281 -0
  18. package/lib/basic-execution-context-DSk5MNtc.cjs.map +1 -0
  19. package/lib/execution-context-BPD3FeFb.d.mts +71 -0
  20. package/lib/execution-context-BPD3FeFb.d.mts.map +1 -0
  21. package/lib/execution-context-BTJdHhbf.d.cts +71 -0
  22. package/lib/execution-context-BTJdHhbf.d.cts.map +1 -0
  23. package/lib/execution-context-token-BXUKB2hf.cjs +112 -0
  24. package/lib/execution-context-token-BXUKB2hf.cjs.map +1 -0
  25. package/lib/execution-context-token-CrXmTvk2.mjs +77 -0
  26. package/lib/execution-context-token-CrXmTvk2.mjs.map +1 -0
  27. package/lib/index.cjs +1687 -4409
  28. package/lib/index.cjs.map +1 -1
  29. package/lib/index.d.cts +838 -3
  30. package/lib/index.d.cts.map +1 -0
  31. package/lib/index.d.mts +838 -3
  32. package/lib/index.d.mts.map +1 -0
  33. package/lib/index.mjs +1573 -4219
  34. package/lib/index.mjs.map +1 -1
  35. package/lib/legacy-compat/index.cjs +72 -371
  36. package/lib/legacy-compat/index.cjs.map +1 -1
  37. package/lib/legacy-compat/index.d.cts +42 -382
  38. package/lib/legacy-compat/index.d.cts.map +1 -1
  39. package/lib/legacy-compat/index.d.mts +41 -382
  40. package/lib/legacy-compat/index.d.mts.map +1 -1
  41. package/lib/legacy-compat/index.mjs +51 -345
  42. package/lib/legacy-compat/index.mjs.map +1 -1
  43. package/lib/module-Bha7GER_.mjs +135 -0
  44. package/lib/module-Bha7GER_.mjs.map +1 -0
  45. package/lib/module-DZewk_2r.cjs +164 -0
  46. package/lib/module-DZewk_2r.cjs.map +1 -0
  47. package/lib/module-graph-DCP9MHG0.cjs +147 -0
  48. package/lib/module-graph-DCP9MHG0.cjs.map +1 -0
  49. package/lib/module-graph-xvVEykFM.mjs +142 -0
  50. package/lib/module-graph-xvVEykFM.mjs.map +1 -0
  51. package/lib/module-metadata-C8S92seK.cjs +95 -0
  52. package/lib/module-metadata-C8S92seK.cjs.map +1 -0
  53. package/lib/module-metadata-k3RTt20x.mjs +66 -0
  54. package/lib/module-metadata-k3RTt20x.mjs.map +1 -0
  55. package/lib/module-options-Dmse0gii.d.mts +51 -0
  56. package/lib/module-options-Dmse0gii.d.mts.map +1 -0
  57. package/lib/module-options-IuDO-p8u.d.cts +51 -0
  58. package/lib/module-options-IuDO-p8u.d.cts.map +1 -0
  59. package/lib/testing/index.cjs +96 -414
  60. package/lib/testing/index.cjs.map +1 -1
  61. package/lib/testing/index.d.cts +25 -385
  62. package/lib/testing/index.d.cts.map +1 -1
  63. package/lib/testing/index.d.mts +24 -385
  64. package/lib/testing/index.d.mts.map +1 -1
  65. package/lib/testing/index.mjs +94 -408
  66. package/lib/testing/index.mjs.map +1 -1
  67. package/lib/transport.cjs +25 -0
  68. package/lib/transport.d.cts +190 -0
  69. package/lib/transport.d.cts.map +1 -0
  70. package/lib/transport.d.mts +190 -0
  71. package/lib/transport.d.mts.map +1 -0
  72. package/lib/transport.mjs +7 -0
  73. package/package.json +37 -17
  74. package/docs/README.md +0 -316
  75. package/docs/adapters.md +0 -308
  76. package/docs/application-setup.md +0 -524
  77. package/docs/attributes.md +0 -689
  78. package/docs/controllers.md +0 -373
  79. package/docs/endpoints.md +0 -444
  80. package/docs/exceptions.md +0 -352
  81. package/docs/guards.md +0 -550
  82. package/docs/legacy-compat.md +0 -356
  83. package/docs/modules.md +0 -377
  84. package/docs/quick-start.md +0 -290
  85. package/docs/services.md +0 -427
  86. package/docs/testing.md +0 -827
  87. package/lib/index-C0Sg16Eb.d.cts +0 -3365
  88. package/lib/index-C0Sg16Eb.d.cts.map +0 -1
  89. package/lib/index-DySQ6Dpd.d.mts +0 -3365
  90. package/lib/index-DySQ6Dpd.d.mts.map +0 -1
  91. package/lib/navios.factory-CO5MB_OK.cjs +0 -4125
  92. package/lib/navios.factory-CO5MB_OK.cjs.map +0 -1
  93. package/lib/navios.factory-D6Y94P9B.mjs +0 -3828
  94. package/lib/navios.factory-D6Y94P9B.mjs.map +0 -1
  95. package/lib/tokens-4J9sredA.mjs +0 -100
  96. package/lib/tokens-4J9sredA.mjs.map +0 -1
  97. package/lib/tokens-CWw9kyeD.cjs +0 -196
  98. package/lib/tokens-CWw9kyeD.cjs.map +0 -1
  99. package/lib/use-guards.decorator-BecoQSmE.mjs +0 -339
  100. package/lib/use-guards.decorator-BecoQSmE.mjs.map +0 -1
  101. package/lib/use-guards.decorator-C4ml9XaT.cjs +0 -447
  102. package/lib/use-guards.decorator-C4ml9XaT.cjs.map +0 -1
  103. package/project.json +0 -66
  104. package/src/__tests__/attribute.factory.spec.mts +0 -300
  105. package/src/__tests__/config.service.spec.mts +0 -43
  106. package/src/__tests__/console-logger.service.spec.mts +0 -312
  107. package/src/__tests__/controller-resolver.spec.mts +0 -229
  108. package/src/__tests__/controller.spec.mts +0 -58
  109. package/src/__tests__/guard-runner.service.spec.mts +0 -399
  110. package/src/__tests__/logger.service.spec.mts +0 -147
  111. package/src/__tests__/responders.spec.mts +0 -340
  112. package/src/__tests__/testing-module.spec.mts +0 -459
  113. package/src/__tests__/unit-testing-module.spec.mts +0 -424
  114. package/src/attribute.factory.mts +0 -325
  115. package/src/config/config-service.interface.mts +0 -47
  116. package/src/config/config.provider.mts +0 -69
  117. package/src/config/config.service.mts +0 -176
  118. package/src/config/index.mts +0 -5
  119. package/src/config/types.mts +0 -58
  120. package/src/config/utils/helpers.mts +0 -23
  121. package/src/config/utils/index.mts +0 -1
  122. package/src/decorators/controller.decorator.mts +0 -81
  123. package/src/decorators/endpoint.decorator.mts +0 -181
  124. package/src/decorators/header.decorator.mts +0 -38
  125. package/src/decorators/http-code.decorator.mts +0 -38
  126. package/src/decorators/index.mts +0 -8
  127. package/src/decorators/module.decorator.mts +0 -106
  128. package/src/decorators/multipart.decorator.mts +0 -169
  129. package/src/decorators/stream.decorator.mts +0 -155
  130. package/src/decorators/use-guards.decorator.mts +0 -73
  131. package/src/exceptions/bad-request.exception.mts +0 -28
  132. package/src/exceptions/conflict.exception.mts +0 -31
  133. package/src/exceptions/forbidden.exception.mts +0 -30
  134. package/src/exceptions/http.exception.mts +0 -33
  135. package/src/exceptions/index.mts +0 -7
  136. package/src/exceptions/internal-server-error.exception.mts +0 -33
  137. package/src/exceptions/not-found.exception.mts +0 -33
  138. package/src/exceptions/unauthorized.exception.mts +0 -30
  139. package/src/factories/adapter.factory.mts +0 -20
  140. package/src/factories/endpoint-adapter.factory.mts +0 -21
  141. package/src/factories/http-adapter.factory.mts +0 -20
  142. package/src/factories/index.mts +0 -8
  143. package/src/factories/multipart-adapter.factory.mts +0 -21
  144. package/src/factories/reply.factory.mts +0 -21
  145. package/src/factories/request.factory.mts +0 -21
  146. package/src/factories/stream-adapter.factory.mts +0 -20
  147. package/src/factories/xml-stream-adapter.factory.mts +0 -20
  148. package/src/index.mts +0 -16
  149. package/src/interfaces/abstract-adapter.interface.mts +0 -32
  150. package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
  151. package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
  152. package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
  153. package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
  154. package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
  155. package/src/interfaces/adapter-environment.interface.mts +0 -74
  156. package/src/interfaces/can-activate.mts +0 -38
  157. package/src/interfaces/http-header.mts +0 -18
  158. package/src/interfaces/index.mts +0 -11
  159. package/src/interfaces/navios-module.mts +0 -28
  160. package/src/interfaces/plugin.interface.mts +0 -139
  161. package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
  162. package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
  163. package/src/legacy-compat/attribute.factory.mts +0 -351
  164. package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
  165. package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
  166. package/src/legacy-compat/decorators/header.decorator.mts +0 -43
  167. package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
  168. package/src/legacy-compat/decorators/index.mts +0 -11
  169. package/src/legacy-compat/decorators/module.decorator.mts +0 -37
  170. package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
  171. package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
  172. package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
  173. package/src/legacy-compat/index.mts +0 -57
  174. package/src/logger/README.md +0 -3
  175. package/src/logger/console-logger.service.mts +0 -714
  176. package/src/logger/index.mts +0 -6
  177. package/src/logger/log-levels.mts +0 -21
  178. package/src/logger/logger-service.interface.mts +0 -42
  179. package/src/logger/logger.service.mts +0 -113
  180. package/src/logger/logger.tokens.mts +0 -44
  181. package/src/logger/utils/cli-colors.util.mts +0 -17
  182. package/src/logger/utils/filter-log-levelts.util.mts +0 -29
  183. package/src/logger/utils/index.mts +0 -5
  184. package/src/logger/utils/is-log-level-enabled.mts +0 -33
  185. package/src/logger/utils/is-log-level.util.mts +0 -10
  186. package/src/logger/utils/shared.utils.mts +0 -51
  187. package/src/metadata/controller.metadata.mts +0 -72
  188. package/src/metadata/handler.metadata.mts +0 -80
  189. package/src/metadata/index.mts +0 -4
  190. package/src/metadata/module.metadata.mts +0 -108
  191. package/src/metadata/navios-managed.metadata.mts +0 -42
  192. package/src/navios.application.mts +0 -484
  193. package/src/navios.environment.mts +0 -42
  194. package/src/navios.factory.mts +0 -158
  195. package/src/responders/enums/framework-error.enum.mts +0 -38
  196. package/src/responders/enums/index.mts +0 -1
  197. package/src/responders/index.mts +0 -4
  198. package/src/responders/interfaces/error-responder.interface.mts +0 -42
  199. package/src/responders/interfaces/error-response.interface.mts +0 -22
  200. package/src/responders/interfaces/index.mts +0 -3
  201. package/src/responders/interfaces/problem-details.interface.mts +0 -34
  202. package/src/responders/services/error-response-producer.service.mts +0 -143
  203. package/src/responders/services/forbidden-responder.service.mts +0 -77
  204. package/src/responders/services/index.mts +0 -5
  205. package/src/responders/services/internal-server-error-responder.service.mts +0 -57
  206. package/src/responders/services/not-found-responder.service.mts +0 -76
  207. package/src/responders/services/validation-error-responder.service.mts +0 -78
  208. package/src/responders/tokens/index.mts +0 -1
  209. package/src/responders/tokens/responder.tokens.mts +0 -84
  210. package/src/services/abstract-handler-adapter.service.mts +0 -366
  211. package/src/services/guard-runner.service.mts +0 -152
  212. package/src/services/index.mts +0 -4
  213. package/src/services/instance-resolver.service.mts +0 -177
  214. package/src/services/module-loader.service.mts +0 -252
  215. package/src/stores/index.mts +0 -1
  216. package/src/stores/request-id.store.mts +0 -85
  217. package/src/testing/index.mts +0 -3
  218. package/src/testing/testing-module.mts +0 -393
  219. package/src/testing/unit-testing-module.mts +0 -298
  220. package/src/tokens/adapter.token.mts +0 -6
  221. package/src/tokens/endpoint-adapter.token.mts +0 -8
  222. package/src/tokens/execution-context.token.mts +0 -9
  223. package/src/tokens/http-adapter.token.mts +0 -8
  224. package/src/tokens/index.mts +0 -10
  225. package/src/tokens/multipart-adapter.token.mts +0 -8
  226. package/src/tokens/navios-options.token.mts +0 -6
  227. package/src/tokens/reply.token.mts +0 -3
  228. package/src/tokens/request.token.mts +0 -3
  229. package/src/tokens/stream-adapter.token.mts +0 -8
  230. package/src/tokens/xml-stream-adapter.token.mts +0 -8
  231. package/src/utils/adapter-supports.util.mts +0 -47
  232. package/src/utils/index.mts +0 -1
  233. package/tsconfig.json +0 -21
  234. package/tsconfig.lib.json +0 -8
  235. package/tsconfig.spec.json +0 -15
  236. package/tsdown.config.mts +0 -35
  237. 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
- }
@@ -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
- }