@navios/core 0.1.8 → 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.
- package/dist/_tsup-dts-rollup.d.mts +19 -2
- package/dist/_tsup-dts-rollup.d.ts +19 -2
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +85 -4
- package/dist/index.mjs +84 -4
- package/package.json +3 -3
- package/src/decorators/endpoint.decorator.mts +1 -1
- package/src/decorators/header.decorator.mts +7 -1
- package/src/decorators/http-code.decorator.mts +6 -1
- package/src/decorators/index.mts +1 -0
- package/src/decorators/stream.decorator.mts +81 -0
- package/src/metadata/endpoint.metadata.mts +8 -3
- package/src/metadata/module.metadata.mts +1 -1
- package/src/services/controller-adapter.service.mts +46 -2
|
@@ -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';
|
|
@@ -379,6 +380,7 @@ declare class ControllerAdapterService {
|
|
|
379
380
|
private provideSchemaForConfig;
|
|
380
381
|
private provideHandler;
|
|
381
382
|
private provideHandlerForConfig;
|
|
383
|
+
private provideHandlerForStream;
|
|
382
384
|
}
|
|
383
385
|
export { ControllerAdapterService }
|
|
384
386
|
export { ControllerAdapterService as ControllerAdapterService_alias_1 }
|
|
@@ -419,7 +421,7 @@ declare interface EndpointMetadata {
|
|
|
419
421
|
type: EndpointType;
|
|
420
422
|
headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>;
|
|
421
423
|
httpMethod: HttpMethod;
|
|
422
|
-
config: BaseEndpointConfig | null;
|
|
424
|
+
config: BaseEndpointConfig | BaseStreamConfig | null;
|
|
423
425
|
guards: Set<ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>>;
|
|
424
426
|
customAttributes: Map<string | symbol, any>;
|
|
425
427
|
}
|
|
@@ -448,7 +450,8 @@ export { EndpointResult as EndpointResult_alias_2 }
|
|
|
448
450
|
|
|
449
451
|
declare enum EndpointType {
|
|
450
452
|
Unknown = "unknown",
|
|
451
|
-
|
|
453
|
+
Endpoint = "endpoint",
|
|
454
|
+
Stream = "stream",
|
|
452
455
|
Handler = "handler"
|
|
453
456
|
}
|
|
454
457
|
export { EndpointType }
|
|
@@ -1445,6 +1448,20 @@ export { setPromiseCollector }
|
|
|
1445
1448
|
export { setPromiseCollector as setPromiseCollector_alias_1 }
|
|
1446
1449
|
export { setPromiseCollector as setPromiseCollector_alias_2 }
|
|
1447
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
|
+
|
|
1448
1465
|
declare const stripEndSlash: (path: string) => string;
|
|
1449
1466
|
export { stripEndSlash }
|
|
1450
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';
|
|
@@ -379,6 +380,7 @@ declare class ControllerAdapterService {
|
|
|
379
380
|
private provideSchemaForConfig;
|
|
380
381
|
private provideHandler;
|
|
381
382
|
private provideHandlerForConfig;
|
|
383
|
+
private provideHandlerForStream;
|
|
382
384
|
}
|
|
383
385
|
export { ControllerAdapterService }
|
|
384
386
|
export { ControllerAdapterService as ControllerAdapterService_alias_1 }
|
|
@@ -419,7 +421,7 @@ declare interface EndpointMetadata {
|
|
|
419
421
|
type: EndpointType;
|
|
420
422
|
headers: Partial<Record<HttpHeader, number | string | string[] | undefined>>;
|
|
421
423
|
httpMethod: HttpMethod;
|
|
422
|
-
config: BaseEndpointConfig | null;
|
|
424
|
+
config: BaseEndpointConfig | BaseStreamConfig | null;
|
|
423
425
|
guards: Set<ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>>;
|
|
424
426
|
customAttributes: Map<string | symbol, any>;
|
|
425
427
|
}
|
|
@@ -448,7 +450,8 @@ export { EndpointResult as EndpointResult_alias_2 }
|
|
|
448
450
|
|
|
449
451
|
declare enum EndpointType {
|
|
450
452
|
Unknown = "unknown",
|
|
451
|
-
|
|
453
|
+
Endpoint = "endpoint",
|
|
454
|
+
Stream = "stream",
|
|
452
455
|
Handler = "handler"
|
|
453
456
|
}
|
|
454
457
|
export { EndpointType }
|
|
@@ -1445,6 +1448,20 @@ export { setPromiseCollector }
|
|
|
1445
1448
|
export { setPromiseCollector as setPromiseCollector_alias_1 }
|
|
1446
1449
|
export { setPromiseCollector as setPromiseCollector_alias_2 }
|
|
1447
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
|
+
|
|
1448
1465
|
declare const stripEndSlash: (path: string) => string;
|
|
1449
1466
|
export { stripEndSlash }
|
|
1450
1467
|
export { stripEndSlash as stripEndSlash_alias_1 }
|
package/dist/index.d.mts
CHANGED
|
@@ -19,6 +19,8 @@ export { Header } from './_tsup-dts-rollup.mjs';
|
|
|
19
19
|
export { HttpCode } from './_tsup-dts-rollup.mjs';
|
|
20
20
|
export { Module } from './_tsup-dts-rollup.mjs';
|
|
21
21
|
export { ModuleOptions } from './_tsup-dts-rollup.mjs';
|
|
22
|
+
export { Stream } from './_tsup-dts-rollup.mjs';
|
|
23
|
+
export { StreamParams } from './_tsup-dts-rollup.mjs';
|
|
22
24
|
export { UseGuards } from './_tsup-dts-rollup.mjs';
|
|
23
25
|
export { HttpException } from './_tsup-dts-rollup.mjs';
|
|
24
26
|
export { BadRequestException } from './_tsup-dts-rollup.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export { Header } from './_tsup-dts-rollup.js';
|
|
|
19
19
|
export { HttpCode } from './_tsup-dts-rollup.js';
|
|
20
20
|
export { Module } from './_tsup-dts-rollup.js';
|
|
21
21
|
export { ModuleOptions } from './_tsup-dts-rollup.js';
|
|
22
|
+
export { Stream } from './_tsup-dts-rollup.js';
|
|
23
|
+
export { StreamParams } from './_tsup-dts-rollup.js';
|
|
22
24
|
export { UseGuards } from './_tsup-dts-rollup.js';
|
|
23
25
|
export { HttpException } from './_tsup-dts-rollup.js';
|
|
24
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["
|
|
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
|
-
|
|
1914
|
+
`[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`
|
|
1913
1915
|
);
|
|
1914
1916
|
}
|
|
1915
1917
|
return metadata;
|
|
@@ -1966,7 +1968,7 @@ function Endpoint(endpoint) {
|
|
|
1966
1968
|
);
|
|
1967
1969
|
}
|
|
1968
1970
|
endpointMetadata.config = config;
|
|
1969
|
-
endpointMetadata.type = "
|
|
1971
|
+
endpointMetadata.type = "endpoint" /* Endpoint */;
|
|
1970
1972
|
endpointMetadata.classMethod = target.name;
|
|
1971
1973
|
endpointMetadata.httpMethod = config.method;
|
|
1972
1974
|
endpointMetadata.url = config.url;
|
|
@@ -1982,6 +1984,11 @@ function Header(name2, value) {
|
|
|
1982
1984
|
throw new Error("[Navios] Header decorator can only be used on methods.");
|
|
1983
1985
|
}
|
|
1984
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
|
+
}
|
|
1985
1992
|
metadata.headers[name2] = value;
|
|
1986
1993
|
return target;
|
|
1987
1994
|
};
|
|
@@ -1996,6 +2003,11 @@ function HttpCode(code) {
|
|
|
1996
2003
|
);
|
|
1997
2004
|
}
|
|
1998
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
|
+
}
|
|
1999
2011
|
metadata.successStatusCode = code;
|
|
2000
2012
|
return target;
|
|
2001
2013
|
};
|
|
@@ -2032,6 +2044,37 @@ function Module(metadata) {
|
|
|
2032
2044
|
};
|
|
2033
2045
|
}
|
|
2034
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
|
+
|
|
2035
2078
|
// packages/core/src/decorators/use-guards.decorator.mts
|
|
2036
2079
|
function UseGuards(...guards) {
|
|
2037
2080
|
return function(target, context) {
|
|
@@ -2333,12 +2376,18 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2333
2376
|
`Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
|
|
2334
2377
|
);
|
|
2335
2378
|
throw new import_common4.NaviosException("Unknown endpoint type");
|
|
2336
|
-
case "
|
|
2379
|
+
case "endpoint" /* Endpoint */:
|
|
2337
2380
|
return this.provideHandlerForConfig(
|
|
2338
2381
|
controller,
|
|
2339
2382
|
executionContext,
|
|
2340
2383
|
endpointMetadata
|
|
2341
2384
|
);
|
|
2385
|
+
case "stream" /* Stream */:
|
|
2386
|
+
return this.provideHandlerForStream(
|
|
2387
|
+
controller,
|
|
2388
|
+
executionContext,
|
|
2389
|
+
endpointMetadata
|
|
2390
|
+
);
|
|
2342
2391
|
case "handler" /* Handler */:
|
|
2343
2392
|
this.logger.error("Not implemented yet");
|
|
2344
2393
|
throw new import_common4.NaviosException("Not implemented yet");
|
|
@@ -2376,6 +2425,37 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2376
2425
|
}
|
|
2377
2426
|
};
|
|
2378
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
|
+
}
|
|
2379
2459
|
};
|
|
2380
2460
|
_init6 = __decoratorStart(null);
|
|
2381
2461
|
_ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
|
|
@@ -2745,6 +2825,7 @@ var NaviosFactory = class {
|
|
|
2745
2825
|
ServiceLocatorInstanceHolderKind,
|
|
2746
2826
|
ServiceLocatorInstanceHolderStatus,
|
|
2747
2827
|
ServiceLocatorManager,
|
|
2828
|
+
Stream,
|
|
2748
2829
|
UnauthorizedException,
|
|
2749
2830
|
UnknownError,
|
|
2750
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["
|
|
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
|
-
|
|
1784
|
+
`[Navios] Module metadata not found for ${target.name}. Make sure to use @Module decorator.`
|
|
1784
1785
|
);
|
|
1785
1786
|
}
|
|
1786
1787
|
return metadata;
|
|
@@ -1837,7 +1838,7 @@ function Endpoint(endpoint) {
|
|
|
1837
1838
|
);
|
|
1838
1839
|
}
|
|
1839
1840
|
endpointMetadata.config = config;
|
|
1840
|
-
endpointMetadata.type = "
|
|
1841
|
+
endpointMetadata.type = "endpoint" /* Endpoint */;
|
|
1841
1842
|
endpointMetadata.classMethod = target.name;
|
|
1842
1843
|
endpointMetadata.httpMethod = config.method;
|
|
1843
1844
|
endpointMetadata.url = config.url;
|
|
@@ -1853,6 +1854,11 @@ function Header(name2, value) {
|
|
|
1853
1854
|
throw new Error("[Navios] Header decorator can only be used on methods.");
|
|
1854
1855
|
}
|
|
1855
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
|
+
}
|
|
1856
1862
|
metadata.headers[name2] = value;
|
|
1857
1863
|
return target;
|
|
1858
1864
|
};
|
|
@@ -1867,6 +1873,11 @@ function HttpCode(code) {
|
|
|
1867
1873
|
);
|
|
1868
1874
|
}
|
|
1869
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
|
+
}
|
|
1870
1881
|
metadata.successStatusCode = code;
|
|
1871
1882
|
return target;
|
|
1872
1883
|
};
|
|
@@ -1903,6 +1914,37 @@ function Module(metadata) {
|
|
|
1903
1914
|
};
|
|
1904
1915
|
}
|
|
1905
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
|
+
|
|
1906
1948
|
// packages/core/src/decorators/use-guards.decorator.mts
|
|
1907
1949
|
function UseGuards(...guards) {
|
|
1908
1950
|
return function(target, context) {
|
|
@@ -2204,12 +2246,18 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2204
2246
|
`Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
|
|
2205
2247
|
);
|
|
2206
2248
|
throw new NaviosException4("Unknown endpoint type");
|
|
2207
|
-
case "
|
|
2249
|
+
case "endpoint" /* Endpoint */:
|
|
2208
2250
|
return this.provideHandlerForConfig(
|
|
2209
2251
|
controller,
|
|
2210
2252
|
executionContext,
|
|
2211
2253
|
endpointMetadata
|
|
2212
2254
|
);
|
|
2255
|
+
case "stream" /* Stream */:
|
|
2256
|
+
return this.provideHandlerForStream(
|
|
2257
|
+
controller,
|
|
2258
|
+
executionContext,
|
|
2259
|
+
endpointMetadata
|
|
2260
|
+
);
|
|
2213
2261
|
case "handler" /* Handler */:
|
|
2214
2262
|
this.logger.error("Not implemented yet");
|
|
2215
2263
|
throw new NaviosException4("Not implemented yet");
|
|
@@ -2247,6 +2295,37 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2247
2295
|
}
|
|
2248
2296
|
};
|
|
2249
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
|
+
}
|
|
2250
2329
|
};
|
|
2251
2330
|
_init6 = __decoratorStart(null);
|
|
2252
2331
|
_ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
|
|
@@ -2618,6 +2697,7 @@ export {
|
|
|
2618
2697
|
ServiceLocatorInstanceHolderKind,
|
|
2619
2698
|
ServiceLocatorInstanceHolderStatus,
|
|
2620
2699
|
ServiceLocatorManager,
|
|
2700
|
+
Stream,
|
|
2621
2701
|
UnauthorizedException,
|
|
2622
2702
|
UnknownError,
|
|
2623
2703
|
UseGuards,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navios/core",
|
|
3
|
-
"version": "0.1.
|
|
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.
|
|
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.
|
|
35
|
+
"@navios/common": "^0.1.2",
|
|
36
36
|
"tsx": "^4.19.4",
|
|
37
37
|
"typescript": "^5.8.3",
|
|
38
38
|
"zod": "^3.24.4"
|
|
@@ -90,7 +90,7 @@ export function Endpoint<
|
|
|
90
90
|
}
|
|
91
91
|
// @ts-expect-error We don't need to set correctly in the metadata
|
|
92
92
|
endpointMetadata.config = config
|
|
93
|
-
endpointMetadata.type = EndpointType.
|
|
93
|
+
endpointMetadata.type = EndpointType.Endpoint
|
|
94
94
|
endpointMetadata.classMethod = target.name
|
|
95
95
|
endpointMetadata.httpMethod = config.method
|
|
96
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
|
package/src/decorators/index.mts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
}
|