@navios/core 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +115 -0
- package/README.md +18 -1
- package/docs/README.md +1 -0
- package/docs/legacy-compat.md +320 -0
- package/docs/testing.md +140 -17
- package/lib/index-DW9EPAE6.d.mts +2156 -0
- package/lib/index-DW9EPAE6.d.mts.map +1 -0
- package/lib/index-pHp-dIGt.d.cts +2156 -0
- package/lib/index-pHp-dIGt.d.cts.map +1 -0
- package/lib/index.cjs +157 -0
- package/lib/index.d.cts +3 -0
- package/lib/index.d.mts +3 -190
- package/lib/index.mjs +4 -1459
- package/lib/legacy-compat/index.cjs +315 -0
- package/lib/legacy-compat/index.cjs.map +1 -0
- package/lib/legacy-compat/index.d.cts +219 -0
- package/lib/legacy-compat/index.d.cts.map +1 -0
- package/lib/legacy-compat/index.d.mts +219 -0
- package/lib/legacy-compat/index.d.mts.map +1 -0
- package/lib/legacy-compat/index.mjs +308 -0
- package/lib/legacy-compat/index.mjs.map +1 -0
- package/lib/src-DyvCDuKO.mjs +5443 -0
- package/lib/src-DyvCDuKO.mjs.map +1 -0
- package/lib/src-QnxR5b7c.cjs +5800 -0
- package/lib/src-QnxR5b7c.cjs.map +1 -0
- package/lib/testing/index.cjs +106 -0
- package/lib/testing/index.cjs.map +1 -0
- package/lib/testing/index.d.cts +156 -0
- package/lib/testing/index.d.cts.map +1 -0
- package/lib/testing/index.d.mts +156 -0
- package/lib/testing/index.d.mts.map +1 -0
- package/lib/testing/index.mjs +100 -0
- package/lib/testing/index.mjs.map +1 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs +622 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs.map +1 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs +454 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs.map +1 -0
- package/package.json +28 -8
- package/project.json +2 -2
- package/src/attribute.factory.mts +154 -0
- package/src/config/config-service.interface.mts +31 -0
- package/src/config/config.provider.mts +36 -0
- package/src/config/config.service.mts +94 -4
- package/src/decorators/controller.decorator.mts +28 -0
- package/src/decorators/endpoint.decorator.mts +76 -0
- package/src/decorators/header.decorator.mts +19 -0
- package/src/decorators/http-code.decorator.mts +20 -0
- package/src/decorators/module.decorator.mts +34 -0
- package/src/decorators/multipart.decorator.mts +41 -0
- package/src/decorators/stream.decorator.mts +33 -0
- package/src/decorators/use-guards.decorator.mts +29 -0
- package/src/exceptions/bad-request.exception.mts +21 -0
- package/src/exceptions/conflict.exception.mts +24 -0
- package/src/exceptions/forbidden.exception.mts +23 -0
- package/src/exceptions/http.exception.mts +26 -0
- package/src/exceptions/internal-server-error.exception.mts +26 -0
- package/src/exceptions/not-found.exception.mts +23 -0
- package/src/exceptions/unauthorized.exception.mts +23 -0
- package/src/index.mts +1 -0
- package/src/interfaces/abstract-execution-context.inteface.mts +35 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +52 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +2 -2
- package/src/interfaces/can-activate.mts +31 -0
- package/src/interfaces/index.mts +1 -0
- package/src/interfaces/navios-module.mts +25 -0
- package/src/interfaces/plugin.interface.mts +105 -0
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +420 -0
- package/src/legacy-compat/__type-tests__/tsconfig.json +15 -0
- package/src/legacy-compat/context-compat.mts +93 -0
- package/src/legacy-compat/decorators/controller.decorator.mts +31 -0
- package/src/legacy-compat/decorators/endpoint.decorator.mts +99 -0
- package/src/legacy-compat/decorators/header.decorator.mts +42 -0
- package/src/legacy-compat/decorators/http-code.decorator.mts +38 -0
- package/src/legacy-compat/decorators/index.mts +9 -0
- package/src/legacy-compat/decorators/module.decorator.mts +37 -0
- package/src/legacy-compat/decorators/multipart.decorator.mts +93 -0
- package/src/legacy-compat/decorators/stream.decorator.mts +76 -0
- package/src/legacy-compat/decorators/use-guards.decorator.mts +80 -0
- package/src/legacy-compat/index.mts +40 -0
- package/src/logger/console-logger.service.mts +15 -2
- package/src/logger/log-levels.mts +9 -0
- package/src/logger/logger.service.mts +21 -0
- package/src/logger/logger.tokens.mts +23 -0
- package/src/navios.application.mts +228 -4
- package/src/navios.factory.mts +60 -1
- package/src/services/guard-runner.service.mts +12 -11
- package/src/services/module-loader.service.mts +118 -12
- package/src/stores/index.mts +1 -0
- package/src/stores/request-id.store.mts +43 -0
- package/src/testing/index.mts +2 -0
- package/src/testing/testing-module.mts +231 -0
- package/tsconfig.lib.json +1 -1
- package/tsconfig.spec.json +3 -0
- package/tsdown.config.mts +35 -0
- package/vitest.config.mts +6 -0
- package/lib/_tsup-dts-rollup.d.mts +0 -1365
- package/lib/_tsup-dts-rollup.d.ts +0 -1365
- package/lib/index.d.ts +0 -190
- package/lib/index.js +0 -1540
- package/lib/index.js.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/tsup.config.mts +0 -13
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Available log levels in order of severity (lowest to highest).
|
|
3
|
+
*/
|
|
1
4
|
export const LOG_LEVELS = [
|
|
2
5
|
'verbose',
|
|
3
6
|
'debug',
|
|
@@ -6,7 +9,13 @@ export const LOG_LEVELS = [
|
|
|
6
9
|
'error',
|
|
7
10
|
'fatal',
|
|
8
11
|
] as const satisfies string[]
|
|
12
|
+
|
|
9
13
|
/**
|
|
14
|
+
* Log level type.
|
|
15
|
+
*
|
|
16
|
+
* Represents the severity level of a log message.
|
|
17
|
+
* Levels are: 'verbose', 'debug', 'log', 'warn', 'error', 'fatal'
|
|
18
|
+
*
|
|
10
19
|
* @publicApi
|
|
11
20
|
*/
|
|
12
21
|
export type LogLevel = (typeof LOG_LEVELS)[number]
|
|
@@ -5,6 +5,25 @@ import type { LoggerOptions } from './logger.tokens.mjs'
|
|
|
5
5
|
|
|
6
6
|
import { Logger, LoggerOutput } from './logger.tokens.mjs'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Logger service instance that can be injected into services and controllers.
|
|
10
|
+
*
|
|
11
|
+
* Provides contextualized logging with automatic context injection.
|
|
12
|
+
* The context is set when the logger is injected using the `inject` function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* @Injectable()
|
|
17
|
+
* export class UserService {
|
|
18
|
+
* private logger = inject(Logger, { context: UserService.name })
|
|
19
|
+
*
|
|
20
|
+
* async findUser(id: string) {
|
|
21
|
+
* this.logger.log(`Finding user ${id}`)
|
|
22
|
+
* // Logs with context: [UserService]
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
8
27
|
@Injectable({
|
|
9
28
|
token: Logger,
|
|
10
29
|
})
|
|
@@ -41,6 +60,8 @@ export class LoggerInstance implements LoggerService {
|
|
|
41
60
|
optionalParams = this.context
|
|
42
61
|
? optionalParams.concat(this.context)
|
|
43
62
|
: optionalParams
|
|
63
|
+
console.log('log', message, optionalParams)
|
|
64
|
+
console.log('localInstance', this.localInstance)
|
|
44
65
|
this.localInstance?.log(message, ...optionalParams)
|
|
45
66
|
}
|
|
46
67
|
|
|
@@ -5,16 +5,39 @@ import z from 'zod/v4'
|
|
|
5
5
|
import type { LoggerService } from './logger-service.interface.mjs'
|
|
6
6
|
import type { LoggerInstance } from './logger.service.mjs'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Injection token for the logger output service.
|
|
10
|
+
*
|
|
11
|
+
* This token is used to provide a custom logger implementation.
|
|
12
|
+
* By default, it's bound to ConsoleLogger.
|
|
13
|
+
*/
|
|
8
14
|
export const LoggerOutput = InjectionToken.create<LoggerService>('LoggerOutput')
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Schema for logger options.
|
|
18
|
+
*/
|
|
10
19
|
export const loggerOptionsSchema = z
|
|
11
20
|
.object({
|
|
12
21
|
context: z.string().optional(),
|
|
13
22
|
})
|
|
14
23
|
.optional()
|
|
15
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Options for creating a logger instance.
|
|
27
|
+
*/
|
|
16
28
|
export type LoggerOptions = z.infer<typeof loggerOptionsSchema>
|
|
17
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Injection token for the Logger service.
|
|
32
|
+
*
|
|
33
|
+
* Use this token to inject a contextualized logger instance.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const logger = inject(Logger, { context: 'MyService' })
|
|
38
|
+
* logger.log('Hello world') // Logs with context: [MyService]
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
18
41
|
export const Logger = InjectionToken.create<
|
|
19
42
|
LoggerInstance,
|
|
20
43
|
typeof loggerOptionsSchema
|
|
@@ -6,6 +6,8 @@ import type {
|
|
|
6
6
|
AbstractHttpAdapterInterface,
|
|
7
7
|
AbstractHttpListenOptions,
|
|
8
8
|
NaviosModule,
|
|
9
|
+
PluginContext,
|
|
10
|
+
PluginDefinition,
|
|
9
11
|
} from './interfaces/index.mjs'
|
|
10
12
|
import type { LoggerService, LogLevel } from './logger/index.mjs'
|
|
11
13
|
import type { NaviosEnvironmentOptions } from './navios.environment.mjs'
|
|
@@ -15,19 +17,64 @@ import { Logger } from './logger/index.mjs'
|
|
|
15
17
|
import { NaviosEnvironment } from './navios.environment.mjs'
|
|
16
18
|
import { ModuleLoaderService } from './services/index.mjs'
|
|
17
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Options for configuring the Navios application context.
|
|
22
|
+
* These options control dependency injection and logging behavior.
|
|
23
|
+
*/
|
|
18
24
|
export interface NaviosApplicationContextOptions {
|
|
19
25
|
/**
|
|
20
|
-
* Specifies the logger to use.
|
|
26
|
+
* Specifies the logger to use. Pass `false` to turn off logging.
|
|
27
|
+
*
|
|
28
|
+
* - `LoggerService` instance: Use a custom logger implementation
|
|
29
|
+
* - `LogLevel[]`: Enable specific log levels (e.g., ['error', 'warn', 'log'])
|
|
30
|
+
* - `false`: Disable logging completely
|
|
21
31
|
*/
|
|
22
32
|
logger?: LoggerService | LogLevel[] | false
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Specifies a custom container to use. Useful for testing.
|
|
36
|
+
* If not provided, a new Container will be created.
|
|
37
|
+
*/
|
|
38
|
+
container?: Container
|
|
23
39
|
}
|
|
24
40
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Complete options for creating a Navios application.
|
|
43
|
+
* Extends NaviosApplicationContextOptions with adapter configuration.
|
|
44
|
+
*/
|
|
45
|
+
export interface NaviosApplicationOptions extends NaviosApplicationContextOptions {
|
|
46
|
+
/**
|
|
47
|
+
* HTTP adapter environment(s) to use for the application.
|
|
48
|
+
* Can be a single adapter or an array of adapters.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* adapter: defineFastifyEnvironment()
|
|
53
|
+
* // or
|
|
54
|
+
* adapter: [defineFastifyEnvironment(), defineBunEnvironment()]
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
28
57
|
adapter: NaviosEnvironmentOptions | NaviosEnvironmentOptions[]
|
|
29
58
|
}
|
|
30
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Main application class for Navios.
|
|
62
|
+
*
|
|
63
|
+
* This class represents a Navios application instance and provides methods
|
|
64
|
+
* for initializing, configuring, and managing the HTTP server.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
69
|
+
* adapter: defineFastifyEnvironment(),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* app.setGlobalPrefix('/api')
|
|
73
|
+
* app.enableCors({ origin: ['http://localhost:3000'] })
|
|
74
|
+
* await app.init()
|
|
75
|
+
* await app.listen({ port: 3000, host: '0.0.0.0' })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
31
78
|
@Injectable()
|
|
32
79
|
export class NaviosApplication {
|
|
33
80
|
private environment = inject(NaviosEnvironment)
|
|
@@ -42,9 +89,22 @@ export class NaviosApplication {
|
|
|
42
89
|
private options: NaviosApplicationOptions = {
|
|
43
90
|
adapter: [],
|
|
44
91
|
}
|
|
92
|
+
private plugins: PluginDefinition<any>[] = []
|
|
45
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Indicates whether the application has been initialized.
|
|
96
|
+
* Set to `true` after `init()` completes successfully.
|
|
97
|
+
*/
|
|
46
98
|
isInitialized = false
|
|
47
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Sets up the application with the provided module and options.
|
|
102
|
+
* This is called automatically by NaviosFactory.create().
|
|
103
|
+
*
|
|
104
|
+
* @param appModule - The root application module
|
|
105
|
+
* @param options - Application configuration options
|
|
106
|
+
* @internal
|
|
107
|
+
*/
|
|
48
108
|
async setup(
|
|
49
109
|
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
50
110
|
options: NaviosApplicationOptions = {
|
|
@@ -58,10 +118,61 @@ export class NaviosApplication {
|
|
|
58
118
|
}
|
|
59
119
|
}
|
|
60
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Gets the dependency injection container used by this application.
|
|
123
|
+
*
|
|
124
|
+
* @returns The Container instance
|
|
125
|
+
*/
|
|
61
126
|
getContainer() {
|
|
62
127
|
return this.container
|
|
63
128
|
}
|
|
64
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Registers a plugin to be initialized after modules are loaded.
|
|
132
|
+
*
|
|
133
|
+
* Plugins are initialized in the order they are registered,
|
|
134
|
+
* after all modules are loaded but before the server starts listening.
|
|
135
|
+
*
|
|
136
|
+
* @param definition - Plugin definition with options
|
|
137
|
+
* @returns this for method chaining
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* import { defineOpenApiPlugin } from '@navios/openapi-fastify'
|
|
142
|
+
*
|
|
143
|
+
* app.usePlugin(defineOpenApiPlugin({
|
|
144
|
+
* info: { title: 'My API', version: '1.0.0' },
|
|
145
|
+
* }))
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
usePlugin<TOptions>(definition: PluginDefinition<TOptions>): this {
|
|
149
|
+
this.plugins.push(definition)
|
|
150
|
+
return this
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Initializes the application.
|
|
155
|
+
*
|
|
156
|
+
* This method:
|
|
157
|
+
* - Loads all modules and their dependencies
|
|
158
|
+
* - Sets up the HTTP server if an adapter is configured
|
|
159
|
+
* - Calls onModuleInit hooks on all modules
|
|
160
|
+
* - Initializes registered plugins
|
|
161
|
+
* - Marks the application as initialized
|
|
162
|
+
*
|
|
163
|
+
* Must be called before `listen()`.
|
|
164
|
+
*
|
|
165
|
+
* @throws Error if app module is not set
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
170
|
+
* adapter: defineFastifyEnvironment(),
|
|
171
|
+
* })
|
|
172
|
+
* await app.init()
|
|
173
|
+
* await app.listen({ port: 3000 })
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
65
176
|
async init() {
|
|
66
177
|
if (!this.appModule) {
|
|
67
178
|
throw new Error('App module is not set. Call setAppModule() first.')
|
|
@@ -70,6 +181,7 @@ export class NaviosApplication {
|
|
|
70
181
|
if (this.environment.hasHttpSetup()) {
|
|
71
182
|
await this.httpApplication?.setupHttpServer(this.options)
|
|
72
183
|
}
|
|
184
|
+
await this.initPlugins()
|
|
73
185
|
await this.initModules()
|
|
74
186
|
if (this.environment.hasHttpSetup()) {
|
|
75
187
|
await this.httpApplication?.ready()
|
|
@@ -84,6 +196,44 @@ export class NaviosApplication {
|
|
|
84
196
|
await this.httpApplication?.onModulesInit(modules)
|
|
85
197
|
}
|
|
86
198
|
|
|
199
|
+
private async initPlugins() {
|
|
200
|
+
if (this.plugins.length === 0) return
|
|
201
|
+
|
|
202
|
+
let server: any = null
|
|
203
|
+
try {
|
|
204
|
+
server = this.httpApplication?.getServer() ?? null
|
|
205
|
+
} catch {
|
|
206
|
+
// ignore
|
|
207
|
+
}
|
|
208
|
+
const context: PluginContext = {
|
|
209
|
+
modules: this.moduleLoader.getAllModules(),
|
|
210
|
+
server,
|
|
211
|
+
container: this.container,
|
|
212
|
+
globalPrefix: this.httpApplication?.getGlobalPrefix() ?? '',
|
|
213
|
+
moduleLoader: this.moduleLoader,
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const { plugin, options } of this.plugins) {
|
|
217
|
+
this.logger.debug(`Initializing plugin: ${plugin.name}`)
|
|
218
|
+
await plugin.register(context, options)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Enables CORS (Cross-Origin Resource Sharing) for the application.
|
|
224
|
+
*
|
|
225
|
+
* @param options - CORS configuration options (adapter-specific)
|
|
226
|
+
* @throws Error if HTTP application is not set
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* app.enableCors({
|
|
231
|
+
* origin: ['http://localhost:3000', 'https://example.com'],
|
|
232
|
+
* methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
|
233
|
+
* credentials: true,
|
|
234
|
+
* })
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
87
237
|
enableCors(options: any) {
|
|
88
238
|
if (!this.httpApplication) {
|
|
89
239
|
throw new Error('HTTP application is not set')
|
|
@@ -91,6 +241,21 @@ export class NaviosApplication {
|
|
|
91
241
|
this.httpApplication.enableCors(options)
|
|
92
242
|
}
|
|
93
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Enables multipart/form-data support for file uploads.
|
|
246
|
+
*
|
|
247
|
+
* @param options - Multipart configuration options (adapter-specific)
|
|
248
|
+
* @throws Error if HTTP application is not set
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* app.enableMultipart({
|
|
253
|
+
* limits: {
|
|
254
|
+
* fileSize: 1024 * 1024 * 10, // 10MB
|
|
255
|
+
* },
|
|
256
|
+
* })
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
94
259
|
enableMultipart(options: any) {
|
|
95
260
|
if (!this.httpApplication) {
|
|
96
261
|
throw new Error('HTTP application is not set')
|
|
@@ -98,6 +263,18 @@ export class NaviosApplication {
|
|
|
98
263
|
this.httpApplication.enableMultipart(options)
|
|
99
264
|
}
|
|
100
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Sets a global prefix for all routes.
|
|
268
|
+
*
|
|
269
|
+
* @param prefix - The prefix to prepend to all route URLs (e.g., '/api')
|
|
270
|
+
* @throws Error if HTTP application is not set
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* app.setGlobalPrefix('/api/v1')
|
|
275
|
+
* // All routes will be prefixed with /api/v1
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
101
278
|
setGlobalPrefix(prefix: string) {
|
|
102
279
|
if (!this.httpApplication) {
|
|
103
280
|
throw new Error('HTTP application is not set')
|
|
@@ -105,6 +282,22 @@ export class NaviosApplication {
|
|
|
105
282
|
this.httpApplication.setGlobalPrefix(prefix)
|
|
106
283
|
}
|
|
107
284
|
|
|
285
|
+
/**
|
|
286
|
+
* Gets the underlying HTTP server instance.
|
|
287
|
+
*
|
|
288
|
+
* The type of the returned server depends on the adapter used:
|
|
289
|
+
* - Fastify adapter: Returns FastifyInstance
|
|
290
|
+
* - Bun adapter: Returns Bun.Server
|
|
291
|
+
*
|
|
292
|
+
* @returns The HTTP server instance
|
|
293
|
+
* @throws Error if HTTP application is not set
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* const server = app.getServer()
|
|
298
|
+
* // Use adapter-specific server methods
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
108
301
|
getServer() {
|
|
109
302
|
if (!this.httpApplication) {
|
|
110
303
|
throw new Error('HTTP application is not set')
|
|
@@ -112,6 +305,17 @@ export class NaviosApplication {
|
|
|
112
305
|
return this.httpApplication.getServer()
|
|
113
306
|
}
|
|
114
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Starts the HTTP server and begins listening for requests.
|
|
310
|
+
*
|
|
311
|
+
* @param options - Listen options (port, host, etc.)
|
|
312
|
+
* @throws Error if HTTP application is not set
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* await app.listen({ port: 3000, host: '0.0.0.0' })
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
115
319
|
async listen(options: AbstractHttpListenOptions) {
|
|
116
320
|
if (!this.httpApplication) {
|
|
117
321
|
throw new Error('HTTP application is not set')
|
|
@@ -119,6 +323,12 @@ export class NaviosApplication {
|
|
|
119
323
|
await this.httpApplication.listen(options)
|
|
120
324
|
}
|
|
121
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Disposes of application resources.
|
|
328
|
+
*
|
|
329
|
+
* Cleans up the HTTP server and module loader.
|
|
330
|
+
* This method is called automatically by `close()`.
|
|
331
|
+
*/
|
|
122
332
|
async dispose() {
|
|
123
333
|
if (this.httpApplication) {
|
|
124
334
|
await this.httpApplication.dispose()
|
|
@@ -128,6 +338,20 @@ export class NaviosApplication {
|
|
|
128
338
|
}
|
|
129
339
|
}
|
|
130
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Closes the application and cleans up all resources.
|
|
343
|
+
*
|
|
344
|
+
* This is an alias for `dispose()`.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```typescript
|
|
348
|
+
* // Graceful shutdown
|
|
349
|
+
* process.on('SIGTERM', async () => {
|
|
350
|
+
* await app.close()
|
|
351
|
+
* process.exit(0)
|
|
352
|
+
* })
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
131
355
|
async close() {
|
|
132
356
|
await this.dispose()
|
|
133
357
|
}
|
package/src/navios.factory.mts
CHANGED
|
@@ -16,14 +16,72 @@ import { ConsoleLogger, isNil, LoggerOutput } from './logger/index.mjs'
|
|
|
16
16
|
import { NaviosApplication } from './navios.application.mjs'
|
|
17
17
|
import { NaviosEnvironment } from './navios.environment.mjs'
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Factory class for creating and configuring Navios applications.
|
|
21
|
+
*
|
|
22
|
+
* This is the main entry point for bootstrapping a Navios application.
|
|
23
|
+
* It handles dependency injection container setup, adapter registration,
|
|
24
|
+
* and logger configuration.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { NaviosFactory } from '@navios/core'
|
|
29
|
+
* import { defineFastifyEnvironment } from '@navios/adapter-fastify'
|
|
30
|
+
*
|
|
31
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
32
|
+
* adapter: defineFastifyEnvironment(),
|
|
33
|
+
* logger: ['log', 'error', 'warn'],
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* await app.init()
|
|
37
|
+
* await app.listen({ port: 3000 })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
19
40
|
export class NaviosFactory {
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new Navios application instance.
|
|
43
|
+
*
|
|
44
|
+
* This method sets up the dependency injection container, registers the HTTP adapter,
|
|
45
|
+
* configures logging, and initializes the application with the provided module.
|
|
46
|
+
*
|
|
47
|
+
* @param appModule - The root application module class decorated with @Module()
|
|
48
|
+
* @param options - Configuration options for the application
|
|
49
|
+
* @param options.adapter - HTTP adapter environment (required for HTTP server functionality)
|
|
50
|
+
* @param options.logger - Logger configuration. Can be:
|
|
51
|
+
* - A LoggerService instance for custom logging
|
|
52
|
+
* - An array of LogLevel strings to enable specific log levels
|
|
53
|
+
* - `false` to disable logging
|
|
54
|
+
* @param options.container - Optional custom dependency injection container (useful for testing)
|
|
55
|
+
* @returns A configured NaviosApplication instance ready to be initialized
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Basic setup with Fastify adapter
|
|
60
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
61
|
+
* adapter: defineFastifyEnvironment(),
|
|
62
|
+
* })
|
|
63
|
+
*
|
|
64
|
+
* // With custom logger configuration
|
|
65
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
66
|
+
* adapter: defineFastifyEnvironment(),
|
|
67
|
+
* logger: ['error', 'warn', 'log'],
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* // With custom container for testing
|
|
71
|
+
* const container = new Container()
|
|
72
|
+
* const app = await NaviosFactory.create(AppModule, {
|
|
73
|
+
* adapter: defineFastifyEnvironment(),
|
|
74
|
+
* container,
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
20
78
|
static async create(
|
|
21
79
|
appModule: ClassTypeWithInstance<NaviosModule>,
|
|
22
80
|
options: NaviosApplicationOptions = {
|
|
23
81
|
adapter: [],
|
|
24
82
|
},
|
|
25
83
|
) {
|
|
26
|
-
const container = new Container()
|
|
84
|
+
const container = options.container ?? new Container()
|
|
27
85
|
await this.registerLoggerConfiguration(container, options)
|
|
28
86
|
const adapters = Array.isArray(options.adapter)
|
|
29
87
|
? options.adapter
|
|
@@ -61,6 +119,7 @@ export class NaviosFactory {
|
|
|
61
119
|
loggerInstance?.setup({
|
|
62
120
|
logLevels: logger,
|
|
63
121
|
})
|
|
122
|
+
return
|
|
64
123
|
}
|
|
65
124
|
if ((logger as boolean) !== true && !isNil(logger)) {
|
|
66
125
|
container
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ClassTypeWithInstance } from '@navios/di'
|
|
1
|
+
import type { ClassTypeWithInstance, ScopedContainer } from '@navios/di'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { inject, Injectable, InjectionToken } from '@navios/di'
|
|
4
4
|
|
|
5
5
|
import type {
|
|
6
6
|
AbstractExecutionContext,
|
|
@@ -13,20 +13,24 @@ import type {
|
|
|
13
13
|
} from '../metadata/index.mjs'
|
|
14
14
|
|
|
15
15
|
import { HttpException } from '../exceptions/index.mjs'
|
|
16
|
+
import { Logger } from '../logger/index.mjs'
|
|
16
17
|
|
|
17
18
|
@Injectable()
|
|
18
19
|
export class GuardRunnerService {
|
|
19
|
-
|
|
20
|
+
private readonly logger = inject(Logger, {
|
|
21
|
+
context: GuardRunnerService.name,
|
|
22
|
+
})
|
|
20
23
|
async runGuards(
|
|
21
24
|
allGuards: Set<
|
|
22
25
|
| ClassTypeWithInstance<CanActivate>
|
|
23
26
|
| InjectionToken<CanActivate, undefined>
|
|
24
27
|
>,
|
|
25
28
|
executionContext: AbstractExecutionContext,
|
|
29
|
+
context: ScopedContainer,
|
|
26
30
|
) {
|
|
27
31
|
let canActivate = true
|
|
28
32
|
for (const guard of Array.from(allGuards).reverse()) {
|
|
29
|
-
const guardInstance = await
|
|
33
|
+
const guardInstance = await context.get(
|
|
30
34
|
guard as InjectionToken<CanActivate, undefined>,
|
|
31
35
|
)
|
|
32
36
|
if (!guardInstance.canActivate) {
|
|
@@ -47,13 +51,10 @@ export class GuardRunnerService {
|
|
|
47
51
|
.send(error.response)
|
|
48
52
|
return false
|
|
49
53
|
} else {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
message: 'Internal server error',
|
|
55
|
-
error: (error as Error).message,
|
|
56
|
-
})
|
|
54
|
+
this.logger.error('Error running guard', error)
|
|
55
|
+
executionContext.getReply().status(500).send({
|
|
56
|
+
message: 'Internal server error',
|
|
57
|
+
})
|
|
57
58
|
return false
|
|
58
59
|
}
|
|
59
60
|
}
|