@navios/core 0.1.13 → 0.1.15
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/dist/_tsup-dts-rollup.d.mts +133 -49
- package/dist/_tsup-dts-rollup.d.ts +133 -49
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +1184 -1090
- package/dist/index.mjs +1179 -1090
- package/package.json +1 -1
- package/src/adapters/endpoint-adapter.service.mts +75 -0
- package/src/adapters/handler-adapter.interface.mts +21 -0
- package/src/adapters/index.mts +4 -0
- package/src/adapters/multipart-adapter.service.mts +130 -0
- package/src/adapters/stream-adapter.service.mts +95 -0
- package/src/attribute.factory.mts +13 -13
- package/src/config/config.provider.mts +8 -2
- package/src/decorators/controller.decorator.mts +0 -2
- package/src/decorators/endpoint.decorator.mts +7 -3
- package/src/decorators/header.decorator.mts +1 -6
- package/src/decorators/http-code.decorator.mts +1 -6
- package/src/decorators/module.decorator.mts +13 -15
- package/src/decorators/multipart.decorator.mts +7 -3
- package/src/decorators/stream.decorator.mts +7 -3
- package/src/index.mts +1 -0
- package/src/logger/console-logger.service.mts +41 -3
- package/src/logger/logger.service.mts +0 -1
- package/src/metadata/controller.metadata.mts +3 -3
- package/src/metadata/{endpoint.metadata.mts → handler.metadata.mts} +17 -24
- package/src/metadata/index.mts +1 -1
- package/src/navios.application.mts +3 -4
- package/src/service-locator/__tests__/injection-token.spec.mts +10 -5
- package/src/service-locator/decorators/injectable.decorator.mts +53 -4
- package/src/service-locator/inject.mts +14 -0
- package/src/service-locator/interfaces/factory.interface.mts +9 -1
- package/src/service-locator/service-locator.mts +1 -0
- package/src/service-locator/sync-injector.mts +14 -0
- package/src/services/controller-adapter.service.mts +59 -240
- package/src/services/execution-context.mts +4 -3
package/package.json
CHANGED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { BaseEndpointConfig } from '@navios/common'
|
|
2
|
+
import type { FastifyReply, FastifyRequest } from 'fastify'
|
|
3
|
+
|
|
4
|
+
import type { HandlerMetadata } from '../metadata/index.mjs'
|
|
5
|
+
import type { ClassType } from '../service-locator/index.mjs'
|
|
6
|
+
import type { ExecutionContext } from '../services/index.mjs'
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
inject,
|
|
10
|
+
Injectable,
|
|
11
|
+
InjectionToken,
|
|
12
|
+
} from '../service-locator/index.mjs'
|
|
13
|
+
import { StreamAdapterService } from './stream-adapter.service.mjs'
|
|
14
|
+
|
|
15
|
+
export const EndpointAdapterToken =
|
|
16
|
+
InjectionToken.create<EndpointAdapterService>(
|
|
17
|
+
Symbol.for('EndpointAdapterService'),
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
@Injectable({
|
|
21
|
+
token: EndpointAdapterToken,
|
|
22
|
+
})
|
|
23
|
+
export class EndpointAdapterService extends StreamAdapterService {
|
|
24
|
+
override hasSchema(
|
|
25
|
+
handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
|
|
26
|
+
): boolean {
|
|
27
|
+
const config = handlerMetadata.config
|
|
28
|
+
return super.hasSchema(handlerMetadata) || !!config.responseSchema
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
override provideSchema(
|
|
32
|
+
handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
|
|
33
|
+
): Record<string, any> {
|
|
34
|
+
const config = handlerMetadata.config
|
|
35
|
+
const schema = super.provideSchema(handlerMetadata)
|
|
36
|
+
if (config.responseSchema) {
|
|
37
|
+
schema.response = {
|
|
38
|
+
200: config.responseSchema,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return schema
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override provideHandler(
|
|
46
|
+
controller: ClassType,
|
|
47
|
+
executionContext: ExecutionContext,
|
|
48
|
+
handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
|
|
49
|
+
): (request: FastifyRequest, reply: FastifyReply) => Promise<any> {
|
|
50
|
+
const getters = this.prepareArguments(handlerMetadata)
|
|
51
|
+
const formatArguments = async (request: FastifyRequest) => {
|
|
52
|
+
const argument: Record<string, any> = {}
|
|
53
|
+
const promises: Promise<void>[] = []
|
|
54
|
+
for (const getter of getters) {
|
|
55
|
+
const res = getter(argument, request)
|
|
56
|
+
if (res instanceof Promise) {
|
|
57
|
+
promises.push(res)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await Promise.all(promises)
|
|
61
|
+
return argument
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return async function (request, reply) {
|
|
65
|
+
const controllerInstance = await inject(controller)
|
|
66
|
+
const argument = await formatArguments(request)
|
|
67
|
+
const result =
|
|
68
|
+
await controllerInstance[handlerMetadata.classMethod](argument)
|
|
69
|
+
reply
|
|
70
|
+
.status(handlerMetadata.successStatusCode)
|
|
71
|
+
.headers(handlerMetadata.headers)
|
|
72
|
+
.send(result)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FastifyReply, FastifyRequest } from 'fastify'
|
|
2
|
+
|
|
3
|
+
import type { HandlerMetadata } from '../metadata/index.mjs'
|
|
4
|
+
import type { ClassType } from '../service-locator/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,130 @@
|
|
|
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 { ZodArray, ZodObject, ZodOptional } from 'zod'
|
|
7
|
+
|
|
8
|
+
import type { HandlerMetadata } from '../metadata/index.mjs'
|
|
9
|
+
|
|
10
|
+
import { Injectable, InjectionToken } from '../service-locator/index.mjs'
|
|
11
|
+
import { EndpointAdapterService } from './endpoint-adapter.service.mjs'
|
|
12
|
+
|
|
13
|
+
export const MultipartAdapterToken =
|
|
14
|
+
InjectionToken.create<MultipartAdapterService>(
|
|
15
|
+
Symbol.for('MultipartAdapterService'),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
@Injectable({
|
|
19
|
+
token: MultipartAdapterToken,
|
|
20
|
+
})
|
|
21
|
+
export class MultipartAdapterService extends EndpointAdapterService {
|
|
22
|
+
prepareArguments(
|
|
23
|
+
handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
|
|
24
|
+
): ((target: Record<string, any>, request: FastifyRequest) => void)[] {
|
|
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.url.includes('$')) {
|
|
36
|
+
getters.push((target, request) => {
|
|
37
|
+
target.urlParams = request.params
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
const requestSchema = config.requestSchema as unknown as AnyZodObject
|
|
41
|
+
const shape = requestSchema._def.shape()
|
|
42
|
+
const structure = this.analyzeSchema(shape)
|
|
43
|
+
getters.push(async (target, request) => {
|
|
44
|
+
const req: Record<string, any> = {}
|
|
45
|
+
for await (const part of request.parts()) {
|
|
46
|
+
await this.populateRequest(structure, part, req)
|
|
47
|
+
}
|
|
48
|
+
target.data = requestSchema.parse(req)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return getters
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async populateRequest(
|
|
55
|
+
structure: {
|
|
56
|
+
[p: string]: { isArray: boolean; isOptional: boolean; isObject: boolean }
|
|
57
|
+
},
|
|
58
|
+
part: MultipartFile | MultipartValue<unknown>,
|
|
59
|
+
req: Record<string, any>,
|
|
60
|
+
) {
|
|
61
|
+
const { isArray, isObject } = structure[part.fieldname] ?? {}
|
|
62
|
+
if (isArray && !req[part.fieldname]) {
|
|
63
|
+
req[part.fieldname] = []
|
|
64
|
+
}
|
|
65
|
+
let value
|
|
66
|
+
if (part.type === 'file') {
|
|
67
|
+
value = new File([await part.toBuffer()], part.filename, {
|
|
68
|
+
type: part.mimetype,
|
|
69
|
+
})
|
|
70
|
+
} else {
|
|
71
|
+
value = part.value
|
|
72
|
+
if (isObject && typeof value === 'string') {
|
|
73
|
+
value = JSON.parse(value)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (isArray) {
|
|
78
|
+
req[part.fieldname].push(value)
|
|
79
|
+
} else {
|
|
80
|
+
req[part.fieldname] = value
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private analyzeSchema(shape: ZodRawShape) {
|
|
85
|
+
return Object.keys(shape).reduce(
|
|
86
|
+
(target, key) => {
|
|
87
|
+
let schema = shape[key]
|
|
88
|
+
const isOptional = schema instanceof ZodOptional
|
|
89
|
+
if (isOptional) {
|
|
90
|
+
schema = (schema as ZodOptional<any>).unwrap()
|
|
91
|
+
}
|
|
92
|
+
const isArray = schema instanceof ZodArray
|
|
93
|
+
if (isArray) {
|
|
94
|
+
schema = (schema as ZodArray<any>).element
|
|
95
|
+
}
|
|
96
|
+
const isObject = schema instanceof ZodObject
|
|
97
|
+
return {
|
|
98
|
+
...target,
|
|
99
|
+
[key]: {
|
|
100
|
+
isArray,
|
|
101
|
+
isOptional,
|
|
102
|
+
isObject,
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
{} as Record<
|
|
107
|
+
string,
|
|
108
|
+
{ isArray: boolean; isOptional: boolean; isObject: boolean }
|
|
109
|
+
>,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
override provideSchema(
|
|
114
|
+
handlerMetadata: HandlerMetadata<BaseEndpointConfig>,
|
|
115
|
+
): Record<string, any> {
|
|
116
|
+
const schema: Record<string, any> = {}
|
|
117
|
+
const { querySchema, responseSchema } = handlerMetadata.config
|
|
118
|
+
|
|
119
|
+
if (querySchema) {
|
|
120
|
+
schema.querystring = querySchema
|
|
121
|
+
}
|
|
122
|
+
if (responseSchema) {
|
|
123
|
+
schema.response = {
|
|
124
|
+
200: responseSchema,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return schema
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { BaseStreamConfig } from '@navios/common'
|
|
2
|
+
import type { FastifyReply, FastifyRequest } from 'fastify'
|
|
3
|
+
|
|
4
|
+
import type { HandlerMetadata } from '../metadata/index.mjs'
|
|
5
|
+
import type { ClassType } from '../service-locator/index.mjs'
|
|
6
|
+
import type { ExecutionContext } from '../services/index.mjs'
|
|
7
|
+
import type { HandlerAdapterInterface } from './handler-adapter.interface.mjs'
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
inject,
|
|
11
|
+
Injectable,
|
|
12
|
+
InjectionToken,
|
|
13
|
+
} from '../service-locator/index.mjs'
|
|
14
|
+
|
|
15
|
+
export const StreamAdapterToken = InjectionToken.create<StreamAdapterService>(
|
|
16
|
+
Symbol.for('StreamAdapterService'),
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
@Injectable({
|
|
20
|
+
token: StreamAdapterToken,
|
|
21
|
+
})
|
|
22
|
+
export class StreamAdapterService implements HandlerAdapterInterface {
|
|
23
|
+
hasSchema(handlerMetadata: HandlerMetadata<BaseStreamConfig>): boolean {
|
|
24
|
+
const config = handlerMetadata.config
|
|
25
|
+
return !!config.requestSchema || !!config.querySchema
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
prepareArguments(handlerMetadata: HandlerMetadata<BaseStreamConfig>) {
|
|
29
|
+
const config = handlerMetadata.config
|
|
30
|
+
const getters: ((
|
|
31
|
+
target: Record<string, any>,
|
|
32
|
+
request: FastifyRequest,
|
|
33
|
+
) => void | Promise<void>)[] = []
|
|
34
|
+
if (config.querySchema) {
|
|
35
|
+
getters.push((target, request) => {
|
|
36
|
+
target.params = request.query
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
if (config.requestSchema) {
|
|
40
|
+
getters.push((target, request) => {
|
|
41
|
+
target.data = request.body
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
if (config.url.includes('$')) {
|
|
45
|
+
getters.push((target, request) => {
|
|
46
|
+
target.urlParams = request.params
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return getters
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
provideHandler(
|
|
54
|
+
controller: ClassType,
|
|
55
|
+
executionContext: ExecutionContext,
|
|
56
|
+
handlerMetadata: HandlerMetadata<BaseStreamConfig>,
|
|
57
|
+
): (request: FastifyRequest, reply: FastifyReply) => Promise<any> {
|
|
58
|
+
const getters = this.prepareArguments(handlerMetadata)
|
|
59
|
+
const formatArguments = async (request: FastifyRequest) => {
|
|
60
|
+
const argument: Record<string, any> = {}
|
|
61
|
+
const promises: Promise<void>[] = []
|
|
62
|
+
for (const getter of getters) {
|
|
63
|
+
const res = getter(argument, request)
|
|
64
|
+
if (res instanceof Promise) {
|
|
65
|
+
promises.push(res)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
await Promise.all(promises)
|
|
69
|
+
return argument
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return async function (request: FastifyRequest, reply: FastifyReply) {
|
|
73
|
+
const controllerInstance = await inject(controller)
|
|
74
|
+
const argument = await formatArguments(request)
|
|
75
|
+
|
|
76
|
+
await controllerInstance[handlerMetadata.classMethod](argument, reply)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
provideSchema(
|
|
81
|
+
handlerMetadata: HandlerMetadata<BaseStreamConfig>,
|
|
82
|
+
): Record<string, any> {
|
|
83
|
+
const schema: Record<string, any> = {}
|
|
84
|
+
const { querySchema, requestSchema } = handlerMetadata.config
|
|
85
|
+
|
|
86
|
+
if (querySchema) {
|
|
87
|
+
schema.querystring = querySchema
|
|
88
|
+
}
|
|
89
|
+
if (requestSchema) {
|
|
90
|
+
schema.body = requestSchema
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return schema
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -2,7 +2,7 @@ import type { z, ZodType } from 'zod'
|
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
4
|
ControllerMetadata,
|
|
5
|
-
|
|
5
|
+
HandlerMetadata,
|
|
6
6
|
ModuleMetadata,
|
|
7
7
|
} from './metadata/index.mjs'
|
|
8
8
|
import type { ClassType } from './service-locator/index.mjs'
|
|
@@ -86,30 +86,30 @@ export class AttributeFactory {
|
|
|
86
86
|
|
|
87
87
|
static get(
|
|
88
88
|
attribute: ClassAttribute,
|
|
89
|
-
target: ModuleMetadata | ControllerMetadata |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
147
|
+
target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
|
|
148
148
|
): boolean
|
|
149
149
|
static has(
|
|
150
150
|
attribute: ClassAttribute | ClassSchemaAttribute<any>,
|
|
151
|
-
target: ModuleMetadata | ControllerMetadata |
|
|
151
|
+
target: ModuleMetadata | ControllerMetadata | HandlerMetadata<any>,
|
|
152
152
|
) {
|
|
153
153
|
return target.customAttributes.has(attribute.token)
|
|
154
154
|
}
|
|
@@ -29,13 +29,19 @@ export class ConfigProviderFactory {
|
|
|
29
29
|
context: 'ConfigService',
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
async create(
|
|
32
|
+
async create(
|
|
33
|
+
ctx: any,
|
|
34
|
+
args: z.infer<typeof ConfigProviderOptions>,
|
|
35
|
+
): Promise<ConfigService> {
|
|
33
36
|
const { load } = args
|
|
34
37
|
const logger = this.logger
|
|
35
38
|
try {
|
|
36
39
|
const config = await load()
|
|
37
40
|
|
|
38
|
-
return new ConfigServiceInstance(
|
|
41
|
+
return new ConfigServiceInstance(
|
|
42
|
+
config,
|
|
43
|
+
logger,
|
|
44
|
+
) as unknown as ConfigService
|
|
39
45
|
} catch (error) {
|
|
40
46
|
logger.error('Error loading config', error)
|
|
41
47
|
throw error
|
|
@@ -4,7 +4,6 @@ import { getControllerMetadata } from '../metadata/index.mjs'
|
|
|
4
4
|
import {
|
|
5
5
|
Injectable,
|
|
6
6
|
InjectableScope,
|
|
7
|
-
InjectableType,
|
|
8
7
|
InjectionToken,
|
|
9
8
|
} from '../service-locator/index.mjs'
|
|
10
9
|
|
|
@@ -29,7 +28,6 @@ export function Controller({ guards }: ControllerOptions = {}) {
|
|
|
29
28
|
}
|
|
30
29
|
return Injectable({
|
|
31
30
|
token,
|
|
32
|
-
type: InjectableType.Class,
|
|
33
31
|
scope: InjectableScope.Instance,
|
|
34
32
|
})(target, context)
|
|
35
33
|
}
|
|
@@ -7,7 +7,8 @@ import type { AnyZodObject, z, ZodType } from 'zod'
|
|
|
7
7
|
|
|
8
8
|
import { ZodDiscriminatedUnion } from 'zod'
|
|
9
9
|
|
|
10
|
-
import {
|
|
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(
|
|
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.
|
|
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 {
|
|
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 {
|
|
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
|
|
@@ -4,7 +4,6 @@ import { getModuleMetadata } from '../metadata/index.mjs'
|
|
|
4
4
|
import {
|
|
5
5
|
Injectable,
|
|
6
6
|
InjectableScope,
|
|
7
|
-
InjectableType,
|
|
8
7
|
InjectionToken,
|
|
9
8
|
} from '../service-locator/index.mjs'
|
|
10
9
|
|
|
@@ -14,7 +13,13 @@ export interface ModuleOptions {
|
|
|
14
13
|
guards?: ClassType[] | Set<ClassType>
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export function Module(
|
|
16
|
+
export function Module(
|
|
17
|
+
{ controllers = [], imports = [], guards = [] }: ModuleOptions = {
|
|
18
|
+
controllers: [],
|
|
19
|
+
imports: [],
|
|
20
|
+
guards: [],
|
|
21
|
+
},
|
|
22
|
+
) {
|
|
18
23
|
return (target: ClassType, context: ClassDecoratorContext) => {
|
|
19
24
|
if (context.kind !== 'class') {
|
|
20
25
|
throw new Error('[Navios] @Module decorator can only be used on classes.')
|
|
@@ -22,25 +27,18 @@ export function Module(metadata: ModuleOptions) {
|
|
|
22
27
|
// Register the module in the service locator
|
|
23
28
|
const token = InjectionToken.create(target)
|
|
24
29
|
const moduleMetadata = getModuleMetadata(target, context)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
moduleMetadata.controllers.add(controller)
|
|
28
|
-
}
|
|
30
|
+
for (const controller of controllers) {
|
|
31
|
+
moduleMetadata.controllers.add(controller)
|
|
29
32
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
moduleMetadata.imports.add(importedModule)
|
|
33
|
-
}
|
|
33
|
+
for (const importedModule of imports) {
|
|
34
|
+
moduleMetadata.imports.add(importedModule)
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
moduleMetadata.guards.add(guard)
|
|
38
|
-
}
|
|
36
|
+
for (const guard of Array.from(guards).reverse()) {
|
|
37
|
+
moduleMetadata.guards.add(guard)
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
return Injectable({
|
|
42
41
|
token,
|
|
43
|
-
type: InjectableType.Class,
|
|
44
42
|
scope: InjectableScope.Singleton,
|
|
45
43
|
})(target, context)
|
|
46
44
|
}
|
|
@@ -7,7 +7,8 @@ import type { AnyZodObject, z, ZodType } from 'zod'
|
|
|
7
7
|
|
|
8
8
|
import { ZodDiscriminatedUnion } from 'zod'
|
|
9
9
|
|
|
10
|
-
import {
|
|
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(
|
|
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.
|
|
97
|
+
endpointMetadata.adapterToken = MultipartAdapterToken
|
|
94
98
|
endpointMetadata.classMethod = target.name
|
|
95
99
|
endpointMetadata.httpMethod = config.method
|
|
96
100
|
endpointMetadata.url = config.url
|
|
@@ -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 {
|
|
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(
|
|
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.
|
|
78
|
+
endpointMetadata.adapterToken = StreamAdapterToken
|
|
75
79
|
endpointMetadata.classMethod = target.name
|
|
76
80
|
endpointMetadata.httpMethod = config.method
|
|
77
81
|
endpointMetadata.url = config.url
|
package/src/index.mts
CHANGED