@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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navios/core",
3
- "version": "0.1.14",
3
+ "version": "0.2.0",
4
4
  "author": {
5
5
  "name": "Oleksandr Hanzha",
6
6
  "email": "alex@granted.name"
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/Arilas/navios.git"
12
12
  },
13
13
  "license": "MIT",
14
- "typings": "./dist/index.d.ts",
14
+ "typings": "./dist/src/index.d.mts",
15
15
  "main": "./dist/index.js",
16
16
  "module": "./dist/index.mjs",
17
17
  "peerDependencies": {
@@ -40,6 +40,7 @@
40
40
  "dependencies": {
41
41
  "@fastify/cors": "^11.0.1",
42
42
  "@fastify/multipart": "^9.0.3",
43
+ "@navios/di": "^0.1.2",
43
44
  "fastify": "^5.3.2",
44
45
  "fastify-type-provider-zod": "^4.0.2"
45
46
  }
@@ -0,0 +1,72 @@
1
+ import type { BaseEndpointConfig } from '@navios/common'
2
+ import type { ClassType } from '@navios/di'
3
+ import type { FastifyReply, FastifyRequest } from 'fastify'
4
+
5
+ import { inject, Injectable, InjectionToken } from '@navios/di'
6
+
7
+ import type { HandlerMetadata } from '../metadata/index.mjs'
8
+ import type { ExecutionContext } from '../services/index.mjs'
9
+
10
+ import { StreamAdapterService } from './stream-adapter.service.mjs'
11
+
12
+ export const EndpointAdapterToken =
13
+ InjectionToken.create<EndpointAdapterService>(
14
+ Symbol.for('EndpointAdapterService'),
15
+ )
16
+
17
+ @Injectable({
18
+ token: EndpointAdapterToken,
19
+ })
20
+ export class EndpointAdapterService extends StreamAdapterService {
21
+ override hasSchema(
22
+ handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
23
+ ): boolean {
24
+ const config = handlerMetadata.config
25
+ return super.hasSchema(handlerMetadata) || !!config.responseSchema
26
+ }
27
+
28
+ override provideSchema(
29
+ handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
30
+ ): Record<string, any> {
31
+ const config = handlerMetadata.config
32
+ const schema = super.provideSchema(handlerMetadata)
33
+ if (config.responseSchema) {
34
+ schema.response = {
35
+ 200: config.responseSchema,
36
+ }
37
+ }
38
+
39
+ return schema
40
+ }
41
+
42
+ override provideHandler(
43
+ controller: ClassType,
44
+ executionContext: ExecutionContext,
45
+ handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
46
+ ): (request: FastifyRequest, reply: FastifyReply) => Promise<any> {
47
+ const getters = this.prepareArguments(handlerMetadata)
48
+ const formatArguments = async (request: FastifyRequest) => {
49
+ const argument: Record<string, any> = {}
50
+ const promises: Promise<void>[] = []
51
+ for (const getter of getters) {
52
+ const res = getter(argument, request)
53
+ if (res instanceof Promise) {
54
+ promises.push(res)
55
+ }
56
+ }
57
+ await Promise.all(promises)
58
+ return argument
59
+ }
60
+
61
+ return async function (request, reply) {
62
+ const controllerInstance = await inject(controller)
63
+ const argument = await formatArguments(request)
64
+ const result =
65
+ await controllerInstance[handlerMetadata.classMethod](argument)
66
+ reply
67
+ .status(handlerMetadata.successStatusCode)
68
+ .headers(handlerMetadata.headers)
69
+ .send(result)
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,21 @@
1
+ import type { ClassType } from '@navios/di'
2
+ import type { FastifyReply, FastifyRequest } from 'fastify'
3
+
4
+ import type { HandlerMetadata } from '../metadata/index.mjs'
5
+ import type { ExecutionContext } from '../services/index.mjs'
6
+
7
+ export interface HandlerAdapterInterface {
8
+ provideSchema?: (handlerMetadata: HandlerMetadata<any>) => Record<string, any>
9
+ hasSchema?: (handlerMetadata: HandlerMetadata<any>) => boolean
10
+ prepareArguments?: (
11
+ handlerMetadata: HandlerMetadata<any>,
12
+ ) => ((
13
+ target: Record<string, any>,
14
+ request: FastifyRequest,
15
+ ) => Promise<void> | void)[]
16
+ provideHandler: (
17
+ controller: ClassType,
18
+ executionContext: ExecutionContext,
19
+ handlerMetadata: HandlerMetadata<any>,
20
+ ) => (request: FastifyRequest, reply: FastifyReply) => Promise<any>
21
+ }
@@ -0,0 +1,4 @@
1
+ export * from './endpoint-adapter.service.mjs'
2
+ export * from './handler-adapter.interface.mjs'
3
+ export * from './multipart-adapter.service.mjs'
4
+ export * from './stream-adapter.service.mjs'
@@ -0,0 +1,131 @@
1
+ import type { MultipartFile, MultipartValue } from '@fastify/multipart'
2
+ import type { BaseEndpointConfig } from '@navios/common'
3
+ import type { FastifyRequest } from 'fastify'
4
+ import type { AnyZodObject, ZodRawShape } from 'zod'
5
+
6
+ import { Injectable, InjectionToken } from '@navios/di'
7
+
8
+ import { ZodArray, ZodObject, ZodOptional } from 'zod'
9
+
10
+ import type { HandlerMetadata } from '../metadata/index.mjs'
11
+
12
+ import { EndpointAdapterService } from './endpoint-adapter.service.mjs'
13
+
14
+ export const MultipartAdapterToken =
15
+ InjectionToken.create<MultipartAdapterService>(
16
+ Symbol.for('MultipartAdapterService'),
17
+ )
18
+
19
+ @Injectable({
20
+ token: MultipartAdapterToken,
21
+ })
22
+ export class MultipartAdapterService extends EndpointAdapterService {
23
+ prepareArguments(
24
+ handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
25
+ ): ((target: Record<string, any>, request: FastifyRequest) => void)[] {
26
+ const config = handlerMetadata.config
27
+ const getters: ((
28
+ target: Record<string, any>,
29
+ request: FastifyRequest,
30
+ ) => void | Promise<void>)[] = []
31
+ if (config.querySchema) {
32
+ getters.push((target, request) => {
33
+ target.params = request.query
34
+ })
35
+ }
36
+ if (config.url.includes('$')) {
37
+ getters.push((target, request) => {
38
+ target.urlParams = request.params
39
+ })
40
+ }
41
+ const requestSchema = config.requestSchema as unknown as AnyZodObject
42
+ const shape = requestSchema._def.shape()
43
+ const structure = this.analyzeSchema(shape)
44
+ getters.push(async (target, request) => {
45
+ const req: Record<string, any> = {}
46
+ for await (const part of request.parts()) {
47
+ await this.populateRequest(structure, part, req)
48
+ }
49
+ target.data = requestSchema.parse(req)
50
+ })
51
+
52
+ return getters
53
+ }
54
+
55
+ private async populateRequest(
56
+ structure: {
57
+ [p: string]: { isArray: boolean; isOptional: boolean; isObject: boolean }
58
+ },
59
+ part: MultipartFile | MultipartValue<unknown>,
60
+ req: Record<string, any>,
61
+ ) {
62
+ const { isArray, isObject } = structure[part.fieldname] ?? {}
63
+ if (isArray && !req[part.fieldname]) {
64
+ req[part.fieldname] = []
65
+ }
66
+ let value
67
+ if (part.type === 'file') {
68
+ value = new File([await part.toBuffer()], part.filename, {
69
+ type: part.mimetype,
70
+ })
71
+ } else {
72
+ value = part.value
73
+ if (isObject && typeof value === 'string') {
74
+ value = JSON.parse(value)
75
+ }
76
+ }
77
+
78
+ if (isArray) {
79
+ req[part.fieldname].push(value)
80
+ } else {
81
+ req[part.fieldname] = value
82
+ }
83
+ }
84
+
85
+ private analyzeSchema(shape: ZodRawShape) {
86
+ return Object.keys(shape).reduce(
87
+ (target, key) => {
88
+ let schema = shape[key]
89
+ const isOptional = schema instanceof ZodOptional
90
+ if (isOptional) {
91
+ schema = (schema as ZodOptional<any>).unwrap()
92
+ }
93
+ const isArray = schema instanceof ZodArray
94
+ if (isArray) {
95
+ schema = (schema as ZodArray<any>).element
96
+ }
97
+ const isObject = schema instanceof ZodObject
98
+ return {
99
+ ...target,
100
+ [key]: {
101
+ isArray,
102
+ isOptional,
103
+ isObject,
104
+ },
105
+ }
106
+ },
107
+ {} as Record<
108
+ string,
109
+ { isArray: boolean; isOptional: boolean; isObject: boolean }
110
+ >,
111
+ )
112
+ }
113
+
114
+ override provideSchema(
115
+ handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
116
+ ): Record<string, any> {
117
+ const schema: Record<string, any> = {}
118
+ const { querySchema, responseSchema } = handlerMetadata.config
119
+
120
+ if (querySchema) {
121
+ schema.querystring = querySchema
122
+ }
123
+ if (responseSchema) {
124
+ schema.response = {
125
+ 200: responseSchema,
126
+ }
127
+ }
128
+
129
+ return schema
130
+ }
131
+ }
@@ -0,0 +1,91 @@
1
+ import type { BaseStreamConfig } from '@navios/common'
2
+ import type { ClassType } from '@navios/di'
3
+ import type { FastifyReply, FastifyRequest } from 'fastify'
4
+
5
+ import { inject, Injectable, InjectionToken } from '@navios/di'
6
+
7
+ import type { HandlerMetadata } from '../metadata/index.mjs'
8
+ import type { ExecutionContext } from '../services/index.mjs'
9
+ import type { HandlerAdapterInterface } from './handler-adapter.interface.mjs'
10
+
11
+ export const StreamAdapterToken = InjectionToken.create<StreamAdapterService>(
12
+ Symbol.for('StreamAdapterService'),
13
+ )
14
+
15
+ @Injectable({
16
+ token: StreamAdapterToken,
17
+ })
18
+ export class StreamAdapterService implements HandlerAdapterInterface {
19
+ hasSchema(handlerMetadata: HandlerMetadata<BaseStreamConfig>): boolean {
20
+ const config = handlerMetadata.config
21
+ return !!config.requestSchema || !!config.querySchema
22
+ }
23
+
24
+ prepareArguments(handlerMetadata: HandlerMetadata<BaseStreamConfig>) {
25
+ const config = handlerMetadata.config
26
+ const getters: ((
27
+ target: Record<string, any>,
28
+ request: FastifyRequest,
29
+ ) => void | Promise<void>)[] = []
30
+ if (config.querySchema) {
31
+ getters.push((target, request) => {
32
+ target.params = request.query
33
+ })
34
+ }
35
+ if (config.requestSchema) {
36
+ getters.push((target, request) => {
37
+ target.data = request.body
38
+ })
39
+ }
40
+ if (config.url.includes('$')) {
41
+ getters.push((target, request) => {
42
+ target.urlParams = request.params
43
+ })
44
+ }
45
+
46
+ return getters
47
+ }
48
+
49
+ provideHandler(
50
+ controller: ClassType,
51
+ executionContext: ExecutionContext,
52
+ handlerMetadata: HandlerMetadata<BaseStreamConfig>,
53
+ ): (request: FastifyRequest, reply: FastifyReply) => Promise<any> {
54
+ const getters = this.prepareArguments(handlerMetadata)
55
+ const formatArguments = async (request: FastifyRequest) => {
56
+ const argument: Record<string, any> = {}
57
+ const promises: Promise<void>[] = []
58
+ for (const getter of getters) {
59
+ const res = getter(argument, request)
60
+ if (res instanceof Promise) {
61
+ promises.push(res)
62
+ }
63
+ }
64
+ await Promise.all(promises)
65
+ return argument
66
+ }
67
+
68
+ return async function (request: FastifyRequest, reply: FastifyReply) {
69
+ const controllerInstance = await inject(controller)
70
+ const argument = await formatArguments(request)
71
+
72
+ await controllerInstance[handlerMetadata.classMethod](argument, reply)
73
+ }
74
+ }
75
+
76
+ provideSchema(
77
+ handlerMetadata: HandlerMetadata<BaseStreamConfig>,
78
+ ): Record<string, any> {
79
+ const schema: Record<string, any> = {}
80
+ const { querySchema, requestSchema } = handlerMetadata.config
81
+
82
+ if (querySchema) {
83
+ schema.querystring = querySchema
84
+ }
85
+ if (requestSchema) {
86
+ schema.body = requestSchema
87
+ }
88
+
89
+ return schema
90
+ }
91
+ }
@@ -1,11 +1,11 @@
1
+ import type { ClassType } from '@navios/di'
1
2
  import type { z, ZodType } from 'zod'
2
3
 
3
4
  import type {
4
5
  ControllerMetadata,
5
- EndpointMetadata,
6
+ HandlerMetadata,
6
7
  ModuleMetadata,
7
8
  } from './metadata/index.mjs'
8
- import type { ClassType } from './service-locator/index.mjs'
9
9
 
10
10
  import {
11
11
  getControllerMetadata,
@@ -86,30 +86,30 @@ export class AttributeFactory {
86
86
 
87
87
  static get(
88
88
  attribute: ClassAttribute,
89
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
89
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
90
90
  ): true | null
91
91
  static get<T extends ZodType>(
92
92
  attribute: ClassSchemaAttribute<T>,
93
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
93
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
94
94
  ): z.output<T> | null
95
95
  static get(
96
96
  attribute: ClassAttribute | ClassSchemaAttribute<any>,
97
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
97
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
98
98
  ) {
99
99
  return target.customAttributes.get(attribute.token) ?? null
100
100
  }
101
101
 
102
102
  static getAll(
103
103
  attribute: ClassAttribute,
104
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
104
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
105
105
  ): Array<true> | null
106
106
  static getAll<T extends ZodType>(
107
107
  attribute: ClassSchemaAttribute<T>,
108
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
108
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
109
109
  ): Array<z.output<T>> | null
110
110
  static getAll(
111
111
  attribute: ClassAttribute | ClassSchemaAttribute<any>,
112
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
112
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
113
113
  ) {
114
114
  const values = Array.from(target.customAttributes.entries())
115
115
  .filter(([key]) => key === attribute.token)
@@ -119,15 +119,15 @@ export class AttributeFactory {
119
119
 
120
120
  static getLast(
121
121
  attribute: ClassAttribute,
122
- target: (ModuleMetadata | ControllerMetadata | EndpointMetadata)[],
122
+ target: (ModuleMetadata | ControllerMetadata | HandlerMetadata<any>)[],
123
123
  ): true | null
124
124
  static getLast<T extends ZodType>(
125
125
  attribute: ClassSchemaAttribute<T>,
126
- target: (ModuleMetadata | ControllerMetadata | EndpointMetadata)[],
126
+ target: (ModuleMetadata | ControllerMetadata | HandlerMetadata<any>)[],
127
127
  ): z.output<T> | null
128
128
  static getLast(
129
129
  attribute: ClassAttribute | ClassSchemaAttribute<any>,
130
- target: (ModuleMetadata | ControllerMetadata | EndpointMetadata)[],
130
+ target: (ModuleMetadata | ControllerMetadata | HandlerMetadata<any>)[],
131
131
  ) {
132
132
  for (let i = target.length - 1; i >= 0; i--) {
133
133
  const value = target[i].customAttributes.get(attribute.token)
@@ -140,15 +140,15 @@ export class AttributeFactory {
140
140
 
141
141
  static has(
142
142
  attribute: ClassAttribute,
143
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
143
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
144
144
  ): boolean
145
145
  static has<T extends ZodType>(
146
146
  attribute: ClassSchemaAttribute<T>,
147
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
147
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
148
148
  ): boolean
149
149
  static has(
150
150
  attribute: ClassAttribute | ClassSchemaAttribute<any>,
151
- target: ModuleMetadata | ControllerMetadata | EndpointMetadata,
151
+ target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
152
152
  ) {
153
153
  return target.customAttributes.has(attribute.token)
154
154
  }
@@ -1,14 +1,15 @@
1
- import { z } from 'zod'
2
-
3
- import type { ConfigService } from './config-service.interface.mjs'
4
-
5
- import { Logger } from '../logger/index.mjs'
6
1
  import {
7
2
  Injectable,
8
3
  InjectableType,
9
4
  InjectionToken,
10
5
  syncInject,
11
- } from '../service-locator/index.mjs'
6
+ } from '@navios/di'
7
+
8
+ import { z } from 'zod'
9
+
10
+ import type { ConfigService } from './config-service.interface.mjs'
11
+
12
+ import { Logger } from '../logger/index.mjs'
12
13
  import { ConfigServiceInstance } from './config.service.mjs'
13
14
 
14
15
  export const ConfigProviderOptions = z.object({
@@ -29,13 +30,19 @@ export class ConfigProviderFactory {
29
30
  context: 'ConfigService',
30
31
  })
31
32
 
32
- async create(ctx: any, args: z.infer<typeof ConfigProviderOptions>) {
33
+ async create(
34
+ ctx: any,
35
+ args: z.infer<typeof ConfigProviderOptions>,
36
+ ): Promise<ConfigService> {
33
37
  const { load } = args
34
38
  const logger = this.logger
35
39
  try {
36
40
  const config = await load()
37
41
 
38
- return new ConfigServiceInstance(config, logger)
42
+ return new ConfigServiceInstance(
43
+ config,
44
+ logger,
45
+ ) as unknown as ConfigService
39
46
  } catch (error) {
40
47
  logger.error('Error loading config', error)
41
48
  throw error
@@ -46,8 +53,5 @@ export class ConfigProviderFactory {
46
53
  export function provideConfig<ConfigMap extends Record<string, unknown>>(
47
54
  options: z.input<typeof ConfigProviderOptions>,
48
55
  ) {
49
- return InjectionToken.bound(ConfigProvider, options) as InjectionToken<
50
- ConfigServiceInstance<ConfigMap>,
51
- undefined
52
- >
56
+ return InjectionToken.bound(ConfigProvider, options)
53
57
  }
@@ -1,12 +1,8 @@
1
- import type { ClassType } from '../service-locator/index.mjs'
1
+ import type { ClassType } from '@navios/di'
2
+
3
+ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
2
4
 
3
5
  import { getControllerMetadata } from '../metadata/index.mjs'
4
- import {
5
- Injectable,
6
- InjectableScope,
7
- InjectableType,
8
- InjectionToken,
9
- } from '../service-locator/index.mjs'
10
6
 
11
7
  export interface ControllerOptions {
12
8
  guards?: ClassType[] | Set<ClassType>
@@ -29,7 +25,6 @@ export function Controller({ guards }: ControllerOptions = {}) {
29
25
  }
30
26
  return Injectable({
31
27
  token,
32
- type: InjectableType.Class,
33
28
  scope: InjectableScope.Instance,
34
29
  })(target, context)
35
30
  }
@@ -7,7 +7,8 @@ import type { AnyZodObject, z, ZodType } from 'zod'
7
7
 
8
8
  import { ZodDiscriminatedUnion } from 'zod'
9
9
 
10
- import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
10
+ import { EndpointAdapterToken } from '../adapters/index.mjs'
11
+ import { getEndpointMetadata } from '../metadata/index.mjs'
11
12
 
12
13
  export type EndpointParams<
13
14
  EndpointDeclaration extends {
@@ -82,7 +83,10 @@ export function Endpoint<
82
83
  }
83
84
  const config = endpoint.config
84
85
  if (context.metadata) {
85
- let endpointMetadata = getEndpointMetadata(target, context)
86
+ let endpointMetadata = getEndpointMetadata<BaseEndpointConfig>(
87
+ target,
88
+ context,
89
+ )
86
90
  if (endpointMetadata.config && endpointMetadata.config.url) {
87
91
  throw new Error(
88
92
  `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`,
@@ -90,7 +94,7 @@ export function Endpoint<
90
94
  }
91
95
  // @ts-expect-error We don't need to set correctly in the metadata
92
96
  endpointMetadata.config = config
93
- endpointMetadata.type = EndpointType.Endpoint
97
+ endpointMetadata.adapterToken = EndpointAdapterToken
94
98
  endpointMetadata.classMethod = target.name
95
99
  endpointMetadata.httpMethod = config.method
96
100
  endpointMetadata.url = config.url
@@ -1,6 +1,6 @@
1
1
  import type { HttpHeader } from 'fastify/types/utils.js'
2
2
 
3
- import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
3
+ import { getEndpointMetadata } from '../metadata/index.mjs'
4
4
 
5
5
  export function Header(name: HttpHeader, value: string | number | string[]) {
6
6
  return <T extends Function>(
@@ -11,11 +11,6 @@ export function Header(name: HttpHeader, value: string | number | string[]) {
11
11
  throw new Error('[Navios] Header decorator can only be used on methods.')
12
12
  }
13
13
  const metadata = getEndpointMetadata(target, context)
14
- if (metadata.type === EndpointType.Stream) {
15
- throw new Error(
16
- '[Navios] HttpCode decorator cannot be used on stream endpoints.',
17
- )
18
- }
19
14
 
20
15
  metadata.headers[name] = value
21
16
 
@@ -1,4 +1,4 @@
1
- import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
1
+ import { getEndpointMetadata } from '../metadata/index.mjs'
2
2
 
3
3
  export function HttpCode(code: number) {
4
4
  return <T extends Function>(
@@ -11,11 +11,6 @@ export function HttpCode(code: number) {
11
11
  )
12
12
  }
13
13
  const metadata = getEndpointMetadata(target, context)
14
- if (metadata.type === EndpointType.Stream) {
15
- throw new Error(
16
- '[Navios] HttpCode decorator cannot be used on stream endpoints.',
17
- )
18
- }
19
14
  metadata.successStatusCode = code
20
15
 
21
16
  return target
@@ -1,12 +1,8 @@
1
- import type { ClassType } from '../service-locator/index.mjs'
1
+ import type { ClassType } from '@navios/di'
2
+
3
+ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
2
4
 
3
5
  import { getModuleMetadata } from '../metadata/index.mjs'
4
- import {
5
- Injectable,
6
- InjectableScope,
7
- InjectableType,
8
- InjectionToken,
9
- } from '../service-locator/index.mjs'
10
6
 
11
7
  export interface ModuleOptions {
12
8
  controllers?: ClassType[] | Set<ClassType>
@@ -14,7 +10,13 @@ export interface ModuleOptions {
14
10
  guards?: ClassType[] | Set<ClassType>
15
11
  }
16
12
 
17
- export function Module(metadata: ModuleOptions) {
13
+ export function Module(
14
+ { controllers = [], imports = [], guards = [] }: ModuleOptions = {
15
+ controllers: [],
16
+ imports: [],
17
+ guards: [],
18
+ },
19
+ ) {
18
20
  return (target: ClassType, context: ClassDecoratorContext) => {
19
21
  if (context.kind !== 'class') {
20
22
  throw new Error('[Navios] @Module decorator can only be used on classes.')
@@ -22,25 +24,18 @@ export function Module(metadata: ModuleOptions) {
22
24
  // Register the module in the service locator
23
25
  const token = InjectionToken.create(target)
24
26
  const moduleMetadata = getModuleMetadata(target, context)
25
- if (metadata.controllers) {
26
- for (const controller of metadata.controllers) {
27
- moduleMetadata.controllers.add(controller)
28
- }
27
+ for (const controller of controllers) {
28
+ moduleMetadata.controllers.add(controller)
29
29
  }
30
- if (metadata.imports) {
31
- for (const importedModule of metadata.imports) {
32
- moduleMetadata.imports.add(importedModule)
33
- }
30
+ for (const importedModule of imports) {
31
+ moduleMetadata.imports.add(importedModule)
34
32
  }
35
- if (metadata.guards) {
36
- for (const guard of Array.from(metadata.guards).reverse()) {
37
- moduleMetadata.guards.add(guard)
38
- }
33
+ for (const guard of Array.from(guards).reverse()) {
34
+ moduleMetadata.guards.add(guard)
39
35
  }
40
36
 
41
37
  return Injectable({
42
38
  token,
43
- type: InjectableType.Class,
44
39
  scope: InjectableScope.Singleton,
45
40
  })(target, context)
46
41
  }
@@ -7,7 +7,8 @@ import type { AnyZodObject, z, ZodType } from 'zod'
7
7
 
8
8
  import { ZodDiscriminatedUnion } from 'zod'
9
9
 
10
- import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
10
+ import { MultipartAdapterToken } from '../adapters/index.mjs'
11
+ import { getEndpointMetadata } from '../metadata/index.mjs'
11
12
 
12
13
  export type MultipartParams<
13
14
  EndpointDeclaration extends {
@@ -82,7 +83,10 @@ export function Multipart<
82
83
  }
83
84
  const config = endpoint.config
84
85
  if (context.metadata) {
85
- let endpointMetadata = getEndpointMetadata(target, context)
86
+ let endpointMetadata = getEndpointMetadata<BaseEndpointConfig>(
87
+ target,
88
+ context,
89
+ )
86
90
  if (endpointMetadata.config && endpointMetadata.config.url) {
87
91
  throw new Error(
88
92
  `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`,
@@ -90,7 +94,7 @@ export function Multipart<
90
94
  }
91
95
  // @ts-expect-error We don't need to set correctly in the metadata
92
96
  endpointMetadata.config = config
93
- endpointMetadata.type = EndpointType.Multipart
97
+ endpointMetadata.adapterToken = MultipartAdapterToken
94
98
  endpointMetadata.classMethod = target.name
95
99
  endpointMetadata.httpMethod = config.method
96
100
  endpointMetadata.url = config.url