@navios/core 0.4.0 → 0.5.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.
- package/README.md +95 -2
- package/docs/README.md +310 -3
- package/docs/adapters.md +308 -0
- package/docs/application-setup.md +524 -0
- package/docs/attributes.md +689 -0
- package/docs/controllers.md +373 -0
- package/docs/endpoints.md +444 -0
- package/docs/exceptions.md +316 -0
- package/docs/guards.md +550 -0
- package/docs/modules.md +251 -0
- package/docs/quick-start.md +295 -0
- package/docs/services.md +428 -0
- package/docs/testing.md +704 -0
- package/lib/_tsup-dts-rollup.d.mts +300 -235
- package/lib/_tsup-dts-rollup.d.ts +300 -235
- package/lib/index.d.mts +47 -26
- package/lib/index.d.ts +47 -26
- package/lib/index.js +633 -1072
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +631 -1064
- package/lib/index.mjs.map +1 -1
- package/package.json +4 -7
- package/project.json +9 -1
- package/src/__tests__/config.service.spec.mts +11 -9
- package/src/__tests__/controller.spec.mts +0 -1
- package/src/config/config.service.mts +2 -2
- package/src/decorators/controller.decorator.mts +1 -1
- package/src/decorators/endpoint.decorator.mts +2 -2
- package/src/decorators/header.decorator.mts +1 -1
- package/src/decorators/multipart.decorator.mts +1 -1
- package/src/decorators/stream.decorator.mts +2 -3
- package/src/factories/endpoint-adapter.factory.mts +21 -0
- package/src/factories/http-adapter.factory.mts +20 -0
- package/src/factories/index.mts +6 -0
- package/src/factories/multipart-adapter.factory.mts +21 -0
- package/src/factories/reply.factory.mts +21 -0
- package/src/factories/request.factory.mts +21 -0
- package/src/factories/stream-adapter.factory.mts +20 -0
- package/src/index.mts +1 -1
- package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
- package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
- package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
- package/src/interfaces/can-activate.mts +4 -2
- package/src/interfaces/http-header.mts +18 -0
- package/src/interfaces/index.mts +6 -0
- package/src/logger/console-logger.service.mts +28 -44
- package/src/logger/index.mts +1 -2
- package/src/logger/logger.service.mts +9 -128
- package/src/logger/logger.tokens.mts +21 -0
- package/src/metadata/handler.metadata.mts +7 -5
- package/src/navios.application.mts +65 -172
- package/src/navios.environment.mts +30 -0
- package/src/navios.factory.mts +53 -12
- package/src/services/guard-runner.service.mts +19 -9
- package/src/services/index.mts +0 -2
- package/src/services/module-loader.service.mts +4 -3
- package/src/tokens/endpoint-adapter.token.mts +8 -0
- package/src/tokens/execution-context.token.mts +2 -2
- package/src/tokens/http-adapter.token.mts +8 -0
- package/src/tokens/index.mts +4 -1
- package/src/tokens/multipart-adapter.token.mts +8 -0
- package/src/tokens/reply.token.mts +1 -5
- package/src/tokens/request.token.mts +1 -7
- package/src/tokens/stream-adapter.token.mts +8 -0
- package/docs/recipes/prisma.md +0 -60
- package/e2e/endpoints/get.spec.mts +0 -97
- package/e2e/endpoints/post.spec.mts +0 -113
- package/examples/simple-test/api/index.mts +0 -64
- package/examples/simple-test/config/config.service.mts +0 -14
- package/examples/simple-test/config/configuration.mts +0 -7
- package/examples/simple-test/index.mts +0 -16
- package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
- package/examples/simple-test/src/acl/acl.guard.mts +0 -14
- package/examples/simple-test/src/acl/app.guard.mts +0 -27
- package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
- package/examples/simple-test/src/acl/public.attribute.mts +0 -21
- package/examples/simple-test/src/app.module.mts +0 -9
- package/examples/simple-test/src/user/user.controller.mts +0 -72
- package/examples/simple-test/src/user/user.module.mts +0 -14
- package/examples/simple-test/src/user/user.service.mts +0 -14
- package/src/adapters/endpoint-adapter.service.mts +0 -72
- package/src/adapters/handler-adapter.interface.mts +0 -21
- package/src/adapters/index.mts +0 -4
- package/src/adapters/multipart-adapter.service.mts +0 -135
- package/src/adapters/stream-adapter.service.mts +0 -91
- package/src/logger/logger.factory.mts +0 -36
- package/src/logger/pino-wrapper.mts +0 -64
- package/src/services/controller-adapter.service.mts +0 -124
- package/src/services/execution-context.mts +0 -54
- package/src/tokens/application.token.mts +0 -9
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import type { ClassType } from '@navios/di'
|
|
2
|
-
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
|
|
3
|
-
import type { ZodTypeProvider } from 'fastify-type-provider-zod'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
getGlobalServiceLocator,
|
|
7
|
-
inject,
|
|
8
|
-
Injectable,
|
|
9
|
-
InjectionToken,
|
|
10
|
-
syncInject,
|
|
11
|
-
} from '@navios/di'
|
|
12
|
-
|
|
13
|
-
import type { HandlerAdapterInterface } from '../adapters/index.mjs'
|
|
14
|
-
import type { ModuleMetadata } from '../metadata/index.mjs'
|
|
15
|
-
|
|
16
|
-
import { Logger } from '../logger/index.mjs'
|
|
17
|
-
import { extractControllerMetadata } from '../metadata/index.mjs'
|
|
18
|
-
import { ExecutionContextToken, Reply, Request } from '../tokens/index.mjs'
|
|
19
|
-
import { ExecutionContext } from './execution-context.mjs'
|
|
20
|
-
import { GuardRunnerService } from './guard-runner.service.mjs'
|
|
21
|
-
|
|
22
|
-
@Injectable()
|
|
23
|
-
export class ControllerAdapterService {
|
|
24
|
-
guardRunner = syncInject(GuardRunnerService)
|
|
25
|
-
private logger = syncInject(Logger, {
|
|
26
|
-
context: ControllerAdapterService.name,
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
async setupController(
|
|
30
|
-
controller: ClassType,
|
|
31
|
-
instance: FastifyInstance,
|
|
32
|
-
moduleMetadata: ModuleMetadata,
|
|
33
|
-
) {
|
|
34
|
-
const controllerMetadata = extractControllerMetadata(controller)
|
|
35
|
-
for (const endpoint of controllerMetadata.endpoints) {
|
|
36
|
-
const { classMethod, url, httpMethod, adapterToken } = endpoint
|
|
37
|
-
|
|
38
|
-
if (!url || !adapterToken) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
`[Navios] Malformed Endpoint ${controller.name}:${classMethod}`,
|
|
41
|
-
)
|
|
42
|
-
}
|
|
43
|
-
const adapter = await inject(
|
|
44
|
-
adapterToken as InjectionToken<HandlerAdapterInterface>,
|
|
45
|
-
)
|
|
46
|
-
const executionContext = new ExecutionContext(
|
|
47
|
-
moduleMetadata,
|
|
48
|
-
controllerMetadata,
|
|
49
|
-
endpoint,
|
|
50
|
-
)
|
|
51
|
-
const hasSchema = adapter.hasSchema?.(endpoint) ?? false
|
|
52
|
-
if (hasSchema) {
|
|
53
|
-
instance.withTypeProvider<ZodTypeProvider>().route({
|
|
54
|
-
method: httpMethod,
|
|
55
|
-
url: url.replaceAll('$', ':'),
|
|
56
|
-
schema: adapter.provideSchema?.(endpoint) ?? {},
|
|
57
|
-
preHandler: this.providePreHandler(executionContext),
|
|
58
|
-
handler: this.wrapHandler(
|
|
59
|
-
executionContext,
|
|
60
|
-
adapter.provideHandler(controller, executionContext, endpoint),
|
|
61
|
-
),
|
|
62
|
-
})
|
|
63
|
-
} else {
|
|
64
|
-
instance.route({
|
|
65
|
-
method: httpMethod,
|
|
66
|
-
url: url.replaceAll('$', ':'),
|
|
67
|
-
preHandler: this.providePreHandler(executionContext),
|
|
68
|
-
handler: this.wrapHandler(
|
|
69
|
-
executionContext,
|
|
70
|
-
adapter.provideHandler(controller, executionContext, endpoint),
|
|
71
|
-
),
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.logger.debug(
|
|
76
|
-
`Registered ${httpMethod} ${url} for ${controller.name}:${classMethod}`,
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
providePreHandler(executionContext: ExecutionContext) {
|
|
82
|
-
const guards = this.guardRunner.makeContext(executionContext)
|
|
83
|
-
return guards.size > 0
|
|
84
|
-
? this.wrapHandler(
|
|
85
|
-
executionContext,
|
|
86
|
-
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
87
|
-
let canActivate = true
|
|
88
|
-
canActivate = await this.guardRunner.runGuards(
|
|
89
|
-
guards,
|
|
90
|
-
executionContext,
|
|
91
|
-
)
|
|
92
|
-
if (!canActivate) {
|
|
93
|
-
return reply
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
)
|
|
97
|
-
: undefined
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private wrapHandler(
|
|
101
|
-
executionContext: ExecutionContext,
|
|
102
|
-
handler: (request: FastifyRequest, reply: FastifyReply) => Promise<void>,
|
|
103
|
-
) {
|
|
104
|
-
const locator = getGlobalServiceLocator()
|
|
105
|
-
return async (request: FastifyRequest, reply: FastifyReply) => {
|
|
106
|
-
locator.storeInstance(request, Request)
|
|
107
|
-
locator.storeInstance(reply, Reply)
|
|
108
|
-
locator.storeInstance(executionContext, ExecutionContextToken)
|
|
109
|
-
executionContext.provideRequest(request)
|
|
110
|
-
executionContext.provideReply(reply)
|
|
111
|
-
try {
|
|
112
|
-
return await handler(request, reply)
|
|
113
|
-
} finally {
|
|
114
|
-
Promise.all([
|
|
115
|
-
locator.removeInstance(Request),
|
|
116
|
-
locator.removeInstance(Reply),
|
|
117
|
-
locator.removeInstance(ExecutionContextToken),
|
|
118
|
-
]).catch((err) => {
|
|
119
|
-
this.logger.warn(`Error removing instances: ${err}`)
|
|
120
|
-
})
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { FastifyReply, FastifyRequest } from 'fastify'
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
ControllerMetadata,
|
|
5
|
-
HandlerMetadata,
|
|
6
|
-
ModuleMetadata,
|
|
7
|
-
} from '../metadata/index.mjs'
|
|
8
|
-
|
|
9
|
-
export class ExecutionContext {
|
|
10
|
-
private request: FastifyRequest | undefined
|
|
11
|
-
private reply: FastifyReply | undefined
|
|
12
|
-
constructor(
|
|
13
|
-
private readonly module: ModuleMetadata,
|
|
14
|
-
private readonly controller: ControllerMetadata,
|
|
15
|
-
private readonly handler: HandlerMetadata,
|
|
16
|
-
) {}
|
|
17
|
-
getModule(): ModuleMetadata {
|
|
18
|
-
return this.module
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
getController(): ControllerMetadata {
|
|
22
|
-
return this.controller
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
getHandler(): HandlerMetadata {
|
|
26
|
-
return this.handler
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
getRequest(): FastifyRequest {
|
|
30
|
-
if (!this.request) {
|
|
31
|
-
throw new Error(
|
|
32
|
-
'[Navios] Request is not set. Make sure to set it before using it.',
|
|
33
|
-
)
|
|
34
|
-
}
|
|
35
|
-
return this.request
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
getReply(): FastifyReply {
|
|
39
|
-
if (!this.reply) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
'[Navios] Reply is not set. Make sure to set it before using it.',
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
return this.reply
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
provideRequest(request: FastifyRequest): void {
|
|
48
|
-
this.request = request
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
provideReply(reply: FastifyReply): void {
|
|
52
|
-
this.reply = reply
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { FastifyInstance } from 'fastify'
|
|
2
|
-
|
|
3
|
-
import { InjectionToken } from '@navios/di'
|
|
4
|
-
|
|
5
|
-
const ApplicationInjectionToken = 'ApplicationInjectionToken'
|
|
6
|
-
|
|
7
|
-
export const Application = InjectionToken.create<FastifyInstance>(
|
|
8
|
-
ApplicationInjectionToken,
|
|
9
|
-
)
|