@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.
Files changed (92) hide show
  1. package/README.md +95 -2
  2. package/docs/README.md +310 -3
  3. package/docs/adapters.md +308 -0
  4. package/docs/application-setup.md +524 -0
  5. package/docs/attributes.md +689 -0
  6. package/docs/controllers.md +373 -0
  7. package/docs/endpoints.md +444 -0
  8. package/docs/exceptions.md +316 -0
  9. package/docs/guards.md +550 -0
  10. package/docs/modules.md +251 -0
  11. package/docs/quick-start.md +295 -0
  12. package/docs/services.md +428 -0
  13. package/docs/testing.md +704 -0
  14. package/lib/_tsup-dts-rollup.d.mts +300 -235
  15. package/lib/_tsup-dts-rollup.d.ts +300 -235
  16. package/lib/index.d.mts +47 -26
  17. package/lib/index.d.ts +47 -26
  18. package/lib/index.js +633 -1072
  19. package/lib/index.js.map +1 -1
  20. package/lib/index.mjs +631 -1064
  21. package/lib/index.mjs.map +1 -1
  22. package/package.json +4 -7
  23. package/project.json +9 -1
  24. package/src/__tests__/config.service.spec.mts +11 -9
  25. package/src/__tests__/controller.spec.mts +0 -1
  26. package/src/config/config.service.mts +2 -2
  27. package/src/decorators/controller.decorator.mts +1 -1
  28. package/src/decorators/endpoint.decorator.mts +2 -2
  29. package/src/decorators/header.decorator.mts +1 -1
  30. package/src/decorators/multipart.decorator.mts +1 -1
  31. package/src/decorators/stream.decorator.mts +2 -3
  32. package/src/factories/endpoint-adapter.factory.mts +21 -0
  33. package/src/factories/http-adapter.factory.mts +20 -0
  34. package/src/factories/index.mts +6 -0
  35. package/src/factories/multipart-adapter.factory.mts +21 -0
  36. package/src/factories/reply.factory.mts +21 -0
  37. package/src/factories/request.factory.mts +21 -0
  38. package/src/factories/stream-adapter.factory.mts +20 -0
  39. package/src/index.mts +1 -1
  40. package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
  41. package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
  42. package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
  43. package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
  44. package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
  45. package/src/interfaces/can-activate.mts +4 -2
  46. package/src/interfaces/http-header.mts +18 -0
  47. package/src/interfaces/index.mts +6 -0
  48. package/src/logger/console-logger.service.mts +28 -44
  49. package/src/logger/index.mts +1 -2
  50. package/src/logger/logger.service.mts +9 -128
  51. package/src/logger/logger.tokens.mts +21 -0
  52. package/src/metadata/handler.metadata.mts +7 -5
  53. package/src/navios.application.mts +65 -172
  54. package/src/navios.environment.mts +30 -0
  55. package/src/navios.factory.mts +53 -12
  56. package/src/services/guard-runner.service.mts +19 -9
  57. package/src/services/index.mts +0 -2
  58. package/src/services/module-loader.service.mts +4 -3
  59. package/src/tokens/endpoint-adapter.token.mts +8 -0
  60. package/src/tokens/execution-context.token.mts +2 -2
  61. package/src/tokens/http-adapter.token.mts +8 -0
  62. package/src/tokens/index.mts +4 -1
  63. package/src/tokens/multipart-adapter.token.mts +8 -0
  64. package/src/tokens/reply.token.mts +1 -5
  65. package/src/tokens/request.token.mts +1 -7
  66. package/src/tokens/stream-adapter.token.mts +8 -0
  67. package/docs/recipes/prisma.md +0 -60
  68. package/e2e/endpoints/get.spec.mts +0 -97
  69. package/e2e/endpoints/post.spec.mts +0 -113
  70. package/examples/simple-test/api/index.mts +0 -64
  71. package/examples/simple-test/config/config.service.mts +0 -14
  72. package/examples/simple-test/config/configuration.mts +0 -7
  73. package/examples/simple-test/index.mts +0 -16
  74. package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
  75. package/examples/simple-test/src/acl/acl.guard.mts +0 -14
  76. package/examples/simple-test/src/acl/app.guard.mts +0 -27
  77. package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
  78. package/examples/simple-test/src/acl/public.attribute.mts +0 -21
  79. package/examples/simple-test/src/app.module.mts +0 -9
  80. package/examples/simple-test/src/user/user.controller.mts +0 -72
  81. package/examples/simple-test/src/user/user.module.mts +0 -14
  82. package/examples/simple-test/src/user/user.service.mts +0 -14
  83. package/src/adapters/endpoint-adapter.service.mts +0 -72
  84. package/src/adapters/handler-adapter.interface.mts +0 -21
  85. package/src/adapters/index.mts +0 -4
  86. package/src/adapters/multipart-adapter.service.mts +0 -135
  87. package/src/adapters/stream-adapter.service.mts +0 -91
  88. package/src/logger/logger.factory.mts +0 -36
  89. package/src/logger/pino-wrapper.mts +0 -64
  90. package/src/services/controller-adapter.service.mts +0 -124
  91. package/src/services/execution-context.mts +0 -54
  92. package/src/tokens/application.token.mts +0 -9
@@ -0,0 +1,308 @@
1
+ # Navios HTTP Adapters
2
+
3
+ This document provides detailed information about Navios HTTP adapters, their implementation, and usage patterns.
4
+
5
+ ## Overview
6
+
7
+ Navios uses an adapter pattern to provide HTTP server functionality. Adapters are responsible for:
8
+
9
+ - Binding HTTP requests and responses to Navios's internal format
10
+ - Providing server lifecycle management (start, stop, graceful shutdown)
11
+ - Handling server-specific features (middleware, plugins, etc.)
12
+ - Abstracting runtime-specific implementations
13
+
14
+ ## Adapter Architecture
15
+
16
+ Each adapter implements the following core interfaces:
17
+
18
+ ### HttpAdapterToken
19
+
20
+ Provides the main HTTP server functionality:
21
+
22
+ - Server initialization and configuration
23
+ - Request/response handling
24
+ - Server lifecycle management
25
+
26
+ ### EndpointAdapterToken
27
+
28
+ Handles endpoint-specific functionality:
29
+
30
+ - Route registration
31
+ - Request parameter extraction
32
+ - Response formatting
33
+
34
+ ### StreamAdapterToken
35
+
36
+ Manages streaming responses:
37
+
38
+ - File streaming
39
+ - Server-sent events
40
+ - Large response handling
41
+
42
+ ### MultipartAdapterToken
43
+
44
+ Handles multipart requests:
45
+
46
+ - File uploads
47
+ - Form data processing
48
+ - Binary data handling
49
+
50
+ ## Available Adapters
51
+
52
+ ### @navios/adapter-fastify
53
+
54
+ Built on [Fastify](https://www.fastify.io/), providing a robust HTTP server with excellent performance and a rich ecosystem.
55
+
56
+ #### Installation
57
+
58
+ ```bash
59
+ npm install @navios/adapter-fastify fastify
60
+ ```
61
+
62
+ #### Basic Usage
63
+
64
+ ```ts
65
+ import { defineFastifyEnvironment } from '@navios/adapter-fastify'
66
+ import { NaviosFactory } from '@navios/core'
67
+
68
+ const app = await NaviosFactory.create(AppModule, {
69
+ adapter: defineFastifyEnvironment(),
70
+ })
71
+ ```
72
+
73
+ #### Features
74
+
75
+ - **High Performance**: Fastify is one of the fastest Node.js web frameworks
76
+ - **Schema-based Validation**: Built-in JSON Schema validation
77
+ - **Rich Ecosystem**: Extensive plugin system
78
+ - **TypeScript Support**: Full TypeScript integration
79
+ - **Production Ready**: Battle-tested in production environments
80
+
81
+ #### Fastify-specific Features
82
+
83
+ ##### Hooks Integration
84
+
85
+ The adapter integrates with Fastify's hook system:
86
+
87
+ ```ts
88
+ // Access to underlying Fastify instance (if needed)
89
+ const fastifyInstance = app.getServer()
90
+
91
+ // Add Fastify hooks
92
+ fastifyInstance.addHook('preHandler', async (request, reply) => {
93
+ // Custom pre-handler logic
94
+ })
95
+ ```
96
+
97
+ ##### Plugin System
98
+
99
+ You can register Fastify plugins:
100
+
101
+ ```ts
102
+ import fastifyStatic from '@fastify/static'
103
+
104
+ const fastifyInstance = app.getServer()
105
+ await fastifyInstance.register(fastifyStatic, {
106
+ root: path.join(__dirname, 'public'),
107
+ prefix: '/public/',
108
+ })
109
+ ```
110
+
111
+ #### Configuration Options
112
+
113
+ ```ts
114
+ const app = await NaviosFactory.create(AppModule, {
115
+ adapter: defineFastifyEnvironment({
116
+ // Fastify server options
117
+ trustProxy: true,
118
+ }),
119
+ })
120
+ ```
121
+
122
+ ### @navios/adapter-bun
123
+
124
+ Built for [Bun](https://bun.sh/) runtime, providing optimal performance and native integration.
125
+
126
+ #### Installation
127
+
128
+ ```bash
129
+ npm install @navios/adapter-bun
130
+ ```
131
+
132
+ #### Basic Usage
133
+
134
+ ```ts
135
+ import { defineBunEnvironment } from '@navios/adapter-bun'
136
+ import { NaviosFactory } from '@navios/core'
137
+
138
+ const app = await NaviosFactory.create(AppModule, {
139
+ adapter: defineBunEnvironment(),
140
+ })
141
+ ```
142
+
143
+ #### Features
144
+
145
+ - **Native Performance**: Built on Bun's native HTTP server
146
+ - **Fast Startup**: Optimized for quick application startup
147
+ - **Memory Efficient**: Lower memory footprint
148
+ - **Modern Runtime**: Takes advantage of Bun's modern JavaScript features
149
+
150
+ #### Bun-specific Features
151
+
152
+ #### Configuration Options
153
+
154
+ ```ts
155
+ const app = await NaviosFactory.create(AppModule, {
156
+ adapter: defineBunEnvironment({
157
+ // Bun server options
158
+ development: process.env.NODE_ENV === 'development',
159
+ }),
160
+ })
161
+ ```
162
+
163
+ ## Adapter Comparison
164
+
165
+ | Feature | Fastify Adapter | Bun Adapter |
166
+ | ------------------ | ---------------------- | ----------- |
167
+ | **Runtime** | Node.js | Bun |
168
+ | **Performance** | High | Very High |
169
+ | **Memory Usage** | Moderate | Low |
170
+ | **Startup Time** | Fast | Very Fast |
171
+ | **Ecosystem** | Rich (Fastify plugins) | Growing |
172
+ | **WebSocket** | Via plugins | Native |
173
+ | **File Serving** | Via plugins | Native |
174
+ | **Production Use** | Mature | Emerging |
175
+ | **Community** | Large | Growing |
176
+
177
+ ## Creating Custom Adapters
178
+
179
+ To create a custom adapter, implement the required tokens:
180
+
181
+ ```ts
182
+ import {
183
+ EndpointAdapterToken,
184
+ HttpAdapterToken,
185
+ MultipartAdapterToken,
186
+ StreamAdapterToken,
187
+ } from '@navios/core'
188
+
189
+ export function defineCustomEnvironment() {
190
+ const httpTokens = new Map([
191
+ [HttpAdapterToken, CustomHttpAdapter],
192
+ [EndpointAdapterToken, CustomEndpointAdapter],
193
+ [StreamAdapterToken, CustomStreamAdapter],
194
+ [MultipartAdapterToken, CustomMultipartAdapter],
195
+ ])
196
+
197
+ return { httpTokens }
198
+ }
199
+ ```
200
+
201
+ ### HttpAdapter Implementation
202
+
203
+ ```ts
204
+ import { AbstractHttpAdapter } from '@navios/core'
205
+ import { Injectable } from '@navios/di'
206
+
207
+ @Injectable()
208
+ export class CustomHttpAdapter extends AbstractHttpAdapter {
209
+ async init() {
210
+ // Initialize your HTTP server
211
+ }
212
+
213
+ async listen(options: any) {
214
+ // Start listening for requests
215
+ }
216
+
217
+ async close() {
218
+ // Graceful shutdown
219
+ }
220
+
221
+ // Implement other required methods...
222
+ }
223
+ ```
224
+
225
+ ## Best Practices
226
+
227
+ ### Adapter Selection
228
+
229
+ 1. **Choose based on runtime**: Use Fastify for Node.js, Bun for Bun runtime
230
+ 2. **Consider ecosystem needs**: Fastify has more plugins available
231
+ 3. **Performance requirements**: Bun offers better raw performance
232
+ 4. **Team expertise**: Consider your team's familiarity with the underlying technology
233
+
234
+ ### Configuration
235
+
236
+ 1. **Environment-specific settings**: Configure adapters differently for dev/prod
237
+ 2. **Security**: Enable appropriate security features for production
238
+ 3. **Monitoring**: Set up proper logging and metrics collection
239
+ 4. **Resource limits**: Configure appropriate timeouts and limits
240
+
241
+ ### Error Handling
242
+
243
+ 1. **Consistent error responses**: Use Navios exceptions for consistent API responses
244
+ 2. **Logging**: Ensure proper error logging across adapters
245
+ 3. **Graceful degradation**: Handle adapter-specific errors appropriately
246
+
247
+ ### Performance Optimization
248
+
249
+ 1. **Connection pooling**: Configure appropriate connection limits
250
+ 2. **Caching**: Use adapter-specific caching when available
251
+ 3. **Compression**: Enable response compression for better performance
252
+ 4. **Static files**: Use efficient static file serving strategies
253
+
254
+ ## Troubleshooting
255
+
256
+ ### Common Issues
257
+
258
+ #### Fastify Adapter
259
+
260
+ - **Plugin conflicts**: Ensure Fastify plugins are compatible
261
+ - **Schema validation**: Check JSON schema definitions for validation errors
262
+ - **Memory leaks**: Monitor for unclosed connections or resources
263
+
264
+ #### Bun Adapter
265
+
266
+ - **Runtime compatibility**: Ensure code is compatible with Bun runtime
267
+ - **Module resolution**: Check import/export statements work with Bun
268
+ - **Native features**: Verify Bun-specific features are used correctly
269
+
270
+ ### Debugging
271
+
272
+ 1. **Enable logging**: Use appropriate log levels for debugging
273
+ 2. **Check adapter health**: Monitor adapter-specific metrics
274
+ 3. **Validate configuration**: Ensure adapter configuration is correct
275
+ 4. **Test isolation**: Test adapters in isolation when troubleshooting
276
+
277
+ ## Migration Guide
278
+
279
+ ### From Express to Navios
280
+
281
+ If migrating from Express, the Fastify adapter provides the most familiar experience:
282
+
283
+ ```ts
284
+ // Express-style (before)
285
+ app.get('/users/:id', (req, res) => {
286
+ res.json({ id: req.params.id })
287
+ })
288
+
289
+ // Navios with Fastify adapter (after)
290
+ @Controller()
291
+ export class UserController {
292
+ @Endpoint(getUserEndpoint)
293
+ async getUser(request: EndpointParams<typeof getUserEndpoint>) {
294
+ return { id: request.id }
295
+ }
296
+ }
297
+ ```
298
+
299
+ ### Between Adapters
300
+
301
+ Switching adapters is straightforward:
302
+
303
+ 1. Update dependencies
304
+ 2. Change the adapter import and function call
305
+ 3. Update any adapter-specific configurations
306
+ 4. Test thoroughly in your specific environment
307
+
308
+ The rest of your application code remains unchanged due to Navios's adapter abstraction.