@navios/core 1.1.0 → 2.0.0-alpha.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 (241) hide show
  1. package/CHANGELOG.md +53 -11
  2. package/README.md +67 -220
  3. package/lib/async-context-6pdlnNLA.d.mts +237 -0
  4. package/lib/async-context-6pdlnNLA.d.mts.map +1 -0
  5. package/lib/async-context-Dqk7pE-D.d.cts +237 -0
  6. package/lib/async-context-Dqk7pE-D.d.cts.map +1 -0
  7. package/lib/attribute-factory-B3VuYnp7.d.cts +70 -0
  8. package/lib/attribute-factory-B3VuYnp7.d.cts.map +1 -0
  9. package/lib/attribute-factory-mKOXoVnV.d.mts +70 -0
  10. package/lib/attribute-factory-mKOXoVnV.d.mts.map +1 -0
  11. package/lib/attribute-value-CjX9ZkLq.cjs +172 -0
  12. package/lib/attribute-value-CjX9ZkLq.cjs.map +1 -0
  13. package/lib/attribute-value-DMEA255r.mjs +119 -0
  14. package/lib/attribute-value-DMEA255r.mjs.map +1 -0
  15. package/lib/basic-execution-context-CeGuhMOz.mjs +246 -0
  16. package/lib/basic-execution-context-CeGuhMOz.mjs.map +1 -0
  17. package/lib/basic-execution-context-DSk5MNtc.cjs +281 -0
  18. package/lib/basic-execution-context-DSk5MNtc.cjs.map +1 -0
  19. package/lib/execution-context-BPD3FeFb.d.mts +71 -0
  20. package/lib/execution-context-BPD3FeFb.d.mts.map +1 -0
  21. package/lib/execution-context-BTJdHhbf.d.cts +71 -0
  22. package/lib/execution-context-BTJdHhbf.d.cts.map +1 -0
  23. package/lib/execution-context-token-BXUKB2hf.cjs +112 -0
  24. package/lib/execution-context-token-BXUKB2hf.cjs.map +1 -0
  25. package/lib/execution-context-token-CrXmTvk2.mjs +77 -0
  26. package/lib/execution-context-token-CrXmTvk2.mjs.map +1 -0
  27. package/lib/index.cjs +1687 -4424
  28. package/lib/index.cjs.map +1 -1
  29. package/lib/index.d.cts +838 -3
  30. package/lib/index.d.cts.map +1 -0
  31. package/lib/index.d.mts +838 -3
  32. package/lib/index.d.mts.map +1 -0
  33. package/lib/index.mjs +1573 -4219
  34. package/lib/index.mjs.map +1 -1
  35. package/lib/legacy-compat/index.cjs +72 -371
  36. package/lib/legacy-compat/index.cjs.map +1 -1
  37. package/lib/legacy-compat/index.d.cts +42 -382
  38. package/lib/legacy-compat/index.d.cts.map +1 -1
  39. package/lib/legacy-compat/index.d.mts +41 -382
  40. package/lib/legacy-compat/index.d.mts.map +1 -1
  41. package/lib/legacy-compat/index.mjs +51 -345
  42. package/lib/legacy-compat/index.mjs.map +1 -1
  43. package/lib/module-Bha7GER_.mjs +135 -0
  44. package/lib/module-Bha7GER_.mjs.map +1 -0
  45. package/lib/module-DZewk_2r.cjs +164 -0
  46. package/lib/module-DZewk_2r.cjs.map +1 -0
  47. package/lib/module-graph-DCP9MHG0.cjs +147 -0
  48. package/lib/module-graph-DCP9MHG0.cjs.map +1 -0
  49. package/lib/module-graph-xvVEykFM.mjs +142 -0
  50. package/lib/module-graph-xvVEykFM.mjs.map +1 -0
  51. package/lib/module-metadata-C8S92seK.cjs +95 -0
  52. package/lib/module-metadata-C8S92seK.cjs.map +1 -0
  53. package/lib/module-metadata-k3RTt20x.mjs +66 -0
  54. package/lib/module-metadata-k3RTt20x.mjs.map +1 -0
  55. package/lib/module-options-Dmse0gii.d.mts +51 -0
  56. package/lib/module-options-Dmse0gii.d.mts.map +1 -0
  57. package/lib/module-options-IuDO-p8u.d.cts +51 -0
  58. package/lib/module-options-IuDO-p8u.d.cts.map +1 -0
  59. package/lib/testing/index.cjs +96 -414
  60. package/lib/testing/index.cjs.map +1 -1
  61. package/lib/testing/index.d.cts +25 -385
  62. package/lib/testing/index.d.cts.map +1 -1
  63. package/lib/testing/index.d.mts +24 -385
  64. package/lib/testing/index.d.mts.map +1 -1
  65. package/lib/testing/index.mjs +94 -408
  66. package/lib/testing/index.mjs.map +1 -1
  67. package/lib/transport.cjs +25 -0
  68. package/lib/transport.d.cts +190 -0
  69. package/lib/transport.d.cts.map +1 -0
  70. package/lib/transport.d.mts +190 -0
  71. package/lib/transport.d.mts.map +1 -0
  72. package/lib/transport.mjs +7 -0
  73. package/package.json +37 -17
  74. package/docs/README.md +0 -316
  75. package/docs/adapters.md +0 -308
  76. package/docs/application-setup.md +0 -524
  77. package/docs/attributes.md +0 -689
  78. package/docs/controllers.md +0 -373
  79. package/docs/endpoints.md +0 -444
  80. package/docs/exceptions.md +0 -352
  81. package/docs/guards.md +0 -550
  82. package/docs/legacy-compat.md +0 -356
  83. package/docs/modules.md +0 -377
  84. package/docs/quick-start.md +0 -290
  85. package/docs/services.md +0 -427
  86. package/docs/testing.md +0 -827
  87. package/lib/index-3LcTrbxz.d.mts +0 -3804
  88. package/lib/index-3LcTrbxz.d.mts.map +0 -1
  89. package/lib/index-B14SekVb.d.cts +0 -3804
  90. package/lib/index-B14SekVb.d.cts.map +0 -1
  91. package/lib/navios.factory-CUrO_p6K.cjs +0 -4538
  92. package/lib/navios.factory-CUrO_p6K.cjs.map +0 -1
  93. package/lib/navios.factory-DjUOOVVL.mjs +0 -4151
  94. package/lib/navios.factory-DjUOOVVL.mjs.map +0 -1
  95. package/lib/tokens-BEuBMGGX.cjs +0 -193
  96. package/lib/tokens-BEuBMGGX.cjs.map +0 -1
  97. package/lib/tokens-COyNGV1I.mjs +0 -97
  98. package/lib/tokens-COyNGV1I.mjs.map +0 -1
  99. package/lib/use-guards.decorator-DLmRl2CV.mjs +0 -336
  100. package/lib/use-guards.decorator-DLmRl2CV.mjs.map +0 -1
  101. package/lib/use-guards.decorator-DhumFTk3.cjs +0 -444
  102. package/lib/use-guards.decorator-DhumFTk3.cjs.map +0 -1
  103. package/project.json +0 -66
  104. package/src/__tests__/attribute.factory.spec.mts +0 -300
  105. package/src/__tests__/config.service.spec.mts +0 -43
  106. package/src/__tests__/console-logger.service.spec.mts +0 -312
  107. package/src/__tests__/controller-resolver.spec.mts +0 -229
  108. package/src/__tests__/controller.spec.mts +0 -58
  109. package/src/__tests__/guard-runner.service.spec.mts +0 -399
  110. package/src/__tests__/logger.service.spec.mts +0 -147
  111. package/src/__tests__/responders.spec.mts +0 -340
  112. package/src/__tests__/testing-module.spec.mts +0 -459
  113. package/src/__tests__/unit-testing-module.spec.mts +0 -424
  114. package/src/attribute.factory.mts +0 -325
  115. package/src/config/config-service.interface.mts +0 -47
  116. package/src/config/config.provider.mts +0 -69
  117. package/src/config/config.service.mts +0 -176
  118. package/src/config/index.mts +0 -5
  119. package/src/config/types.mts +0 -58
  120. package/src/config/utils/helpers.mts +0 -23
  121. package/src/config/utils/index.mts +0 -1
  122. package/src/decorators/controller.decorator.mts +0 -81
  123. package/src/decorators/endpoint.decorator.mts +0 -181
  124. package/src/decorators/header.decorator.mts +0 -38
  125. package/src/decorators/http-code.decorator.mts +0 -38
  126. package/src/decorators/index.mts +0 -8
  127. package/src/decorators/module.decorator.mts +0 -106
  128. package/src/decorators/multipart.decorator.mts +0 -169
  129. package/src/decorators/stream.decorator.mts +0 -155
  130. package/src/decorators/use-guards.decorator.mts +0 -73
  131. package/src/exceptions/bad-request.exception.mts +0 -28
  132. package/src/exceptions/conflict.exception.mts +0 -31
  133. package/src/exceptions/forbidden.exception.mts +0 -30
  134. package/src/exceptions/http.exception.mts +0 -33
  135. package/src/exceptions/index.mts +0 -7
  136. package/src/exceptions/internal-server-error.exception.mts +0 -33
  137. package/src/exceptions/not-found.exception.mts +0 -33
  138. package/src/exceptions/unauthorized.exception.mts +0 -30
  139. package/src/factories/adapter.factory.mts +0 -20
  140. package/src/factories/endpoint-adapter.factory.mts +0 -21
  141. package/src/factories/http-adapter.factory.mts +0 -20
  142. package/src/factories/index.mts +0 -8
  143. package/src/factories/multipart-adapter.factory.mts +0 -21
  144. package/src/factories/reply.factory.mts +0 -21
  145. package/src/factories/request.factory.mts +0 -21
  146. package/src/factories/stream-adapter.factory.mts +0 -20
  147. package/src/factories/xml-stream-adapter.factory.mts +0 -20
  148. package/src/index.mts +0 -16
  149. package/src/interfaces/abstract-adapter.interface.mts +0 -32
  150. package/src/interfaces/abstract-execution-context.inteface.mts +0 -48
  151. package/src/interfaces/abstract-http-adapter.interface.mts +0 -79
  152. package/src/interfaces/abstract-http-cors-options.interface.mts +0 -59
  153. package/src/interfaces/abstract-http-handler-adapter.interface.mts +0 -127
  154. package/src/interfaces/abstract-http-listen-options.interface.mts +0 -4
  155. package/src/interfaces/adapter-environment.interface.mts +0 -74
  156. package/src/interfaces/can-activate.mts +0 -38
  157. package/src/interfaces/http-header.mts +0 -18
  158. package/src/interfaces/index.mts +0 -14
  159. package/src/interfaces/navios-module.mts +0 -28
  160. package/src/interfaces/plugin-context.mts +0 -104
  161. package/src/interfaces/plugin-stage.mts +0 -62
  162. package/src/interfaces/plugin.interface.mts +0 -119
  163. package/src/interfaces/staged-plugin.interface.mts +0 -209
  164. package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +0 -521
  165. package/src/legacy-compat/__type-tests__/tsconfig.json +0 -15
  166. package/src/legacy-compat/attribute.factory.mts +0 -351
  167. package/src/legacy-compat/decorators/controller.decorator.mts +0 -31
  168. package/src/legacy-compat/decorators/endpoint.decorator.mts +0 -73
  169. package/src/legacy-compat/decorators/header.decorator.mts +0 -43
  170. package/src/legacy-compat/decorators/http-code.decorator.mts +0 -39
  171. package/src/legacy-compat/decorators/index.mts +0 -11
  172. package/src/legacy-compat/decorators/module.decorator.mts +0 -37
  173. package/src/legacy-compat/decorators/multipart.decorator.mts +0 -72
  174. package/src/legacy-compat/decorators/stream.decorator.mts +0 -89
  175. package/src/legacy-compat/decorators/use-guards.decorator.mts +0 -80
  176. package/src/legacy-compat/index.mts +0 -57
  177. package/src/logger/README.md +0 -3
  178. package/src/logger/console-logger.service.mts +0 -714
  179. package/src/logger/index.mts +0 -6
  180. package/src/logger/log-levels.mts +0 -21
  181. package/src/logger/logger-service.interface.mts +0 -42
  182. package/src/logger/logger.service.mts +0 -113
  183. package/src/logger/logger.tokens.mts +0 -44
  184. package/src/logger/utils/cli-colors.util.mts +0 -17
  185. package/src/logger/utils/filter-log-levelts.util.mts +0 -29
  186. package/src/logger/utils/index.mts +0 -5
  187. package/src/logger/utils/is-log-level-enabled.mts +0 -33
  188. package/src/logger/utils/is-log-level.util.mts +0 -10
  189. package/src/logger/utils/shared.utils.mts +0 -51
  190. package/src/metadata/controller.metadata.mts +0 -79
  191. package/src/metadata/handler.metadata.mts +0 -80
  192. package/src/metadata/index.mts +0 -4
  193. package/src/metadata/module.metadata.mts +0 -116
  194. package/src/metadata/navios-managed.metadata.mts +0 -42
  195. package/src/navios.application.mts +0 -678
  196. package/src/navios.environment.mts +0 -42
  197. package/src/navios.factory.mts +0 -158
  198. package/src/responders/enums/framework-error.enum.mts +0 -38
  199. package/src/responders/enums/index.mts +0 -1
  200. package/src/responders/index.mts +0 -4
  201. package/src/responders/interfaces/error-responder.interface.mts +0 -42
  202. package/src/responders/interfaces/error-response.interface.mts +0 -22
  203. package/src/responders/interfaces/index.mts +0 -3
  204. package/src/responders/interfaces/problem-details.interface.mts +0 -34
  205. package/src/responders/services/error-response-producer.service.mts +0 -143
  206. package/src/responders/services/forbidden-responder.service.mts +0 -77
  207. package/src/responders/services/index.mts +0 -5
  208. package/src/responders/services/internal-server-error-responder.service.mts +0 -57
  209. package/src/responders/services/not-found-responder.service.mts +0 -76
  210. package/src/responders/services/validation-error-responder.service.mts +0 -78
  211. package/src/responders/tokens/index.mts +0 -1
  212. package/src/responders/tokens/responder.tokens.mts +0 -84
  213. package/src/services/abstract-handler-adapter.service.mts +0 -366
  214. package/src/services/guard-runner.service.mts +0 -152
  215. package/src/services/index.mts +0 -4
  216. package/src/services/instance-resolver.service.mts +0 -177
  217. package/src/services/module-loader.service.mts +0 -255
  218. package/src/stores/index.mts +0 -1
  219. package/src/stores/request-id.store.mts +0 -85
  220. package/src/testing/index.mts +0 -3
  221. package/src/testing/testing-module.mts +0 -393
  222. package/src/testing/unit-testing-module.mts +0 -298
  223. package/src/tokens/adapter.token.mts +0 -6
  224. package/src/tokens/endpoint-adapter.token.mts +0 -8
  225. package/src/tokens/execution-context.token.mts +0 -9
  226. package/src/tokens/http-adapter.token.mts +0 -8
  227. package/src/tokens/index.mts +0 -10
  228. package/src/tokens/multipart-adapter.token.mts +0 -8
  229. package/src/tokens/navios-options.token.mts +0 -6
  230. package/src/tokens/reply.token.mts +0 -3
  231. package/src/tokens/request.token.mts +0 -3
  232. package/src/tokens/stream-adapter.token.mts +0 -8
  233. package/src/tokens/xml-stream-adapter.token.mts +0 -8
  234. package/src/utils/adapter-supports.util.mts +0 -47
  235. package/src/utils/define-plugin.mts +0 -251
  236. package/src/utils/index.mts +0 -2
  237. package/tsconfig.json +0 -21
  238. package/tsconfig.lib.json +0 -8
  239. package/tsconfig.spec.json +0 -15
  240. package/tsdown.config.mts +0 -35
  241. package/vitest.config.mts +0 -15
package/docs/endpoints.md DELETED
@@ -1,444 +0,0 @@
1
- # Endpoints
2
-
3
- Endpoints in Navios are HTTP routes defined as methods in controllers. They are decorated with the `@Endpoint()` decorator and specify how to handle incoming HTTP requests.
4
-
5
- ## What is an Endpoint?
6
-
7
- An endpoint is a controller method that handles a specific HTTP request. It defines:
8
-
9
- - HTTP method (GET, POST, PUT, DELETE, etc.)
10
- - URL pattern with optional parameters
11
- - Request/response schemas for validation
12
- - Business logic to process the request
13
-
14
- ## Creating Endpoints
15
-
16
- ### Basic Endpoint
17
-
18
- First, define your endpoint using `@navios/builder`:
19
-
20
- ```typescript
21
- // api/users.ts
22
- import { builder } from '@navios/builder'
23
-
24
- import { z } from 'zod'
25
-
26
- const UserSchema = z.object({
27
- id: z.string(),
28
- name: z.string(),
29
- email: z.string().email(),
30
- })
31
-
32
- export const api = builder()
33
-
34
- export const getUsersEndpoint = api.declareEndpoint({
35
- method: 'GET',
36
- url: '/users',
37
- responseSchema: z.array(UserSchema),
38
- })
39
- ```
40
-
41
- Then use it in your controller:
42
-
43
- ```typescript
44
- import type { EndpointParams } from '@navios/core'
45
-
46
- import { Controller, Endpoint } from '@navios/core'
47
-
48
- import { getUsersEndpoint } from '../api/users.js'
49
-
50
- // controllers/user.controller.ts
51
-
52
- @Controller()
53
- export class UserController {
54
- @Endpoint(getUsersEndpoint)
55
- async getUsers(params: EndpointParams<typeof getUsersEndpoint>) {
56
- return [
57
- { id: '1', name: 'John', email: 'john@example.com' },
58
- { id: '2', name: 'Jane', email: 'jane@example.com' },
59
- ]
60
- }
61
- }
62
- ```
63
-
64
- > **Note**: Always use `@navios/builder` to declare endpoints instead of passing configuration objects directly to `@Endpoint()`. This ensures type safety and better maintainability.
65
-
66
- ### Endpoint with Path Parameters
67
-
68
- Define the endpoint with path parameters:
69
-
70
- ```typescript
71
- // api/users.ts
72
- export const getUserByIdEndpoint = api.declareEndpoint({
73
- method: 'GET',
74
- url: '/users/$id',
75
- responseSchema: UserSchema,
76
- })
77
- ```
78
-
79
- Use it in your controller:
80
-
81
- ```typescript
82
- @Controller()
83
- export class UserController {
84
- @Endpoint(getUserByIdEndpoint)
85
- async getUserById(params: EndpointParams<typeof getUserByIdEndpoint>) {
86
- // params.id is automatically extracted from the URL
87
- return { id: params.id, name: 'John', email: 'john@example.com' }
88
- }
89
- }
90
- ```
91
-
92
- ### Endpoint with Query Parameters
93
-
94
- Define the endpoint with query parameters:
95
-
96
- ```typescript
97
- // api/users.ts
98
- const QuerySchema = z.object({
99
- page: z.coerce.number().min(1).default(1),
100
- limit: z.coerce.number().min(1).max(100).default(10),
101
- search: z.string().optional(),
102
- })
103
-
104
- export const getUsersWithQueryEndpoint = api.declareEndpoint({
105
- method: 'GET',
106
- url: '/users',
107
- querySchema: QuerySchema,
108
- responseSchema: z.array(UserSchema),
109
- })
110
- ```
111
-
112
- Use it in your controller:
113
-
114
- ```typescript
115
- @Controller()
116
- export class UserController {
117
- @Endpoint(getUsersWithQueryEndpoint)
118
- async getUsers(params: EndpointParams<typeof getUsersWithQueryEndpoint>) {
119
- // query parameters are validated and typed
120
- const { page, limit, search } = params.query
121
- return this.userService.findMany({ page, limit, search })
122
- }
123
- }
124
- ```
125
-
126
- ### Endpoint with Request Body
127
-
128
- Define the endpoint with request body:
129
-
130
- ```typescript
131
- // api/users.ts
132
- const CreateUserSchema = z.object({
133
- name: z.string().min(1),
134
- email: z.string().email(),
135
- age: z.number().min(0).max(120),
136
- })
137
-
138
- export const createUserEndpoint = api.declareEndpoint({
139
- method: 'POST',
140
- url: '/users',
141
- requestSchema: CreateUserSchema,
142
- responseSchema: UserSchema,
143
- })
144
- ```
145
-
146
- Use it in your controller:
147
-
148
- ```typescript
149
- @Controller()
150
- export class UserController {
151
- @Endpoint(createUserEndpoint)
152
- async createUser(params: EndpointParams<typeof createUserEndpoint>) {
153
- // body is validated against CreateUserSchema
154
- return this.userService.create(params.data)
155
- }
156
- }
157
- ```
158
-
159
- ## Endpoint Configuration
160
-
161
- ### Using `@navios/builder` (Recommended)
162
-
163
- The `@Endpoint()` decorator should receive an endpoint definition created with `@navios/builder`. This approach provides better type safety and maintainability:
164
-
165
- ```typescript
166
- import { builder } from '@navios/builder'
167
-
168
- import { z } from 'zod'
169
-
170
- export const api = builder()
171
-
172
- export const myEndpoint = api.declareEndpoint({
173
- method: 'GET', // 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'
174
- url: '/users/$id', // URL pattern with url parameters
175
- responseSchema: z.object({
176
- id: z.string(),
177
- name: z.string(),
178
- }),
179
- requestSchema: z.object({
180
- // Optional: for POST/PUT/PATCH
181
- name: z.string(),
182
- email: z.string().email(),
183
- }),
184
- querySchema: z.object({
185
- // Optional: for query parameters
186
- page: z.coerce.number().default(1),
187
- search: z.string().optional(),
188
- }),
189
- })
190
- ```
191
-
192
- ### Endpoint Definition Properties
193
-
194
- When using `api.declareEndpoint()`, you can specify:
195
-
196
- #### `method` (required)
197
-
198
- - **Type**: `HttpMethod` ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS')
199
- - **Description**: HTTP method for the endpoint
200
-
201
- #### `url` (required)
202
-
203
- - **Type**: `string`
204
- - **Description**: URL pattern with optional parameters (`:param`)
205
- - **Examples**:
206
- - `'/users'` - Simple path
207
- - `'/users/$id'` - With parameter
208
- - `'/users/$id/posts/$postId'` - Multiple parameters
209
-
210
- #### `responseSchema`
211
-
212
- - **Type**: `ZodType`
213
- - **Description**: Zod schema for response validation and type inference
214
-
215
- #### `requestSchema`
216
-
217
- - **Type**: `ZodType`
218
- - **Description**: Zod schema for request body validation (POST/PUT/PATCH)
219
-
220
- #### `querySchema`
221
-
222
- - **Type**: `ZodType`
223
- - **Description**: Zod schema for query parameter validation
224
-
225
- Use `@navios/builder` instead for better developer experience and type safety.
226
-
227
- ## Endpoint Parameters
228
-
229
- Endpoint methods receive a single parameter object with the following properties:
230
-
231
- ### `params`
232
-
233
- URL path parameters extracted from the route:
234
-
235
- ```typescript
236
- // URL: /users/$id
237
- params: EndpointParams<typeof getUserByIdEndpoint> = {
238
- params: { id: string },
239
- }
240
-
241
- // URL: /users/$userId/posts/$postId
242
- params: EndpointParams<typeof getPostEndpoint> = {
243
- params: { userId: string, postId: string },
244
- }
245
- ```
246
-
247
- ### `query`
248
-
249
- Query parameters from the URL:
250
-
251
- ```typescript
252
- // URL: /users?page=1&search=john
253
- params: EndpointParams<typeof getUsersEndpoint> = {
254
- query: { page: number, search: string },
255
- }
256
- ```
257
-
258
- ### `data`
259
-
260
- Request body for POST/PUT/PATCH requests:
261
-
262
- ```typescript
263
- // POST /users with JSON body
264
- params: EndpointParams<typeof createUserEndpoint> = {
265
- data: { name: string, email: string },
266
- }
267
- ```
268
-
269
- ## Type Safety
270
-
271
- Navios provides full type safety for endpoints through TypeScript and Zod:
272
-
273
- ```typescript
274
- const UserParamsSchema = z.object({
275
- id: z.string().uuid(),
276
- })
277
-
278
- const UpdateUserSchema = z.object({
279
- name: z.string().optional(),
280
- email: z.string().email().optional(),
281
- })
282
-
283
- @Controller()
284
- export class UserController {
285
- @Endpoint(updateUserEndpoint)
286
- async updateUser({
287
- params,
288
- body,
289
- }: {
290
- params: z.infer<typeof UserParamsSchema>
291
- body: z.infer<typeof UpdateUserSchema>
292
- }) {
293
- // params and body are fully typed
294
- return this.userService.update(params.id, body)
295
- }
296
- }
297
- ```
298
-
299
- ## HTTP Status Codes
300
-
301
- ### Default Status Codes
302
-
303
- Navios automatically sets appropriate status codes:
304
-
305
- - `GET` requests: `200 OK`
306
- - `POST` requests: `201 Created`
307
- - `PUT`/`PATCH` requests: `200 OK`
308
- - `DELETE` requests: `204 No Content`
309
-
310
- ### Custom Status Codes
311
-
312
- Use the `@HttpCode()` decorator to set custom status codes:
313
-
314
- ```typescript
315
- import { HttpCode } from '@navios/core'
316
-
317
- @Controller()
318
- export class UserController {
319
- @Endpoint(createUserEndpoint)
320
- @HttpCode(202) // Accepted
321
- async verifyUser({ body }: { body: { token: string } }) {
322
- await this.userService.verify(body.token)
323
- return { message: 'Verification started' }
324
- }
325
- }
326
- ```
327
-
328
- ## Headers
329
-
330
- ### Reading Headers
331
-
332
- Access request headers through the headers parameter:
333
-
334
- ```typescript
335
- @Controller()
336
- export class UserController {
337
- @Endpoint(getProfileEndpoint)
338
- async getProfile({ headers }: { headers: Record<string, string> }) {
339
- const authorization = headers.authorization
340
- const userId = this.authService.getUserIdFromToken(authorization)
341
- return this.userService.findById(userId)
342
- }
343
- }
344
- ```
345
-
346
- ### Setting Response Headers
347
-
348
- Use the `@Header()` decorator to set response headers:
349
-
350
- ```typescript
351
- import { Header } from '@navios/core'
352
-
353
- @Controller()
354
- export class UserController {
355
- @Endpoint(usersExportEndpoint)
356
- @Header('Content-Type', 'text/csv')
357
- @Header('Content-Disposition', 'attachment; filename="users.csv"')
358
- async exportUsers() {
359
- return this.userService.exportToCsv()
360
- }
361
- }
362
- ```
363
-
364
- ## Guards on Endpoints
365
-
366
- Apply guards to specific endpoints:
367
-
368
- ```typescript
369
- import { UseGuards } from '@navios/core'
370
-
371
- @Controller()
372
- export class UserController {
373
- @Endpoint(deleteUserEndpoint)
374
- @UseGuards([AuthGuard, AdminGuard])
375
- async deleteUser({ params }: { params: { id: string } }) {
376
- return this.userService.delete(params.id)
377
- }
378
- }
379
- ```
380
-
381
- ## File Uploads
382
-
383
- Handle file uploads with multipart support:
384
-
385
- ```typescript
386
- import { Multipart } from '@navios/core'
387
-
388
- @Controller()
389
- export class UserController {
390
- @Multipart(uploadAvatarEndpoint)
391
- async uploadAvatar({
392
- params,
393
- files,
394
- }: {
395
- params: { id: string }
396
- files: { avatar: File }
397
- }) {
398
- const avatarUrl = await this.storageService.upload(files.avatar)
399
- return this.userService.updateAvatar(params.id, avatarUrl)
400
- }
401
- }
402
- ```
403
-
404
- ## Streaming Responses
405
-
406
- Stream large responses:
407
-
408
- ```typescript
409
- import { Stream } from '@navios/core'
410
-
411
- @Controller()
412
- export class UserController {
413
- @Stream(exportUsersEndpoint)
414
- async exportUsers() {
415
- // Return a readable stream
416
- return this.userService.createExportStream()
417
- }
418
- }
419
- ```
420
-
421
- ## Error Handling
422
-
423
- Endpoints can throw HTTP exceptions:
424
-
425
- ```typescript
426
- import { BadRequestException, NotFoundException } from '@navios/core'
427
-
428
- @Controller()
429
- export class UserController {
430
- @Endpoint(getUserByIdEndpoint)
431
- async getUserById({ params }: { params: { id: string } }) {
432
- if (!params.id) {
433
- throw new BadRequestException('User ID is required')
434
- }
435
-
436
- const user = await this.userService.findById(params.id)
437
- if (!user) {
438
- throw new NotFoundException(`User with ID ${params.id} not found`)
439
- }
440
-
441
- return user
442
- }
443
- }
444
- ```