@navios/core 0.1.8 → 0.1.10

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/dist/index.mjs CHANGED
@@ -1,50 +1,8 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __knownSymbol = (name2, symbol) => (symbol = Symbol[name2]) ? symbol : Symbol.for("Symbol." + name2);
5
- var __typeError = (msg) => {
6
- throw TypeError(msg);
7
- };
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
- var __decoratorStart = (base) => [, , , __create((base == null ? void 0 : base[__knownSymbol("metadata")]) ?? null)];
11
- var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
12
- var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
13
- var __decoratorContext = (kind, name2, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name: name2, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
14
- var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
15
- var __runInitializers = (array, flags, self, value) => {
16
- for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
17
- return value;
18
- };
19
- var __decorateElement = (array, flags, name2, decorators, target, extra) => {
20
- var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
21
- var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
22
- var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
23
- var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name2]() {
24
- return __privateGet(this, extra);
25
- }, set [name2](x) {
26
- return __privateSet(this, extra, x);
27
- } }, name2));
28
- k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name2) : __name(target, name2);
29
- for (var i = decorators.length - 1; i >= 0; i--) {
30
- ctx = __decoratorContext(k, name2, done = {}, array[3], extraInitializers);
31
- if (k) {
32
- ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name2 in x };
33
- if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name2];
34
- if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name2] = y;
35
- }
36
- it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
37
- if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
38
- else if (typeof it !== "object" || it === null) __typeError("Object expected");
39
- else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
40
- }
41
- return k || __decoratorMetadata(array, target), desc && __defProp(target, name2, desc), p ? k ^ 4 ? extra : desc : target;
42
- };
43
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
44
- var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
45
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
46
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
47
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
1
+ import {
2
+ __decorateElement,
3
+ __decoratorStart,
4
+ __runInitializers
5
+ } from "./chunk-Z2AVZ4BT.mjs";
48
6
 
49
7
  // packages/core/src/config/utils/helpers.mts
50
8
  import { env } from "node:process";
@@ -1675,7 +1633,9 @@ function provideConfig(options) {
1675
1633
  var EndpointMetadataKey = Symbol("EndpointMetadataKey");
1676
1634
  var EndpointType = /* @__PURE__ */ ((EndpointType2) => {
1677
1635
  EndpointType2["Unknown"] = "unknown";
1678
- EndpointType2["Config"] = "config";
1636
+ EndpointType2["Endpoint"] = "endpoint";
1637
+ EndpointType2["Stream"] = "stream";
1638
+ EndpointType2["Multipart"] = "multipart";
1679
1639
  EndpointType2["Handler"] = "handler";
1680
1640
  return EndpointType2;
1681
1641
  })(EndpointType || {});
@@ -1780,7 +1740,7 @@ function extractModuleMetadata(target) {
1780
1740
  const metadata = target[ModuleMetadataKey];
1781
1741
  if (!metadata) {
1782
1742
  throw new Error(
1783
- "[Navios] Module metadata not found. Make sure to use @Module decorator."
1743
+ `[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`
1784
1744
  );
1785
1745
  }
1786
1746
  return metadata;
@@ -1837,7 +1797,7 @@ function Endpoint(endpoint) {
1837
1797
  );
1838
1798
  }
1839
1799
  endpointMetadata.config = config;
1840
- endpointMetadata.type = "config" /* Config */;
1800
+ endpointMetadata.type = "endpoint" /* Endpoint */;
1841
1801
  endpointMetadata.classMethod = target.name;
1842
1802
  endpointMetadata.httpMethod = config.method;
1843
1803
  endpointMetadata.url = config.url;
@@ -1853,6 +1813,11 @@ function Header(name2, value) {
1853
1813
  throw new Error("[Navios] Header decorator can only be used on methods.");
1854
1814
  }
1855
1815
  const metadata = getEndpointMetadata(target, context);
1816
+ if (metadata.type === "stream" /* Stream */) {
1817
+ throw new Error(
1818
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
1819
+ );
1820
+ }
1856
1821
  metadata.headers[name2] = value;
1857
1822
  return target;
1858
1823
  };
@@ -1867,6 +1832,11 @@ function HttpCode(code) {
1867
1832
  );
1868
1833
  }
1869
1834
  const metadata = getEndpointMetadata(target, context);
1835
+ if (metadata.type === "stream" /* Stream */) {
1836
+ throw new Error(
1837
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
1838
+ );
1839
+ }
1870
1840
  metadata.successStatusCode = code;
1871
1841
  return target;
1872
1842
  };
@@ -1903,6 +1873,69 @@ function Module(metadata) {
1903
1873
  };
1904
1874
  }
1905
1875
 
1876
+ // packages/core/src/decorators/multipart.decorator.mts
1877
+ import "zod";
1878
+ function Multipart(endpoint) {
1879
+ return (target, context) => {
1880
+ if (typeof target !== "function") {
1881
+ throw new Error(
1882
+ "[Navios] Endpoint decorator can only be used on functions."
1883
+ );
1884
+ }
1885
+ if (context.kind !== "method") {
1886
+ throw new Error(
1887
+ "[Navios] Endpoint decorator can only be used on methods."
1888
+ );
1889
+ }
1890
+ const config = endpoint.config;
1891
+ if (context.metadata) {
1892
+ let endpointMetadata = getEndpointMetadata(target, context);
1893
+ if (endpointMetadata.config && endpointMetadata.config.url) {
1894
+ throw new Error(
1895
+ `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`
1896
+ );
1897
+ }
1898
+ endpointMetadata.config = config;
1899
+ endpointMetadata.type = "multipart" /* Multipart */;
1900
+ endpointMetadata.classMethod = target.name;
1901
+ endpointMetadata.httpMethod = config.method;
1902
+ endpointMetadata.url = config.url;
1903
+ }
1904
+ return target;
1905
+ };
1906
+ }
1907
+
1908
+ // packages/core/src/decorators/stream.decorator.mts
1909
+ function Stream(endpoint) {
1910
+ return (target, context) => {
1911
+ if (typeof target !== "function") {
1912
+ throw new Error(
1913
+ "[Navios] Endpoint decorator can only be used on functions."
1914
+ );
1915
+ }
1916
+ if (context.kind !== "method") {
1917
+ throw new Error(
1918
+ "[Navios] Endpoint decorator can only be used on methods."
1919
+ );
1920
+ }
1921
+ const config = endpoint.config;
1922
+ if (context.metadata) {
1923
+ let endpointMetadata = getEndpointMetadata(target, context);
1924
+ if (endpointMetadata.config && endpointMetadata.config.url) {
1925
+ throw new Error(
1926
+ `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`
1927
+ );
1928
+ }
1929
+ endpointMetadata.config = config;
1930
+ endpointMetadata.type = "stream" /* Stream */;
1931
+ endpointMetadata.classMethod = target.name;
1932
+ endpointMetadata.httpMethod = config.method;
1933
+ endpointMetadata.url = config.url;
1934
+ }
1935
+ return target;
1936
+ };
1937
+ }
1938
+
1906
1939
  // packages/core/src/decorators/use-guards.decorator.mts
1907
1940
  function UseGuards(...guards) {
1908
1941
  return function(target, context) {
@@ -1983,6 +2016,7 @@ var ConflictException = class extends HttpException {
1983
2016
 
1984
2017
  // packages/core/src/services/controller-adapter.service.mts
1985
2018
  import { NaviosException as NaviosException4 } from "@navios/common";
2019
+ import { ZodArray } from "zod";
1986
2020
 
1987
2021
  // packages/core/src/tokens/application.token.mts
1988
2022
  var ApplicationInjectionToken = "ApplicationInjectionToken";
@@ -2187,7 +2221,7 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2187
2221
  if (querySchema) {
2188
2222
  schema.querystring = querySchema;
2189
2223
  }
2190
- if (requestSchema) {
2224
+ if (requestSchema && endpointMetadata.type !== "multipart" /* Multipart */) {
2191
2225
  schema.body = requestSchema;
2192
2226
  }
2193
2227
  if (responseSchema) {
@@ -2204,12 +2238,24 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2204
2238
  `Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
2205
2239
  );
2206
2240
  throw new NaviosException4("Unknown endpoint type");
2207
- case "config" /* Config */:
2241
+ case "endpoint" /* Endpoint */:
2208
2242
  return this.provideHandlerForConfig(
2209
2243
  controller,
2210
2244
  executionContext,
2211
2245
  endpointMetadata
2212
2246
  );
2247
+ case "stream" /* Stream */:
2248
+ return this.provideHandlerForStream(
2249
+ controller,
2250
+ executionContext,
2251
+ endpointMetadata
2252
+ );
2253
+ case "multipart" /* Multipart */:
2254
+ return this.provideHandlerForMultipart(
2255
+ controller,
2256
+ executionContext,
2257
+ endpointMetadata
2258
+ );
2213
2259
  case "handler" /* Handler */:
2214
2260
  this.logger.error("Not implemented yet");
2215
2261
  throw new NaviosException4("Not implemented yet");
@@ -2247,6 +2293,102 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2247
2293
  }
2248
2294
  };
2249
2295
  }
2296
+ provideHandlerForStream(controller, executionContext, endpointMetadata) {
2297
+ return async (request, reply) => {
2298
+ getServiceLocator().registerInstance(Request, request);
2299
+ getServiceLocator().registerInstance(Reply, reply);
2300
+ getServiceLocator().registerInstance(
2301
+ ExecutionContextToken,
2302
+ executionContext
2303
+ );
2304
+ executionContext.provideRequest(request);
2305
+ executionContext.provideReply(reply);
2306
+ const controllerInstance = await inject(controller);
2307
+ try {
2308
+ const { query, params, body } = request;
2309
+ const argument = {};
2310
+ if (query && Object.keys(query).length > 0) {
2311
+ argument.params = query;
2312
+ }
2313
+ if (params && Object.keys(params).length > 0) {
2314
+ argument.urlParams = params;
2315
+ }
2316
+ if (body) {
2317
+ argument.data = body;
2318
+ }
2319
+ await controllerInstance[endpointMetadata.classMethod](argument, reply);
2320
+ } finally {
2321
+ getServiceLocator().removeInstance(Request);
2322
+ getServiceLocator().removeInstance(Reply);
2323
+ getServiceLocator().removeInstance(ExecutionContextToken);
2324
+ }
2325
+ };
2326
+ }
2327
+ provideHandlerForMultipart(controller, executionContext, endpointMetadata) {
2328
+ const config = endpointMetadata.config;
2329
+ const requestSchema = config.requestSchema;
2330
+ const shape = requestSchema._def.shape();
2331
+ return async (request, reply) => {
2332
+ getServiceLocator().registerInstance(Request, request);
2333
+ getServiceLocator().registerInstance(Reply, reply);
2334
+ getServiceLocator().registerInstance(
2335
+ ExecutionContextToken,
2336
+ executionContext
2337
+ );
2338
+ executionContext.provideRequest(request);
2339
+ executionContext.provideReply(reply);
2340
+ const controllerInstance = await inject(controller);
2341
+ try {
2342
+ const parts = request.parts();
2343
+ const { query, params } = request;
2344
+ const argument = {};
2345
+ if (query && Object.keys(query).length > 0) {
2346
+ argument.params = query;
2347
+ }
2348
+ if (params && Object.keys(params).length > 0) {
2349
+ argument.urlParams = params;
2350
+ }
2351
+ const req = {};
2352
+ for await (const part of parts) {
2353
+ if (!shape[part.fieldname]) {
2354
+ throw new NaviosException4(
2355
+ `Invalid field name ${part.fieldname} for multipart request`
2356
+ );
2357
+ }
2358
+ const schema = shape[part.fieldname];
2359
+ if (part.type === "file") {
2360
+ const file = new File([await part.toBuffer()], part.filename, {
2361
+ type: part.mimetype
2362
+ });
2363
+ if (schema instanceof ZodArray) {
2364
+ if (!req[part.fieldname]) {
2365
+ req[part.fieldname] = [];
2366
+ }
2367
+ req[part.fieldname].push(file);
2368
+ } else {
2369
+ req[part.fieldname] = file;
2370
+ }
2371
+ } else {
2372
+ if (schema instanceof ZodArray) {
2373
+ if (!req[part.fieldname]) {
2374
+ req[part.fieldname] = [];
2375
+ }
2376
+ req[part.fieldname].push(part.value);
2377
+ } else {
2378
+ req[part.fieldname] = part.value;
2379
+ }
2380
+ }
2381
+ }
2382
+ argument.body = requestSchema.parse(req);
2383
+ const result = await controllerInstance[endpointMetadata.classMethod](argument);
2384
+ reply.status(endpointMetadata.successStatusCode).headers(endpointMetadata.headers).send(result);
2385
+ } finally {
2386
+ getServiceLocator().removeInstance(Request);
2387
+ getServiceLocator().removeInstance(Reply);
2388
+ getServiceLocator().removeInstance(ExecutionContextToken);
2389
+ }
2390
+ };
2391
+ }
2250
2392
  };
2251
2393
  _init6 = __decoratorStart(null);
2252
2394
  _ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
@@ -2395,6 +2537,7 @@ var _NaviosApplication = class _NaviosApplication {
2395
2537
  });
2396
2538
  server = null;
2397
2539
  corsOptions = null;
2540
+ multipartOptions = null;
2398
2541
  globalPrefix = null;
2399
2542
  appModule = null;
2400
2543
  options = {};
@@ -2416,6 +2559,9 @@ var _NaviosApplication = class _NaviosApplication {
2416
2559
  if (this.corsOptions) {
2417
2560
  await this.server.register(cors, this.corsOptions);
2418
2561
  }
2562
+ if (this.multipartOptions) {
2563
+ await this.configureMultipart(this.server, this.multipartOptions);
2564
+ }
2419
2565
  await this.initModules();
2420
2566
  await this.server.ready();
2421
2567
  this.isInitialized = true;
@@ -2477,6 +2623,22 @@ var _NaviosApplication = class _NaviosApplication {
2477
2623
  return reply.status(404).send(response);
2478
2624
  });
2479
2625
  }
2626
+ async configureMultipart(server, options) {
2627
+ if (options) {
2628
+ try {
2629
+ const multipartModule = await import("./multipart-23QO76VV.mjs");
2630
+ await server.register(
2631
+ multipartModule.default,
2632
+ typeof options === "object" ? options : {}
2633
+ );
2634
+ } catch (error) {
2635
+ this.logger.error(
2636
+ `@fastify/multipart is not installed. Please install it.`
2637
+ );
2638
+ throw error;
2639
+ }
2640
+ }
2641
+ }
2480
2642
  async initModules() {
2481
2643
  const modules = this.moduleLoader.getAllModules();
2482
2644
  const promises = [];
@@ -2507,6 +2669,9 @@ var _NaviosApplication = class _NaviosApplication {
2507
2669
  enableCors(options) {
2508
2670
  this.corsOptions = options;
2509
2671
  }
2672
+ enableMultipart(options) {
2673
+ this.multipartOptions = options;
2674
+ }
2510
2675
  setGlobalPrefix(prefix) {
2511
2676
  this.globalPrefix = prefix;
2512
2677
  }
@@ -2607,6 +2772,7 @@ export {
2607
2772
  Module,
2608
2773
  ModuleLoaderService,
2609
2774
  ModuleMetadataKey,
2775
+ Multipart,
2610
2776
  NaviosApplication,
2611
2777
  NaviosFactory,
2612
2778
  NotFoundException,
@@ -2618,6 +2784,7 @@ export {
2618
2784
  ServiceLocatorInstanceHolderKind,
2619
2785
  ServiceLocatorInstanceHolderStatus,
2620
2786
  ServiceLocatorManager,
2787
+ Stream,
2621
2788
  UnauthorizedException,
2622
2789
  UnknownError,
2623
2790
  UseGuards,