@navios/core 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/lib/{index-BFwNx9WQ.d.cts → index-BDNl7j1G.d.cts} +666 -292
- package/lib/index-BDNl7j1G.d.cts.map +1 -0
- package/lib/{index-D657ijFO.d.mts → index-BoP0cWT6.d.mts} +666 -292
- package/lib/index-BoP0cWT6.d.mts.map +1 -0
- package/lib/index.cjs +15 -6
- package/lib/index.d.cts +2 -2
- package/lib/index.d.mts +2 -2
- package/lib/index.mjs +3 -3
- package/lib/legacy-compat/index.cjs +1 -1
- package/lib/legacy-compat/index.cjs.map +1 -1
- package/lib/legacy-compat/index.d.cts +1 -1
- package/lib/legacy-compat/index.d.cts.map +1 -1
- package/lib/legacy-compat/index.d.mts +1 -1
- package/lib/legacy-compat/index.d.mts.map +1 -1
- package/lib/legacy-compat/index.mjs +1 -1
- package/lib/legacy-compat/index.mjs.map +1 -1
- package/lib/{src-Cb1aTjl0.cjs → src-B6eISODM.cjs} +622 -114
- package/lib/src-B6eISODM.cjs.map +1 -0
- package/lib/{src-DzPY5s6d.mjs → src-gBAChVRL.mjs} +563 -73
- package/lib/src-gBAChVRL.mjs.map +1 -0
- package/lib/testing/index.cjs +6 -6
- package/lib/testing/index.cjs.map +1 -1
- package/lib/testing/index.d.cts +1 -1
- package/lib/testing/index.d.mts +1 -1
- package/lib/testing/index.mjs +2 -2
- package/lib/{use-guards.decorator-DdvUhB03.cjs → use-guards.decorator-COR-9mZY.cjs} +34 -108
- package/lib/use-guards.decorator-COR-9mZY.cjs.map +1 -0
- package/lib/{use-guards.decorator-kZ3lNK8v.mjs → use-guards.decorator-CUww54Nt.mjs} +14 -94
- package/lib/use-guards.decorator-CUww54Nt.mjs.map +1 -0
- package/package.json +12 -12
- package/src/__tests__/controller-resolver.spec.mts +223 -0
- package/src/__tests__/controller.spec.mts +1 -1
- package/src/decorators/controller.decorator.mts +11 -6
- package/src/decorators/endpoint.decorator.mts +60 -12
- package/src/decorators/multipart.decorator.mts +67 -24
- package/src/decorators/stream.decorator.mts +65 -24
- package/src/interfaces/abstract-http-adapter.interface.mts +52 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +31 -1
- package/src/interfaces/index.mts +1 -0
- package/src/interfaces/plugin.interface.mts +105 -0
- package/src/legacy-compat/decorators/endpoint.decorator.mts +1 -1
- package/src/legacy-compat/decorators/multipart.decorator.mts +1 -1
- package/src/legacy-compat/decorators/stream.decorator.mts +1 -1
- package/src/logger/logger.service.mts +3 -3
- package/src/navios.application.mts +91 -27
- package/src/navios.factory.mts +29 -9
- package/src/services/guard-runner.service.mts +46 -9
- package/src/services/index.mts +1 -0
- package/src/services/instance-resolver.service.mts +186 -0
- package/src/services/module-loader.service.mts +102 -1
- package/src/stores/request-id.store.mts +45 -3
- package/src/tokens/index.mts +1 -0
- package/src/tokens/navios-options.token.mts +6 -0
- package/lib/index-BFwNx9WQ.d.cts.map +0 -1
- package/lib/index-D657ijFO.d.mts.map +0 -1
- package/lib/src-Cb1aTjl0.cjs.map +0 -1
- package/lib/src-DzPY5s6d.mjs.map +0 -1
- package/lib/use-guards.decorator-DdvUhB03.cjs.map +0 -1
- package/lib/use-guards.decorator-kZ3lNK8v.mjs.map +0 -1
- package/src/attribute.factory.d.mts +0 -175
- package/src/attribute.factory.d.mts.map +0 -1
- package/src/config/config-service.interface.d.mts +0 -39
- package/src/config/config-service.interface.d.mts.map +0 -1
- package/src/config/config.provider.d.mts +0 -46
- package/src/config/config.provider.d.mts.map +0 -1
- package/src/config/config.service.d.mts +0 -108
- package/src/config/config.service.d.mts.map +0 -1
- package/src/config/index.d.mts +0 -6
- package/src/config/index.d.mts.map +0 -1
- package/src/config/types.d.mts +0 -13
- package/src/config/types.d.mts.map +0 -1
- package/src/config/utils/helpers.d.mts +0 -3
- package/src/config/utils/helpers.d.mts.map +0 -1
- package/src/config/utils/index.d.mts +0 -2
- package/src/config/utils/index.d.mts.map +0 -1
- package/src/decorators/controller.decorator.d.mts +0 -33
- package/src/decorators/controller.decorator.d.mts.map +0 -1
- package/src/decorators/endpoint.decorator.d.mts +0 -89
- package/src/decorators/endpoint.decorator.d.mts.map +0 -1
- package/src/decorators/header.decorator.d.mts +0 -22
- package/src/decorators/header.decorator.d.mts.map +0 -1
- package/src/decorators/http-code.decorator.d.mts +0 -22
- package/src/decorators/http-code.decorator.d.mts.map +0 -1
- package/src/decorators/index.d.mts +0 -9
- package/src/decorators/index.d.mts.map +0 -1
- package/src/decorators/module.decorator.d.mts +0 -42
- package/src/decorators/module.decorator.d.mts.map +0 -1
- package/src/decorators/multipart.decorator.d.mts +0 -54
- package/src/decorators/multipart.decorator.d.mts.map +0 -1
- package/src/decorators/stream.decorator.d.mts +0 -42
- package/src/decorators/stream.decorator.d.mts.map +0 -1
- package/src/decorators/use-guards.decorator.d.mts +0 -33
- package/src/decorators/use-guards.decorator.d.mts.map +0 -1
- package/src/exceptions/bad-request.exception.d.mts +0 -26
- package/src/exceptions/bad-request.exception.d.mts.map +0 -1
- package/src/exceptions/conflict.exception.d.mts +0 -29
- package/src/exceptions/conflict.exception.d.mts.map +0 -1
- package/src/exceptions/forbidden.exception.d.mts +0 -28
- package/src/exceptions/forbidden.exception.d.mts.map +0 -1
- package/src/exceptions/http.exception.d.mts +0 -33
- package/src/exceptions/http.exception.d.mts.map +0 -1
- package/src/exceptions/index.d.mts +0 -8
- package/src/exceptions/index.d.mts.map +0 -1
- package/src/exceptions/internal-server-error.exception.d.mts +0 -31
- package/src/exceptions/internal-server-error.exception.d.mts.map +0 -1
- package/src/exceptions/not-found.exception.d.mts +0 -30
- package/src/exceptions/not-found.exception.d.mts.map +0 -1
- package/src/exceptions/unauthorized.exception.d.mts +0 -28
- package/src/exceptions/unauthorized.exception.d.mts.map +0 -1
- package/src/factories/endpoint-adapter.factory.d.mts +0 -6
- package/src/factories/endpoint-adapter.factory.d.mts.map +0 -1
- package/src/factories/http-adapter.factory.d.mts +0 -6
- package/src/factories/http-adapter.factory.d.mts.map +0 -1
- package/src/factories/index.d.mts +0 -8
- package/src/factories/index.d.mts.map +0 -1
- package/src/factories/multipart-adapter.factory.d.mts +0 -6
- package/src/factories/multipart-adapter.factory.d.mts.map +0 -1
- package/src/factories/reply.factory.d.mts +0 -6
- package/src/factories/reply.factory.d.mts.map +0 -1
- package/src/factories/request.factory.d.mts +0 -6
- package/src/factories/request.factory.d.mts.map +0 -1
- package/src/factories/stream-adapter.factory.d.mts +0 -6
- package/src/factories/stream-adapter.factory.d.mts.map +0 -1
- package/src/factories/xml-stream-adapter.factory.d.mts +0 -6
- package/src/factories/xml-stream-adapter.factory.d.mts.map +0 -1
- package/src/index.d.mts +0 -15
- package/src/index.d.mts.map +0 -1
- package/src/interfaces/abstract-execution-context.inteface.d.mts +0 -44
- package/src/interfaces/abstract-execution-context.inteface.d.mts.map +0 -1
- package/src/interfaces/abstract-http-adapter.interface.d.mts +0 -15
- package/src/interfaces/abstract-http-adapter.interface.d.mts.map +0 -1
- package/src/interfaces/abstract-http-cors-options.interface.d.mts +0 -58
- package/src/interfaces/abstract-http-cors-options.interface.d.mts.map +0 -1
- package/src/interfaces/abstract-http-handler-adapter.interface.d.mts +0 -7
- package/src/interfaces/abstract-http-handler-adapter.interface.d.mts.map +0 -1
- package/src/interfaces/abstract-http-listen-options.interface.d.mts +0 -5
- package/src/interfaces/abstract-http-listen-options.interface.d.mts.map +0 -1
- package/src/interfaces/can-activate.d.mts +0 -36
- package/src/interfaces/can-activate.d.mts.map +0 -1
- package/src/interfaces/http-header.d.mts +0 -10
- package/src/interfaces/http-header.d.mts.map +0 -1
- package/src/interfaces/index.d.mts +0 -9
- package/src/interfaces/index.d.mts.map +0 -1
- package/src/interfaces/navios-module.d.mts +0 -29
- package/src/interfaces/navios-module.d.mts.map +0 -1
- package/src/legacy-compat/context-compat.d.mts +0 -19
- package/src/legacy-compat/context-compat.d.mts.map +0 -1
- package/src/legacy-compat/decorators/controller.decorator.d.mts +0 -21
- package/src/legacy-compat/decorators/controller.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/endpoint.decorator.d.mts +0 -33
- package/src/legacy-compat/decorators/endpoint.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/header.decorator.d.mts +0 -24
- package/src/legacy-compat/decorators/header.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/http-code.decorator.d.mts +0 -22
- package/src/legacy-compat/decorators/http-code.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/index.d.mts +0 -9
- package/src/legacy-compat/decorators/index.d.mts.map +0 -1
- package/src/legacy-compat/decorators/module.decorator.d.mts +0 -22
- package/src/legacy-compat/decorators/module.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/multipart.decorator.d.mts +0 -34
- package/src/legacy-compat/decorators/multipart.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/stream.decorator.d.mts +0 -34
- package/src/legacy-compat/decorators/stream.decorator.d.mts.map +0 -1
- package/src/legacy-compat/decorators/use-guards.decorator.d.mts +0 -29
- package/src/legacy-compat/decorators/use-guards.decorator.d.mts.map +0 -1
- package/src/legacy-compat/index.d.mts +0 -19
- package/src/legacy-compat/index.d.mts.map +0 -1
- package/src/logger/console-logger.service.d.mts +0 -196
- package/src/logger/console-logger.service.d.mts.map +0 -1
- package/src/logger/index.d.mts +0 -7
- package/src/logger/index.d.mts.map +0 -1
- package/src/logger/log-levels.d.mts +0 -14
- package/src/logger/log-levels.d.mts.map +0 -1
- package/src/logger/logger-service.interface.d.mts +0 -36
- package/src/logger/logger-service.interface.d.mts.map +0 -1
- package/src/logger/logger.service.d.mts +0 -57
- package/src/logger/logger.service.d.mts.map +0 -1
- package/src/logger/logger.tokens.d.mts +0 -36
- package/src/logger/logger.tokens.d.mts.map +0 -1
- package/src/logger/utils/cli-colors.util.d.mts +0 -10
- package/src/logger/utils/cli-colors.util.d.mts.map +0 -1
- package/src/logger/utils/filter-log-levelts.util.d.mts +0 -6
- package/src/logger/utils/filter-log-levelts.util.d.mts.map +0 -1
- package/src/logger/utils/index.d.mts +0 -6
- package/src/logger/utils/index.d.mts.map +0 -1
- package/src/logger/utils/is-log-level-enabled.d.mts +0 -8
- package/src/logger/utils/is-log-level-enabled.d.mts.map +0 -1
- package/src/logger/utils/is-log-level.util.d.mts +0 -6
- package/src/logger/utils/is-log-level.util.d.mts.map +0 -1
- package/src/logger/utils/shared.utils.d.mts +0 -14
- package/src/logger/utils/shared.utils.d.mts.map +0 -1
- package/src/metadata/controller.metadata.d.mts +0 -13
- package/src/metadata/controller.metadata.d.mts.map +0 -1
- package/src/metadata/handler.metadata.d.mts +0 -18
- package/src/metadata/handler.metadata.d.mts.map +0 -1
- package/src/metadata/index.d.mts +0 -4
- package/src/metadata/index.d.mts.map +0 -1
- package/src/metadata/module.metadata.d.mts +0 -13
- package/src/metadata/module.metadata.d.mts.map +0 -1
- package/src/navios.application.d.mts +0 -210
- package/src/navios.application.d.mts.map +0 -1
- package/src/navios.environment.d.mts +0 -11
- package/src/navios.environment.d.mts.map +0 -1
- package/src/navios.factory.d.mts +0 -68
- package/src/navios.factory.d.mts.map +0 -1
- package/src/services/guard-runner.service.d.mts +0 -10
- package/src/services/guard-runner.service.d.mts.map +0 -1
- package/src/services/index.d.mts +0 -3
- package/src/services/index.d.mts.map +0 -1
- package/src/services/module-loader.service.d.mts +0 -17
- package/src/services/module-loader.service.d.mts.map +0 -1
- package/src/stores/index.d.mts +0 -2
- package/src/stores/index.d.mts.map +0 -1
- package/src/stores/request-id.store.d.mts +0 -37
- package/src/stores/request-id.store.d.mts.map +0 -1
- package/src/tokens/endpoint-adapter.token.d.mts +0 -4
- package/src/tokens/endpoint-adapter.token.d.mts.map +0 -1
- package/src/tokens/execution-context.token.d.mts +0 -5
- package/src/tokens/execution-context.token.d.mts.map +0 -1
- package/src/tokens/http-adapter.token.d.mts +0 -4
- package/src/tokens/http-adapter.token.d.mts.map +0 -1
- package/src/tokens/index.d.mts +0 -9
- package/src/tokens/index.d.mts.map +0 -1
- package/src/tokens/multipart-adapter.token.d.mts +0 -4
- package/src/tokens/multipart-adapter.token.d.mts.map +0 -1
- package/src/tokens/reply.token.d.mts +0 -3
- package/src/tokens/reply.token.d.mts.map +0 -1
- package/src/tokens/request.token.d.mts +0 -3
- package/src/tokens/request.token.d.mts.map +0 -1
- package/src/tokens/stream-adapter.token.d.mts +0 -4
- package/src/tokens/stream-adapter.token.d.mts.map +0 -1
- package/src/tokens/xml-stream-adapter.token.d.mts +0 -4
- package/src/tokens/xml-stream-adapter.token.d.mts.map +0 -1
|
@@ -11,9 +11,9 @@ import { StreamAdapterToken } from '../tokens/index.mjs'
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Extracts the typed parameters for a stream endpoint handler function.
|
|
14
|
-
*
|
|
14
|
+
*
|
|
15
15
|
* Similar to `EndpointParams`, but specifically for streaming endpoints.
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
17
|
* @typeParam EndpointDeclaration - The stream endpoint declaration from @navios/builder
|
|
18
18
|
*/
|
|
19
19
|
export type StreamParams<
|
|
@@ -46,20 +46,20 @@ export type StreamParams<
|
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Decorator that marks a method as a streaming endpoint.
|
|
49
|
-
*
|
|
49
|
+
*
|
|
50
50
|
* Use this decorator for endpoints that stream data (e.g., file downloads, SSE).
|
|
51
51
|
* The endpoint must be defined using @navios/builder's `declareStream` method.
|
|
52
|
-
*
|
|
52
|
+
*
|
|
53
53
|
* @param endpoint - The stream endpoint declaration from @navios/builder
|
|
54
54
|
* @returns A method decorator
|
|
55
|
-
*
|
|
55
|
+
*
|
|
56
56
|
* @example
|
|
57
57
|
* ```typescript
|
|
58
58
|
* const downloadFileEndpoint = api.declareStream({
|
|
59
59
|
* method: 'get',
|
|
60
60
|
* url: '/files/$fileId',
|
|
61
61
|
* })
|
|
62
|
-
*
|
|
62
|
+
*
|
|
63
63
|
* @Controller()
|
|
64
64
|
* export class FileController {
|
|
65
65
|
* @Stream(downloadFileEndpoint)
|
|
@@ -70,6 +70,51 @@ export type StreamParams<
|
|
|
70
70
|
* }
|
|
71
71
|
* ```
|
|
72
72
|
*/
|
|
73
|
+
export function Stream<
|
|
74
|
+
Method extends HttpMethod = HttpMethod,
|
|
75
|
+
Url extends string = string,
|
|
76
|
+
QuerySchema = undefined,
|
|
77
|
+
RequestSchema = ZodType,
|
|
78
|
+
Params = QuerySchema extends ZodObject
|
|
79
|
+
? RequestSchema extends ZodType
|
|
80
|
+
? EndpointFunctionArgs<Url, QuerySchema, RequestSchema, true>
|
|
81
|
+
: EndpointFunctionArgs<Url, QuerySchema, undefined, true>
|
|
82
|
+
: RequestSchema extends ZodType
|
|
83
|
+
? EndpointFunctionArgs<Url, undefined, RequestSchema, true>
|
|
84
|
+
: EndpointFunctionArgs<Url, undefined, undefined, true>,
|
|
85
|
+
>(endpoint: {
|
|
86
|
+
config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
|
|
87
|
+
}): (
|
|
88
|
+
target: (params: Params, reply: any) => any,
|
|
89
|
+
context: ClassMethodDecoratorContext,
|
|
90
|
+
) => void
|
|
91
|
+
// Bun doesn't support reply parameter
|
|
92
|
+
export function Stream<
|
|
93
|
+
Method extends HttpMethod = HttpMethod,
|
|
94
|
+
Url extends string = string,
|
|
95
|
+
QuerySchema = undefined,
|
|
96
|
+
RequestSchema = ZodType,
|
|
97
|
+
Params = QuerySchema extends ZodObject
|
|
98
|
+
? RequestSchema extends ZodType
|
|
99
|
+
? EndpointFunctionArgs<Url, QuerySchema, RequestSchema, true>
|
|
100
|
+
: EndpointFunctionArgs<Url, QuerySchema, undefined, true>
|
|
101
|
+
: RequestSchema extends ZodType
|
|
102
|
+
? EndpointFunctionArgs<Url, undefined, RequestSchema, true>
|
|
103
|
+
: EndpointFunctionArgs<Url, undefined, undefined, true>,
|
|
104
|
+
>(endpoint: {
|
|
105
|
+
config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
|
|
106
|
+
}): (
|
|
107
|
+
target: (params: Params) => any,
|
|
108
|
+
context: ClassMethodDecoratorContext,
|
|
109
|
+
) => void
|
|
110
|
+
export function Stream<
|
|
111
|
+
Method extends HttpMethod = HttpMethod,
|
|
112
|
+
Url extends string = string,
|
|
113
|
+
QuerySchema = undefined,
|
|
114
|
+
RequestSchema = ZodType,
|
|
115
|
+
>(endpoint: {
|
|
116
|
+
config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
|
|
117
|
+
}): (target: () => any, context: ClassMethodDecoratorContext) => void
|
|
73
118
|
export function Stream<
|
|
74
119
|
Method extends HttpMethod = HttpMethod,
|
|
75
120
|
Url extends string = string,
|
|
@@ -78,24 +123,20 @@ export function Stream<
|
|
|
78
123
|
>(endpoint: {
|
|
79
124
|
config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
|
|
80
125
|
}) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
throw new Error(
|
|
96
|
-
'[Navios] Endpoint decorator can only be used on functions.',
|
|
97
|
-
)
|
|
98
|
-
}
|
|
126
|
+
type Params = QuerySchema extends ZodObject
|
|
127
|
+
? RequestSchema extends ZodType
|
|
128
|
+
? EndpointFunctionArgs<Url, QuerySchema, RequestSchema, true>
|
|
129
|
+
: EndpointFunctionArgs<Url, QuerySchema, undefined, true>
|
|
130
|
+
: RequestSchema extends ZodType
|
|
131
|
+
? EndpointFunctionArgs<Url, undefined, RequestSchema, true>
|
|
132
|
+
: EndpointFunctionArgs<Url, undefined, undefined, true>
|
|
133
|
+
|
|
134
|
+
type Handler =
|
|
135
|
+
| ((params: Params, reply: any) => any)
|
|
136
|
+
| ((params: Params) => any)
|
|
137
|
+
| (() => any)
|
|
138
|
+
|
|
139
|
+
return (target: Handler, context: ClassMethodDecoratorContext) => {
|
|
99
140
|
if (context.kind !== 'method') {
|
|
100
141
|
throw new Error(
|
|
101
142
|
'[Navios] Endpoint decorator can only be used on methods.',
|
|
@@ -2,19 +2,71 @@ import type { ModuleMetadata } from '../metadata/index.mjs'
|
|
|
2
2
|
import type { AbstractHttpCorsOptions } from './abstract-http-cors-options.interface.mjs'
|
|
3
3
|
import type { AbstractHttpListenOptions } from './abstract-http-listen-options.interface.mjs'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Abstract interface for HTTP adapters.
|
|
7
|
+
*
|
|
8
|
+
* Adapters implement this interface to provide runtime-specific HTTP server
|
|
9
|
+
* functionality (Fastify, Bun, etc.).
|
|
10
|
+
*
|
|
11
|
+
* @typeParam ServerInstance - The underlying server type (e.g., FastifyInstance)
|
|
12
|
+
* @typeParam CorsOptions - CORS configuration options type
|
|
13
|
+
* @typeParam Options - Server setup options type
|
|
14
|
+
* @typeParam MultipartOptions - Multipart form handling options type
|
|
15
|
+
*/
|
|
5
16
|
export interface AbstractHttpAdapterInterface<
|
|
6
17
|
ServerInstance,
|
|
7
18
|
CorsOptions = AbstractHttpCorsOptions,
|
|
8
19
|
Options = {},
|
|
9
20
|
MultipartOptions = {},
|
|
10
21
|
> {
|
|
22
|
+
/**
|
|
23
|
+
* Sets up the HTTP server with the provided options.
|
|
24
|
+
*/
|
|
11
25
|
setupHttpServer(options: Options): Promise<void>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Called after all modules are loaded to register routes.
|
|
29
|
+
*/
|
|
12
30
|
onModulesInit(modules: Map<string, ModuleMetadata>): Promise<void>
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Signals that the server is ready to accept requests.
|
|
34
|
+
*/
|
|
13
35
|
ready(): Promise<void>
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the underlying HTTP server instance.
|
|
39
|
+
*/
|
|
14
40
|
getServer(): ServerInstance
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Sets a global prefix for all routes.
|
|
44
|
+
*/
|
|
15
45
|
setGlobalPrefix(prefix: string): void
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Gets the current global prefix.
|
|
49
|
+
* Returns empty string if no prefix is set.
|
|
50
|
+
*/
|
|
51
|
+
getGlobalPrefix(): string
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Enables CORS with the specified options.
|
|
55
|
+
*/
|
|
16
56
|
enableCors(options: CorsOptions): void
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Enables multipart form data handling.
|
|
60
|
+
*/
|
|
17
61
|
enableMultipart(options: MultipartOptions): void
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Starts the server and listens for incoming requests.
|
|
65
|
+
*/
|
|
18
66
|
listen(options: AbstractHttpListenOptions): Promise<string>
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Disposes of the server and cleans up resources.
|
|
70
|
+
*/
|
|
19
71
|
dispose(): Promise<void>
|
|
20
72
|
}
|
|
@@ -2,6 +2,36 @@ import type { ClassType, ScopedContainer } from '@navios/di'
|
|
|
2
2
|
|
|
3
3
|
import type { HandlerMetadata } from '../metadata/index.mjs'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Static handler result - handler can be called without a scoped container.
|
|
7
|
+
* Used when the controller and all its dependencies are singletons.
|
|
8
|
+
*/
|
|
9
|
+
export type StaticHandler<TRequest = any, TReply = any> = {
|
|
10
|
+
isStatic: true
|
|
11
|
+
handler: (request: TRequest, reply: TReply) => Promise<any>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Dynamic handler result - handler requires a scoped container for resolution.
|
|
16
|
+
* Used when the controller or its dependencies need per-request resolution.
|
|
17
|
+
*/
|
|
18
|
+
export type DynamicHandler<TRequest = any, TReply = any> = {
|
|
19
|
+
isStatic: false
|
|
20
|
+
handler: (
|
|
21
|
+
scoped: ScopedContainer,
|
|
22
|
+
request: TRequest,
|
|
23
|
+
reply: TReply,
|
|
24
|
+
) => Promise<any>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Handler result returned by provideHandler.
|
|
29
|
+
* Can be either static (pre-resolved) or dynamic (needs scoped container).
|
|
30
|
+
*/
|
|
31
|
+
export type HandlerResult<TRequest = any, TReply = any> =
|
|
32
|
+
| StaticHandler<TRequest, TReply>
|
|
33
|
+
| DynamicHandler<TRequest, TReply>
|
|
34
|
+
|
|
5
35
|
export interface AbstractHttpHandlerAdapterInterface {
|
|
6
36
|
prepareArguments?: (
|
|
7
37
|
handlerMetadata: HandlerMetadata<any>,
|
|
@@ -9,5 +39,5 @@ export interface AbstractHttpHandlerAdapterInterface {
|
|
|
9
39
|
provideHandler: (
|
|
10
40
|
controller: ClassType,
|
|
11
41
|
handlerMetadata: HandlerMetadata<any>,
|
|
12
|
-
) =>
|
|
42
|
+
) => Promise<HandlerResult>
|
|
13
43
|
}
|
package/src/interfaces/index.mts
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { Container } from '@navios/di'
|
|
2
|
+
|
|
3
|
+
import type { ModuleMetadata } from '../metadata/index.mjs'
|
|
4
|
+
import type { ModuleLoaderService } from '../services/module-loader.service.mjs'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Context provided to plugins during registration.
|
|
8
|
+
*
|
|
9
|
+
* This context gives plugins access to the application's modules,
|
|
10
|
+
* server instance, DI container, and configuration.
|
|
11
|
+
*/
|
|
12
|
+
export interface PluginContext {
|
|
13
|
+
/**
|
|
14
|
+
* All loaded modules with their metadata.
|
|
15
|
+
* Keys are module class names, values are their metadata.
|
|
16
|
+
*/
|
|
17
|
+
modules: Map<string, ModuleMetadata>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The underlying HTTP server instance.
|
|
21
|
+
* Type depends on the adapter used (Fastify, Bun, etc.)
|
|
22
|
+
*/
|
|
23
|
+
server: any
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The dependency injection container.
|
|
27
|
+
*/
|
|
28
|
+
container: Container
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Global route prefix (e.g., '/api/v1').
|
|
32
|
+
* Empty string if no prefix is set.
|
|
33
|
+
*/
|
|
34
|
+
globalPrefix: string
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Module loader service for extending the module tree.
|
|
38
|
+
* Use `moduleLoader.extendModules()` to add controllers dynamically.
|
|
39
|
+
*/
|
|
40
|
+
moduleLoader: ModuleLoaderService
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Base interface for Navios plugins.
|
|
45
|
+
*
|
|
46
|
+
* Plugins are registered using `app.usePlugin()` and are initialized
|
|
47
|
+
* after all modules are loaded but before the server starts listening.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam TOptions - The type of options the plugin accepts
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const myPlugin: NaviosPlugin<{ enabled: boolean }> = {
|
|
54
|
+
* name: 'my-plugin',
|
|
55
|
+
* register: async (context, options) => {
|
|
56
|
+
* if (options.enabled) {
|
|
57
|
+
* // Register routes, services, etc.
|
|
58
|
+
* }
|
|
59
|
+
* },
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export interface NaviosPlugin<TOptions = unknown> {
|
|
64
|
+
/**
|
|
65
|
+
* Plugin name for identification and logging.
|
|
66
|
+
*/
|
|
67
|
+
name: string
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Called after modules are loaded but before the server starts listening.
|
|
71
|
+
*
|
|
72
|
+
* @param context - The plugin context with access to modules and server
|
|
73
|
+
* @param options - Plugin-specific configuration options
|
|
74
|
+
*/
|
|
75
|
+
register(context: PluginContext, options: TOptions): Promise<void> | void
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Plugin definition combining a plugin with its options.
|
|
80
|
+
*
|
|
81
|
+
* This is the type returned by plugin factory functions like `defineOpenApiPlugin()`.
|
|
82
|
+
*
|
|
83
|
+
* @typeParam TOptions - The type of options the plugin accepts
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* function defineMyPlugin(options: MyPluginOptions): PluginDefinition<MyPluginOptions> {
|
|
88
|
+
* return {
|
|
89
|
+
* plugin: myPlugin,
|
|
90
|
+
* options,
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export interface PluginDefinition<TOptions = unknown> {
|
|
96
|
+
/**
|
|
97
|
+
* The plugin instance.
|
|
98
|
+
*/
|
|
99
|
+
plugin: NaviosPlugin<TOptions>
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Options to pass to the plugin's register function.
|
|
103
|
+
*/
|
|
104
|
+
options: TOptions
|
|
105
|
+
}
|
|
@@ -92,7 +92,7 @@ export function Endpoint<
|
|
|
92
92
|
// @ts-expect-error - we don't need to type the value
|
|
93
93
|
const result = originalDecorator(typedDescriptor.value, context)
|
|
94
94
|
if (result !== typedDescriptor.value) {
|
|
95
|
-
typedDescriptor.value = result
|
|
95
|
+
typedDescriptor.value = result as any
|
|
96
96
|
}
|
|
97
97
|
return typedDescriptor
|
|
98
98
|
}
|
|
@@ -86,7 +86,7 @@ export function Multipart<
|
|
|
86
86
|
// @ts-expect-error - we don't need to type the value
|
|
87
87
|
const result = originalDecorator(descriptor.value, context)
|
|
88
88
|
if (result !== descriptor.value) {
|
|
89
|
-
descriptor.value = result
|
|
89
|
+
descriptor.value = result as any
|
|
90
90
|
}
|
|
91
91
|
return descriptor
|
|
92
92
|
}
|
|
@@ -69,7 +69,7 @@ export function Stream<
|
|
|
69
69
|
// @ts-expect-error - we don't need to type the value
|
|
70
70
|
const result = originalDecorator(descriptor.value, context)
|
|
71
71
|
if (result !== descriptor.value) {
|
|
72
|
-
descriptor.value = result
|
|
72
|
+
descriptor.value = result as any
|
|
73
73
|
}
|
|
74
74
|
return descriptor
|
|
75
75
|
}
|
|
@@ -7,16 +7,16 @@ import { Logger, LoggerOutput } from './logger.tokens.mjs'
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Logger service instance that can be injected into services and controllers.
|
|
10
|
-
*
|
|
10
|
+
*
|
|
11
11
|
* Provides contextualized logging with automatic context injection.
|
|
12
12
|
* The context is set when the logger is injected using the `inject` function.
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```typescript
|
|
16
16
|
* @Injectable()
|
|
17
17
|
* export class UserService {
|
|
18
18
|
* private logger = inject(Logger, { context: UserService.name })
|
|
19
|
-
*
|
|
19
|
+
*
|
|
20
20
|
* async findUser(id: string) {
|
|
21
21
|
* this.logger.log(`Finding user ${id}`)
|
|
22
22
|
* // Logs with context: [UserService]
|
|
@@ -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'
|
|
@@ -22,7 +24,7 @@ import { ModuleLoaderService } from './services/index.mjs'
|
|
|
22
24
|
export interface NaviosApplicationContextOptions {
|
|
23
25
|
/**
|
|
24
26
|
* Specifies the logger to use. Pass `false` to turn off logging.
|
|
25
|
-
*
|
|
27
|
+
*
|
|
26
28
|
* - `LoggerService` instance: Use a custom logger implementation
|
|
27
29
|
* - `LogLevel[]`: Enable specific log levels (e.g., ['error', 'warn', 'log'])
|
|
28
30
|
* - `false`: Disable logging completely
|
|
@@ -40,12 +42,11 @@ export interface NaviosApplicationContextOptions {
|
|
|
40
42
|
* Complete options for creating a Navios application.
|
|
41
43
|
* Extends NaviosApplicationContextOptions with adapter configuration.
|
|
42
44
|
*/
|
|
43
|
-
export interface NaviosApplicationOptions
|
|
44
|
-
extends NaviosApplicationContextOptions {
|
|
45
|
+
export interface NaviosApplicationOptions extends NaviosApplicationContextOptions {
|
|
45
46
|
/**
|
|
46
47
|
* HTTP adapter environment(s) to use for the application.
|
|
47
48
|
* Can be a single adapter or an array of adapters.
|
|
48
|
-
*
|
|
49
|
+
*
|
|
49
50
|
* @example
|
|
50
51
|
* ```typescript
|
|
51
52
|
* adapter: defineFastifyEnvironment()
|
|
@@ -54,20 +55,34 @@ export interface NaviosApplicationOptions
|
|
|
54
55
|
* ```
|
|
55
56
|
*/
|
|
56
57
|
adapter: NaviosEnvironmentOptions | NaviosEnvironmentOptions[]
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether to validate response schemas.
|
|
61
|
+
* When `false`, response schema validation is skipped for better performance.
|
|
62
|
+
* @default true
|
|
63
|
+
*/
|
|
64
|
+
validateResponses?: boolean
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether to enable request ID propagation via AsyncLocalStorage.
|
|
68
|
+
* When `true`, request IDs are available via `getRequestId()` throughout the request.
|
|
69
|
+
* @default false
|
|
70
|
+
*/
|
|
71
|
+
enableRequestId?: boolean
|
|
57
72
|
}
|
|
58
73
|
|
|
59
74
|
/**
|
|
60
75
|
* Main application class for Navios.
|
|
61
|
-
*
|
|
76
|
+
*
|
|
62
77
|
* This class represents a Navios application instance and provides methods
|
|
63
78
|
* for initializing, configuring, and managing the HTTP server.
|
|
64
|
-
*
|
|
79
|
+
*
|
|
65
80
|
* @example
|
|
66
81
|
* ```typescript
|
|
67
82
|
* const app = await NaviosFactory.create(AppModule, {
|
|
68
83
|
* adapter: defineFastifyEnvironment(),
|
|
69
84
|
* })
|
|
70
|
-
*
|
|
85
|
+
*
|
|
71
86
|
* app.setGlobalPrefix('/api')
|
|
72
87
|
* app.enableCors({ origin: ['http://localhost:3000'] })
|
|
73
88
|
* await app.init()
|
|
@@ -88,6 +103,7 @@ export class NaviosApplication {
|
|
|
88
103
|
private options: NaviosApplicationOptions = {
|
|
89
104
|
adapter: [],
|
|
90
105
|
}
|
|
106
|
+
private plugins: PluginDefinition<any>[] = []
|
|
91
107
|
|
|
92
108
|
/**
|
|
93
109
|
* Indicates whether the application has been initialized.
|
|
@@ -98,7 +114,7 @@ export class NaviosApplication {
|
|
|
98
114
|
/**
|
|
99
115
|
* Sets up the application with the provided module and options.
|
|
100
116
|
* This is called automatically by NaviosFactory.create().
|
|
101
|
-
*
|
|
117
|
+
*
|
|
102
118
|
* @param appModule - The root application module
|
|
103
119
|
* @param options - Application configuration options
|
|
104
120
|
* @internal
|
|
@@ -118,26 +134,50 @@ export class NaviosApplication {
|
|
|
118
134
|
|
|
119
135
|
/**
|
|
120
136
|
* Gets the dependency injection container used by this application.
|
|
121
|
-
*
|
|
137
|
+
*
|
|
122
138
|
* @returns The Container instance
|
|
123
139
|
*/
|
|
124
140
|
getContainer() {
|
|
125
141
|
return this.container
|
|
126
142
|
}
|
|
127
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Registers a plugin to be initialized after modules are loaded.
|
|
146
|
+
*
|
|
147
|
+
* Plugins are initialized in the order they are registered,
|
|
148
|
+
* after all modules are loaded but before the server starts listening.
|
|
149
|
+
*
|
|
150
|
+
* @param definition - Plugin definition with options
|
|
151
|
+
* @returns this for method chaining
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* import { defineOpenApiPlugin } from '@navios/openapi-fastify'
|
|
156
|
+
*
|
|
157
|
+
* app.usePlugin(defineOpenApiPlugin({
|
|
158
|
+
* info: { title: 'My API', version: '1.0.0' },
|
|
159
|
+
* }))
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
usePlugin<TOptions>(definition: PluginDefinition<TOptions>): this {
|
|
163
|
+
this.plugins.push(definition)
|
|
164
|
+
return this
|
|
165
|
+
}
|
|
166
|
+
|
|
128
167
|
/**
|
|
129
168
|
* Initializes the application.
|
|
130
|
-
*
|
|
169
|
+
*
|
|
131
170
|
* This method:
|
|
132
171
|
* - Loads all modules and their dependencies
|
|
133
172
|
* - Sets up the HTTP server if an adapter is configured
|
|
134
173
|
* - Calls onModuleInit hooks on all modules
|
|
174
|
+
* - Initializes registered plugins
|
|
135
175
|
* - Marks the application as initialized
|
|
136
|
-
*
|
|
176
|
+
*
|
|
137
177
|
* Must be called before `listen()`.
|
|
138
|
-
*
|
|
178
|
+
*
|
|
139
179
|
* @throws Error if app module is not set
|
|
140
|
-
*
|
|
180
|
+
*
|
|
141
181
|
* @example
|
|
142
182
|
* ```typescript
|
|
143
183
|
* const app = await NaviosFactory.create(AppModule, {
|
|
@@ -155,6 +195,7 @@ export class NaviosApplication {
|
|
|
155
195
|
if (this.environment.hasHttpSetup()) {
|
|
156
196
|
await this.httpApplication?.setupHttpServer(this.options)
|
|
157
197
|
}
|
|
198
|
+
await this.initPlugins()
|
|
158
199
|
await this.initModules()
|
|
159
200
|
if (this.environment.hasHttpSetup()) {
|
|
160
201
|
await this.httpApplication?.ready()
|
|
@@ -169,12 +210,35 @@ export class NaviosApplication {
|
|
|
169
210
|
await this.httpApplication?.onModulesInit(modules)
|
|
170
211
|
}
|
|
171
212
|
|
|
213
|
+
private async initPlugins() {
|
|
214
|
+
if (this.plugins.length === 0) return
|
|
215
|
+
|
|
216
|
+
let server: any = null
|
|
217
|
+
try {
|
|
218
|
+
server = this.httpApplication?.getServer() ?? null
|
|
219
|
+
} catch {
|
|
220
|
+
// ignore
|
|
221
|
+
}
|
|
222
|
+
const context: PluginContext = {
|
|
223
|
+
modules: this.moduleLoader.getAllModules(),
|
|
224
|
+
server,
|
|
225
|
+
container: this.container,
|
|
226
|
+
globalPrefix: this.httpApplication?.getGlobalPrefix() ?? '',
|
|
227
|
+
moduleLoader: this.moduleLoader,
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for (const { plugin, options } of this.plugins) {
|
|
231
|
+
this.logger.debug(`Initializing plugin: ${plugin.name}`)
|
|
232
|
+
await plugin.register(context, options)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
172
236
|
/**
|
|
173
237
|
* Enables CORS (Cross-Origin Resource Sharing) for the application.
|
|
174
|
-
*
|
|
238
|
+
*
|
|
175
239
|
* @param options - CORS configuration options (adapter-specific)
|
|
176
240
|
* @throws Error if HTTP application is not set
|
|
177
|
-
*
|
|
241
|
+
*
|
|
178
242
|
* @example
|
|
179
243
|
* ```typescript
|
|
180
244
|
* app.enableCors({
|
|
@@ -193,10 +257,10 @@ export class NaviosApplication {
|
|
|
193
257
|
|
|
194
258
|
/**
|
|
195
259
|
* Enables multipart/form-data support for file uploads.
|
|
196
|
-
*
|
|
260
|
+
*
|
|
197
261
|
* @param options - Multipart configuration options (adapter-specific)
|
|
198
262
|
* @throws Error if HTTP application is not set
|
|
199
|
-
*
|
|
263
|
+
*
|
|
200
264
|
* @example
|
|
201
265
|
* ```typescript
|
|
202
266
|
* app.enableMultipart({
|
|
@@ -215,10 +279,10 @@ export class NaviosApplication {
|
|
|
215
279
|
|
|
216
280
|
/**
|
|
217
281
|
* Sets a global prefix for all routes.
|
|
218
|
-
*
|
|
282
|
+
*
|
|
219
283
|
* @param prefix - The prefix to prepend to all route URLs (e.g., '/api')
|
|
220
284
|
* @throws Error if HTTP application is not set
|
|
221
|
-
*
|
|
285
|
+
*
|
|
222
286
|
* @example
|
|
223
287
|
* ```typescript
|
|
224
288
|
* app.setGlobalPrefix('/api/v1')
|
|
@@ -234,14 +298,14 @@ export class NaviosApplication {
|
|
|
234
298
|
|
|
235
299
|
/**
|
|
236
300
|
* Gets the underlying HTTP server instance.
|
|
237
|
-
*
|
|
301
|
+
*
|
|
238
302
|
* The type of the returned server depends on the adapter used:
|
|
239
303
|
* - Fastify adapter: Returns FastifyInstance
|
|
240
304
|
* - Bun adapter: Returns Bun.Server
|
|
241
|
-
*
|
|
305
|
+
*
|
|
242
306
|
* @returns The HTTP server instance
|
|
243
307
|
* @throws Error if HTTP application is not set
|
|
244
|
-
*
|
|
308
|
+
*
|
|
245
309
|
* @example
|
|
246
310
|
* ```typescript
|
|
247
311
|
* const server = app.getServer()
|
|
@@ -257,10 +321,10 @@ export class NaviosApplication {
|
|
|
257
321
|
|
|
258
322
|
/**
|
|
259
323
|
* Starts the HTTP server and begins listening for requests.
|
|
260
|
-
*
|
|
324
|
+
*
|
|
261
325
|
* @param options - Listen options (port, host, etc.)
|
|
262
326
|
* @throws Error if HTTP application is not set
|
|
263
|
-
*
|
|
327
|
+
*
|
|
264
328
|
* @example
|
|
265
329
|
* ```typescript
|
|
266
330
|
* await app.listen({ port: 3000, host: '0.0.0.0' })
|
|
@@ -275,7 +339,7 @@ export class NaviosApplication {
|
|
|
275
339
|
|
|
276
340
|
/**
|
|
277
341
|
* Disposes of application resources.
|
|
278
|
-
*
|
|
342
|
+
*
|
|
279
343
|
* Cleans up the HTTP server and module loader.
|
|
280
344
|
* This method is called automatically by `close()`.
|
|
281
345
|
*/
|
|
@@ -290,9 +354,9 @@ export class NaviosApplication {
|
|
|
290
354
|
|
|
291
355
|
/**
|
|
292
356
|
* Closes the application and cleans up all resources.
|
|
293
|
-
*
|
|
357
|
+
*
|
|
294
358
|
* This is an alias for `dispose()`.
|
|
295
|
-
*
|
|
359
|
+
*
|
|
296
360
|
* @example
|
|
297
361
|
* ```typescript
|
|
298
362
|
* // Graceful shutdown
|