@navios/core 0.3.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 (96) hide show
  1. package/README.md +96 -3
  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 +313 -280
  15. package/lib/_tsup-dts-rollup.d.ts +313 -280
  16. package/lib/index.d.mts +47 -26
  17. package/lib/index.d.ts +47 -26
  18. package/lib/index.js +633 -1068
  19. package/lib/index.js.map +1 -1
  20. package/lib/index.mjs +632 -1061
  21. package/lib/index.mjs.map +1 -1
  22. package/package.json +11 -12
  23. package/project.json +17 -4
  24. package/src/__tests__/config.service.spec.mts +11 -9
  25. package/src/__tests__/controller.spec.mts +1 -2
  26. package/src/attribute.factory.mts +1 -1
  27. package/src/config/config.provider.mts +2 -2
  28. package/src/config/config.service.mts +4 -4
  29. package/src/decorators/controller.decorator.mts +1 -1
  30. package/src/decorators/endpoint.decorator.mts +9 -10
  31. package/src/decorators/header.decorator.mts +1 -1
  32. package/src/decorators/multipart.decorator.mts +5 -5
  33. package/src/decorators/stream.decorator.mts +5 -6
  34. package/src/factories/endpoint-adapter.factory.mts +21 -0
  35. package/src/factories/http-adapter.factory.mts +20 -0
  36. package/src/factories/index.mts +6 -0
  37. package/src/factories/multipart-adapter.factory.mts +21 -0
  38. package/src/factories/reply.factory.mts +21 -0
  39. package/src/factories/request.factory.mts +21 -0
  40. package/src/factories/stream-adapter.factory.mts +20 -0
  41. package/src/index.mts +1 -1
  42. package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
  43. package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
  44. package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
  45. package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
  46. package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
  47. package/src/interfaces/can-activate.mts +4 -2
  48. package/src/interfaces/http-header.mts +18 -0
  49. package/src/interfaces/index.mts +6 -0
  50. package/src/logger/console-logger.service.mts +28 -44
  51. package/src/logger/index.mts +1 -2
  52. package/src/logger/logger.service.mts +9 -128
  53. package/src/logger/logger.tokens.mts +21 -0
  54. package/src/metadata/handler.metadata.mts +7 -5
  55. package/src/navios.application.mts +65 -172
  56. package/src/navios.environment.mts +30 -0
  57. package/src/navios.factory.mts +53 -12
  58. package/src/services/guard-runner.service.mts +19 -9
  59. package/src/services/index.mts +0 -2
  60. package/src/services/module-loader.service.mts +4 -3
  61. package/src/tokens/endpoint-adapter.token.mts +8 -0
  62. package/src/tokens/execution-context.token.mts +2 -2
  63. package/src/tokens/http-adapter.token.mts +8 -0
  64. package/src/tokens/index.mts +4 -1
  65. package/src/tokens/multipart-adapter.token.mts +8 -0
  66. package/src/tokens/reply.token.mts +1 -5
  67. package/src/tokens/request.token.mts +1 -7
  68. package/src/tokens/stream-adapter.token.mts +8 -0
  69. package/tsconfig.json +6 -1
  70. package/tsconfig.lib.json +8 -0
  71. package/tsconfig.spec.json +12 -0
  72. package/tsup.config.mts +1 -0
  73. package/docs/recipes/prisma.md +0 -60
  74. package/examples/simple-test/api/index.mts +0 -64
  75. package/examples/simple-test/config/config.service.mts +0 -14
  76. package/examples/simple-test/config/configuration.mts +0 -7
  77. package/examples/simple-test/index.mts +0 -16
  78. package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
  79. package/examples/simple-test/src/acl/acl.guard.mts +0 -14
  80. package/examples/simple-test/src/acl/app.guard.mts +0 -27
  81. package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
  82. package/examples/simple-test/src/acl/public.attribute.mts +0 -21
  83. package/examples/simple-test/src/app.module.mts +0 -9
  84. package/examples/simple-test/src/user/user.controller.mts +0 -72
  85. package/examples/simple-test/src/user/user.module.mts +0 -14
  86. package/examples/simple-test/src/user/user.service.mts +0 -14
  87. package/src/adapters/endpoint-adapter.service.mts +0 -72
  88. package/src/adapters/handler-adapter.interface.mts +0 -21
  89. package/src/adapters/index.mts +0 -4
  90. package/src/adapters/multipart-adapter.service.mts +0 -131
  91. package/src/adapters/stream-adapter.service.mts +0 -91
  92. package/src/logger/logger.factory.mts +0 -36
  93. package/src/logger/pino-wrapper.mts +0 -64
  94. package/src/services/controller-adapter.service.mts +0 -124
  95. package/src/services/execution-context.mts +0 -54
  96. package/src/tokens/application.token.mts +0 -9
package/README.md CHANGED
@@ -4,7 +4,24 @@ Navios is a Type-Safe HTTP Server with Zod Validation.
4
4
 
5
5
  It is a powerful tool for building robust and type-safe APIs using TypeScript. It leverages the power of Zod for validation and provides a simple and intuitive API for defining endpoints, request and response schemas, and handling errors.
6
6
 
7
- It uses Fastify under the hood, which is a fast and low-overhead web framework for Node.js. This allows Navios to provide high performance and low latency for your APIs.
7
+ Navios is adapter-based, allowing you to choose the underlying HTTP server implementation. Currently supported adapters include:
8
+
9
+ - **Fastify** (via `@navios/adapter-fastify`) - A fast and low-overhead web framework for Node.js
10
+ - **Bun** (via `@navios/adapter-bun`) - A fast JavaScript runtime optimized for server-side applications
11
+
12
+ ## Prerequisites
13
+
14
+ To work as an HTTP server, you must install one of the supported adapters:
15
+
16
+ ```bash
17
+ # For Fastify adapter
18
+ npm install @navios/adapter-fastify
19
+
20
+ # OR for Bun adapter
21
+ npm install @navios/adapter-bun
22
+ ```
23
+
24
+ The adapter must be provided when creating your Navios application to define the underlying HTTP server implementation.
8
25
 
9
26
  ## Features
10
27
 
@@ -15,6 +32,49 @@ It uses Fastify under the hood, which is a fast and low-overhead web framework f
15
32
  - **Customizable**: The package allows you to customize the behavior of the API client, such as using a custom client.
16
33
  - **Error Handling**: The package provides built-in error handling capabilities, allowing you to handle API errors gracefully and provide meaningful feedback to users.
17
34
 
35
+ ## Adapters
36
+
37
+ Navios uses an adapter pattern to support different HTTP server implementations. Each adapter provides the necessary bindings to integrate with a specific runtime or framework.
38
+
39
+ ### Available Adapters
40
+
41
+ #### Fastify Adapter (`@navios/adapter-fastify`)
42
+
43
+ - Built on top of [Fastify](https://www.fastify.io/), a fast and low-overhead web framework
44
+ - Excellent performance and a rich ecosystem of plugins
45
+ - Full Node.js compatibility
46
+ - Supports all Fastify features including hooks, plugins, and decorators
47
+
48
+ ```ts
49
+ import { defineFastifyEnvironment } from '@navios/adapter-fastify'
50
+
51
+ const app = await NaviosFactory.create(AppModule, {
52
+ adapter: defineFastifyEnvironment(),
53
+ })
54
+ ```
55
+
56
+ #### Bun Adapter (`@navios/adapter-bun`)
57
+
58
+ - Built for [Bun](https://bun.sh/), a fast JavaScript runtime optimized for performance
59
+ - Native HTTP server implementation with excellent performance
60
+ - Optimized for Bun's runtime features
61
+ - Lightweight and efficient
62
+
63
+ ```ts
64
+ import { defineBunEnvironment } from '@navios/adapter-bun'
65
+
66
+ const app = await NaviosFactory.create(AppModule, {
67
+ adapter: defineBunEnvironment(),
68
+ })
69
+ ```
70
+
71
+ ### Choosing an Adapter
72
+
73
+ The choice of adapter depends on your deployment environment and performance requirements:
74
+
75
+ - Use **Fastify adapter** if you need Node.js compatibility, access to the Fastify ecosystem, or are deploying to traditional Node.js environments
76
+ - Use **Bun adapter** if you're running on Bun runtime and want to take advantage of its performance optimizations
77
+
18
78
  ## Main Concepts
19
79
 
20
80
  - **Module**: A module is a collection of controllers, and other modules. It is used to group related functionality together and provide a clear structure for your API. It also can define some shared guards and attributes.
@@ -33,7 +93,7 @@ Define your API in a shared location accessible to both the client and server. T
33
93
  ```ts
34
94
  import { builder } from '@navios/core'
35
95
 
36
- import { z } from 'zod'
96
+ import { z } from 'zod/v4'
37
97
 
38
98
  const api = builder({
39
99
  useDiscriminatorResponse: true,
@@ -66,7 +126,13 @@ const login = api.declareEndpoint({
66
126
  ### Create your server
67
127
 
68
128
  ```bash
129
+ # Install core dependencies
69
130
  yarn install --save @navios/core @navios/builder zod
131
+
132
+ # Install one of the supported adapters
133
+ yarn install --save @navios/adapter-fastify
134
+ # OR
135
+ yarn install --save @navios/adapter-bun
70
136
  ```
71
137
 
72
138
  Create AuthService:
@@ -130,12 +196,20 @@ export class AppModule {}
130
196
  Create your server:
131
197
 
132
198
  ```ts
199
+ // Import your chosen adapter
200
+ import { defineFastifyEnvironment } from '@navios/adapter-fastify'
133
201
  import { NaviosFactory } from '@navios/core'
134
202
 
203
+ // OR import { defineBunEnvironment } from '@navios/adapter-bun'
204
+
135
205
  import { AppModule } from './src/app.module.mjs'
136
206
 
137
207
  export async function boot() {
138
- const app = await NaviosFactory.create(AppModule)
208
+ const app = await NaviosFactory.create(AppModule, {
209
+ // Provide the adapter environment
210
+ adapter: defineFastifyEnvironment(),
211
+ // OR adapter: defineBunEnvironment(),
212
+ })
139
213
  app.setGlobalPrefix('/api')
140
214
  app.enableCors({
141
215
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
@@ -147,3 +221,22 @@ await boot()
147
221
  ```
148
222
 
149
223
  That's it! You have created your first Navios server. You can now run your server and test the `/api/login` endpoint.
224
+
225
+ ## Documentation
226
+
227
+ - **[Quick Start Guide](./docs/quick-start.md)** - Get up and running quickly
228
+ - **[Complete Documentation](./docs/README.md)** - Comprehensive framework documentation
229
+ - **[Adapter Guide](./docs/adapters.md)** - Detailed adapter information and comparison
230
+ - **[API Examples](https://github.com/Arilas/navios/tree/main/examples)** - Working code examples
231
+
232
+ ## Related Packages
233
+
234
+ - **[@navios/adapter-fastify](https://www.npmjs.com/package/@navios/adapter-fastify)** - Fastify HTTP adapter
235
+ - **[@navios/adapter-bun](https://www.npmjs.com/package/@navios/adapter-bun)** - Bun HTTP adapter
236
+ - **[@navios/builder](https://www.npmjs.com/package/@navios/builder)** - Type-safe API definitions
237
+ - **[@navios/di](https://www.npmjs.com/package/@navios/di)** - Dependency injection container
238
+ - **[@navios/jwt](https://www.npmjs.com/package/@navios/jwt)** - JWT authentication utilities
239
+
240
+ ## License
241
+
242
+ MIT
package/docs/README.md CHANGED
@@ -1,6 +1,313 @@
1
- # Introduction
1
+ # Navios Documentation
2
2
 
3
- Navios us a framework for building Type-Safe Node.js servers. It uses TypeScript and Navios builder to generate a server with a specific structure. The framework is designed to be easy to use and understand, while also being powerful and flexible.
3
+ ## Table of Contents
4
4
 
5
- Under the hood, Navios makes use of [Fastify](https://www.fastify.io/) for the server implementation, and [Zod](https://zod.dev/) for schema validation. This allows Navios to provide a type-safe experience while still being performant and easy to use.
5
+ ### Getting Started
6
6
 
7
+ - [Quick Start Guide](./quick-start.md) - Get up and running quickly
8
+ - [Application Setup and Configuration](./application-setup.md) - Comprehensive setup guide
9
+
10
+ ### Core Concepts
11
+
12
+ - [Modules](./modules.md) - Application organization and structure
13
+ - [Controllers](./controllers.md) - Request handlers and routing
14
+ - [Endpoints](./endpoints.md) - HTTP route definitions and configuration
15
+ - [Services and Dependency Injection](./services.md) - Business logic and DI system
16
+ - [Guards](./guards.md) - Authentication, authorization, and request filtering
17
+ - [Exception Handling](./exceptions.md) - Error handling and HTTP exceptions
18
+ - [Attribute System](./attributes.md) - Metadata and custom decorators
19
+
20
+ ### Infrastructure
21
+
22
+ - [HTTP Server Adapters](./adapters.md) - Fastify, Bun, and custom adapters
23
+
24
+ ### Advanced Topics
25
+
26
+ - [Testing Guide](./testing.md) - Unit, integration, and E2E testing strategies
27
+ - Performance Optimization
28
+ - Deployment Patterns
29
+ - Plugin Development
30
+
31
+ ---
32
+
33
+ ## Introduction
34
+
35
+ Navios is a framework for building Type-Safe HTTP servers with TypeScript. It uses TypeScript and Navios builder to create servers with a well-defined structure, emphasizing type safety, validation, and developer experience.
36
+
37
+ The framework is adapter-based, allowing you to choose the underlying HTTP server implementation that best fits your deployment environment and performance requirements.
38
+
39
+ > **Important**: Always use `@navios/builder` to define your API endpoints instead of passing configuration objects directly to decorators. This ensures better type safety, maintainability, and access to advanced features like client generation.
40
+
41
+ ## Architecture Overview
42
+
43
+ Navios follows a modular architecture with the following key components:
44
+
45
+ - **Core Framework** (`@navios/core`) - Provides the main framework features
46
+ - **Adapters** - HTTP server implementations (Fastify, Bun)
47
+ - **Builder** (`@navios/builder`) - Type-safe API definition and client generation
48
+ - **Dependency Injection** (`@navios/di`) - Powerful DI container
49
+ - **Additional Packages** - JWT, scheduling, React Query integration, and more
50
+
51
+ ## HTTP Server Adapters
52
+
53
+ Navios requires an HTTP adapter to function as a server. The adapter pattern allows Navios to support different runtimes and frameworks while maintaining a consistent API.
54
+
55
+ ### Prerequisites
56
+
57
+ **Important**: To use Navios as an HTTP server, you **must** install one of the supported adapters:
58
+
59
+ ```bash
60
+ # For Fastify adapter (Node.js)
61
+ npm install @navios/adapter-fastify
62
+
63
+ # OR for Bun adapter (Bun runtime)
64
+ npm install @navios/adapter-bun
65
+ ```
66
+
67
+ ### Fastify Adapter
68
+
69
+ The Fastify adapter (`@navios/adapter-fastify`) integrates Navios with the [Fastify](https://www.fastify.io/) web framework.
70
+
71
+ #### Features:
72
+
73
+ - High performance HTTP server
74
+ - Rich ecosystem of plugins
75
+ - Full Node.js compatibility
76
+ - Built-in request/response validation
77
+ - Comprehensive middleware support
78
+ - Production-ready with extensive documentation
79
+
80
+ #### Usage:
81
+
82
+ ```ts
83
+ import { defineFastifyEnvironment } from '@navios/adapter-fastify'
84
+ import { NaviosFactory } from '@navios/core'
85
+
86
+ import { AppModule } from './app.module.js'
87
+
88
+ async function bootstrap() {
89
+ const app = await NaviosFactory.create(AppModule, {
90
+ adapter: defineFastifyEnvironment(),
91
+ })
92
+
93
+ // Configure CORS
94
+ app.enableCors({
95
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
96
+ })
97
+
98
+ // Enable multipart support for file uploads
99
+ app.enableMultipart({})
100
+
101
+ await app.init()
102
+ await app.listen({ port: 3000, host: '0.0.0.0' })
103
+ }
104
+
105
+ bootstrap()
106
+ ```
107
+
108
+ #### Dependencies:
109
+
110
+ The Fastify adapter requires `fastify` as a peer dependency:
111
+
112
+ ```json
113
+ {
114
+ "dependencies": {
115
+ "@navios/core": "^0.5.0",
116
+ "@navios/adapter-fastify": "^0.5.0",
117
+ "fastify": "^5.6.0"
118
+ }
119
+ }
120
+ ```
121
+
122
+ ### Bun Adapter
123
+
124
+ The Bun adapter (`@navios/adapter-bun`) integrates Navios with [Bun's](https://bun.sh/) native HTTP server.
125
+
126
+ #### Features:
127
+
128
+ - Optimized for Bun runtime performance
129
+ - Native HTTP server implementation
130
+ - Lightweight with minimal overhead
131
+ - Fast startup times
132
+ - Efficient memory usage
133
+
134
+ #### Usage:
135
+
136
+ ```ts
137
+ import { defineBunEnvironment } from '@navios/adapter-bun'
138
+ import { NaviosFactory } from '@navios/core'
139
+
140
+ import { AppModule } from './app.module.js'
141
+
142
+ async function bootstrap() {
143
+ const app = await NaviosFactory.create(AppModule, {
144
+ adapter: defineBunEnvironment(),
145
+ })
146
+
147
+ // Configure CORS
148
+ app.enableCors({
149
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
150
+ })
151
+
152
+ // Enable multipart support
153
+ app.enableMultipart({})
154
+
155
+ await app.init()
156
+ await app.listen({ port: 3000, host: '0.0.0.0' })
157
+ }
158
+
159
+ bootstrap()
160
+ ```
161
+
162
+ #### Dependencies:
163
+
164
+ The Bun adapter works with Bun runtime and requires no additional HTTP server dependencies:
165
+
166
+ ```json
167
+ {
168
+ "dependencies": {
169
+ "@navios/core": "^0.5.0",
170
+ "@navios/adapter-bun": "^0.5.0"
171
+ }
172
+ }
173
+ ```
174
+
175
+ ## Adapter Selection Guide
176
+
177
+ Choose the appropriate adapter based on your deployment environment and requirements:
178
+
179
+ | Adapter | Runtime | Performance | Ecosystem | Use Case |
180
+ | ------- | ------- | ----------- | --------- | --------------------------------------------- |
181
+ | Fastify | Node.js | High | Rich | Production Node.js apps, need Fastify plugins |
182
+ | Bun | Bun | Very High | Growing | Performance-critical apps, Bun runtime |
183
+
184
+ ### When to use Fastify Adapter:
185
+
186
+ - Deploying to Node.js environments
187
+ - Need access to Fastify's extensive plugin ecosystem
188
+ - Require compatibility with existing Node.js tooling
189
+ - Working with teams familiar with Express/Fastify patterns
190
+ - Need mature production support and documentation
191
+
192
+ ### When to use Bun Adapter:
193
+
194
+ - Running on Bun runtime
195
+ - Performance is critical
196
+ - Want faster startup times
197
+ - Prefer minimal dependencies
198
+ - Building new applications without legacy constraints
199
+
200
+ ## Error Handling
201
+
202
+ Both adapters provide consistent error handling through Navios's exception system:
203
+
204
+ ```ts
205
+ import { BadRequestException, NotFoundException } from '@navios/core'
206
+
207
+ @Controller()
208
+ export class UserController {
209
+ @Endpoint(getUserEndpoint)
210
+ async getUser(request: EndpointParams<typeof getUserEndpoint>) {
211
+ const { id } = request
212
+
213
+ if (!id) {
214
+ throw new BadRequestException('User ID is required')
215
+ }
216
+
217
+ const user = await this.userService.findById(id)
218
+ if (!user) {
219
+ throw new NotFoundException('User not found')
220
+ }
221
+
222
+ return user
223
+ }
224
+ }
225
+ ```
226
+
227
+ ## Configuration
228
+
229
+ Both adapters support the same Navios configuration options:
230
+
231
+ ```ts
232
+ const app = await NaviosFactory.create(AppModule, {
233
+ adapter: defineFastifyEnvironment(), // or defineBunEnvironment()
234
+ logger: ['error', 'warn', 'log'], // Optional: configure logging levels
235
+ })
236
+
237
+ // Global configuration
238
+ app.setGlobalPrefix('/api/v1')
239
+ app.enableCors({
240
+ origin: ['http://localhost:3000'],
241
+ methods: ['GET', 'POST', 'PUT', 'DELETE'],
242
+ credentials: true,
243
+ })
244
+
245
+ // Enable file uploads
246
+ app.enableMultipart({
247
+ limits: {
248
+ fileSize: 1024 * 1024 * 10, // 10MB
249
+ },
250
+ })
251
+ ```
252
+
253
+ ## Advanced Usage
254
+
255
+ ### Custom Adapter Configuration
256
+
257
+ You can pass configuration options to the underlying server:
258
+
259
+ ```ts
260
+ // Fastify-specific configuration
261
+ const app = await NaviosFactory.create(AppModule, {
262
+ adapter: defineFastifyEnvironment({
263
+ // Fastify instance options can be configured here if needed
264
+ }),
265
+ })
266
+
267
+ // Bun-specific configuration
268
+ const app = await NaviosFactory.create(AppModule, {
269
+ adapter: defineBunEnvironment({
270
+ // Bun server options can be configured here if needed
271
+ }),
272
+ })
273
+ ```
274
+
275
+ ### Multiple Adapters
276
+
277
+ You can also configure multiple adapters if needed (advanced use case):
278
+
279
+ ```ts
280
+ const app = await NaviosFactory.create(AppModule, {
281
+ adapter: [
282
+ defineFastifyEnvironment(),
283
+ // Additional adapters...
284
+ ],
285
+ })
286
+ ```
287
+
288
+ ## Migration Between Adapters
289
+
290
+ Switching between adapters is straightforward and requires minimal code changes:
291
+
292
+ 1. Install the new adapter package
293
+ 2. Update the import statement
294
+ 3. Change the adapter function call
295
+ 4. Update any adapter-specific configurations
296
+
297
+ ```ts
298
+ // From Fastify
299
+
300
+ // To Bun
301
+ import { defineBunEnvironment } from '@navios/adapter-bun'
302
+ import { defineFastifyEnvironment } from '@navios/adapter-fastify'
303
+
304
+ const app = await NaviosFactory.create(AppModule, {
305
+ adapter: defineFastifyEnvironment(),
306
+ })
307
+
308
+ const app = await NaviosFactory.create(AppModule, {
309
+ adapter: defineBunEnvironment(),
310
+ })
311
+ ```
312
+
313
+ Your controllers, services, and application logic remain unchanged.