@navios/core 0.1.7 → 0.1.9

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.
@@ -1,5 +1,6 @@
1
1
  import type { AnyZodObject } from 'zod';
2
2
  import type { BaseEndpointConfig } from '@navios/common';
3
+ import type { BaseStreamConfig } from '@navios/common';
3
4
  import type { EndpointFunctionArgs } from '@navios/common';
4
5
  import { FastifyBaseLogger } from 'fastify';
5
6
  import type { FastifyCorsOptions } from '@fastify/cors';
@@ -21,6 +22,7 @@ import { ResolveFastifyRequestType } from 'fastify/types/type-provider.js';
21
22
  import { RouteGenericInterface } from 'fastify';
22
23
  import { ServerResponse } from 'http';
23
24
  import { z } from 'zod';
25
+ import { ZodDiscriminatedUnion } from 'zod';
24
26
  import { ZodOptional } from 'zod';
25
27
  import type { ZodType } from 'zod';
26
28
 
@@ -378,6 +380,7 @@ declare class ControllerAdapterService {
378
380
  private provideSchemaForConfig;
379
381
  private provideHandler;
380
382
  private provideHandlerForConfig;
383
+ private provideHandlerForStream;
381
384
  }
382
385
  export { ControllerAdapterService }
383
386
  export { ControllerAdapterService as ControllerAdapterService_alias_1 }
@@ -418,7 +421,7 @@ declare interface EndpointMetadata {
418
421
  type: EndpointType;
419
422
  headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>;
420
423
  httpMethod: HttpMethod;
421
- config: BaseEndpointConfig | null;
424
+ config: BaseEndpointConfig | BaseStreamConfig | null;
422
425
  guards: Set<ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>>;
423
426
  customAttributes: Map<string | symbol, any>;
424
427
  }
@@ -438,9 +441,17 @@ export { EndpointParams }
438
441
  export { EndpointParams as EndpointParams_alias_1 }
439
442
  export { EndpointParams as EndpointParams_alias_2 }
440
443
 
444
+ declare type EndpointResult<EndpointDeclaration extends {
445
+ config: BaseEndpointConfig<any, any, any, any, any>;
446
+ }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<any, infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
447
+ export { EndpointResult }
448
+ export { EndpointResult as EndpointResult_alias_1 }
449
+ export { EndpointResult as EndpointResult_alias_2 }
450
+
441
451
  declare enum EndpointType {
442
452
  Unknown = "unknown",
443
- Config = "config",
453
+ Endpoint = "endpoint",
454
+ Stream = "stream",
444
455
  Handler = "handler"
445
456
  }
446
457
  export { EndpointType }
@@ -1437,6 +1448,20 @@ export { setPromiseCollector }
1437
1448
  export { setPromiseCollector as setPromiseCollector_alias_1 }
1438
1449
  export { setPromiseCollector as setPromiseCollector_alias_2 }
1439
1450
 
1451
+ declare function Stream<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema = undefined, RequestSchema = ZodType>(endpoint: {
1452
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>;
1453
+ }): (target: (params: QuerySchema extends AnyZodObject ? RequestSchema extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, RequestSchema> : EndpointFunctionArgs<Url, QuerySchema, undefined> : RequestSchema extends ZodType ? EndpointFunctionArgs<Url, undefined, RequestSchema> : EndpointFunctionArgs<Url, undefined, undefined>, reply: FastifyReply) => Promise<void>, context: ClassMethodDecoratorContext) => (params: QuerySchema extends AnyZodObject ? RequestSchema extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, RequestSchema> : EndpointFunctionArgs<Url, QuerySchema, undefined> : RequestSchema extends ZodType ? EndpointFunctionArgs<Url, undefined, RequestSchema> : EndpointFunctionArgs<Url, undefined, undefined>, reply: FastifyReply) => Promise<void>;
1454
+ export { Stream }
1455
+ export { Stream as Stream_alias_1 }
1456
+ export { Stream as Stream_alias_2 }
1457
+
1458
+ declare type StreamParams<EndpointDeclaration extends {
1459
+ config: BaseStreamConfig<any, any, any, any>;
1460
+ }, Url extends string = EndpointDeclaration['config']['url'], QuerySchema = EndpointDeclaration['config']['querySchema']> = QuerySchema extends AnyZodObject ? EndpointDeclaration['config']['requestSchema'] extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, EndpointDeclaration['config']['requestSchema']> : EndpointFunctionArgs<Url, QuerySchema, undefined> : EndpointDeclaration['config']['requestSchema'] extends ZodType ? EndpointFunctionArgs<Url, undefined, EndpointDeclaration['config']['requestSchema']> : EndpointFunctionArgs<Url, undefined, undefined>;
1461
+ export { StreamParams }
1462
+ export { StreamParams as StreamParams_alias_1 }
1463
+ export { StreamParams as StreamParams_alias_2 }
1464
+
1440
1465
  declare const stripEndSlash: (path: string) => string;
1441
1466
  export { stripEndSlash }
1442
1467
  export { stripEndSlash as stripEndSlash_alias_1 }
@@ -1,5 +1,6 @@
1
1
  import type { AnyZodObject } from 'zod';
2
2
  import type { BaseEndpointConfig } from '@navios/common';
3
+ import type { BaseStreamConfig } from '@navios/common';
3
4
  import type { EndpointFunctionArgs } from '@navios/common';
4
5
  import { FastifyBaseLogger } from 'fastify';
5
6
  import type { FastifyCorsOptions } from '@fastify/cors';
@@ -21,6 +22,7 @@ import { ResolveFastifyRequestType } from 'fastify/types/type-provider.js';
21
22
  import { RouteGenericInterface } from 'fastify';
22
23
  import { ServerResponse } from 'http';
23
24
  import { z } from 'zod';
25
+ import { ZodDiscriminatedUnion } from 'zod';
24
26
  import { ZodOptional } from 'zod';
25
27
  import type { ZodType } from 'zod';
26
28
 
@@ -378,6 +380,7 @@ declare class ControllerAdapterService {
378
380
  private provideSchemaForConfig;
379
381
  private provideHandler;
380
382
  private provideHandlerForConfig;
383
+ private provideHandlerForStream;
381
384
  }
382
385
  export { ControllerAdapterService }
383
386
  export { ControllerAdapterService as ControllerAdapterService_alias_1 }
@@ -418,7 +421,7 @@ declare interface EndpointMetadata {
418
421
  type: EndpointType;
419
422
  headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>;
420
423
  httpMethod: HttpMethod;
421
- config: BaseEndpointConfig | null;
424
+ config: BaseEndpointConfig | BaseStreamConfig | null;
422
425
  guards: Set<ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>>;
423
426
  customAttributes: Map<string | symbol, any>;
424
427
  }
@@ -438,9 +441,17 @@ export { EndpointParams }
438
441
  export { EndpointParams as EndpointParams_alias_1 }
439
442
  export { EndpointParams as EndpointParams_alias_2 }
440
443
 
444
+ declare type EndpointResult<EndpointDeclaration extends {
445
+ config: BaseEndpointConfig<any, any, any, any, any>;
446
+ }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<any, infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
447
+ export { EndpointResult }
448
+ export { EndpointResult as EndpointResult_alias_1 }
449
+ export { EndpointResult as EndpointResult_alias_2 }
450
+
441
451
  declare enum EndpointType {
442
452
  Unknown = "unknown",
443
- Config = "config",
453
+ Endpoint = "endpoint",
454
+ Stream = "stream",
444
455
  Handler = "handler"
445
456
  }
446
457
  export { EndpointType }
@@ -1437,6 +1448,20 @@ export { setPromiseCollector }
1437
1448
  export { setPromiseCollector as setPromiseCollector_alias_1 }
1438
1449
  export { setPromiseCollector as setPromiseCollector_alias_2 }
1439
1450
 
1451
+ declare function Stream<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema = undefined, RequestSchema = ZodType>(endpoint: {
1452
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>;
1453
+ }): (target: (params: QuerySchema extends AnyZodObject ? RequestSchema extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, RequestSchema> : EndpointFunctionArgs<Url, QuerySchema, undefined> : RequestSchema extends ZodType ? EndpointFunctionArgs<Url, undefined, RequestSchema> : EndpointFunctionArgs<Url, undefined, undefined>, reply: FastifyReply) => Promise<void>, context: ClassMethodDecoratorContext) => (params: QuerySchema extends AnyZodObject ? RequestSchema extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, RequestSchema> : EndpointFunctionArgs<Url, QuerySchema, undefined> : RequestSchema extends ZodType ? EndpointFunctionArgs<Url, undefined, RequestSchema> : EndpointFunctionArgs<Url, undefined, undefined>, reply: FastifyReply) => Promise<void>;
1454
+ export { Stream }
1455
+ export { Stream as Stream_alias_1 }
1456
+ export { Stream as Stream_alias_2 }
1457
+
1458
+ declare type StreamParams<EndpointDeclaration extends {
1459
+ config: BaseStreamConfig<any, any, any, any>;
1460
+ }, Url extends string = EndpointDeclaration['config']['url'], QuerySchema = EndpointDeclaration['config']['querySchema']> = QuerySchema extends AnyZodObject ? EndpointDeclaration['config']['requestSchema'] extends ZodType ? EndpointFunctionArgs<Url, QuerySchema, EndpointDeclaration['config']['requestSchema']> : EndpointFunctionArgs<Url, QuerySchema, undefined> : EndpointDeclaration['config']['requestSchema'] extends ZodType ? EndpointFunctionArgs<Url, undefined, EndpointDeclaration['config']['requestSchema']> : EndpointFunctionArgs<Url, undefined, undefined>;
1461
+ export { StreamParams }
1462
+ export { StreamParams as StreamParams_alias_1 }
1463
+ export { StreamParams as StreamParams_alias_2 }
1464
+
1440
1465
  declare const stripEndSlash: (path: string) => string;
1441
1466
  export { stripEndSlash }
1442
1467
  export { stripEndSlash as stripEndSlash_alias_1 }
package/dist/index.d.mts CHANGED
@@ -14,10 +14,13 @@ export { Controller } from './_tsup-dts-rollup.mjs';
14
14
  export { ControllerOptions } from './_tsup-dts-rollup.mjs';
15
15
  export { Endpoint } from './_tsup-dts-rollup.mjs';
16
16
  export { EndpointParams } from './_tsup-dts-rollup.mjs';
17
+ export { EndpointResult } from './_tsup-dts-rollup.mjs';
17
18
  export { Header } from './_tsup-dts-rollup.mjs';
18
19
  export { HttpCode } from './_tsup-dts-rollup.mjs';
19
20
  export { Module } from './_tsup-dts-rollup.mjs';
20
21
  export { ModuleOptions } from './_tsup-dts-rollup.mjs';
22
+ export { Stream } from './_tsup-dts-rollup.mjs';
23
+ export { StreamParams } from './_tsup-dts-rollup.mjs';
21
24
  export { UseGuards } from './_tsup-dts-rollup.mjs';
22
25
  export { HttpException } from './_tsup-dts-rollup.mjs';
23
26
  export { BadRequestException } from './_tsup-dts-rollup.mjs';
package/dist/index.d.ts CHANGED
@@ -14,10 +14,13 @@ export { Controller } from './_tsup-dts-rollup.js';
14
14
  export { ControllerOptions } from './_tsup-dts-rollup.js';
15
15
  export { Endpoint } from './_tsup-dts-rollup.js';
16
16
  export { EndpointParams } from './_tsup-dts-rollup.js';
17
+ export { EndpointResult } from './_tsup-dts-rollup.js';
17
18
  export { Header } from './_tsup-dts-rollup.js';
18
19
  export { HttpCode } from './_tsup-dts-rollup.js';
19
20
  export { Module } from './_tsup-dts-rollup.js';
20
21
  export { ModuleOptions } from './_tsup-dts-rollup.js';
22
+ export { Stream } from './_tsup-dts-rollup.js';
23
+ export { StreamParams } from './_tsup-dts-rollup.js';
21
24
  export { UseGuards } from './_tsup-dts-rollup.js';
22
25
  export { HttpException } from './_tsup-dts-rollup.js';
23
26
  export { BadRequestException } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -132,6 +132,7 @@ __export(src_exports, {
132
132
  ServiceLocatorInstanceHolderKind: () => ServiceLocatorInstanceHolderKind,
133
133
  ServiceLocatorInstanceHolderStatus: () => ServiceLocatorInstanceHolderStatus,
134
134
  ServiceLocatorManager: () => ServiceLocatorManager,
135
+ Stream: () => Stream,
135
136
  UnauthorizedException: () => UnauthorizedException,
136
137
  UnknownError: () => UnknownError,
137
138
  UseGuards: () => UseGuards,
@@ -1804,7 +1805,8 @@ function provideConfig(options) {
1804
1805
  var EndpointMetadataKey = Symbol("EndpointMetadataKey");
1805
1806
  var EndpointType = /* @__PURE__ */ ((EndpointType2) => {
1806
1807
  EndpointType2["Unknown"] = "unknown";
1807
- EndpointType2["Config"] = "config";
1808
+ EndpointType2["Endpoint"] = "endpoint";
1809
+ EndpointType2["Stream"] = "stream";
1808
1810
  EndpointType2["Handler"] = "handler";
1809
1811
  return EndpointType2;
1810
1812
  })(EndpointType || {});
@@ -1909,7 +1911,7 @@ function extractModuleMetadata(target) {
1909
1911
  const metadata = target[ModuleMetadataKey];
1910
1912
  if (!metadata) {
1911
1913
  throw new Error(
1912
- "[Navios] Module metadata not found. Make sure to use @Module decorator."
1914
+ `[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`
1913
1915
  );
1914
1916
  }
1915
1917
  return metadata;
@@ -1944,6 +1946,7 @@ function Controller({ guards } = {}) {
1944
1946
  }
1945
1947
 
1946
1948
  // packages/core/src/decorators/endpoint.decorator.mts
1949
+ var import_zod4 = require("zod");
1947
1950
  function Endpoint(endpoint) {
1948
1951
  return (target, context) => {
1949
1952
  if (typeof target !== "function") {
@@ -1965,7 +1968,7 @@ function Endpoint(endpoint) {
1965
1968
  );
1966
1969
  }
1967
1970
  endpointMetadata.config = config;
1968
- endpointMetadata.type = "config" /* Config */;
1971
+ endpointMetadata.type = "endpoint" /* Endpoint */;
1969
1972
  endpointMetadata.classMethod = target.name;
1970
1973
  endpointMetadata.httpMethod = config.method;
1971
1974
  endpointMetadata.url = config.url;
@@ -1981,6 +1984,11 @@ function Header(name2, value) {
1981
1984
  throw new Error("[Navios] Header decorator can only be used on methods.");
1982
1985
  }
1983
1986
  const metadata = getEndpointMetadata(target, context);
1987
+ if (metadata.type === "stream" /* Stream */) {
1988
+ throw new Error(
1989
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
1990
+ );
1991
+ }
1984
1992
  metadata.headers[name2] = value;
1985
1993
  return target;
1986
1994
  };
@@ -1995,6 +2003,11 @@ function HttpCode(code) {
1995
2003
  );
1996
2004
  }
1997
2005
  const metadata = getEndpointMetadata(target, context);
2006
+ if (metadata.type === "stream" /* Stream */) {
2007
+ throw new Error(
2008
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
2009
+ );
2010
+ }
1998
2011
  metadata.successStatusCode = code;
1999
2012
  return target;
2000
2013
  };
@@ -2031,6 +2044,37 @@ function Module(metadata) {
2031
2044
  };
2032
2045
  }
2033
2046
 
2047
+ // packages/core/src/decorators/stream.decorator.mts
2048
+ function Stream(endpoint) {
2049
+ return (target, context) => {
2050
+ if (typeof target !== "function") {
2051
+ throw new Error(
2052
+ "[Navios] Endpoint decorator can only be used on functions."
2053
+ );
2054
+ }
2055
+ if (context.kind !== "method") {
2056
+ throw new Error(
2057
+ "[Navios] Endpoint decorator can only be used on methods."
2058
+ );
2059
+ }
2060
+ const config = endpoint.config;
2061
+ if (context.metadata) {
2062
+ let endpointMetadata = getEndpointMetadata(target, context);
2063
+ if (endpointMetadata.config && endpointMetadata.config.url) {
2064
+ throw new Error(
2065
+ `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`
2066
+ );
2067
+ }
2068
+ endpointMetadata.config = config;
2069
+ endpointMetadata.type = "stream" /* Stream */;
2070
+ endpointMetadata.classMethod = target.name;
2071
+ endpointMetadata.httpMethod = config.method;
2072
+ endpointMetadata.url = config.url;
2073
+ }
2074
+ return target;
2075
+ };
2076
+ }
2077
+
2034
2078
  // packages/core/src/decorators/use-guards.decorator.mts
2035
2079
  function UseGuards(...guards) {
2036
2080
  return function(target, context) {
@@ -2332,12 +2376,18 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2332
2376
  `Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
2333
2377
  );
2334
2378
  throw new import_common4.NaviosException("Unknown endpoint type");
2335
- case "config" /* Config */:
2379
+ case "endpoint" /* Endpoint */:
2336
2380
  return this.provideHandlerForConfig(
2337
2381
  controller,
2338
2382
  executionContext,
2339
2383
  endpointMetadata
2340
2384
  );
2385
+ case "stream" /* Stream */:
2386
+ return this.provideHandlerForStream(
2387
+ controller,
2388
+ executionContext,
2389
+ endpointMetadata
2390
+ );
2341
2391
  case "handler" /* Handler */:
2342
2392
  this.logger.error("Not implemented yet");
2343
2393
  throw new import_common4.NaviosException("Not implemented yet");
@@ -2375,6 +2425,37 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2375
2425
  }
2376
2426
  };
2377
2427
  }
2428
+ provideHandlerForStream(controller, executionContext, endpointMetadata) {
2429
+ return async (request, reply) => {
2430
+ getServiceLocator().registerInstance(Request, request);
2431
+ getServiceLocator().registerInstance(Reply, reply);
2432
+ getServiceLocator().registerInstance(
2433
+ ExecutionContextToken,
2434
+ executionContext
2435
+ );
2436
+ executionContext.provideRequest(request);
2437
+ executionContext.provideReply(reply);
2438
+ const controllerInstance = await inject(controller);
2439
+ try {
2440
+ const { query, params, body } = request;
2441
+ const argument = {};
2442
+ if (query && Object.keys(query).length > 0) {
2443
+ argument.params = query;
2444
+ }
2445
+ if (params && Object.keys(params).length > 0) {
2446
+ argument.urlParams = params;
2447
+ }
2448
+ if (body) {
2449
+ argument.data = body;
2450
+ }
2451
+ await controllerInstance[endpointMetadata.classMethod](argument, reply);
2452
+ } finally {
2453
+ getServiceLocator().removeInstance(Request);
2454
+ getServiceLocator().removeInstance(Reply);
2455
+ getServiceLocator().removeInstance(ExecutionContextToken);
2456
+ }
2457
+ };
2458
+ }
2378
2459
  };
2379
2460
  _init6 = __decoratorStart(null);
2380
2461
  _ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
@@ -2744,6 +2825,7 @@ var NaviosFactory = class {
2744
2825
  ServiceLocatorInstanceHolderKind,
2745
2826
  ServiceLocatorInstanceHolderStatus,
2746
2827
  ServiceLocatorManager,
2828
+ Stream,
2747
2829
  UnauthorizedException,
2748
2830
  UnknownError,
2749
2831
  UseGuards,
package/dist/index.mjs CHANGED
@@ -1675,7 +1675,8 @@ function provideConfig(options) {
1675
1675
  var EndpointMetadataKey = Symbol("EndpointMetadataKey");
1676
1676
  var EndpointType = /* @__PURE__ */ ((EndpointType2) => {
1677
1677
  EndpointType2["Unknown"] = "unknown";
1678
- EndpointType2["Config"] = "config";
1678
+ EndpointType2["Endpoint"] = "endpoint";
1679
+ EndpointType2["Stream"] = "stream";
1679
1680
  EndpointType2["Handler"] = "handler";
1680
1681
  return EndpointType2;
1681
1682
  })(EndpointType || {});
@@ -1780,7 +1781,7 @@ function extractModuleMetadata(target) {
1780
1781
  const metadata = target[ModuleMetadataKey];
1781
1782
  if (!metadata) {
1782
1783
  throw new Error(
1783
- "[Navios] Module metadata not found. Make sure to use @Module decorator."
1784
+ `[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`
1784
1785
  );
1785
1786
  }
1786
1787
  return metadata;
@@ -1815,6 +1816,7 @@ function Controller({ guards } = {}) {
1815
1816
  }
1816
1817
 
1817
1818
  // packages/core/src/decorators/endpoint.decorator.mts
1819
+ import "zod";
1818
1820
  function Endpoint(endpoint) {
1819
1821
  return (target, context) => {
1820
1822
  if (typeof target !== "function") {
@@ -1836,7 +1838,7 @@ function Endpoint(endpoint) {
1836
1838
  );
1837
1839
  }
1838
1840
  endpointMetadata.config = config;
1839
- endpointMetadata.type = "config" /* Config */;
1841
+ endpointMetadata.type = "endpoint" /* Endpoint */;
1840
1842
  endpointMetadata.classMethod = target.name;
1841
1843
  endpointMetadata.httpMethod = config.method;
1842
1844
  endpointMetadata.url = config.url;
@@ -1852,6 +1854,11 @@ function Header(name2, value) {
1852
1854
  throw new Error("[Navios] Header decorator can only be used on methods.");
1853
1855
  }
1854
1856
  const metadata = getEndpointMetadata(target, context);
1857
+ if (metadata.type === "stream" /* Stream */) {
1858
+ throw new Error(
1859
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
1860
+ );
1861
+ }
1855
1862
  metadata.headers[name2] = value;
1856
1863
  return target;
1857
1864
  };
@@ -1866,6 +1873,11 @@ function HttpCode(code) {
1866
1873
  );
1867
1874
  }
1868
1875
  const metadata = getEndpointMetadata(target, context);
1876
+ if (metadata.type === "stream" /* Stream */) {
1877
+ throw new Error(
1878
+ "[Navios] HttpCode decorator cannot be used on stream endpoints."
1879
+ );
1880
+ }
1869
1881
  metadata.successStatusCode = code;
1870
1882
  return target;
1871
1883
  };
@@ -1902,6 +1914,37 @@ function Module(metadata) {
1902
1914
  };
1903
1915
  }
1904
1916
 
1917
+ // packages/core/src/decorators/stream.decorator.mts
1918
+ function Stream(endpoint) {
1919
+ return (target, context) => {
1920
+ if (typeof target !== "function") {
1921
+ throw new Error(
1922
+ "[Navios] Endpoint decorator can only be used on functions."
1923
+ );
1924
+ }
1925
+ if (context.kind !== "method") {
1926
+ throw new Error(
1927
+ "[Navios] Endpoint decorator can only be used on methods."
1928
+ );
1929
+ }
1930
+ const config = endpoint.config;
1931
+ if (context.metadata) {
1932
+ let endpointMetadata = getEndpointMetadata(target, context);
1933
+ if (endpointMetadata.config && endpointMetadata.config.url) {
1934
+ throw new Error(
1935
+ `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`
1936
+ );
1937
+ }
1938
+ endpointMetadata.config = config;
1939
+ endpointMetadata.type = "stream" /* Stream */;
1940
+ endpointMetadata.classMethod = target.name;
1941
+ endpointMetadata.httpMethod = config.method;
1942
+ endpointMetadata.url = config.url;
1943
+ }
1944
+ return target;
1945
+ };
1946
+ }
1947
+
1905
1948
  // packages/core/src/decorators/use-guards.decorator.mts
1906
1949
  function UseGuards(...guards) {
1907
1950
  return function(target, context) {
@@ -2203,12 +2246,18 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2203
2246
  `Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
2204
2247
  );
2205
2248
  throw new NaviosException4("Unknown endpoint type");
2206
- case "config" /* Config */:
2249
+ case "endpoint" /* Endpoint */:
2207
2250
  return this.provideHandlerForConfig(
2208
2251
  controller,
2209
2252
  executionContext,
2210
2253
  endpointMetadata
2211
2254
  );
2255
+ case "stream" /* Stream */:
2256
+ return this.provideHandlerForStream(
2257
+ controller,
2258
+ executionContext,
2259
+ endpointMetadata
2260
+ );
2212
2261
  case "handler" /* Handler */:
2213
2262
  this.logger.error("Not implemented yet");
2214
2263
  throw new NaviosException4("Not implemented yet");
@@ -2246,6 +2295,37 @@ var _ControllerAdapterService = class _ControllerAdapterService {
2246
2295
  }
2247
2296
  };
2248
2297
  }
2298
+ provideHandlerForStream(controller, executionContext, endpointMetadata) {
2299
+ return async (request, reply) => {
2300
+ getServiceLocator().registerInstance(Request, request);
2301
+ getServiceLocator().registerInstance(Reply, reply);
2302
+ getServiceLocator().registerInstance(
2303
+ ExecutionContextToken,
2304
+ executionContext
2305
+ );
2306
+ executionContext.provideRequest(request);
2307
+ executionContext.provideReply(reply);
2308
+ const controllerInstance = await inject(controller);
2309
+ try {
2310
+ const { query, params, body } = request;
2311
+ const argument = {};
2312
+ if (query && Object.keys(query).length > 0) {
2313
+ argument.params = query;
2314
+ }
2315
+ if (params && Object.keys(params).length > 0) {
2316
+ argument.urlParams = params;
2317
+ }
2318
+ if (body) {
2319
+ argument.data = body;
2320
+ }
2321
+ await controllerInstance[endpointMetadata.classMethod](argument, reply);
2322
+ } finally {
2323
+ getServiceLocator().removeInstance(Request);
2324
+ getServiceLocator().removeInstance(Reply);
2325
+ getServiceLocator().removeInstance(ExecutionContextToken);
2326
+ }
2327
+ };
2328
+ }
2249
2329
  };
2250
2330
  _init6 = __decoratorStart(null);
2251
2331
  _ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
@@ -2617,6 +2697,7 @@ export {
2617
2697
  ServiceLocatorInstanceHolderKind,
2618
2698
  ServiceLocatorInstanceHolderStatus,
2619
2699
  ServiceLocatorManager,
2700
+ Stream,
2620
2701
  UnauthorizedException,
2621
2702
  UnknownError,
2622
2703
  UseGuards,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navios/core",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "author": {
5
5
  "name": "Oleksandr Hanzha",
6
6
  "email": "alex@granted.name"
@@ -15,7 +15,7 @@
15
15
  "main": "./dist/index.js",
16
16
  "module": "./dist/index.mjs",
17
17
  "peerDependencies": {
18
- "@navios/common": "^0.1.1",
18
+ "@navios/common": "^0.1.2",
19
19
  "zod": "^3.23.8"
20
20
  },
21
21
  "exports": {
@@ -32,7 +32,7 @@
32
32
  "./package.json": "./package.json"
33
33
  },
34
34
  "devDependencies": {
35
- "@navios/common": "^0.1.1",
35
+ "@navios/common": "^0.1.2",
36
36
  "tsx": "^4.19.4",
37
37
  "typescript": "^5.8.3",
38
38
  "zod": "^3.24.4"
@@ -5,6 +5,8 @@ import type {
5
5
  } from '@navios/common'
6
6
  import type { AnyZodObject, z, ZodType } from 'zod'
7
7
 
8
+ import { ZodDiscriminatedUnion } from 'zod'
9
+
8
10
  import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
9
11
 
10
12
  export type EndpointParams<
@@ -29,6 +31,18 @@ export type EndpointParams<
29
31
  >
30
32
  : EndpointFunctionArgs<Url, undefined, undefined>
31
33
 
34
+ export type EndpointResult<
35
+ EndpointDeclaration extends {
36
+ config: BaseEndpointConfig<any, any, any, any, any>
37
+ },
38
+ > =
39
+ EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<
40
+ any,
41
+ infer Options
42
+ >
43
+ ? Promise<z.input<Options[number]>>
44
+ : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>
45
+
32
46
  export function Endpoint<
33
47
  Method extends HttpMethod = HttpMethod,
34
48
  Url extends string = string,
@@ -76,7 +90,7 @@ export function Endpoint<
76
90
  }
77
91
  // @ts-expect-error We don't need to set correctly in the metadata
78
92
  endpointMetadata.config = config
79
- endpointMetadata.type = EndpointType.Config
93
+ endpointMetadata.type = EndpointType.Endpoint
80
94
  endpointMetadata.classMethod = target.name
81
95
  endpointMetadata.httpMethod = config.method
82
96
  endpointMetadata.url = config.url
@@ -1,6 +1,6 @@
1
1
  import type { HttpHeader } from 'fastify/types/utils.js'
2
2
 
3
- import { getEndpointMetadata } from '../metadata/index.mjs'
3
+ import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
4
4
 
5
5
  export function Header(name: HttpHeader, value: string | number | string[]) {
6
6
  return <T extends Function>(
@@ -11,6 +11,12 @@ export function Header(name: HttpHeader, value: string | number | string[]) {
11
11
  throw new Error('[Navios] Header decorator can only be used on methods.')
12
12
  }
13
13
  const metadata = getEndpointMetadata(target, context)
14
+ if (metadata.type === EndpointType.Stream) {
15
+ throw new Error(
16
+ '[Navios] HttpCode decorator cannot be used on stream endpoints.',
17
+ )
18
+ }
19
+
14
20
  metadata.headers[name] = value
15
21
 
16
22
  return target
@@ -1,4 +1,4 @@
1
- import { getEndpointMetadata } from '../metadata/index.mjs'
1
+ import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
2
2
 
3
3
  export function HttpCode(code: number) {
4
4
  return <T extends Function>(
@@ -11,6 +11,11 @@ export function HttpCode(code: number) {
11
11
  )
12
12
  }
13
13
  const metadata = getEndpointMetadata(target, context)
14
+ if (metadata.type === EndpointType.Stream) {
15
+ throw new Error(
16
+ '[Navios] HttpCode decorator cannot be used on stream endpoints.',
17
+ )
18
+ }
14
19
  metadata.successStatusCode = code
15
20
 
16
21
  return target
@@ -3,4 +3,5 @@ export * from './endpoint.decorator.mjs'
3
3
  export * from './header.decorator.mjs'
4
4
  export * from './http-code.decorator.mjs'
5
5
  export * from './module.decorator.mjs'
6
+ export * from './stream.decorator.mjs'
6
7
  export * from './use-guards.decorator.mjs'
@@ -0,0 +1,81 @@
1
+ import type {
2
+ BaseStreamConfig,
3
+ EndpointFunctionArgs,
4
+ HttpMethod,
5
+ } from '@navios/common'
6
+ import type { FastifyReply } from 'fastify'
7
+ import type { AnyZodObject, ZodType } from 'zod'
8
+
9
+ import { EndpointType, getEndpointMetadata } from '../metadata/index.mjs'
10
+
11
+ export type StreamParams<
12
+ EndpointDeclaration extends {
13
+ config: BaseStreamConfig<any, any, any, any>
14
+ },
15
+ Url extends string = EndpointDeclaration['config']['url'],
16
+ QuerySchema = EndpointDeclaration['config']['querySchema'],
17
+ > = QuerySchema extends AnyZodObject
18
+ ? EndpointDeclaration['config']['requestSchema'] extends ZodType
19
+ ? EndpointFunctionArgs<
20
+ Url,
21
+ QuerySchema,
22
+ EndpointDeclaration['config']['requestSchema']
23
+ >
24
+ : EndpointFunctionArgs<Url, QuerySchema, undefined>
25
+ : EndpointDeclaration['config']['requestSchema'] extends ZodType
26
+ ? EndpointFunctionArgs<
27
+ Url,
28
+ undefined,
29
+ EndpointDeclaration['config']['requestSchema']
30
+ >
31
+ : EndpointFunctionArgs<Url, undefined, undefined>
32
+
33
+ export function Stream<
34
+ Method extends HttpMethod = HttpMethod,
35
+ Url extends string = string,
36
+ QuerySchema = undefined,
37
+ RequestSchema = ZodType,
38
+ >(endpoint: {
39
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
40
+ }) {
41
+ return (
42
+ target: (
43
+ params: QuerySchema extends AnyZodObject
44
+ ? RequestSchema extends ZodType
45
+ ? EndpointFunctionArgs<Url, QuerySchema, RequestSchema>
46
+ : EndpointFunctionArgs<Url, QuerySchema, undefined>
47
+ : RequestSchema extends ZodType
48
+ ? EndpointFunctionArgs<Url, undefined, RequestSchema>
49
+ : EndpointFunctionArgs<Url, undefined, undefined>,
50
+ reply: FastifyReply,
51
+ ) => Promise<void>,
52
+ context: ClassMethodDecoratorContext,
53
+ ) => {
54
+ if (typeof target !== 'function') {
55
+ throw new Error(
56
+ '[Navios] Endpoint decorator can only be used on functions.',
57
+ )
58
+ }
59
+ if (context.kind !== 'method') {
60
+ throw new Error(
61
+ '[Navios] Endpoint decorator can only be used on methods.',
62
+ )
63
+ }
64
+ const config = endpoint.config
65
+ if (context.metadata) {
66
+ let endpointMetadata = getEndpointMetadata(target, context)
67
+ if (endpointMetadata.config && endpointMetadata.config.url) {
68
+ throw new Error(
69
+ `[Navios] Endpoint ${config.method} ${config.url} already exists. Please use a different method or url.`,
70
+ )
71
+ }
72
+ // @ts-expect-error We don't need to set correctly in the metadata
73
+ endpointMetadata.config = config
74
+ endpointMetadata.type = EndpointType.Stream
75
+ endpointMetadata.classMethod = target.name
76
+ endpointMetadata.httpMethod = config.method
77
+ endpointMetadata.url = config.url
78
+ }
79
+ return target
80
+ }
81
+ }
@@ -1,4 +1,8 @@
1
- import type { BaseEndpointConfig, HttpMethod } from '@navios/common'
1
+ import type {
2
+ BaseEndpointConfig,
3
+ BaseStreamConfig,
4
+ HttpMethod,
5
+ } from '@navios/common'
2
6
  import type { HttpHeader } from 'fastify/types/utils.js'
3
7
 
4
8
  import type { CanActivate } from '../interfaces/index.mjs'
@@ -11,7 +15,8 @@ export const EndpointMetadataKey = Symbol('EndpointMetadataKey')
11
15
 
12
16
  export enum EndpointType {
13
17
  Unknown = 'unknown',
14
- Config = 'config',
18
+ Endpoint = 'endpoint',
19
+ Stream = 'stream',
15
20
  Handler = 'handler',
16
21
  }
17
22
 
@@ -22,7 +27,7 @@ export interface EndpointMetadata {
22
27
  type: EndpointType
23
28
  headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>
24
29
  httpMethod: HttpMethod
25
- config: BaseEndpointConfig | null
30
+ config: BaseEndpointConfig | BaseStreamConfig | null
26
31
  guards: Set<
27
32
  ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>
28
33
  >
@@ -50,7 +50,7 @@ export function extractModuleMetadata(target: ClassType): ModuleMetadata {
50
50
  const metadata = target[ModuleMetadataKey] as ModuleMetadata | undefined
51
51
  if (!metadata) {
52
52
  throw new Error(
53
- '[Navios] Module metadata not found. Make sure to use @Module decorator.',
53
+ `[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`,
54
54
  )
55
55
  }
56
56
  return metadata
@@ -1,3 +1,4 @@
1
+ import type { BaseEndpointConfig } from '@navios/common'
1
2
  import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
2
3
  import type { ZodTypeProvider } from 'fastify-type-provider-zod'
3
4
 
@@ -94,7 +95,7 @@ export class ControllerAdapterService {
94
95
  return {}
95
96
  }
96
97
  const { querySchema, requestSchema, responseSchema } =
97
- endpointMetadata.config
98
+ endpointMetadata.config as BaseEndpointConfig
98
99
  const schema: Record<string, any> = {}
99
100
  if (querySchema) {
100
101
  schema.querystring = querySchema
@@ -122,12 +123,18 @@ export class ControllerAdapterService {
122
123
  `Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`,
123
124
  )
124
125
  throw new NaviosException('Unknown endpoint type')
125
- case EndpointType.Config:
126
+ case EndpointType.Endpoint:
126
127
  return this.provideHandlerForConfig(
127
128
  controller,
128
129
  executionContext,
129
130
  endpointMetadata,
130
131
  )
132
+ case EndpointType.Stream:
133
+ return this.provideHandlerForStream(
134
+ controller,
135
+ executionContext,
136
+ endpointMetadata,
137
+ )
131
138
  case EndpointType.Handler:
132
139
  this.logger.error('Not implemented yet')
133
140
  throw new NaviosException('Not implemented yet')
@@ -174,4 +181,41 @@ export class ControllerAdapterService {
174
181
  }
175
182
  }
176
183
  }
184
+
185
+ private provideHandlerForStream(
186
+ controller: ClassType,
187
+ executionContext: ExecutionContext,
188
+ endpointMetadata: EndpointMetadata,
189
+ ): (request: FastifyRequest, reply: FastifyReply) => Promise<void> {
190
+ return async (request, reply) => {
191
+ getServiceLocator().registerInstance(Request, request)
192
+ getServiceLocator().registerInstance(Reply, reply)
193
+ getServiceLocator().registerInstance(
194
+ ExecutionContextToken,
195
+ executionContext,
196
+ )
197
+ executionContext.provideRequest(request)
198
+ executionContext.provideReply(reply)
199
+ const controllerInstance = await inject(controller)
200
+ try {
201
+ const { query, params, body } = request
202
+ const argument: Record<string, any> = {}
203
+ if (query && Object.keys(query).length > 0) {
204
+ argument.params = query
205
+ }
206
+ if (params && Object.keys(params).length > 0) {
207
+ argument.urlParams = params
208
+ }
209
+ if (body) {
210
+ argument.data = body
211
+ }
212
+
213
+ await controllerInstance[endpointMetadata.classMethod](argument, reply)
214
+ } finally {
215
+ getServiceLocator().removeInstance(Request)
216
+ getServiceLocator().removeInstance(Reply)
217
+ getServiceLocator().removeInstance(ExecutionContextToken)
218
+ }
219
+ }
220
+ }
177
221
  }