@navios/core 0.8.0 → 0.9.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/lib/{index-BDNl7j1G.d.cts → index-B2ulzZIr.d.cts} +65 -13
  3. package/lib/index-B2ulzZIr.d.cts.map +1 -0
  4. package/lib/{index-BoP0cWT6.d.mts → index-C8lUQePd.d.mts} +65 -13
  5. package/lib/index-C8lUQePd.d.mts.map +1 -0
  6. package/lib/index.cjs +5 -2
  7. package/lib/index.d.cts +2 -2
  8. package/lib/index.d.mts +2 -2
  9. package/lib/index.mjs +3 -3
  10. package/lib/legacy-compat/index.cjs +133 -1
  11. package/lib/legacy-compat/index.cjs.map +1 -1
  12. package/lib/legacy-compat/index.d.cts +219 -7
  13. package/lib/legacy-compat/index.d.cts.map +1 -1
  14. package/lib/legacy-compat/index.d.mts +219 -7
  15. package/lib/legacy-compat/index.d.mts.map +1 -1
  16. package/lib/legacy-compat/index.mjs +128 -2
  17. package/lib/legacy-compat/index.mjs.map +1 -1
  18. package/lib/{src-gBAChVRL.mjs → src-Baabu2R8.mjs} +17 -12
  19. package/lib/src-Baabu2R8.mjs.map +1 -0
  20. package/lib/{src-B6eISODM.cjs → src-Cu9OAnfp.cjs} +16 -11
  21. package/lib/src-Cu9OAnfp.cjs.map +1 -0
  22. package/lib/testing/index.cjs +346 -29
  23. package/lib/testing/index.cjs.map +1 -1
  24. package/lib/testing/index.d.cts +299 -63
  25. package/lib/testing/index.d.cts.map +1 -1
  26. package/lib/testing/index.d.mts +299 -63
  27. package/lib/testing/index.d.mts.map +1 -1
  28. package/lib/testing/index.mjs +347 -31
  29. package/lib/testing/index.mjs.map +1 -1
  30. package/lib/{use-guards.decorator-CUww54Nt.mjs → use-guards.decorator-ChJVzYLW.mjs} +38 -9
  31. package/lib/use-guards.decorator-ChJVzYLW.mjs.map +1 -0
  32. package/lib/{use-guards.decorator-COR-9mZY.cjs → use-guards.decorator-EvI2m2DK.cjs} +56 -9
  33. package/lib/use-guards.decorator-EvI2m2DK.cjs.map +1 -0
  34. package/package.json +4 -4
  35. package/src/__tests__/controller-resolver.spec.mts +19 -13
  36. package/src/__tests__/testing-module.spec.mts +459 -0
  37. package/src/__tests__/unit-testing-module.spec.mts +424 -0
  38. package/src/attribute.factory.mts +19 -3
  39. package/src/decorators/controller.decorator.mts +19 -2
  40. package/src/decorators/module.decorator.mts +23 -5
  41. package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +114 -10
  42. package/src/legacy-compat/attribute.factory.mts +365 -0
  43. package/src/legacy-compat/context-compat.mts +2 -0
  44. package/src/legacy-compat/decorators/index.mts +1 -0
  45. package/src/legacy-compat/decorators/injectable.decorator.mts +41 -0
  46. package/src/legacy-compat/decorators/multipart.decorator.mts +4 -4
  47. package/src/legacy-compat/decorators/stream.decorator.mts +21 -14
  48. package/src/legacy-compat/index.mts +14 -3
  49. package/src/metadata/index.mts +1 -0
  50. package/src/metadata/navios-managed.metadata.mts +42 -0
  51. package/src/navios.application.mts +9 -9
  52. package/src/navios.environment.mts +3 -1
  53. package/src/navios.factory.mts +9 -27
  54. package/src/services/instance-resolver.service.mts +8 -7
  55. package/src/services/module-loader.service.mts +3 -2
  56. package/src/testing/index.mts +1 -0
  57. package/src/testing/testing-module.mts +255 -93
  58. package/src/testing/unit-testing-module.mts +298 -0
  59. package/lib/index-BDNl7j1G.d.cts.map +0 -1
  60. package/lib/index-BoP0cWT6.d.mts.map +0 -1
  61. package/lib/src-B6eISODM.cjs.map +0 -1
  62. package/lib/src-gBAChVRL.mjs.map +0 -1
  63. package/lib/use-guards.decorator-COR-9mZY.cjs.map +0 -1
  64. package/lib/use-guards.decorator-CUww54Nt.mjs.map +0 -1
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * Legacy-compatible decorators for projects that cannot use Stage 3 decorators.
3
- *
3
+ *
4
4
  * These decorators use the TypeScript experimental decorator API and convert
5
5
  * the arguments to Stage 3 format internally.
6
- *
6
+ *
7
7
  * @example
8
8
  * ```typescript
9
9
  * import { Module, Controller, Endpoint } from '@navios/core/legacy-compat'
10
- *
10
+ *
11
11
  * @Module({
12
12
  * controllers: [UserController],
13
13
  * })
@@ -36,5 +36,16 @@ export {
36
36
  HttpCode,
37
37
  Multipart,
38
38
  Stream,
39
+ Injectable,
39
40
  } from './decorators/index.mjs'
40
41
 
42
+ // Export legacy-compatible AttributeFactory
43
+ export {
44
+ AttributeFactory,
45
+ LegacyAttributeFactory,
46
+ type LegacyClassAttribute,
47
+ type LegacyClassSchemaAttribute,
48
+ } from './attribute.factory.mjs'
49
+
50
+ // Export context compatibility utilities
51
+ export * from './context-compat.mjs'
@@ -1,3 +1,4 @@
1
1
  export * from './controller.metadata.mjs'
2
2
  export * from './handler.metadata.mjs'
3
3
  export * from './module.metadata.mjs'
4
+ export * from './navios-managed.metadata.mjs'
@@ -0,0 +1,42 @@
1
+ import type { ClassType } from '@navios/di'
2
+
3
+ /**
4
+ * Symbol used to identify Navios-managed classes (Controller, Module, MessageController, etc.).
5
+ * This allows AttributeFactory to work with any Navios-managed class type generically.
6
+ */
7
+ export const NaviosManagedMetadataKey = Symbol('NaviosManagedMetadataKey')
8
+
9
+ /**
10
+ * Interface for metadata that has custom attributes.
11
+ * Used by Navios-managed classes (Controller, Module, MessageController, etc.).
12
+ */
13
+ export interface ManagedMetadata {
14
+ customAttributes: Map<string | symbol, any>
15
+ }
16
+
17
+ /**
18
+ * Gets managed metadata from a class if it exists.
19
+ * This is a generic function that works with any Navios-managed class type.
20
+ *
21
+ * @param target - The class to check
22
+ * @returns The metadata with customAttributes, or null if not managed
23
+ */
24
+ export function getManagedMetadata(target: ClassType): ManagedMetadata | null {
25
+ // @ts-expect-error - Checking for managed metadata key
26
+ if (target[NaviosManagedMetadataKey]) {
27
+ // @ts-expect-error - Accessing managed metadata
28
+ return target[NaviosManagedMetadataKey] as ManagedMetadata
29
+ }
30
+ return null
31
+ }
32
+
33
+ /**
34
+ * Checks if a class is Navios-managed (has managed metadata).
35
+ *
36
+ * @param target - The class to check
37
+ * @returns true if the class is Navios-managed
38
+ */
39
+ export function hasManagedMetadata(target: ClassType): boolean {
40
+ return getManagedMetadata(target) !== null
41
+ }
42
+
@@ -1,4 +1,4 @@
1
- import type { ClassTypeWithInstance } from '@navios/di'
1
+ import type { ClassTypeWithInstance, Registry } from '@navios/di'
2
2
 
3
3
  import { Container, inject, Injectable } from '@navios/di'
4
4
 
@@ -19,9 +19,9 @@ import { ModuleLoaderService } from './services/index.mjs'
19
19
 
20
20
  /**
21
21
  * Options for configuring the Navios application context.
22
- * These options control dependency injection and logging behavior.
22
+ * These options control the application configuration.
23
23
  */
24
- export interface NaviosApplicationContextOptions {
24
+ export interface NaviosApplicationOptions {
25
25
  /**
26
26
  * Specifies the logger to use. Pass `false` to turn off logging.
27
27
  *
@@ -31,18 +31,18 @@ export interface NaviosApplicationContextOptions {
31
31
  */
32
32
  logger?: LoggerService | LogLevel[] | false
33
33
 
34
+ /**
35
+ * Specifies a custom registry to use. Useful for testing.
36
+ * If not provided, a new Registry will be created.
37
+ */
38
+ registry?: Registry
39
+
34
40
  /**
35
41
  * Specifies a custom container to use. Useful for testing.
36
42
  * If not provided, a new Container will be created.
37
43
  */
38
44
  container?: Container
39
- }
40
45
 
41
- /**
42
- * Complete options for creating a Navios application.
43
- * Extends NaviosApplicationContextOptions with adapter configuration.
44
- */
45
- export interface NaviosApplicationOptions extends NaviosApplicationContextOptions {
46
46
  /**
47
47
  * HTTP adapter environment(s) to use for the application.
48
48
  * Can be a single adapter or an array of adapters.
@@ -17,7 +17,9 @@ export class NaviosEnvironment {
17
17
  setupHttpEnvironment(
18
18
  tokens: Map<InjectionToken<any, undefined>, AnyInjectableType>,
19
19
  ) {
20
- this.httpTokens = tokens
20
+ for (const [token, value] of tokens) {
21
+ this.httpTokens.set(token, value)
22
+ }
21
23
  }
22
24
 
23
25
  getHttpToken(token: InjectionToken<any, undefined>) {
@@ -4,13 +4,10 @@ import type {
4
4
  InjectionToken,
5
5
  } from '@navios/di'
6
6
 
7
- import { Container, InjectableScope, InjectableType } from '@navios/di'
7
+ import { Container } from '@navios/di'
8
8
 
9
9
  import type { NaviosModule } from './interfaces/index.mjs'
10
- import type {
11
- NaviosApplicationContextOptions,
12
- NaviosApplicationOptions,
13
- } from './navios.application.mjs'
10
+ import type { NaviosApplicationOptions } from './navios.application.mjs'
14
11
 
15
12
  import { ConsoleLogger, isNil, LoggerOutput } from './logger/index.mjs'
16
13
  import { NaviosApplication } from './navios.application.mjs'
@@ -83,7 +80,7 @@ export class NaviosFactory {
83
80
  adapter: [],
84
81
  },
85
82
  ) {
86
- const container = options.container ?? new Container()
83
+ const container = options.container ?? new Container(options.registry)
87
84
 
88
85
  // Set request ID flag early, before any adapters are registered
89
86
  if (options.enableRequestId === true) {
@@ -91,15 +88,7 @@ export class NaviosFactory {
91
88
  }
92
89
 
93
90
  // Store options in container for DI access by adapters
94
- container
95
- .getServiceLocator()
96
- .getManager()
97
- .storeCreatedHolder(
98
- NaviosOptionsToken.toString(),
99
- options,
100
- InjectableType.Class,
101
- InjectableScope.Singleton,
102
- )
91
+ container.addInstance(NaviosOptionsToken, options)
103
92
 
104
93
  await this.registerLoggerConfiguration(container, options)
105
94
  const adapters = Array.isArray(options.adapter)
@@ -128,28 +117,21 @@ export class NaviosFactory {
128
117
 
129
118
  private static async registerLoggerConfiguration(
130
119
  container: Container,
131
- options: NaviosApplicationContextOptions,
120
+ options: NaviosApplicationOptions,
132
121
  ) {
133
122
  const { logger } = options
134
- if (Array.isArray(logger) || isNil(logger)) {
123
+ if (Array.isArray(logger) || isNil(logger) || options.enableRequestId) {
135
124
  const loggerInstance = (await container.get(
136
125
  LoggerOutput,
137
126
  )) as ConsoleLogger
138
127
  loggerInstance?.setup({
139
- logLevels: logger,
128
+ logLevels: Array.isArray(logger) ? logger : undefined,
129
+ requestId: options.enableRequestId ?? false,
140
130
  })
141
131
  return
142
132
  }
143
133
  if ((logger as boolean) !== true && !isNil(logger)) {
144
- container
145
- .getServiceLocator()
146
- .getManager()
147
- .storeCreatedHolder(
148
- LoggerOutput.toString(),
149
- logger,
150
- InjectableType.Class,
151
- InjectableScope.Singleton,
152
- )
134
+ container.addInstance(LoggerOutput, logger)
153
135
  }
154
136
  }
155
137
  }
@@ -110,16 +110,13 @@ export class InstanceResolverService {
110
110
  // Class has request-scoped dependencies, update its scope to Request
111
111
  // so it will be resolved per-request from the scoped container
112
112
  const token = getInjectableToken(classType)
113
- this.container
114
- .getRegistry()
115
- .updateScope(token, InjectableScope.Request)
113
+ this.container.getRegistry().updateScope(token, InjectableScope.Request)
116
114
  }
117
115
 
118
116
  return {
119
117
  cached: cachedInstance !== null,
120
118
  instance: cachedInstance,
121
- resolve: (scoped: ScopedContainer) =>
122
- scoped.get(classType) as Promise<T>,
119
+ resolve: (scoped: ScopedContainer) => scoped.get(classType) as Promise<T>,
123
120
  }
124
121
  }
125
122
 
@@ -133,7 +130,9 @@ export class InstanceResolverService {
133
130
  * @param classTypes - The classes to resolve
134
131
  * @returns A resolution result containing either all cached instances or resolver function
135
132
  */
136
- async resolveMany<T>(classTypes: ClassType[]): Promise<MultiInstanceResolution<T>> {
133
+ async resolveMany<T>(
134
+ classTypes: ClassType[],
135
+ ): Promise<MultiInstanceResolution<T>> {
137
136
  if (classTypes.length === 0) {
138
137
  return {
139
138
  cached: true,
@@ -170,7 +169,9 @@ export class InstanceResolverService {
170
169
  instances: cachedInstances,
171
170
  classTypes,
172
171
  resolve: (scoped: ScopedContainer) =>
173
- Promise.all(classTypes.map((classType) => scoped.get(classType) as Promise<T>)),
172
+ Promise.all(
173
+ classTypes.map((classType) => scoped.get(classType) as Promise<T>),
174
+ ),
174
175
  }
175
176
  }
176
177
  }
@@ -1,6 +1,6 @@
1
1
  import type { ClassType, ClassTypeWithInstance } from '@navios/di'
2
2
 
3
- import { Container, inject, Injectable } from '@navios/di'
3
+ import { Container, getInjectableToken, inject, Injectable } from '@navios/di'
4
4
 
5
5
  import type { NaviosModule } from '../interfaces/index.mjs'
6
6
  import type { ModuleMetadata } from '../metadata/index.mjs'
@@ -135,7 +135,8 @@ export class ModuleLoaderService {
135
135
  if (parentMetadata) {
136
136
  this.mergeMetadata(metadata, parentMetadata)
137
137
  }
138
- const moduleName = module.name
138
+ const moduleToken = getInjectableToken(module)
139
+ const moduleName = moduleToken.id
139
140
  if (this.modulesMetadata.has(moduleName)) {
140
141
  return
141
142
  }
@@ -1,2 +1,3 @@
1
1
  export * from '@navios/di/testing'
2
2
  export * from './testing-module.mjs'
3
+ export * from './unit-testing-module.mjs'