@navios/core 0.1.14 → 0.2.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 (71) hide show
  1. package/dist/_tsup-dts-rollup.d.mts +185 -434
  2. package/dist/_tsup-dts-rollup.d.ts +185 -434
  3. package/dist/index.d.mts +62 -46
  4. package/dist/index.d.ts +62 -46
  5. package/dist/index.js +482 -1328
  6. package/dist/index.mjs +484 -1292
  7. package/package.json +3 -2
  8. package/src/adapters/endpoint-adapter.service.mts +72 -0
  9. package/src/adapters/handler-adapter.interface.mts +21 -0
  10. package/src/adapters/index.mts +4 -0
  11. package/src/adapters/multipart-adapter.service.mts +131 -0
  12. package/src/adapters/stream-adapter.service.mts +91 -0
  13. package/src/attribute.factory.mts +14 -14
  14. package/src/config/config.provider.mts +16 -12
  15. package/src/decorators/controller.decorator.mts +3 -8
  16. package/src/decorators/endpoint.decorator.mts +7 -3
  17. package/src/decorators/header.decorator.mts +1 -6
  18. package/src/decorators/http-code.decorator.mts +1 -6
  19. package/src/decorators/module.decorator.mts +16 -21
  20. package/src/decorators/multipart.decorator.mts +7 -3
  21. package/src/decorators/stream.decorator.mts +7 -3
  22. package/src/decorators/use-guards.decorator.mts +3 -2
  23. package/src/index.mts +2 -1
  24. package/src/logger/console-logger.service.mts +3 -2
  25. package/src/logger/logger.factory.mts +4 -5
  26. package/src/logger/logger.service.mts +2 -2
  27. package/src/metadata/controller.metadata.mts +6 -5
  28. package/src/metadata/{endpoint.metadata.mts → handler.metadata.mts} +18 -28
  29. package/src/metadata/index.mts +1 -2
  30. package/src/metadata/module.metadata.mts +3 -2
  31. package/src/navios.application.mts +12 -12
  32. package/src/navios.factory.mts +4 -2
  33. package/src/services/controller-adapter.service.mts +65 -245
  34. package/src/services/execution-context.mts +4 -3
  35. package/src/services/guard-runner.service.mts +4 -6
  36. package/src/services/module-loader.service.mts +4 -2
  37. package/src/tokens/application.token.mts +1 -1
  38. package/src/tokens/execution-context.token.mts +2 -1
  39. package/src/tokens/reply.token.mts +1 -1
  40. package/src/tokens/request.token.mts +1 -1
  41. package/src/metadata/injectable.metadata.mts +0 -11
  42. package/src/service-locator/__tests__/injectable.spec.mts +0 -171
  43. package/src/service-locator/__tests__/injection-token.spec.mts +0 -124
  44. package/src/service-locator/decorators/get-injectable-token.mts +0 -19
  45. package/src/service-locator/decorators/index.mts +0 -2
  46. package/src/service-locator/decorators/injectable.decorator.mts +0 -64
  47. package/src/service-locator/enums/index.mts +0 -1
  48. package/src/service-locator/enums/injectable-scope.enum.mts +0 -10
  49. package/src/service-locator/errors/errors.enum.mts +0 -8
  50. package/src/service-locator/errors/factory-not-found.mts +0 -8
  51. package/src/service-locator/errors/factory-token-not-resolved.mts +0 -10
  52. package/src/service-locator/errors/index.mts +0 -7
  53. package/src/service-locator/errors/instance-destroying.mts +0 -8
  54. package/src/service-locator/errors/instance-expired.mts +0 -8
  55. package/src/service-locator/errors/instance-not-found.mts +0 -8
  56. package/src/service-locator/errors/unknown-error.mts +0 -15
  57. package/src/service-locator/event-emitter.mts +0 -107
  58. package/src/service-locator/index.mts +0 -15
  59. package/src/service-locator/inject.mts +0 -28
  60. package/src/service-locator/injection-token.mts +0 -92
  61. package/src/service-locator/injector.mts +0 -18
  62. package/src/service-locator/interfaces/factory.interface.mts +0 -3
  63. package/src/service-locator/override.mts +0 -22
  64. package/src/service-locator/proxy-service-locator.mts +0 -99
  65. package/src/service-locator/resolve-service.mts +0 -46
  66. package/src/service-locator/service-locator-abstract-factory-context.mts +0 -23
  67. package/src/service-locator/service-locator-event-bus.mts +0 -96
  68. package/src/service-locator/service-locator-instance-holder.mts +0 -63
  69. package/src/service-locator/service-locator-manager.mts +0 -89
  70. package/src/service-locator/service-locator.mts +0 -535
  71. package/src/service-locator/sync-injector.mts +0 -52
@@ -6,7 +6,8 @@ import type {
6
6
  import type { FastifyReply } from 'fastify'
7
7
  import type { AnyZodObject, ZodType } from 'zod'
8
8
 
9
- import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
9
+ import { StreamAdapterToken } from '../adapters/index.mjs'
10
+ import { getEndpointMetadata } from '../metadata/index.mjs'
10
11
 
11
12
  export type StreamParams<
12
13
  EndpointDeclaration extends {
@@ -63,7 +64,10 @@ export function Stream<
63
64
  }
64
65
  const config = endpoint.config
65
66
  if (context.metadata) {
66
- let endpointMetadata = getEndpointMetadata(target, context)
67
+ let endpointMetadata = getEndpointMetadata<BaseStreamConfig>(
68
+ target,
69
+ context,
70
+ )
67
71
  if (endpointMetadata.config && endpointMetadata.config.url) {
68
72
  throw new Error(
69
73
  `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`,
@@ -71,7 +75,7 @@ export function Stream<
71
75
  }
72
76
  // @ts-expect-error We don't need to set correctly in the metadata
73
77
  endpointMetadata.config = config
74
- endpointMetadata.type = EndpointType.Stream
78
+ endpointMetadata.adapterToken = StreamAdapterToken
75
79
  endpointMetadata.classMethod = target.name
76
80
  endpointMetadata.httpMethod = config.method
77
81
  endpointMetadata.url = config.url
@@ -1,9 +1,10 @@
1
- import type { CanActivate } from '../interfaces/index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../interfaces/index.mjs'
7
8
 
8
9
  import {
9
10
  getControllerMetadata,
package/src/index.mts CHANGED
@@ -1,10 +1,11 @@
1
+ export * from '@navios/di'
2
+ export * from './adapters/index.mjs'
1
3
  export * from './config/index.mjs'
2
4
  export * from './decorators/index.mjs'
3
5
  export * from './exceptions/index.mjs'
4
6
  export * from './interfaces/index.mjs'
5
7
  export * from './logger/index.mjs'
6
8
  export * from './metadata/index.mjs'
7
- export * from './service-locator/index.mjs'
8
9
  export * from './services/index.mjs'
9
10
  export * from './tokens/index.mjs'
10
11
  export * from './attribute.factory.mjs'
@@ -2,10 +2,11 @@ import type { InspectOptions } from 'util'
2
2
 
3
3
  import { inspect } from 'util'
4
4
 
5
+ import { getGlobalServiceLocator, Injectable } from '@navios/di'
6
+
5
7
  import type { LogLevel } from './log-levels.mjs'
6
8
  import type { LoggerService } from './logger-service.interface.mjs'
7
9
 
8
- import { getServiceLocator, Injectable } from '../service-locator/index.mjs'
9
10
  import { Request } from '../tokens/index.mjs'
10
11
  import {
11
12
  clc,
@@ -178,7 +179,7 @@ export class ConsoleLogger implements LoggerService {
178
179
  this.originalContext = context
179
180
  }
180
181
  if (opts?.requestId) {
181
- const locator = getServiceLocator()
182
+ const locator = getGlobalServiceLocator()
182
183
  locator
183
184
  .getEventBus()
184
185
  .on(locator.getInstanceIdentifier(Request, undefined), 'create', () => {
@@ -1,10 +1,9 @@
1
+ import { Injectable, InjectableType, InjectionToken } from '@navios/di'
2
+
1
3
  import { z } from 'zod'
2
4
 
3
- import {
4
- Injectable,
5
- InjectableType,
6
- InjectionToken,
7
- } from '../service-locator/index.mjs'
5
+ import type { LoggerService } from './logger-service.interface.mjs'
6
+
8
7
  import { LoggerInstance } from './logger.service.mjs'
9
8
 
10
9
  export const LoggerInjectionToken = 'LoggerInjectionToken'
@@ -1,7 +1,8 @@
1
+ import { Injectable } from '@navios/di'
2
+
1
3
  import type { LogLevel } from './log-levels.mjs'
2
4
  import type { LoggerService } from './logger-service.interface.mjs'
3
5
 
4
- import { Injectable } from '../service-locator/index.mjs'
5
6
  import { ConsoleLogger } from './console-logger.service.mjs'
6
7
  import { isLogLevelEnabled, isObject } from './utils/index.mjs'
7
8
 
@@ -16,7 +17,6 @@ const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
16
17
  month: '2-digit',
17
18
  })
18
19
 
19
- // @ts-expect-error We don't need to support this in the current version
20
20
  @Injectable()
21
21
  export class LoggerInstance implements LoggerService {
22
22
  protected static staticInstanceRef?: LoggerService = DEFAULT_LOGGER
@@ -1,17 +1,18 @@
1
- import type { CanActivate } from '../interfaces/index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
7
- import type { EndpointMetadata } from './endpoint.metadata.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../interfaces/index.mjs'
8
+ import type { HandlerMetadata } from './handler.metadata.mjs'
8
9
 
9
- import { getAllEndpointMetadata } from './endpoint.metadata.mjs'
10
+ import { getAllEndpointMetadata } from './handler.metadata.mjs'
10
11
 
11
12
  export const ControllerMetadataKey = Symbol('ControllerMetadataKey')
12
13
 
13
14
  export interface ControllerMetadata {
14
- endpoints: Set<EndpointMetadata>
15
+ endpoints: Set<HandlerMetadata>
15
16
  guards: Set<
16
17
  ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>
17
18
  >
@@ -1,34 +1,23 @@
1
- import type {
2
- BaseEndpointConfig,
3
- BaseStreamConfig,
4
- HttpMethod,
5
- } from '@navios/common'
1
+ import type { HttpMethod } from '@navios/common'
2
+ import type { ClassTypeWithInstance, InjectionToken } from '@navios/di'
6
3
  import type { HttpHeader } from 'fastify/types/utils.js'
7
4
 
5
+ import type { HandlerAdapterInterface } from '../adapters/index.mjs'
8
6
  import type { CanActivate } from '../interfaces/index.mjs'
9
- import type {
10
- ClassTypeWithInstance,
11
- InjectionToken,
12
- } from '../service-locator/index.mjs'
13
7
 
14
8
  export const EndpointMetadataKey = Symbol('EndpointMetadataKey')
15
9
 
16
- export enum EndpointType {
17
- Unknown = 'unknown',
18
- Endpoint = 'endpoint',
19
- Stream = 'stream',
20
- Multipart = 'multipart',
21
- Handler = 'handler',
22
- }
23
-
24
- export interface EndpointMetadata {
10
+ export interface HandlerMetadata<Config = null> {
25
11
  classMethod: string
26
12
  url: string
27
13
  successStatusCode: number
28
- type: EndpointType
14
+ adapterToken:
15
+ | InjectionToken<HandlerAdapterInterface, undefined>
16
+ | ClassTypeWithInstance<HandlerAdapterInterface>
17
+ | null
29
18
  headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>
30
19
  httpMethod: HttpMethod
31
- config: BaseEndpointConfig | BaseStreamConfig | null
20
+ config: Config
32
21
  guards: Set<
33
22
  ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>
34
23
  >
@@ -37,25 +26,25 @@ export interface EndpointMetadata {
37
26
 
38
27
  export function getAllEndpointMetadata(
39
28
  context: ClassMethodDecoratorContext | ClassDecoratorContext,
40
- ): Set<EndpointMetadata> {
29
+ ): Set<HandlerMetadata<any>> {
41
30
  if (context.metadata) {
42
31
  const metadata = context.metadata[EndpointMetadataKey] as
43
- | Set<EndpointMetadata>
32
+ | Set<HandlerMetadata>
44
33
  | undefined
45
34
  if (metadata) {
46
35
  return metadata
47
36
  } else {
48
- context.metadata[EndpointMetadataKey] = new Set<EndpointMetadata>()
49
- return context.metadata[EndpointMetadataKey] as Set<EndpointMetadata>
37
+ context.metadata[EndpointMetadataKey] = new Set<HandlerMetadata<any>>()
38
+ return context.metadata[EndpointMetadataKey] as Set<HandlerMetadata<any>>
50
39
  }
51
40
  }
52
41
  throw new Error('[Navios] Wrong environment.')
53
42
  }
54
43
 
55
- export function getEndpointMetadata(
44
+ export function getEndpointMetadata<Config = any>(
56
45
  target: Function,
57
46
  context: ClassMethodDecoratorContext,
58
- ): EndpointMetadata {
47
+ ): HandlerMetadata<Config> {
59
48
  if (context.metadata) {
60
49
  const metadata = getAllEndpointMetadata(context)
61
50
  if (metadata) {
@@ -65,13 +54,14 @@ export function getEndpointMetadata(
65
54
  if (endpointMetadata) {
66
55
  return endpointMetadata
67
56
  } else {
68
- const newMetadata: EndpointMetadata = {
57
+ const newMetadata: HandlerMetadata<Config> = {
69
58
  classMethod: target.name,
70
59
  url: '',
71
60
  successStatusCode: 200,
61
+ adapterToken: null,
72
62
  headers: {},
73
- type: EndpointType.Unknown,
74
63
  httpMethod: 'GET',
64
+ // @ts-expect-error We are using a generic type here
75
65
  config: null,
76
66
  guards: new Set<
77
67
  | ClassTypeWithInstance<CanActivate>
@@ -1,4 +1,3 @@
1
1
  export * from './controller.metadata.mjs'
2
- export * from './endpoint.metadata.mjs'
3
- export * from './injectable.metadata.mjs'
2
+ export * from './handler.metadata.mjs'
4
3
  export * from './module.metadata.mjs'
@@ -1,9 +1,10 @@
1
- import type { CanActivate } from '../index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../index.mjs'
7
8
 
8
9
  export const ModuleMetadataKey = Symbol('ControllerMetadataKey')
9
10
 
@@ -1,11 +1,19 @@
1
1
  import type { FastifyCorsOptions } from '@fastify/cors'
2
2
  import type { FastifyMultipartOptions } from '@fastify/multipart'
3
+ import type { ClassTypeWithInstance } from '@navios/di'
3
4
  import type {
4
5
  FastifyInstance,
5
6
  FastifyListenOptions,
6
7
  FastifyServerOptions,
7
8
  } from 'fastify'
8
9
 
10
+ import {
11
+ getGlobalServiceLocator,
12
+ inject,
13
+ Injectable,
14
+ syncInject,
15
+ } from '@navios/di'
16
+
9
17
  import cors from '@fastify/cors'
10
18
  import multipart from '@fastify/multipart'
11
19
  import { fastify } from 'fastify'
@@ -16,16 +24,9 @@ import {
16
24
 
17
25
  import type { NaviosModule } from './interfaces/index.mjs'
18
26
  import type { LoggerService, LogLevel } from './logger/index.mjs'
19
- import type { ClassTypeWithInstance } from './service-locator/index.mjs'
20
27
 
21
28
  import { HttpException } from './exceptions/index.mjs'
22
29
  import { Logger, PinoWrapper } from './logger/index.mjs'
23
- import {
24
- getServiceLocator,
25
- inject,
26
- Injectable,
27
- syncInject,
28
- } from './service-locator/index.mjs'
29
30
  import {
30
31
  ControllerAdapterService,
31
32
  ModuleLoaderService,
@@ -75,7 +76,7 @@ export class NaviosApplication {
75
76
  await this.moduleLoader.loadModules(this.appModule)
76
77
  this.server = await this.getFastifyInstance(this.options)
77
78
  this.configureFastifyInstance(this.server)
78
- getServiceLocator().registerInstance(Application, this.server)
79
+ getGlobalServiceLocator().storeInstance(this.server, Application)
79
80
  // Add schema validator and serializer
80
81
  this.server.setValidatorCompiler(validatorCompiler)
81
82
  this.server.setSerializerCompiler(serializerCompiler)
@@ -149,7 +150,7 @@ export class NaviosApplication {
149
150
  message: 'Not Found',
150
151
  error: 'NotFound',
151
152
  }
152
- this.logger.error(`Route not found: ${req.url}`)
153
+ this.logger.error(`Route not found: [${req.method}] ${req.url}`)
153
154
  return reply.status(404).send(response)
154
155
  })
155
156
  }
@@ -178,15 +179,14 @@ export class NaviosApplication {
178
179
  }
179
180
  promises.push(
180
181
  this.server!.register(
181
- (instance, opts, done) => {
182
+ async (instance, opts) => {
182
183
  for (const controller of moduleMetadata.controllers) {
183
- this.controllerAdapter.setupController(
184
+ await this.controllerAdapter.setupController(
184
185
  controller,
185
186
  instance,
186
187
  moduleMetadata,
187
188
  )
188
189
  }
189
- done()
190
190
  },
191
191
  {
192
192
  prefix: this.globalPrefix ?? '',
@@ -1,13 +1,15 @@
1
+ import type { ClassTypeWithInstance } from '@navios/di'
2
+
3
+ import { inject } from '@navios/di'
4
+
1
5
  import type { NaviosModule } from './interfaces/index.mjs'
2
6
  import type {
3
7
  NaviosApplicationContextOptions,
4
8
  NaviosApplicationOptions,
5
9
  } from './navios.application.mjs'
6
- import type { ClassTypeWithInstance } from './service-locator/index.mjs'
7
10
 
8
11
  import { isNil, LoggerInstance } from './logger/index.mjs'
9
12
  import { NaviosApplication } from './navios.application.mjs'
10
- import { inject } from './service-locator/index.mjs'
11
13
 
12
14
  export class NaviosFactory {
13
15
  static async create(