@navios/core 0.1.0 → 0.1.2

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 (38) hide show
  1. package/dist/index.d.mts +485 -11
  2. package/dist/index.d.ts +485 -11
  3. package/dist/index.js +1028 -136
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +993 -135
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +1 -1
  8. package/src/config/config-service.interface.mts +16 -0
  9. package/src/config/config.provider.mts +63 -0
  10. package/src/config/config.service.mts +69 -0
  11. package/src/config/index.mts +5 -0
  12. package/src/config/types.mts +58 -0
  13. package/src/config/utils/helpers.mts +23 -0
  14. package/src/config/utils/index.mts +1 -0
  15. package/src/index.mts +2 -0
  16. package/src/logger/README.md +3 -0
  17. package/src/logger/console-logger.service.mts +588 -0
  18. package/src/logger/index.mts +7 -0
  19. package/src/logger/log-levels.mts +12 -0
  20. package/src/logger/logger-service.interface.mts +42 -0
  21. package/src/logger/logger.factory.mts +37 -0
  22. package/src/logger/logger.service.mts +214 -0
  23. package/src/logger/pino-wrapper.mts +63 -0
  24. package/src/logger/utils/cli-colors.util.mts +17 -0
  25. package/src/logger/utils/filter-log-levelts.util.mts +29 -0
  26. package/src/logger/utils/index.mts +5 -0
  27. package/src/logger/utils/is-log-level-enabled.mts +33 -0
  28. package/src/logger/utils/is-log-level.util.mts +10 -0
  29. package/src/logger/utils/shared.utils.mts +51 -0
  30. package/src/navios.application.mts +71 -18
  31. package/src/navios.factory.mts +18 -1
  32. package/src/service-locator/inject.mts +6 -1
  33. package/src/service-locator/injection-token.mts +11 -6
  34. package/src/service-locator/proxy-service-locator.mts +28 -9
  35. package/src/service-locator/service-locator.mts +53 -16
  36. package/src/service-locator/sync-injector.mts +6 -1
  37. package/src/services/controller-adapter.service.mts +8 -0
  38. package/src/services/module-loader.service.mts +6 -1
@@ -1,4 +1,4 @@
1
- import type { AnyZodObject, input, output } from 'zod'
1
+ import type { AnyZodObject, output, z, ZodOptional } from 'zod'
2
2
 
3
3
  import type { FactoryNotFound, UnknownError } from './index.mjs'
4
4
  import type { InjectionToken } from './injection-token.mjs'
@@ -33,11 +33,17 @@ export class ProxyServiceLocator implements ServiceLocator {
33
33
  ): void {
34
34
  return this.serviceLocator.registerAbstractFactory(token, factory)
35
35
  }
36
- public getInstance<Instance, Schema extends AnyZodObject | undefined>(
36
+ public getInstance<
37
+ Instance,
38
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
39
+ >(
37
40
  token: InjectionToken<Instance, Schema>,
38
- args: Schema extends AnyZodObject ? input<Schema> : undefined,
41
+ args: Schema extends AnyZodObject
42
+ ? z.input<Schema>
43
+ : Schema extends ZodOptional<AnyZodObject>
44
+ ? z.input<Schema> | undefined
45
+ : undefined,
39
46
  ): Promise<[undefined, Instance] | [UnknownError | FactoryNotFound]> {
40
- // @ts-expect-error
41
47
  return this.ctx.inject(token, args).then(
42
48
  (instance) => {
43
49
  return [undefined, instance]
@@ -47,16 +53,29 @@ export class ProxyServiceLocator implements ServiceLocator {
47
53
  },
48
54
  )
49
55
  }
50
- public getOrThrowInstance<Instance, Schema extends AnyZodObject | undefined>(
56
+ public getOrThrowInstance<
57
+ Instance,
58
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
59
+ >(
51
60
  token: InjectionToken<Instance, Schema>,
52
- args: Schema extends AnyZodObject ? input<Schema> : undefined,
61
+ args: Schema extends AnyZodObject
62
+ ? z.input<Schema>
63
+ : Schema extends ZodOptional<AnyZodObject>
64
+ ? z.input<Schema> | undefined
65
+ : undefined,
53
66
  ): Promise<Instance> {
54
- // @ts-expect-error We need to pass the args to the ctx.inject method
55
67
  return this.ctx.inject(token, args)
56
68
  }
57
- public getSyncInstance<Instance, Schema extends AnyZodObject | undefined>(
69
+ public getSyncInstance<
70
+ Instance,
71
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
72
+ >(
58
73
  token: InjectionToken<Instance, Schema>,
59
- args: Schema extends AnyZodObject ? input<Schema> : undefined,
74
+ args: Schema extends AnyZodObject
75
+ ? z.input<Schema>
76
+ : Schema extends ZodOptional<AnyZodObject>
77
+ ? z.input<Schema> | undefined
78
+ : undefined,
60
79
  ): Instance | null {
61
80
  return this.serviceLocator.getSyncInstance(token, args)
62
81
  }
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  /* eslint-disable @typescript-eslint/no-empty-object-type */
3
- import type { AnyZodObject, z } from 'zod'
3
+ import type { AnyZodObject, z, ZodOptional } from 'zod'
4
4
 
5
5
  import type { ServiceLocatorAbstractFactoryContext } from './service-locator-abstract-factory-context.mjs'
6
6
  import type { ServiceLocatorInstanceHolder } from './service-locator-instance-holder.mjs'
@@ -65,7 +65,7 @@ export class ServiceLocator {
65
65
 
66
66
  public registerAbstractFactory<
67
67
  Instance,
68
- Schema extends AnyZodObject | undefined,
68
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
69
69
  >(
70
70
  token: InjectionToken<Instance, Schema>,
71
71
  factory: (
@@ -88,10 +88,14 @@ export class ServiceLocator {
88
88
 
89
89
  public getInstanceIdentifier<
90
90
  Instance,
91
- Schema extends AnyZodObject | undefined,
91
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
92
92
  >(
93
93
  token: InjectionToken<Instance, Schema>,
94
- args: Schema extends AnyZodObject ? z.input<Schema> : undefined,
94
+ args: Schema extends AnyZodObject
95
+ ? z.input<Schema>
96
+ : Schema extends ZodOptional<AnyZodObject>
97
+ ? z.input<Schema> | undefined
98
+ : undefined,
95
99
  ): string {
96
100
  const validatedArgs = token.schema
97
101
  ? token.schema.safeParse(args)
@@ -106,9 +110,16 @@ export class ServiceLocator {
106
110
  return this.makeInstanceName(token, validatedArgs)
107
111
  }
108
112
 
109
- public async getInstance<Instance, Schema extends AnyZodObject | undefined>(
113
+ public async getInstance<
114
+ Instance,
115
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
116
+ >(
110
117
  token: InjectionToken<Instance, Schema>,
111
- args: Schema extends AnyZodObject ? z.input<Schema> : undefined,
118
+ args: Schema extends AnyZodObject
119
+ ? z.input<Schema>
120
+ : Schema extends ZodOptional<AnyZodObject>
121
+ ? z.input<Schema> | undefined
122
+ : undefined,
112
123
  ): Promise<[undefined, Instance] | [UnknownError | FactoryNotFound]> {
113
124
  const validatedArgs = token.schema
114
125
  ? token.schema.safeParse(args)
@@ -161,10 +172,14 @@ export class ServiceLocator {
161
172
 
162
173
  public async getOrThrowInstance<
163
174
  Instance,
164
- Schema extends AnyZodObject | undefined,
175
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
165
176
  >(
166
177
  token: InjectionToken<Instance, Schema>,
167
- args: Schema extends AnyZodObject ? z.input<Schema> : undefined,
178
+ args: Schema extends AnyZodObject
179
+ ? z.input<Schema>
180
+ : Schema extends ZodOptional<AnyZodObject>
181
+ ? z.input<Schema> | undefined
182
+ : undefined,
168
183
  ): Promise<Instance> {
169
184
  const [error, instance] = await this.getInstance(token, args)
170
185
  if (error) {
@@ -185,11 +200,15 @@ export class ServiceLocator {
185
200
 
186
201
  private async createInstance<
187
202
  Instance,
188
- Schema extends AnyZodObject | undefined,
203
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
189
204
  >(
190
205
  instanceName: string,
191
206
  token: InjectionToken<Instance, Schema>,
192
- args: Schema extends AnyZodObject ? z.input<Schema> : undefined,
207
+ args: Schema extends AnyZodObject
208
+ ? z.input<Schema>
209
+ : Schema extends ZodOptional<AnyZodObject>
210
+ ? z.input<Schema> | undefined
211
+ : undefined,
193
212
  ): Promise<[undefined, Instance] | [FactoryNotFound | UnknownError]> {
194
213
  this.logger?.log(
195
214
  `[ServiceLocator]#createInstance() Creating instance for ${instanceName}`,
@@ -206,8 +225,12 @@ export class ServiceLocator {
206
225
 
207
226
  private async createInstanceFromAbstractFactory<
208
227
  Instance,
209
- Schema extends AnyZodObject | undefined,
210
- Args extends Schema extends AnyZodObject ? z.input<Schema> : undefined,
228
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
229
+ Args extends Schema extends AnyZodObject
230
+ ? z.input<Schema>
231
+ : Schema extends ZodOptional<AnyZodObject>
232
+ ? z.input<Schema> | undefined
233
+ : undefined,
211
234
  >(
212
235
  instanceName: string,
213
236
  token: InjectionToken<Instance, Schema>,
@@ -320,7 +343,6 @@ export class ServiceLocator {
320
343
  : undefined
321
344
  const instanceName = self.makeInstanceName(token, validatedArgs)
322
345
  dependencies.add(instanceName)
323
- // @ts-expect-error TS2322 This is correct type
324
346
  return self.getOrThrowInstance(injectionToken, args)
325
347
  }
326
348
  throw new Error(
@@ -338,9 +360,16 @@ export class ServiceLocator {
338
360
  }
339
361
  }
340
362
 
341
- public getSyncInstance<Instance, Schema extends AnyZodObject | undefined>(
363
+ public getSyncInstance<
364
+ Instance,
365
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
366
+ >(
342
367
  token: InjectionToken<Instance, Schema>,
343
- args: Schema extends AnyZodObject ? z.input<Schema> : undefined,
368
+ args: Schema extends AnyZodObject
369
+ ? z.input<Schema>
370
+ : Schema extends ZodOptional<AnyZodObject>
371
+ ? z.input<Schema> | undefined
372
+ : undefined,
344
373
  ): Instance | null {
345
374
  const validatedArgs = token.schema
346
375
  ? token.schema.safeParse(args)
@@ -427,7 +456,15 @@ export class ServiceLocator {
427
456
  makeInstanceName(token: InjectionToken<any, any>, args: any) {
428
457
  let stringifiedArgs = args
429
458
  ? ':' +
430
- JSON.stringify(args)
459
+ JSON.stringify(args, (_, value) => {
460
+ if (typeof value === 'function') {
461
+ return `function:${value.name}(${value.length})`
462
+ }
463
+ if (typeof value === 'symbol') {
464
+ return value.toString()
465
+ }
466
+ return value
467
+ })
431
468
  .replaceAll(/"/g, '')
432
469
  .replaceAll(/:/g, '=')
433
470
  .replaceAll(/,/g, '|')
@@ -1,4 +1,4 @@
1
- import type { AnyZodObject, z } from 'zod'
1
+ import type { AnyZodObject, z, ZodOptional } from 'zod'
2
2
 
3
3
  import type { ClassType } from './injection-token.mjs'
4
4
 
@@ -13,6 +13,11 @@ export function syncInject<T, S extends AnyZodObject>(
13
13
  token: InjectionToken<T, S>,
14
14
  args: z.input<S>,
15
15
  ): T
16
+ export function syncInject<T, S extends ZodOptional<AnyZodObject>>(
17
+ token: InjectionToken<T, S>,
18
+ args: z.input<S>,
19
+ ): T
20
+
16
21
  export function syncInject<T>(token: InjectionToken<T, undefined>): T
17
22
  export function syncInject<
18
23
  T,
@@ -5,6 +5,7 @@ import type { ModuleMetadata } from '../metadata/index.mjs'
5
5
  import type { ClassType } from '../service-locator/index.mjs'
6
6
 
7
7
  import { HttpException } from '../exceptions/index.mjs'
8
+ import { Logger } from '../logger/index.mjs'
8
9
  import { extractControllerMetadata } from '../metadata/index.mjs'
9
10
  import {
10
11
  getServiceLocator,
@@ -19,6 +20,9 @@ import { GuardRunnerService } from './guard-runner.service.mjs'
19
20
  @Injectable()
20
21
  export class ControllerAdapterService {
21
22
  guardRunner = syncInject(GuardRunnerService)
23
+ private logger = syncInject(Logger, {
24
+ context: ControllerAdapterService.name,
25
+ })
22
26
 
23
27
  setupController(
24
28
  controller: ClassType,
@@ -119,6 +123,10 @@ export class ControllerAdapterService {
119
123
  }
120
124
  },
121
125
  })
126
+
127
+ this.logger.debug(
128
+ `Registered ${httpMethod} ${url} for ${controller.name}:${classMethod}`,
129
+ )
122
130
  }
123
131
  }
124
132
  }
@@ -2,11 +2,15 @@ import type { NaviosModule } from '../interfaces/index.mjs'
2
2
  import type { ModuleMetadata } from '../metadata/index.mjs'
3
3
  import type { ClassTypeWithInstance } from '../service-locator/index.mjs'
4
4
 
5
+ import { Logger } from '../logger/index.mjs'
5
6
  import { extractModuleMetadata } from '../metadata/index.mjs'
6
- import { inject, Injectable } from '../service-locator/index.mjs'
7
+ import { inject, Injectable, syncInject } from '../service-locator/index.mjs'
7
8
 
8
9
  @Injectable()
9
10
  export class ModuleLoaderService {
11
+ private logger = syncInject(Logger, {
12
+ context: ModuleLoaderService.name,
13
+ })
10
14
  private modulesMetadata: Map<string, ModuleMetadata> = new Map()
11
15
  private loadedModules: Map<string, any> = new Map()
12
16
  private initialized = false
@@ -41,6 +45,7 @@ export class ModuleLoaderService {
41
45
  if (instance.onModuleInit) {
42
46
  await instance.onModuleInit()
43
47
  }
48
+ this.logger.debug(`Module ${moduleName} loaded`)
44
49
  this.loadedModules.set(moduleName, instance)
45
50
  }
46
51