@navios/core 0.1.9 → 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";
@@ -1677,6 +1635,7 @@ var EndpointType = /* @__PURE__ */ ((EndpointType2) => {
1677
1635
  EndpointType2["Unknown"] = "unknown";
1678
1636
  EndpointType2["Endpoint"] = "endpoint";
1679
1637
  EndpointType2["Stream"] = "stream";
1638
+ EndpointType2["Multipart"] = "multipart";
1680
1639
  EndpointType2["Handler"] = "handler";
1681
1640
  return EndpointType2;
1682
1641
  })(EndpointType || {});
@@ -1914,6 +1873,38 @@ function Module(metadata) {
1914
1873
  };
1915
1874
  }
1916
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
+
1917
1908
  // packages/core/src/decorators/stream.decorator.mts
1918
1909
  function Stream(endpoint) {
1919
1910
  return (target, context) => {
@@ -2025,6 +2016,7 @@ var ConflictException = class extends HttpException {
2025
2016
 
2026
2017
  // packages/core/src/services/controller-adapter.service.mts
2027
2018
  import { NaviosException as NaviosException4 } from "@navios/common";
2019
+ import { ZodArray } from "zod";
2028
2020
 
2029
2021
  // packages/core/src/tokens/application.token.mts
2030
2022
  var ApplicationInjectionToken = "ApplicationInjectionToken";
@@ -2229,7 +2221,7 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2229
2221
  if (querySchema) {
2230
2222
  schema.querystring = querySchema;
2231
2223
  }
2232
- if (requestSchema) {
2224
+ if (requestSchema && endpointMetadata.type !== "multipart" /* Multipart */) {
2233
2225
  schema.body = requestSchema;
2234
2226
  }
2235
2227
  if (responseSchema) {
@@ -2258,6 +2250,12 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2258
2250
  executionContext,
2259
2251
  endpointMetadata
2260
2252
  );
2253
+ case "multipart" /* Multipart */:
2254
+ return this.provideHandlerForMultipart(
2255
+ controller,
2256
+ executionContext,
2257
+ endpointMetadata
2258
+ );
2261
2259
  case "handler" /* Handler */:
2262
2260
  this.logger.error("Not implemented yet");
2263
2261
  throw new NaviosException4("Not implemented yet");
@@ -2326,6 +2324,71 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2326
2324
  }
2327
2325
  };
2328
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
+ }
2329
2392
  };
2330
2393
  _init6 = __decoratorStart(null);
2331
2394
  _ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
@@ -2474,6 +2537,7 @@ var _NaviosApplication = class _NaviosApplication {
2474
2537
  });
2475
2538
  server = null;
2476
2539
  corsOptions = null;
2540
+ multipartOptions = null;
2477
2541
  globalPrefix = null;
2478
2542
  appModule = null;
2479
2543
  options = {};
@@ -2495,6 +2559,9 @@ var _NaviosApplication = class _NaviosApplication {
2495
2559
  if (this.corsOptions) {
2496
2560
  await this.server.register(cors, this.corsOptions);
2497
2561
  }
2562
+ if (this.multipartOptions) {
2563
+ await this.configureMultipart(this.server, this.multipartOptions);
2564
+ }
2498
2565
  await this.initModules();
2499
2566
  await this.server.ready();
2500
2567
  this.isInitialized = true;
@@ -2556,6 +2623,22 @@ var _NaviosApplication = class _NaviosApplication {
2556
2623
  return reply.status(404).send(response);
2557
2624
  });
2558
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
+ }
2559
2642
  async initModules() {
2560
2643
  const modules = this.moduleLoader.getAllModules();
2561
2644
  const promises = [];
@@ -2586,6 +2669,9 @@ var _NaviosApplication = class _NaviosApplication {
2586
2669
  enableCors(options) {
2587
2670
  this.corsOptions = options;
2588
2671
  }
2672
+ enableMultipart(options) {
2673
+ this.multipartOptions = options;
2674
+ }
2589
2675
  setGlobalPrefix(prefix) {
2590
2676
  this.globalPrefix = prefix;
2591
2677
  }
@@ -2686,6 +2772,7 @@ export {
2686
2772
  Module,
2687
2773
  ModuleLoaderService,
2688
2774
  ModuleMetadataKey,
2775
+ Multipart,
2689
2776
  NaviosApplication,
2690
2777
  NaviosFactory,
2691
2778
  NotFoundException,