@navios/core 0.6.0 → 0.7.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 +115 -0
- package/README.md +18 -1
- package/docs/README.md +1 -0
- package/docs/legacy-compat.md +320 -0
- package/docs/testing.md +140 -17
- package/lib/index-DW9EPAE6.d.mts +2156 -0
- package/lib/index-DW9EPAE6.d.mts.map +1 -0
- package/lib/index-pHp-dIGt.d.cts +2156 -0
- package/lib/index-pHp-dIGt.d.cts.map +1 -0
- package/lib/index.cjs +157 -0
- package/lib/index.d.cts +3 -0
- package/lib/index.d.mts +3 -190
- package/lib/index.mjs +4 -1459
- package/lib/legacy-compat/index.cjs +315 -0
- package/lib/legacy-compat/index.cjs.map +1 -0
- package/lib/legacy-compat/index.d.cts +219 -0
- package/lib/legacy-compat/index.d.cts.map +1 -0
- package/lib/legacy-compat/index.d.mts +219 -0
- package/lib/legacy-compat/index.d.mts.map +1 -0
- package/lib/legacy-compat/index.mjs +308 -0
- package/lib/legacy-compat/index.mjs.map +1 -0
- package/lib/src-DyvCDuKO.mjs +5443 -0
- package/lib/src-DyvCDuKO.mjs.map +1 -0
- package/lib/src-QnxR5b7c.cjs +5800 -0
- package/lib/src-QnxR5b7c.cjs.map +1 -0
- package/lib/testing/index.cjs +106 -0
- package/lib/testing/index.cjs.map +1 -0
- package/lib/testing/index.d.cts +156 -0
- package/lib/testing/index.d.cts.map +1 -0
- package/lib/testing/index.d.mts +156 -0
- package/lib/testing/index.d.mts.map +1 -0
- package/lib/testing/index.mjs +100 -0
- package/lib/testing/index.mjs.map +1 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs +622 -0
- package/lib/use-guards.decorator-B6q_N0sf.cjs.map +1 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs +454 -0
- package/lib/use-guards.decorator-kZ3lNK8v.mjs.map +1 -0
- package/package.json +28 -8
- package/project.json +2 -2
- package/src/attribute.factory.mts +154 -0
- package/src/config/config-service.interface.mts +31 -0
- package/src/config/config.provider.mts +36 -0
- package/src/config/config.service.mts +94 -4
- package/src/decorators/controller.decorator.mts +28 -0
- package/src/decorators/endpoint.decorator.mts +76 -0
- package/src/decorators/header.decorator.mts +19 -0
- package/src/decorators/http-code.decorator.mts +20 -0
- package/src/decorators/module.decorator.mts +34 -0
- package/src/decorators/multipart.decorator.mts +41 -0
- package/src/decorators/stream.decorator.mts +33 -0
- package/src/decorators/use-guards.decorator.mts +29 -0
- package/src/exceptions/bad-request.exception.mts +21 -0
- package/src/exceptions/conflict.exception.mts +24 -0
- package/src/exceptions/forbidden.exception.mts +23 -0
- package/src/exceptions/http.exception.mts +26 -0
- package/src/exceptions/internal-server-error.exception.mts +26 -0
- package/src/exceptions/not-found.exception.mts +23 -0
- package/src/exceptions/unauthorized.exception.mts +23 -0
- package/src/index.mts +1 -0
- package/src/interfaces/abstract-execution-context.inteface.mts +35 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +52 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +2 -2
- package/src/interfaces/can-activate.mts +31 -0
- package/src/interfaces/index.mts +1 -0
- package/src/interfaces/navios-module.mts +25 -0
- package/src/interfaces/plugin.interface.mts +105 -0
- package/src/legacy-compat/__type-tests__/legacy-decorators.spec-d.mts +420 -0
- package/src/legacy-compat/__type-tests__/tsconfig.json +15 -0
- package/src/legacy-compat/context-compat.mts +93 -0
- package/src/legacy-compat/decorators/controller.decorator.mts +31 -0
- package/src/legacy-compat/decorators/endpoint.decorator.mts +99 -0
- package/src/legacy-compat/decorators/header.decorator.mts +42 -0
- package/src/legacy-compat/decorators/http-code.decorator.mts +38 -0
- package/src/legacy-compat/decorators/index.mts +9 -0
- package/src/legacy-compat/decorators/module.decorator.mts +37 -0
- package/src/legacy-compat/decorators/multipart.decorator.mts +93 -0
- package/src/legacy-compat/decorators/stream.decorator.mts +76 -0
- package/src/legacy-compat/decorators/use-guards.decorator.mts +80 -0
- package/src/legacy-compat/index.mts +40 -0
- package/src/logger/console-logger.service.mts +15 -2
- package/src/logger/log-levels.mts +9 -0
- package/src/logger/logger.service.mts +21 -0
- package/src/logger/logger.tokens.mts +23 -0
- package/src/navios.application.mts +228 -4
- package/src/navios.factory.mts +60 -1
- package/src/services/guard-runner.service.mts +12 -11
- package/src/services/module-loader.service.mts +118 -12
- package/src/stores/index.mts +1 -0
- package/src/stores/request-id.store.mts +43 -0
- package/src/testing/index.mts +2 -0
- package/src/testing/testing-module.mts +231 -0
- package/tsconfig.lib.json +1 -1
- package/tsconfig.spec.json +3 -0
- package/tsdown.config.mts +35 -0
- package/vitest.config.mts +6 -0
- package/lib/_tsup-dts-rollup.d.mts +0 -1365
- package/lib/_tsup-dts-rollup.d.ts +0 -1365
- package/lib/index.d.ts +0 -190
- package/lib/index.js +0 -1540
- package/lib/index.js.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/tsup.config.mts +0 -13
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
const require_src = require('./src-QnxR5b7c.cjs');
|
|
2
|
+
let _navios_di = require("@navios/di");
|
|
3
|
+
|
|
4
|
+
//#region src/metadata/handler.metadata.mts
|
|
5
|
+
const EndpointMetadataKey = Symbol("EndpointMetadataKey");
|
|
6
|
+
function getAllEndpointMetadata(context) {
|
|
7
|
+
if (context.metadata) {
|
|
8
|
+
const metadata = context.metadata[EndpointMetadataKey];
|
|
9
|
+
if (metadata) return metadata;
|
|
10
|
+
else {
|
|
11
|
+
context.metadata[EndpointMetadataKey] = /* @__PURE__ */ new Set();
|
|
12
|
+
return context.metadata[EndpointMetadataKey];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
throw new Error("[Navios] Wrong environment.");
|
|
16
|
+
}
|
|
17
|
+
function getEndpointMetadata(target, context) {
|
|
18
|
+
if (context.metadata) {
|
|
19
|
+
const metadata = getAllEndpointMetadata(context);
|
|
20
|
+
if (metadata) {
|
|
21
|
+
const endpointMetadata = Array.from(metadata).find((item) => item.classMethod === target.name);
|
|
22
|
+
if (endpointMetadata) return endpointMetadata;
|
|
23
|
+
else {
|
|
24
|
+
const newMetadata = {
|
|
25
|
+
classMethod: target.name,
|
|
26
|
+
url: "",
|
|
27
|
+
successStatusCode: 200,
|
|
28
|
+
adapterToken: null,
|
|
29
|
+
headers: {},
|
|
30
|
+
httpMethod: "GET",
|
|
31
|
+
config: null,
|
|
32
|
+
guards: /* @__PURE__ */ new Set(),
|
|
33
|
+
customAttributes: /* @__PURE__ */ new Map()
|
|
34
|
+
};
|
|
35
|
+
metadata.add(newMetadata);
|
|
36
|
+
return newMetadata;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
throw new Error("[Navios] Wrong environment.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/metadata/controller.metadata.mts
|
|
45
|
+
const ControllerMetadataKey = Symbol("ControllerMetadataKey");
|
|
46
|
+
function getControllerMetadata(target, context) {
|
|
47
|
+
if (context.metadata) {
|
|
48
|
+
const metadata = context.metadata[ControllerMetadataKey];
|
|
49
|
+
if (metadata) return metadata;
|
|
50
|
+
else {
|
|
51
|
+
const newMetadata = {
|
|
52
|
+
endpoints: getAllEndpointMetadata(context),
|
|
53
|
+
guards: /* @__PURE__ */ new Set(),
|
|
54
|
+
customAttributes: /* @__PURE__ */ new Map()
|
|
55
|
+
};
|
|
56
|
+
context.metadata[ControllerMetadataKey] = newMetadata;
|
|
57
|
+
target[ControllerMetadataKey] = newMetadata;
|
|
58
|
+
return newMetadata;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
throw new Error("[Navios] Wrong environment.");
|
|
62
|
+
}
|
|
63
|
+
function extractControllerMetadata(target) {
|
|
64
|
+
const metadata = target[ControllerMetadataKey];
|
|
65
|
+
if (!metadata) throw new Error("[Navios] Controller metadata not found. Make sure to use @Controller decorator.");
|
|
66
|
+
return metadata;
|
|
67
|
+
}
|
|
68
|
+
function hasControllerMetadata(target) {
|
|
69
|
+
return !!target[ControllerMetadataKey];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/metadata/module.metadata.mts
|
|
74
|
+
const ModuleMetadataKey = Symbol("ControllerMetadataKey");
|
|
75
|
+
function getModuleMetadata(target, context) {
|
|
76
|
+
if (context.metadata) {
|
|
77
|
+
const metadata = context.metadata[ModuleMetadataKey];
|
|
78
|
+
if (metadata) return metadata;
|
|
79
|
+
else {
|
|
80
|
+
const newMetadata = {
|
|
81
|
+
controllers: /* @__PURE__ */ new Set(),
|
|
82
|
+
imports: /* @__PURE__ */ new Set(),
|
|
83
|
+
guards: /* @__PURE__ */ new Set(),
|
|
84
|
+
customAttributes: /* @__PURE__ */ new Map()
|
|
85
|
+
};
|
|
86
|
+
context.metadata[ModuleMetadataKey] = newMetadata;
|
|
87
|
+
target[ModuleMetadataKey] = newMetadata;
|
|
88
|
+
return newMetadata;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
throw new Error("[Navios] Wrong environment.");
|
|
92
|
+
}
|
|
93
|
+
function extractModuleMetadata(target) {
|
|
94
|
+
const metadata = target[ModuleMetadataKey];
|
|
95
|
+
if (!metadata) throw new Error(`[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`);
|
|
96
|
+
return metadata;
|
|
97
|
+
}
|
|
98
|
+
function hasModuleMetadata(target) {
|
|
99
|
+
return !!target[ModuleMetadataKey];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/decorators/controller.decorator.mts
|
|
104
|
+
/**
|
|
105
|
+
* Decorator that marks a class as a Navios controller.
|
|
106
|
+
*
|
|
107
|
+
* Controllers handle HTTP requests and define endpoints.
|
|
108
|
+
* They are request-scoped by default, meaning a new instance is created for each request.
|
|
109
|
+
*
|
|
110
|
+
* @param options - Controller configuration options
|
|
111
|
+
* @returns A class decorator
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* @Controller({ guards: [AuthGuard] })
|
|
116
|
+
* export class UserController {
|
|
117
|
+
* @Endpoint(getUserEndpoint)
|
|
118
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
119
|
+
* // Handle request
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/ function Controller({ guards } = {}) {
|
|
124
|
+
return function(target, context) {
|
|
125
|
+
if (context.kind !== "class") throw new Error("[Navios] @Controller decorator can only be used on classes.");
|
|
126
|
+
const token = _navios_di.InjectionToken.create(target);
|
|
127
|
+
if (context.metadata) {
|
|
128
|
+
const controllerMetadata = getControllerMetadata(target, context);
|
|
129
|
+
if (guards) for (const guard of Array.from(guards).reverse()) controllerMetadata.guards.add(guard);
|
|
130
|
+
}
|
|
131
|
+
return (0, _navios_di.Injectable)({
|
|
132
|
+
token,
|
|
133
|
+
scope: _navios_di.InjectableScope.Request
|
|
134
|
+
})(target, context);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/tokens/endpoint-adapter.token.mts
|
|
140
|
+
const EndpointAdapterToken = _navios_di.InjectionToken.create("EndpointAdapterToken");
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/tokens/execution-context.token.mts
|
|
144
|
+
const ExecutionContextInjectionToken = "ExecutionContextInjectionToken";
|
|
145
|
+
const ExecutionContext = _navios_di.InjectionToken.create(ExecutionContextInjectionToken);
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/tokens/http-adapter.token.mts
|
|
149
|
+
const HttpAdapterToken = _navios_di.InjectionToken.create("HttpAdapterToken");
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/tokens/multipart-adapter.token.mts
|
|
153
|
+
const MultipartAdapterToken = _navios_di.InjectionToken.create("MultipartAdapterToken");
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/tokens/reply.token.mts
|
|
157
|
+
const Reply = _navios_di.InjectionToken.create("ReplyToken");
|
|
158
|
+
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/tokens/request.token.mts
|
|
161
|
+
const Request = _navios_di.InjectionToken.create("RequestToken");
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/tokens/stream-adapter.token.mts
|
|
165
|
+
const StreamAdapterToken = _navios_di.InjectionToken.create("StreamAdapterToken");
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/tokens/xml-stream-adapter.token.mts
|
|
169
|
+
const XmlStreamAdapterToken = _navios_di.InjectionToken.create("XmlStreamAdapterToken");
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/decorators/endpoint.decorator.mts
|
|
173
|
+
/**
|
|
174
|
+
* Decorator that marks a method as an HTTP endpoint.
|
|
175
|
+
*
|
|
176
|
+
* The endpoint must be defined using @navios/builder's `declareEndpoint` method.
|
|
177
|
+
* This ensures type safety and consistency between client and server.
|
|
178
|
+
*
|
|
179
|
+
* @param endpoint - The endpoint declaration from @navios/builder
|
|
180
|
+
* @returns A method decorator
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* import { builder } from '@navios/builder'
|
|
185
|
+
*
|
|
186
|
+
* const api = builder()
|
|
187
|
+
* const getUserEndpoint = api.declareEndpoint({
|
|
188
|
+
* method: 'get',
|
|
189
|
+
* url: '/users/$userId',
|
|
190
|
+
* responseSchema: z.object({ id: z.string(), name: z.string() }),
|
|
191
|
+
* })
|
|
192
|
+
*
|
|
193
|
+
* @Controller()
|
|
194
|
+
* export class UserController {
|
|
195
|
+
* @Endpoint(getUserEndpoint)
|
|
196
|
+
* async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
197
|
+
* const { userId } = request.urlParams
|
|
198
|
+
* return { id: userId, name: 'John' }
|
|
199
|
+
* }
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/ function Endpoint(endpoint) {
|
|
203
|
+
return (target, context) => {
|
|
204
|
+
if (context.kind !== "method") throw new Error("[Navios] Endpoint decorator can only be used on methods.");
|
|
205
|
+
const config = endpoint.config;
|
|
206
|
+
if (context.metadata) {
|
|
207
|
+
let endpointMetadata = getEndpointMetadata(target, context);
|
|
208
|
+
if (endpointMetadata.config && endpointMetadata.config.url) throw new Error(`[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`);
|
|
209
|
+
endpointMetadata.config = config;
|
|
210
|
+
endpointMetadata.adapterToken = EndpointAdapterToken;
|
|
211
|
+
endpointMetadata.classMethod = target.name;
|
|
212
|
+
endpointMetadata.httpMethod = config.method;
|
|
213
|
+
endpointMetadata.url = config.url;
|
|
214
|
+
}
|
|
215
|
+
return target;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/decorators/header.decorator.mts
|
|
221
|
+
/**
|
|
222
|
+
* Decorator that sets a custom HTTP response header for an endpoint.
|
|
223
|
+
*
|
|
224
|
+
* @param name - The header name (e.g., 'Content-Type', 'Cache-Control')
|
|
225
|
+
* @param value - The header value (string, number, or array of strings)
|
|
226
|
+
* @returns A method decorator
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* @Controller()
|
|
231
|
+
* export class UserController {
|
|
232
|
+
* @Endpoint(getUserEndpoint)
|
|
233
|
+
* @Header('Cache-Control', 'max-age=3600')
|
|
234
|
+
* async getUser() {
|
|
235
|
+
* return { id: '1', name: 'John' }
|
|
236
|
+
* }
|
|
237
|
+
* }
|
|
238
|
+
* ```
|
|
239
|
+
*/ function Header(name, value) {
|
|
240
|
+
return (target, context) => {
|
|
241
|
+
if (context.kind !== "method") throw new Error("[Navios] Header decorator can only be used on methods.");
|
|
242
|
+
const metadata = getEndpointMetadata(target, context);
|
|
243
|
+
metadata.headers[name] = value;
|
|
244
|
+
return target;
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region src/decorators/http-code.decorator.mts
|
|
250
|
+
/**
|
|
251
|
+
* Decorator that sets a custom HTTP status code for successful responses.
|
|
252
|
+
*
|
|
253
|
+
* By default, endpoints return 200 OK. Use this decorator to return a different status code.
|
|
254
|
+
*
|
|
255
|
+
* @param code - The HTTP status code to return (e.g., 201, 204, 202)
|
|
256
|
+
* @returns A method decorator
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```typescript
|
|
260
|
+
* @Controller()
|
|
261
|
+
* export class UserController {
|
|
262
|
+
* @Endpoint(createUserEndpoint)
|
|
263
|
+
* @HttpCode(201)
|
|
264
|
+
* async createUser() {
|
|
265
|
+
* return { id: '1', name: 'John' }
|
|
266
|
+
* }
|
|
267
|
+
* }
|
|
268
|
+
* ```
|
|
269
|
+
*/ function HttpCode(code) {
|
|
270
|
+
return (target, context) => {
|
|
271
|
+
if (context.kind !== "method") throw new Error("[Navios] HttpCode decorator can only be used on methods.");
|
|
272
|
+
const metadata = getEndpointMetadata(target, context);
|
|
273
|
+
metadata.successStatusCode = code;
|
|
274
|
+
return target;
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/decorators/module.decorator.mts
|
|
280
|
+
/**
|
|
281
|
+
* Decorator that marks a class as a Navios module.
|
|
282
|
+
*
|
|
283
|
+
* Modules are the basic building blocks of a Navios application.
|
|
284
|
+
* They organize controllers, services, and other modules into logical units.
|
|
285
|
+
*
|
|
286
|
+
* @param options - Module configuration options
|
|
287
|
+
* @returns A class decorator
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* @Module({
|
|
292
|
+
* controllers: [UserController, AuthController],
|
|
293
|
+
* imports: [DatabaseModule],
|
|
294
|
+
* guards: [AuthGuard],
|
|
295
|
+
* })
|
|
296
|
+
* export class AppModule {}
|
|
297
|
+
* ```
|
|
298
|
+
*/ function Module({ controllers = [], imports = [], guards = [] } = {
|
|
299
|
+
controllers: [],
|
|
300
|
+
imports: [],
|
|
301
|
+
guards: []
|
|
302
|
+
}) {
|
|
303
|
+
return (target, context) => {
|
|
304
|
+
if (context.kind !== "class") throw new Error("[Navios] @Module decorator can only be used on classes.");
|
|
305
|
+
const token = _navios_di.InjectionToken.create(target);
|
|
306
|
+
const moduleMetadata = getModuleMetadata(target, context);
|
|
307
|
+
for (const controller of controllers) moduleMetadata.controllers.add(controller);
|
|
308
|
+
for (const importedModule of imports) moduleMetadata.imports.add(importedModule);
|
|
309
|
+
for (const guard of Array.from(guards).reverse()) moduleMetadata.guards.add(guard);
|
|
310
|
+
return (0, _navios_di.Injectable)({
|
|
311
|
+
token,
|
|
312
|
+
scope: _navios_di.InjectableScope.Singleton
|
|
313
|
+
})(target, context);
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
//#endregion
|
|
318
|
+
//#region src/decorators/multipart.decorator.mts
|
|
319
|
+
/**
|
|
320
|
+
* Decorator that marks a method as a multipart/form-data endpoint.
|
|
321
|
+
*
|
|
322
|
+
* Use this decorator for endpoints that handle file uploads or form data.
|
|
323
|
+
* The endpoint must be defined using @navios/builder's `declareMultipart` method.
|
|
324
|
+
*
|
|
325
|
+
* @param endpoint - The multipart endpoint declaration from @navios/builder
|
|
326
|
+
* @returns A method decorator
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```typescript
|
|
330
|
+
* const uploadFileEndpoint = api.declareMultipart({
|
|
331
|
+
* method: 'post',
|
|
332
|
+
* url: '/upload',
|
|
333
|
+
* requestSchema: z.object({ file: z.instanceof(File) }),
|
|
334
|
+
* responseSchema: z.object({ url: z.string() }),
|
|
335
|
+
* })
|
|
336
|
+
*
|
|
337
|
+
* @Controller()
|
|
338
|
+
* export class FileController {
|
|
339
|
+
* @Multipart(uploadFileEndpoint)
|
|
340
|
+
* async uploadFile(request: MultipartParams<typeof uploadFileEndpoint>) {
|
|
341
|
+
* const { file } = request.data
|
|
342
|
+
* // Handle file upload
|
|
343
|
+
* return { url: 'https://example.com/file.jpg' }
|
|
344
|
+
* }
|
|
345
|
+
* }
|
|
346
|
+
* ```
|
|
347
|
+
*/ function Multipart(endpoint) {
|
|
348
|
+
return (target, context) => {
|
|
349
|
+
if (typeof target !== "function") throw new Error("[Navios] Endpoint decorator can only be used on functions.");
|
|
350
|
+
if (context.kind !== "method") throw new Error("[Navios] Endpoint decorator can only be used on methods.");
|
|
351
|
+
const config = endpoint.config;
|
|
352
|
+
if (context.metadata) {
|
|
353
|
+
let endpointMetadata = getEndpointMetadata(target, context);
|
|
354
|
+
if (endpointMetadata.config && endpointMetadata.config.url) throw new Error(`[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`);
|
|
355
|
+
endpointMetadata.config = config;
|
|
356
|
+
endpointMetadata.adapterToken = MultipartAdapterToken;
|
|
357
|
+
endpointMetadata.classMethod = target.name;
|
|
358
|
+
endpointMetadata.httpMethod = config.method;
|
|
359
|
+
endpointMetadata.url = config.url;
|
|
360
|
+
}
|
|
361
|
+
return target;
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region src/decorators/stream.decorator.mts
|
|
367
|
+
/**
|
|
368
|
+
* Decorator that marks a method as a streaming endpoint.
|
|
369
|
+
*
|
|
370
|
+
* Use this decorator for endpoints that stream data (e.g., file downloads, SSE).
|
|
371
|
+
* The endpoint must be defined using @navios/builder's `declareStream` method.
|
|
372
|
+
*
|
|
373
|
+
* @param endpoint - The stream endpoint declaration from @navios/builder
|
|
374
|
+
* @returns A method decorator
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```typescript
|
|
378
|
+
* const downloadFileEndpoint = api.declareStream({
|
|
379
|
+
* method: 'get',
|
|
380
|
+
* url: '/files/$fileId',
|
|
381
|
+
* })
|
|
382
|
+
*
|
|
383
|
+
* @Controller()
|
|
384
|
+
* export class FileController {
|
|
385
|
+
* @Stream(downloadFileEndpoint)
|
|
386
|
+
* async downloadFile(request: StreamParams<typeof downloadFileEndpoint>, reply: any) {
|
|
387
|
+
* const { fileId } = request.urlParams
|
|
388
|
+
* // Stream file data to reply
|
|
389
|
+
* }
|
|
390
|
+
* }
|
|
391
|
+
* ```
|
|
392
|
+
*/ function Stream(endpoint) {
|
|
393
|
+
return (target, context) => {
|
|
394
|
+
if (typeof target !== "function") throw new Error("[Navios] Endpoint decorator can only be used on functions.");
|
|
395
|
+
if (context.kind !== "method") throw new Error("[Navios] Endpoint decorator can only be used on methods.");
|
|
396
|
+
const config = endpoint.config;
|
|
397
|
+
if (context.metadata) {
|
|
398
|
+
let endpointMetadata = getEndpointMetadata(target, context);
|
|
399
|
+
if (endpointMetadata.config && endpointMetadata.config.url) throw new Error(`[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`);
|
|
400
|
+
endpointMetadata.config = config;
|
|
401
|
+
endpointMetadata.adapterToken = StreamAdapterToken;
|
|
402
|
+
endpointMetadata.classMethod = target.name;
|
|
403
|
+
endpointMetadata.httpMethod = config.method;
|
|
404
|
+
endpointMetadata.url = config.url;
|
|
405
|
+
}
|
|
406
|
+
return target;
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
//#endregion
|
|
411
|
+
//#region src/decorators/use-guards.decorator.mts
|
|
412
|
+
/**
|
|
413
|
+
* Decorator that applies guards to a controller or endpoint.
|
|
414
|
+
*
|
|
415
|
+
* Guards are used for authentication, authorization, and request validation.
|
|
416
|
+
* They implement the `CanActivate` interface and are executed before the endpoint handler.
|
|
417
|
+
* Guards can be applied at the module, controller, or endpoint level.
|
|
418
|
+
*
|
|
419
|
+
* @param guards - Guard classes or injection tokens to apply
|
|
420
|
+
* @returns A class or method decorator
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```typescript
|
|
424
|
+
* // Apply to a controller
|
|
425
|
+
* @Controller()
|
|
426
|
+
* @UseGuards(AuthGuard, RoleGuard)
|
|
427
|
+
* export class UserController {
|
|
428
|
+
* @Endpoint(getUserEndpoint)
|
|
429
|
+
* async getUser() { }
|
|
430
|
+
* }
|
|
431
|
+
*
|
|
432
|
+
* // Apply to a specific endpoint
|
|
433
|
+
* @Controller()
|
|
434
|
+
* export class UserController {
|
|
435
|
+
* @Endpoint(getUserEndpoint)
|
|
436
|
+
* @UseGuards(AuthGuard)
|
|
437
|
+
* async getUser() { }
|
|
438
|
+
* }
|
|
439
|
+
* ```
|
|
440
|
+
*/ function UseGuards(...guards) {
|
|
441
|
+
return function(target, context) {
|
|
442
|
+
if (context.kind === "class") {
|
|
443
|
+
const controllerMetadata = getControllerMetadata(target, context);
|
|
444
|
+
for (const guard of guards.reverse()) controllerMetadata.guards.add(guard);
|
|
445
|
+
} else if (context.kind === "method") {
|
|
446
|
+
const endpointMetadata = getEndpointMetadata(target, context);
|
|
447
|
+
for (const guard of guards.reverse()) endpointMetadata.guards.add(guard);
|
|
448
|
+
} else throw new Error("[Navios] @UseGuards decorator can only be used on classes or methods.");
|
|
449
|
+
return target;
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
//#endregion
|
|
454
|
+
Object.defineProperty(exports, 'Controller', {
|
|
455
|
+
enumerable: true,
|
|
456
|
+
get: function () {
|
|
457
|
+
return Controller;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
Object.defineProperty(exports, 'ControllerMetadataKey', {
|
|
461
|
+
enumerable: true,
|
|
462
|
+
get: function () {
|
|
463
|
+
return ControllerMetadataKey;
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
Object.defineProperty(exports, 'Endpoint', {
|
|
467
|
+
enumerable: true,
|
|
468
|
+
get: function () {
|
|
469
|
+
return Endpoint;
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
Object.defineProperty(exports, 'EndpointAdapterToken', {
|
|
473
|
+
enumerable: true,
|
|
474
|
+
get: function () {
|
|
475
|
+
return EndpointAdapterToken;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
Object.defineProperty(exports, 'EndpointMetadataKey', {
|
|
479
|
+
enumerable: true,
|
|
480
|
+
get: function () {
|
|
481
|
+
return EndpointMetadataKey;
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
Object.defineProperty(exports, 'ExecutionContext', {
|
|
485
|
+
enumerable: true,
|
|
486
|
+
get: function () {
|
|
487
|
+
return ExecutionContext;
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
Object.defineProperty(exports, 'ExecutionContextInjectionToken', {
|
|
491
|
+
enumerable: true,
|
|
492
|
+
get: function () {
|
|
493
|
+
return ExecutionContextInjectionToken;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
Object.defineProperty(exports, 'Header', {
|
|
497
|
+
enumerable: true,
|
|
498
|
+
get: function () {
|
|
499
|
+
return Header;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
Object.defineProperty(exports, 'HttpAdapterToken', {
|
|
503
|
+
enumerable: true,
|
|
504
|
+
get: function () {
|
|
505
|
+
return HttpAdapterToken;
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
Object.defineProperty(exports, 'HttpCode', {
|
|
509
|
+
enumerable: true,
|
|
510
|
+
get: function () {
|
|
511
|
+
return HttpCode;
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
Object.defineProperty(exports, 'Module', {
|
|
515
|
+
enumerable: true,
|
|
516
|
+
get: function () {
|
|
517
|
+
return Module;
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
Object.defineProperty(exports, 'ModuleMetadataKey', {
|
|
521
|
+
enumerable: true,
|
|
522
|
+
get: function () {
|
|
523
|
+
return ModuleMetadataKey;
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
Object.defineProperty(exports, 'Multipart', {
|
|
527
|
+
enumerable: true,
|
|
528
|
+
get: function () {
|
|
529
|
+
return Multipart;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
Object.defineProperty(exports, 'MultipartAdapterToken', {
|
|
533
|
+
enumerable: true,
|
|
534
|
+
get: function () {
|
|
535
|
+
return MultipartAdapterToken;
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
Object.defineProperty(exports, 'Reply', {
|
|
539
|
+
enumerable: true,
|
|
540
|
+
get: function () {
|
|
541
|
+
return Reply;
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
Object.defineProperty(exports, 'Request', {
|
|
545
|
+
enumerable: true,
|
|
546
|
+
get: function () {
|
|
547
|
+
return Request;
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
Object.defineProperty(exports, 'Stream', {
|
|
551
|
+
enumerable: true,
|
|
552
|
+
get: function () {
|
|
553
|
+
return Stream;
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
Object.defineProperty(exports, 'StreamAdapterToken', {
|
|
557
|
+
enumerable: true,
|
|
558
|
+
get: function () {
|
|
559
|
+
return StreamAdapterToken;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
Object.defineProperty(exports, 'UseGuards', {
|
|
563
|
+
enumerable: true,
|
|
564
|
+
get: function () {
|
|
565
|
+
return UseGuards;
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
Object.defineProperty(exports, 'XmlStreamAdapterToken', {
|
|
569
|
+
enumerable: true,
|
|
570
|
+
get: function () {
|
|
571
|
+
return XmlStreamAdapterToken;
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
Object.defineProperty(exports, 'extractControllerMetadata', {
|
|
575
|
+
enumerable: true,
|
|
576
|
+
get: function () {
|
|
577
|
+
return extractControllerMetadata;
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
Object.defineProperty(exports, 'extractModuleMetadata', {
|
|
581
|
+
enumerable: true,
|
|
582
|
+
get: function () {
|
|
583
|
+
return extractModuleMetadata;
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
Object.defineProperty(exports, 'getAllEndpointMetadata', {
|
|
587
|
+
enumerable: true,
|
|
588
|
+
get: function () {
|
|
589
|
+
return getAllEndpointMetadata;
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
Object.defineProperty(exports, 'getControllerMetadata', {
|
|
593
|
+
enumerable: true,
|
|
594
|
+
get: function () {
|
|
595
|
+
return getControllerMetadata;
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
Object.defineProperty(exports, 'getEndpointMetadata', {
|
|
599
|
+
enumerable: true,
|
|
600
|
+
get: function () {
|
|
601
|
+
return getEndpointMetadata;
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
Object.defineProperty(exports, 'getModuleMetadata', {
|
|
605
|
+
enumerable: true,
|
|
606
|
+
get: function () {
|
|
607
|
+
return getModuleMetadata;
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
Object.defineProperty(exports, 'hasControllerMetadata', {
|
|
611
|
+
enumerable: true,
|
|
612
|
+
get: function () {
|
|
613
|
+
return hasControllerMetadata;
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
Object.defineProperty(exports, 'hasModuleMetadata', {
|
|
617
|
+
enumerable: true,
|
|
618
|
+
get: function () {
|
|
619
|
+
return hasModuleMetadata;
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
//# sourceMappingURL=use-guards.decorator-B6q_N0sf.cjs.map
|