@navios/core 1.1.0 → 2.0.0-alpha.1
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/CHANGELOG.md +85 -13
- package/README.md +69 -220
- package/lib/async-context-BAdt7bno.d.mts +239 -0
- package/lib/async-context-BAdt7bno.d.mts.map +1 -0
- package/lib/async-context-RsWmy2Hi.d.cts +239 -0
- package/lib/async-context-RsWmy2Hi.d.cts.map +1 -0
- package/lib/attribute-factory-BW1kgBqf.d.mts +70 -0
- package/lib/attribute-factory-BW1kgBqf.d.mts.map +1 -0
- package/lib/attribute-factory-DqN4x5XM.d.cts +70 -0
- package/lib/attribute-factory-DqN4x5XM.d.cts.map +1 -0
- package/lib/attribute-value-BNiJjxl1.mjs +225 -0
- package/lib/attribute-value-BNiJjxl1.mjs.map +1 -0
- package/lib/attribute-value-Cux5HhOL.cjs +314 -0
- package/lib/attribute-value-Cux5HhOL.cjs.map +1 -0
- package/lib/basic-execution-context-5uq2DvEA.cjs +318 -0
- package/lib/basic-execution-context-5uq2DvEA.cjs.map +1 -0
- package/lib/basic-execution-context-u1VktGoM.mjs +277 -0
- package/lib/basic-execution-context-u1VktGoM.mjs.map +1 -0
- package/lib/execution-context-BVSmLkYp.d.cts +71 -0
- package/lib/execution-context-BVSmLkYp.d.cts.map +1 -0
- package/lib/execution-context-C5F9rojO.d.mts +71 -0
- package/lib/execution-context-C5F9rojO.d.mts.map +1 -0
- package/lib/execution-context-token--cKS4cJv.cjs +270 -0
- package/lib/execution-context-token--cKS4cJv.cjs.map +1 -0
- package/lib/execution-context-token-Cb3M3Ao0.mjs +223 -0
- package/lib/execution-context-token-Cb3M3Ao0.mjs.map +1 -0
- package/lib/index.cjs +1672 -4426
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +842 -3
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +842 -3
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +1559 -4221
- package/lib/index.mjs.map +1 -1
- package/lib/legacy-compat/index.cjs +72 -371
- package/lib/legacy-compat/index.cjs.map +1 -1
- package/lib/legacy-compat/index.d.cts +42 -382
- package/lib/legacy-compat/index.d.cts.map +1 -1
- package/lib/legacy-compat/index.d.mts +41 -382
- package/lib/legacy-compat/index.d.mts.map +1 -1
- package/lib/legacy-compat/index.mjs +51 -345
- package/lib/legacy-compat/index.mjs.map +1 -1
- package/lib/module-Bz83ZnJ5.cjs +173 -0
- package/lib/module-Bz83ZnJ5.cjs.map +1 -0
- package/lib/module-options-CSqQUSTm.d.mts +51 -0
- package/lib/module-options-CSqQUSTm.d.mts.map +1 -0
- package/lib/module-options-DSRBri4J.d.cts +51 -0
- package/lib/module-options-DSRBri4J.d.cts.map +1 -0
- package/lib/module-zPXwR2qp.mjs +138 -0
- package/lib/module-zPXwR2qp.mjs.map +1 -0
- package/lib/settled-within-B6gpu2XW.cjs +32 -0
- package/lib/settled-within-B6gpu2XW.cjs.map +1 -0
- package/lib/settled-within-Bc2pj8WB.mjs +21 -0
- package/lib/settled-within-Bc2pj8WB.mjs.map +1 -0
- package/lib/testing/index.cjs +74 -414
- package/lib/testing/index.cjs.map +1 -1
- package/lib/testing/index.d.cts +25 -385
- package/lib/testing/index.d.cts.map +1 -1
- package/lib/testing/index.d.mts +24 -385
- package/lib/testing/index.d.mts.map +1 -1
- package/lib/testing/index.mjs +72 -408
- package/lib/testing/index.mjs.map +1 -1
- package/lib/transport.cjs +36 -0
- package/lib/transport.d.cts +254 -0
- package/lib/transport.d.cts.map +1 -0
- package/lib/transport.d.mts +254 -0
- package/lib/transport.d.mts.map +1 -0
- package/lib/transport.mjs +6 -0
- package/package.json +37 -17
- package/docs/README.md +0 -316
- package/docs/adapters.md +0 -308
- package/docs/application-setup.md +0 -524
- package/docs/attributes.md +0 -689
- package/docs/controllers.md +0 -373
- package/docs/endpoints.md +0 -444
- package/docs/exceptions.md +0 -352
- package/docs/guards.md +0 -550
- package/docs/legacy-compat.md +0 -356
- package/docs/modules.md +0 -377
- package/docs/quick-start.md +0 -290
- package/docs/services.md +0 -427
- package/docs/testing.md +0 -827
- package/lib/index-3LcTrbxz.d.mts +0 -3804
- package/lib/index-3LcTrbxz.d.mts.map +0 -1
- package/lib/index-B14SekVb.d.cts +0 -3804
- package/lib/index-B14SekVb.d.cts.map +0 -1
- package/lib/navios.factory-CUrO_p6K.cjs +0 -4538
- package/lib/navios.factory-CUrO_p6K.cjs.map +0 -1
- package/lib/navios.factory-DjUOOVVL.mjs +0 -4151
- package/lib/navios.factory-DjUOOVVL.mjs.map +0 -1
- package/lib/tokens-BEuBMGGX.cjs +0 -193
- package/lib/tokens-BEuBMGGX.cjs.map +0 -1
- package/lib/tokens-COyNGV1I.mjs +0 -97
- package/lib/tokens-COyNGV1I.mjs.map +0 -1
- package/lib/use-guards.decorator-DLmRl2CV.mjs +0 -336
- package/lib/use-guards.decorator-DLmRl2CV.mjs.map +0 -1
- package/lib/use-guards.decorator-DhumFTk3.cjs +0 -444
- package/lib/use-guards.decorator-DhumFTk3.cjs.map +0 -1
- package/project.json +0 -66
- package/src/__tests__/attribute.factory.spec.mts +0 -300
- package/src/__tests__/config.service.spec.mts +0 -43
- package/src/__tests__/console-logger.service.spec.mts +0 -312
- package/src/__tests__/controller-resolver.spec.mts +0 -229
- package/src/__tests__/controller.spec.mts +0 -58
- package/src/__tests__/guard-runner.service.spec.mts +0 -399
- package/src/__tests__/logger.service.spec.mts +0 -147
- package/src/__tests__/responders.spec.mts +0 -340
- package/src/__tests__/testing-module.spec.mts +0 -459
- package/src/__tests__/unit-testing-module.spec.mts +0 -424
- package/src/attribute.factory.mts +0 -325
- package/src/config/config-service.interface.mts +0 -47
- package/src/config/config.provider.mts +0 -69
- package/src/config/config.service.mts +0 -176
- package/src/config/index.mts +0 -5
- package/src/config/types.mts +0 -58
- package/src/config/utils/helpers.mts +0 -23
- package/src/config/utils/index.mts +0 -1
- package/src/decorators/controller.decorator.mts +0 -81
- package/src/decorators/endpoint.decorator.mts +0 -181
- package/src/decorators/header.decorator.mts +0 -38
- package/src/decorators/http-code.decorator.mts +0 -38
- package/src/decorators/index.mts +0 -8
- package/src/decorators/module.decorator.mts +0 -106
- package/src/decorators/multipart.decorator.mts +0 -169
- package/src/decorators/stream.decorator.mts +0 -155
- package/src/decorators/use-guards.decorator.mts +0 -73
- package/src/exceptions/bad-request.exception.mts +0 -28
- package/src/exceptions/conflict.exception.mts +0 -31
- package/src/exceptions/forbidden.exception.mts +0 -30
- package/src/exceptions/http.exception.mts +0 -33
- package/src/exceptions/index.mts +0 -7
- package/src/exceptions/internal-server-error.exception.mts +0 -33
- package/src/exceptions/not-found.exception.mts +0 -33
- package/src/exceptions/unauthorized.exception.mts +0 -30
- package/src/factories/adapter.factory.mts +0 -20
- package/src/factories/endpoint-adapter.factory.mts +0 -21
- package/src/factories/http-adapter.factory.mts +0 -20
- package/src/factories/index.mts +0 -8
- package/src/factories/multipart-adapter.factory.mts +0 -21
- package/src/factories/reply.factory.mts +0 -21
- package/src/factories/request.factory.mts +0 -21
- package/src/factories/stream-adapter.factory.mts +0 -20
- package/src/factories/xml-stream-adapter.factory.mts +0 -20
- package/src/index.mts +0 -16
- package/src/interfaces/abstract-adapter.interface.mts +0 -32
- package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
- package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
- package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
- package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
- package/src/interfaces/adapter-environment.interface.mts +0 -74
- package/src/interfaces/can-activate.mts +0 -38
- package/src/interfaces/http-header.mts +0 -18
- package/src/interfaces/index.mts +0 -14
- package/src/interfaces/navios-module.mts +0 -28
- package/src/interfaces/plugin-context.mts +0 -104
- package/src/interfaces/plugin-stage.mts +0 -62
- package/src/interfaces/plugin.interface.mts +0 -119
- package/src/interfaces/staged-plugin.interface.mts +0 -209
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
- package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
- package/src/legacy-compat/attribute.factory.mts +0 -351
- package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
- package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
- package/src/legacy-compat/decorators/header.decorator.mts +0 -43
- package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
- package/src/legacy-compat/decorators/index.mts +0 -11
- package/src/legacy-compat/decorators/module.decorator.mts +0 -37
- package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
- package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
- package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
- package/src/legacy-compat/index.mts +0 -57
- package/src/logger/README.md +0 -3
- package/src/logger/console-logger.service.mts +0 -714
- package/src/logger/index.mts +0 -6
- package/src/logger/log-levels.mts +0 -21
- package/src/logger/logger-service.interface.mts +0 -42
- package/src/logger/logger.service.mts +0 -113
- package/src/logger/logger.tokens.mts +0 -44
- package/src/logger/utils/cli-colors.util.mts +0 -17
- package/src/logger/utils/filter-log-levelts.util.mts +0 -29
- package/src/logger/utils/index.mts +0 -5
- package/src/logger/utils/is-log-level-enabled.mts +0 -33
- package/src/logger/utils/is-log-level.util.mts +0 -10
- package/src/logger/utils/shared.utils.mts +0 -51
- package/src/metadata/controller.metadata.mts +0 -79
- package/src/metadata/handler.metadata.mts +0 -80
- package/src/metadata/index.mts +0 -4
- package/src/metadata/module.metadata.mts +0 -116
- package/src/metadata/navios-managed.metadata.mts +0 -42
- package/src/navios.application.mts +0 -678
- package/src/navios.environment.mts +0 -42
- package/src/navios.factory.mts +0 -158
- package/src/responders/enums/framework-error.enum.mts +0 -38
- package/src/responders/enums/index.mts +0 -1
- package/src/responders/index.mts +0 -4
- package/src/responders/interfaces/error-responder.interface.mts +0 -42
- package/src/responders/interfaces/error-response.interface.mts +0 -22
- package/src/responders/interfaces/index.mts +0 -3
- package/src/responders/interfaces/problem-details.interface.mts +0 -34
- package/src/responders/services/error-response-producer.service.mts +0 -143
- package/src/responders/services/forbidden-responder.service.mts +0 -77
- package/src/responders/services/index.mts +0 -5
- package/src/responders/services/internal-server-error-responder.service.mts +0 -57
- package/src/responders/services/not-found-responder.service.mts +0 -76
- package/src/responders/services/validation-error-responder.service.mts +0 -78
- package/src/responders/tokens/index.mts +0 -1
- package/src/responders/tokens/responder.tokens.mts +0 -84
- package/src/services/abstract-handler-adapter.service.mts +0 -366
- package/src/services/guard-runner.service.mts +0 -152
- package/src/services/index.mts +0 -4
- package/src/services/instance-resolver.service.mts +0 -177
- package/src/services/module-loader.service.mts +0 -255
- package/src/stores/index.mts +0 -1
- package/src/stores/request-id.store.mts +0 -85
- package/src/testing/index.mts +0 -3
- package/src/testing/testing-module.mts +0 -393
- package/src/testing/unit-testing-module.mts +0 -298
- package/src/tokens/adapter.token.mts +0 -6
- package/src/tokens/endpoint-adapter.token.mts +0 -8
- package/src/tokens/execution-context.token.mts +0 -9
- package/src/tokens/http-adapter.token.mts +0 -8
- package/src/tokens/index.mts +0 -10
- package/src/tokens/multipart-adapter.token.mts +0 -8
- package/src/tokens/navios-options.token.mts +0 -6
- package/src/tokens/reply.token.mts +0 -3
- package/src/tokens/request.token.mts +0 -3
- package/src/tokens/stream-adapter.token.mts +0 -8
- package/src/tokens/xml-stream-adapter.token.mts +0 -8
- package/src/utils/adapter-supports.util.mts +0 -47
- package/src/utils/define-plugin.mts +0 -251
- package/src/utils/index.mts +0 -2
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -8
- package/tsconfig.spec.json +0 -15
- package/tsdown.config.mts +0 -35
- package/vitest.config.mts +0 -15
package/docs/legacy-compat.md
DELETED
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
# Legacy-Compatible Decorators
|
|
2
|
-
|
|
3
|
-
Navios provides a legacy-compatible decorator API for projects that cannot use Stage 3 decorators (the standard decorator proposal). These decorators use the TypeScript experimental decorator API and convert the arguments to Stage 3 format internally.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The legacy-compat decorators are located in `@navios/core/legacy-compat` and provide the same functionality as the standard decorators, but with compatibility for TypeScript's experimental decorator system.
|
|
8
|
-
|
|
9
|
-
## When to Use Legacy-Compatible Decorators
|
|
10
|
-
|
|
11
|
-
Use legacy-compatible decorators if:
|
|
12
|
-
|
|
13
|
-
- Your project uses TypeScript with `experimentalDecorators` enabled
|
|
14
|
-
- You cannot migrate to Stage 3 decorators due to tooling or compatibility constraints
|
|
15
|
-
- You need to maintain compatibility with older TypeScript versions or build tools
|
|
16
|
-
|
|
17
|
-
## Installation and Setup
|
|
18
|
-
|
|
19
|
-
No additional installation is required. The legacy-compat decorators are included in `@navios/core`.
|
|
20
|
-
|
|
21
|
-
To use legacy-compatible decorators, import them from the `legacy-compat` subpath:
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
import {
|
|
25
|
-
Controller,
|
|
26
|
-
Endpoint,
|
|
27
|
-
Header,
|
|
28
|
-
HttpCode,
|
|
29
|
-
Module,
|
|
30
|
-
Multipart,
|
|
31
|
-
Stream,
|
|
32
|
-
UseGuards,
|
|
33
|
-
} from '@navios/core/legacy-compat'
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Available Decorators
|
|
37
|
-
|
|
38
|
-
All standard Navios decorators are available in legacy-compatible form:
|
|
39
|
-
|
|
40
|
-
### Class Decorators
|
|
41
|
-
|
|
42
|
-
#### `@Module(options?)`
|
|
43
|
-
|
|
44
|
-
Defines an application module. Can be applied to classes.
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
import { Module } from '@navios/core/legacy-compat'
|
|
48
|
-
|
|
49
|
-
@Module({
|
|
50
|
-
controllers: [UserController, AuthController],
|
|
51
|
-
imports: [DatabaseModule],
|
|
52
|
-
guards: [AuthGuard],
|
|
53
|
-
})
|
|
54
|
-
export class AppModule {}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
#### `@Controller(options?)`
|
|
58
|
-
|
|
59
|
-
Defines a request controller. Can be applied to classes.
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
import { Controller } from '@navios/core/legacy-compat'
|
|
63
|
-
|
|
64
|
-
@Controller({ guards: [AuthGuard] })
|
|
65
|
-
export class UserController {
|
|
66
|
-
// ...
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Method Decorators
|
|
71
|
-
|
|
72
|
-
#### `@Endpoint(endpoint)`
|
|
73
|
-
|
|
74
|
-
Defines an HTTP endpoint with type-safe request/response schemas. Must be applied to methods.
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
import type { EndpointParams, EndpointResult } from '@navios/core/legacy-compat'
|
|
78
|
-
|
|
79
|
-
import { Controller, Endpoint } from '@navios/core/legacy-compat'
|
|
80
|
-
|
|
81
|
-
@Controller()
|
|
82
|
-
export class UserController {
|
|
83
|
-
@Endpoint(getUserEndpoint)
|
|
84
|
-
async getUser(
|
|
85
|
-
request: EndpointParams<typeof getUserEndpoint>,
|
|
86
|
-
): Promise<EndpointResult<typeof getUserEndpoint>> {
|
|
87
|
-
const { id } = request
|
|
88
|
-
return { id, name: 'John Doe' }
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
#### `@Multipart(endpoint)`
|
|
94
|
-
|
|
95
|
-
Defines a multipart/form-data endpoint for file uploads. Must be applied to methods.
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
import type {
|
|
99
|
-
MultipartParams,
|
|
100
|
-
MultipartResult,
|
|
101
|
-
} from '@navios/core/legacy-compat'
|
|
102
|
-
|
|
103
|
-
import { Controller, Multipart } from '@navios/core/legacy-compat'
|
|
104
|
-
|
|
105
|
-
@Controller()
|
|
106
|
-
export class FileController {
|
|
107
|
-
@Multipart(uploadFileEndpoint)
|
|
108
|
-
async uploadFile(
|
|
109
|
-
request: MultipartParams<typeof uploadFileEndpoint>,
|
|
110
|
-
): Promise<MultipartResult<typeof uploadFileEndpoint>> {
|
|
111
|
-
const { file } = request.data
|
|
112
|
-
return { url: 'https://example.com/file.jpg' }
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
#### `@Stream(endpoint)`
|
|
118
|
-
|
|
119
|
-
Defines a streaming endpoint. Must be applied to methods.
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
import type { StreamParams } from '@navios/core/legacy-compat'
|
|
123
|
-
|
|
124
|
-
import { Controller, Stream } from '@navios/core/legacy-compat'
|
|
125
|
-
|
|
126
|
-
@Controller()
|
|
127
|
-
export class FileController {
|
|
128
|
-
@Stream(downloadFileEndpoint)
|
|
129
|
-
async downloadFile(
|
|
130
|
-
request: StreamParams<typeof downloadFileEndpoint>,
|
|
131
|
-
reply: any,
|
|
132
|
-
): Promise<void> {
|
|
133
|
-
const { fileId } = request.urlParams
|
|
134
|
-
// Stream file data to reply
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
#### `@UseGuards(...guards)`
|
|
140
|
-
|
|
141
|
-
Applies guards to controllers or endpoints. Can be applied to classes or methods.
|
|
142
|
-
|
|
143
|
-
```typescript
|
|
144
|
-
import { Controller, Endpoint, UseGuards } from '@navios/core/legacy-compat'
|
|
145
|
-
|
|
146
|
-
// Apply to a controller
|
|
147
|
-
@Controller()
|
|
148
|
-
@UseGuards(AuthGuard, RoleGuard)
|
|
149
|
-
export class UserController {
|
|
150
|
-
// All endpoints in this controller will use AuthGuard and RoleGuard
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Apply to a specific endpoint
|
|
154
|
-
@Controller()
|
|
155
|
-
export class UserController {
|
|
156
|
-
@Endpoint(getUserEndpoint)
|
|
157
|
-
@UseGuards(AuthGuard)
|
|
158
|
-
async getUser() {
|
|
159
|
-
// Only this endpoint uses AuthGuard
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
#### `@Header(name, value)`
|
|
165
|
-
|
|
166
|
-
Sets custom response headers. Must be applied to methods.
|
|
167
|
-
|
|
168
|
-
```typescript
|
|
169
|
-
import { Controller, Endpoint, Header } from '@navios/core/legacy-compat'
|
|
170
|
-
|
|
171
|
-
@Controller()
|
|
172
|
-
export class UserController {
|
|
173
|
-
@Endpoint(getUserEndpoint)
|
|
174
|
-
@Header('Cache-Control', 'max-age=3600')
|
|
175
|
-
async getUser() {
|
|
176
|
-
return { id: '1', name: 'John' }
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
#### `@HttpCode(code)`
|
|
182
|
-
|
|
183
|
-
Sets a custom HTTP status code for the response. Must be applied to methods.
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
import { Controller, Endpoint, HttpCode } from '@navios/core/legacy-compat'
|
|
187
|
-
|
|
188
|
-
@Controller()
|
|
189
|
-
export class UserController {
|
|
190
|
-
@Endpoint(createUserEndpoint)
|
|
191
|
-
@HttpCode(201)
|
|
192
|
-
async createUser() {
|
|
193
|
-
return { id: '1', name: 'John' }
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## Type Utilities
|
|
199
|
-
|
|
200
|
-
The legacy-compat module also exports type utilities for working with endpoints:
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
import type {
|
|
204
|
-
ControllerOptions,
|
|
205
|
-
EndpointParams,
|
|
206
|
-
EndpointResult,
|
|
207
|
-
ModuleOptions,
|
|
208
|
-
MultipartParams,
|
|
209
|
-
MultipartResult,
|
|
210
|
-
StreamParams,
|
|
211
|
-
} from '@navios/core/legacy-compat'
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
## Complete Example
|
|
215
|
-
|
|
216
|
-
Here's a complete example using legacy-compatible decorators:
|
|
217
|
-
|
|
218
|
-
```typescript
|
|
219
|
-
import type { EndpointParams, EndpointResult } from '@navios/core/legacy-compat'
|
|
220
|
-
|
|
221
|
-
import { builder } from '@navios/builder'
|
|
222
|
-
import { inject, Injectable } from '@navios/core'
|
|
223
|
-
import {
|
|
224
|
-
Controller,
|
|
225
|
-
Endpoint,
|
|
226
|
-
HttpCode,
|
|
227
|
-
Module,
|
|
228
|
-
UseGuards,
|
|
229
|
-
} from '@navios/core/legacy-compat'
|
|
230
|
-
|
|
231
|
-
import { z } from 'zod/v4'
|
|
232
|
-
|
|
233
|
-
// Define API endpoints
|
|
234
|
-
const api = builder()
|
|
235
|
-
|
|
236
|
-
const getUserEndpoint = api.declareEndpoint({
|
|
237
|
-
method: 'get',
|
|
238
|
-
url: '/users/:id',
|
|
239
|
-
responseSchema: z.object({
|
|
240
|
-
id: z.string(),
|
|
241
|
-
name: z.string(),
|
|
242
|
-
email: z.string().email(),
|
|
243
|
-
}),
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
const createUserEndpoint = api.declareEndpoint({
|
|
247
|
-
method: 'post',
|
|
248
|
-
url: '/users',
|
|
249
|
-
requestSchema: z.object({
|
|
250
|
-
name: z.string(),
|
|
251
|
-
email: z.string().email(),
|
|
252
|
-
}),
|
|
253
|
-
responseSchema: z.object({
|
|
254
|
-
id: z.string(),
|
|
255
|
-
name: z.string(),
|
|
256
|
-
email: z.string().email(),
|
|
257
|
-
}),
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
// Service
|
|
261
|
-
@Injectable()
|
|
262
|
-
export class UserService {
|
|
263
|
-
async findById(id: string) {
|
|
264
|
-
// Implementation
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async create(data: { name: string; email: string }) {
|
|
268
|
-
// Implementation
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// Controller
|
|
273
|
-
@Controller()
|
|
274
|
-
@UseGuards(AuthGuard)
|
|
275
|
-
export class UserController {
|
|
276
|
-
userService = inject(UserService)
|
|
277
|
-
|
|
278
|
-
@Endpoint(getUserEndpoint)
|
|
279
|
-
async getUser(
|
|
280
|
-
request: EndpointParams<typeof getUserEndpoint>,
|
|
281
|
-
): Promise<EndpointResult<typeof getUserEndpoint>> {
|
|
282
|
-
const { id } = request.urlParams
|
|
283
|
-
return await this.userService.findById(id)
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
@Endpoint(createUserEndpoint)
|
|
287
|
-
@HttpCode(201)
|
|
288
|
-
async createUser(
|
|
289
|
-
request: EndpointParams<typeof createUserEndpoint>,
|
|
290
|
-
): Promise<EndpointResult<typeof createUserEndpoint>> {
|
|
291
|
-
const { name, email } = request.body
|
|
292
|
-
return await this.userService.create({ name, email })
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Module
|
|
297
|
-
@Module({
|
|
298
|
-
controllers: [UserController],
|
|
299
|
-
})
|
|
300
|
-
export class AppModule {}
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
## Migration from Standard Decorators
|
|
304
|
-
|
|
305
|
-
If you're migrating from standard decorators to legacy-compatible decorators:
|
|
306
|
-
|
|
307
|
-
1. Update your imports to use the `legacy-compat` subpath:
|
|
308
|
-
|
|
309
|
-
```typescript
|
|
310
|
-
// Before
|
|
311
|
-
import { Controller, Endpoint, Module } from '@navios/core'
|
|
312
|
-
// After
|
|
313
|
-
import { Controller, Endpoint, Module } from '@navios/core/legacy-compat'
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
2. Ensure your `tsconfig.json` has `experimentalDecorators` enabled:
|
|
317
|
-
|
|
318
|
-
```json
|
|
319
|
-
{
|
|
320
|
-
"compilerOptions": {
|
|
321
|
-
"experimentalDecorators": true
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
3. The decorator usage remains the same - no changes to how you apply them.
|
|
327
|
-
|
|
328
|
-
## Differences from Standard Decorators
|
|
329
|
-
|
|
330
|
-
The legacy-compatible decorators:
|
|
331
|
-
|
|
332
|
-
- Use TypeScript's experimental decorator API instead of Stage 3 decorators
|
|
333
|
-
- Convert decorator arguments internally to work with the standard Navios implementation
|
|
334
|
-
- Provide the same functionality and type safety as standard decorators
|
|
335
|
-
- May have slightly different type inference behavior in some edge cases
|
|
336
|
-
|
|
337
|
-
## TypeScript Configuration
|
|
338
|
-
|
|
339
|
-
To use legacy-compatible decorators, ensure your `tsconfig.json` includes:
|
|
340
|
-
|
|
341
|
-
```json
|
|
342
|
-
{
|
|
343
|
-
"compilerOptions": {
|
|
344
|
-
"experimentalDecorators": true,
|
|
345
|
-
"emitDecoratorMetadata": true
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
The `emitDecoratorMetadata` option is optional but recommended for better runtime behavior with dependency injection.
|
|
351
|
-
|
|
352
|
-
## Notes
|
|
353
|
-
|
|
354
|
-
- Legacy-compatible decorators are fully compatible with all Navios features including guards, attributes, dependency injection, and adapters
|
|
355
|
-
- You can mix legacy-compatible decorators with standard decorators in the same project, but it's recommended to use one approach consistently
|
|
356
|
-
- The legacy-compat module is maintained alongside the standard decorators and receives the same updates and bug fixes
|
package/docs/modules.md
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
# Modules
|
|
2
|
-
|
|
3
|
-
Modules in Navios are the primary building blocks for organizing your application. They provide a way to group related controllers, services, and other providers into cohesive units that can be easily managed and imported.
|
|
4
|
-
|
|
5
|
-
## What is a Module?
|
|
6
|
-
|
|
7
|
-
A module is a TypeScript class decorated with the `@Module()` decorator. It serves as a container for controllers, other modules, and shared guards. Modules help organize your application into logical boundaries and enable dependency injection across the application.
|
|
8
|
-
|
|
9
|
-
## Creating a Module
|
|
10
|
-
|
|
11
|
-
### Basic Module
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
import { Module } from '@navios/core'
|
|
15
|
-
|
|
16
|
-
@Module()
|
|
17
|
-
export class AppModule {}
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
### Module with Controllers
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
import { Module } from '@navios/core'
|
|
24
|
-
|
|
25
|
-
import { ProductController } from './product.controller'
|
|
26
|
-
import { UserController } from './user.controller'
|
|
27
|
-
|
|
28
|
-
@Module({
|
|
29
|
-
controllers: [UserController, ProductController],
|
|
30
|
-
})
|
|
31
|
-
export class AppModule {}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Module with Imports
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
import { Module } from '@navios/core'
|
|
38
|
-
|
|
39
|
-
import { AuthModule } from './auth/auth.module'
|
|
40
|
-
import { UserModule } from './user/user.module'
|
|
41
|
-
|
|
42
|
-
@Module({
|
|
43
|
-
imports: [UserModule, AuthModule],
|
|
44
|
-
})
|
|
45
|
-
export class AppModule {}
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### Module with Guards
|
|
49
|
-
|
|
50
|
-
```typescript
|
|
51
|
-
import { Module } from '@navios/core'
|
|
52
|
-
|
|
53
|
-
import { AuthGuard } from './auth.guard'
|
|
54
|
-
import { UserController } from './user.controller'
|
|
55
|
-
|
|
56
|
-
@Module({
|
|
57
|
-
controllers: [UserController],
|
|
58
|
-
guards: [AuthGuard], // Applied to all controllers in this module
|
|
59
|
-
})
|
|
60
|
-
export class UserModule {}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Module Options
|
|
64
|
-
|
|
65
|
-
The `@Module()` decorator accepts the following options:
|
|
66
|
-
|
|
67
|
-
### `controllers`
|
|
68
|
-
|
|
69
|
-
- **Type**: `ClassType[] | Set<ClassType>`
|
|
70
|
-
- **Description**: Array of controller classes that belong to this module
|
|
71
|
-
- **Example**:
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
@Module({
|
|
75
|
-
controllers: [UserController, PostController],
|
|
76
|
-
})
|
|
77
|
-
export class UserModule {}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### `imports`
|
|
81
|
-
|
|
82
|
-
- **Type**: `ClassType[] | Set<ClassType>`
|
|
83
|
-
- **Description**: Array of other modules to import into this module
|
|
84
|
-
- **Example**:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
@Module({
|
|
88
|
-
imports: [DatabaseModule, AuthModule],
|
|
89
|
-
})
|
|
90
|
-
export class AppModule {}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### `guards`
|
|
94
|
-
|
|
95
|
-
- **Type**: `ClassType[] | Set<ClassType>`
|
|
96
|
-
- **Description**: Array of guard classes that will be applied to all controllers in this module
|
|
97
|
-
- **Example**:
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
@Module({
|
|
101
|
-
guards: [AuthGuard, RoleGuard],
|
|
102
|
-
})
|
|
103
|
-
export class ProtectedModule {}
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Module Lifecycle
|
|
107
|
-
|
|
108
|
-
Modules in Navios follow a specific lifecycle:
|
|
109
|
-
|
|
110
|
-
1. **Registration**: Modules are registered with the dependency injection container
|
|
111
|
-
2. **Import Resolution**: Imported modules are loaded recursively
|
|
112
|
-
3. **Controller Registration**: Controllers are registered and their endpoints discovered
|
|
113
|
-
4. **Guard Application**: Module-level guards are applied to all controllers
|
|
114
|
-
5. **Initialization**: Module initialization hooks are called
|
|
115
|
-
|
|
116
|
-
## Module Lifecycle Methods
|
|
117
|
-
|
|
118
|
-
### `onModuleInit`
|
|
119
|
-
|
|
120
|
-
The `onModuleInit` lifecycle method is called after the module has been initialized and all its dependencies have been resolved. This is useful for performing setup tasks, initializing connections, or running startup logic.
|
|
121
|
-
|
|
122
|
-
#### Interface
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
interface NaviosModule {
|
|
126
|
-
onModuleInit(): void | Promise<void>
|
|
127
|
-
}
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
#### Usage
|
|
131
|
-
|
|
132
|
-
To use the `onModuleInit` lifecycle method, implement the `NaviosModule` interface in your module class:
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
import { Module, NaviosModule } from '@navios/core'
|
|
136
|
-
|
|
137
|
-
@Module({
|
|
138
|
-
controllers: [UserController],
|
|
139
|
-
})
|
|
140
|
-
export class UserModule implements NaviosModule {
|
|
141
|
-
onModuleInit() {
|
|
142
|
-
console.log('UserModule has been initialized')
|
|
143
|
-
// Perform initialization logic here
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
#### Async Initialization
|
|
149
|
-
|
|
150
|
-
The `onModuleInit` method can be asynchronous, allowing you to perform async setup tasks:
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
import { Module, NaviosModule } from '@navios/core'
|
|
154
|
-
|
|
155
|
-
@Module({
|
|
156
|
-
controllers: [DatabaseController],
|
|
157
|
-
})
|
|
158
|
-
export class DatabaseModule implements NaviosModule {
|
|
159
|
-
async onModuleInit() {
|
|
160
|
-
console.log('Initializing database connection...')
|
|
161
|
-
await this.connectToDatabase()
|
|
162
|
-
console.log('Database connection established')
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private async connectToDatabase() {
|
|
166
|
-
// Database connection logic
|
|
167
|
-
return new Promise((resolve) => setTimeout(resolve, 1000))
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
#### Common Use Cases
|
|
173
|
-
|
|
174
|
-
- **Database Connections**: Initialize database connections or verify connectivity
|
|
175
|
-
- **Cache Warming**: Pre-populate caches with frequently accessed data
|
|
176
|
-
- **External Service Setup**: Initialize connections to external APIs or services
|
|
177
|
-
- **Configuration Validation**: Validate required configuration settings
|
|
178
|
-
- **Background Tasks**: Start background processes or scheduled tasks
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import { Module, NaviosModule } from '@navios/core'
|
|
182
|
-
|
|
183
|
-
@Module({
|
|
184
|
-
controllers: [CacheController],
|
|
185
|
-
})
|
|
186
|
-
export class CacheModule implements NaviosModule {
|
|
187
|
-
private cache = new Map<string, any>()
|
|
188
|
-
|
|
189
|
-
async onModuleInit() {
|
|
190
|
-
// Warm up the cache with initial data
|
|
191
|
-
await this.warmUpCache()
|
|
192
|
-
|
|
193
|
-
// Validate configuration
|
|
194
|
-
this.validateConfiguration()
|
|
195
|
-
|
|
196
|
-
console.log('CacheModule initialized successfully')
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private async warmUpCache() {
|
|
200
|
-
// Pre-populate cache with frequently accessed data
|
|
201
|
-
this.cache.set('app:config', await this.loadAppConfig())
|
|
202
|
-
this.cache.set('user:defaults', await this.loadUserDefaults())
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
private validateConfiguration() {
|
|
206
|
-
if (!process.env.CACHE_TTL) {
|
|
207
|
-
throw new Error('CACHE_TTL environment variable is required')
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private async loadAppConfig() {
|
|
212
|
-
// Load application configuration
|
|
213
|
-
return { theme: 'dark', language: 'en' }
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
private async loadUserDefaults() {
|
|
217
|
-
// Load default user settings
|
|
218
|
-
return { notifications: true, theme: 'auto' }
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
#### Execution Order
|
|
224
|
-
|
|
225
|
-
The `onModuleInit` methods are called in dependency order:
|
|
226
|
-
|
|
227
|
-
1. **Imported modules first**: All imported modules' `onModuleInit` methods are called before the current module
|
|
228
|
-
2. **Current module last**: The current module's `onModuleInit` method is called after all its dependencies
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
@Module({
|
|
232
|
-
imports: [DatabaseModule, CacheModule], // These initialize first
|
|
233
|
-
})
|
|
234
|
-
export class AppModule implements NaviosModule {
|
|
235
|
-
onModuleInit() {
|
|
236
|
-
// This runs after DatabaseModule and CacheModule have been initialized
|
|
237
|
-
console.log('AppModule initialized - all dependencies are ready')
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## Module Metadata
|
|
243
|
-
|
|
244
|
-
Each module decorated with `@Module()` has associated metadata that Navios uses internally:
|
|
245
|
-
|
|
246
|
-
```typescript
|
|
247
|
-
export interface ModuleMetadata {
|
|
248
|
-
controllers: Set<ClassType>
|
|
249
|
-
imports: Set<ClassType>
|
|
250
|
-
guards: Set<ClassType>
|
|
251
|
-
attributes: Map<symbol, unknown>
|
|
252
|
-
}
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
## Best Practices
|
|
256
|
-
|
|
257
|
-
### 1. Feature-Based Organization
|
|
258
|
-
|
|
259
|
-
Organize modules around business features rather than technical layers:
|
|
260
|
-
|
|
261
|
-
```typescript
|
|
262
|
-
// ✅ Good - Feature-based
|
|
263
|
-
@Module({
|
|
264
|
-
controllers: [UserController],
|
|
265
|
-
imports: [UserDatabaseModule],
|
|
266
|
-
})
|
|
267
|
-
export class UserModule {}
|
|
268
|
-
|
|
269
|
-
// ❌ Avoid - Layer-based
|
|
270
|
-
@Module({
|
|
271
|
-
controllers: [UserController, ProductController, OrderController],
|
|
272
|
-
})
|
|
273
|
-
export class ControllersModule {}
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
### 2. Single Responsibility
|
|
277
|
-
|
|
278
|
-
Each module should have a single, well-defined responsibility:
|
|
279
|
-
|
|
280
|
-
```typescript
|
|
281
|
-
// ✅ Good - Single responsibility
|
|
282
|
-
@Module({
|
|
283
|
-
controllers: [AuthController],
|
|
284
|
-
imports: [JwtModule],
|
|
285
|
-
})
|
|
286
|
-
export class AuthModule {}
|
|
287
|
-
|
|
288
|
-
// ❌ Avoid - Multiple responsibilities
|
|
289
|
-
@Module({
|
|
290
|
-
controllers: [AuthController, UserController, ProductController],
|
|
291
|
-
})
|
|
292
|
-
export class EverythingModule {}
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### 3. Explicit Dependencies
|
|
296
|
-
|
|
297
|
-
Always explicitly import the modules you depend on:
|
|
298
|
-
|
|
299
|
-
```typescript
|
|
300
|
-
// ✅ Good - Explicit imports
|
|
301
|
-
@Module({
|
|
302
|
-
imports: [AuthModule, DatabaseModule],
|
|
303
|
-
controllers: [UserController],
|
|
304
|
-
})
|
|
305
|
-
export class UserModule {}
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
### 4. Module Composition
|
|
309
|
-
|
|
310
|
-
Build complex applications by composing smaller, focused modules:
|
|
311
|
-
|
|
312
|
-
```typescript
|
|
313
|
-
@Module({
|
|
314
|
-
imports: [AuthModule, UserModule, ProductModule, OrderModule],
|
|
315
|
-
})
|
|
316
|
-
export class AppModule {}
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
## Advanced Usage
|
|
320
|
-
|
|
321
|
-
### Conditional Module Loading
|
|
322
|
-
|
|
323
|
-
You can conditionally include modules based on environment or configuration:
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
import { Module } from '@navios/core'
|
|
327
|
-
|
|
328
|
-
const imports = [CoreModule]
|
|
329
|
-
if (process.env.NODE_ENV === 'development') {
|
|
330
|
-
imports.push(DevToolsModule)
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
@Module({
|
|
334
|
-
imports,
|
|
335
|
-
controllers: [AppController],
|
|
336
|
-
})
|
|
337
|
-
export class AppModule {}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### Module with Complex Guard Setup
|
|
341
|
-
|
|
342
|
-
```typescript
|
|
343
|
-
import { Module } from '@navios/core'
|
|
344
|
-
|
|
345
|
-
import { AuthGuard, RoleGuard, ThrottleGuard } from './guards'
|
|
346
|
-
|
|
347
|
-
@Module({
|
|
348
|
-
guards: [
|
|
349
|
-
AuthGuard, // Applied first
|
|
350
|
-
RoleGuard, // Applied second
|
|
351
|
-
ThrottleGuard, // Applied last
|
|
352
|
-
],
|
|
353
|
-
controllers: [AdminController],
|
|
354
|
-
})
|
|
355
|
-
export class AdminModule {}
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
## Testing Modules
|
|
359
|
-
|
|
360
|
-
When testing modules, you can create test-specific module configurations:
|
|
361
|
-
|
|
362
|
-
```typescript
|
|
363
|
-
import { Module } from '@navios/core'
|
|
364
|
-
|
|
365
|
-
import { MockUserService } from './mocks/user.service'
|
|
366
|
-
import { UserController } from './user.controller'
|
|
367
|
-
|
|
368
|
-
@Module({
|
|
369
|
-
controllers: [UserController],
|
|
370
|
-
// Use mock services for testing
|
|
371
|
-
})
|
|
372
|
-
export class TestUserModule {}
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
## Module Discovery
|
|
376
|
-
|
|
377
|
-
Navios automatically discovers and registers modules through the module tree starting from your root application module.
|