@navios/core 0.1.2 → 0.1.3
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.d.mts +21 -3
- package/dist/index.d.ts +21 -3
- package/dist/index.js +184 -85
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +181 -85
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/config/config.provider.mts +0 -1
- package/src/decorators/endpoint.decorator.mts +7 -2
- package/src/decorators/header.decorator.mts +18 -0
- package/src/decorators/http-code.decorator.mts +18 -0
- package/src/decorators/index.mts +2 -0
- package/src/logger/logger.service.mts +0 -1
- package/src/logger/pino-wrapper.mts +6 -5
- package/src/metadata/endpoint.metadata.mts +13 -0
- package/src/navios.application.mts +36 -1
- package/src/services/controller-adapter.service.mts +116 -71
package/dist/index.mjs
CHANGED
|
@@ -1444,7 +1444,6 @@ var _LoggerInstance = class _LoggerInstance {
|
|
|
1444
1444
|
}
|
|
1445
1445
|
static overrideLogger(logger) {
|
|
1446
1446
|
var _a, _b;
|
|
1447
|
-
console.log(logger);
|
|
1448
1447
|
if (Array.isArray(logger)) {
|
|
1449
1448
|
_LoggerInstance.logLevels = logger;
|
|
1450
1449
|
return (_b = (_a = this.staticInstanceRef) == null ? void 0 : _a.setLogLevels) == null ? void 0 : _b.call(_a, logger);
|
|
@@ -1516,8 +1515,7 @@ var PinoWrapper = class _PinoWrapper {
|
|
|
1516
1515
|
warn(message, ...optionalParams) {
|
|
1517
1516
|
this.logger.warn(message, ...optionalParams);
|
|
1518
1517
|
}
|
|
1519
|
-
info(
|
|
1520
|
-
this.logger.log(message, ...optionalParams);
|
|
1518
|
+
info() {
|
|
1521
1519
|
}
|
|
1522
1520
|
debug(message, ...optionalParams) {
|
|
1523
1521
|
var _a, _b;
|
|
@@ -1527,7 +1525,7 @@ var PinoWrapper = class _PinoWrapper {
|
|
|
1527
1525
|
var _a, _b;
|
|
1528
1526
|
(_b = (_a = this.logger).verbose) == null ? void 0 : _b.call(_a, message, ...optionalParams);
|
|
1529
1527
|
}
|
|
1530
|
-
silent(
|
|
1528
|
+
silent() {
|
|
1531
1529
|
}
|
|
1532
1530
|
child(options) {
|
|
1533
1531
|
const keys = Object.keys(options);
|
|
@@ -1546,9 +1544,9 @@ var PinoWrapper = class _PinoWrapper {
|
|
|
1546
1544
|
}
|
|
1547
1545
|
const levels = LoggerInstance["logLevels"];
|
|
1548
1546
|
if (levels) {
|
|
1549
|
-
return levels
|
|
1547
|
+
return levels.find((level) => level !== "verbose");
|
|
1550
1548
|
}
|
|
1551
|
-
return "
|
|
1549
|
+
return "warn";
|
|
1552
1550
|
}
|
|
1553
1551
|
};
|
|
1554
1552
|
|
|
@@ -1647,6 +1645,12 @@ function makeConfigToken(options) {
|
|
|
1647
1645
|
|
|
1648
1646
|
// packages/core/src/metadata/endpoint.metadata.mts
|
|
1649
1647
|
var EndpointMetadataKey = Symbol("EndpointMetadataKey");
|
|
1648
|
+
var EndpointType = /* @__PURE__ */ ((EndpointType2) => {
|
|
1649
|
+
EndpointType2["Unknown"] = "unknown";
|
|
1650
|
+
EndpointType2["Config"] = "config";
|
|
1651
|
+
EndpointType2["Handler"] = "handler";
|
|
1652
|
+
return EndpointType2;
|
|
1653
|
+
})(EndpointType || {});
|
|
1650
1654
|
function getAllEndpointMetadata(context) {
|
|
1651
1655
|
if (context.metadata) {
|
|
1652
1656
|
const metadata = context.metadata[EndpointMetadataKey];
|
|
@@ -1672,6 +1676,9 @@ function getEndpointMetadata(target, context) {
|
|
|
1672
1676
|
const newMetadata = {
|
|
1673
1677
|
classMethod: target.name,
|
|
1674
1678
|
url: "",
|
|
1679
|
+
successStatusCode: 200,
|
|
1680
|
+
headers: {},
|
|
1681
|
+
type: "unknown" /* Unknown */,
|
|
1675
1682
|
httpMethod: "GET",
|
|
1676
1683
|
config: null,
|
|
1677
1684
|
guards: /* @__PURE__ */ new Set(),
|
|
@@ -1801,6 +1808,7 @@ function Endpoint(endpoint) {
|
|
|
1801
1808
|
);
|
|
1802
1809
|
}
|
|
1803
1810
|
endpointMetadata.config = config;
|
|
1811
|
+
endpointMetadata.type = "config" /* Config */;
|
|
1804
1812
|
endpointMetadata.classMethod = target.name;
|
|
1805
1813
|
endpointMetadata.httpMethod = config.method;
|
|
1806
1814
|
endpointMetadata.url = config.url;
|
|
@@ -1809,6 +1817,32 @@ function Endpoint(endpoint) {
|
|
|
1809
1817
|
};
|
|
1810
1818
|
}
|
|
1811
1819
|
|
|
1820
|
+
// packages/core/src/decorators/header.decorator.mts
|
|
1821
|
+
function Header(name2, value) {
|
|
1822
|
+
return (target, context) => {
|
|
1823
|
+
if (context.kind !== "method") {
|
|
1824
|
+
throw new Error("[Navios] Header decorator can only be used on methods.");
|
|
1825
|
+
}
|
|
1826
|
+
const metadata = getEndpointMetadata(target, context);
|
|
1827
|
+
metadata.headers[name2] = value;
|
|
1828
|
+
return target;
|
|
1829
|
+
};
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
// packages/core/src/decorators/http-code.decorator.mts
|
|
1833
|
+
function HttpCode(code) {
|
|
1834
|
+
return (target, context) => {
|
|
1835
|
+
if (context.kind !== "method") {
|
|
1836
|
+
throw new Error(
|
|
1837
|
+
"[Navios] HttpCode decorator can only be used on methods."
|
|
1838
|
+
);
|
|
1839
|
+
}
|
|
1840
|
+
const metadata = getEndpointMetadata(target, context);
|
|
1841
|
+
metadata.successStatusCode = code;
|
|
1842
|
+
return target;
|
|
1843
|
+
};
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1812
1846
|
// packages/core/src/decorators/module.decorator.mts
|
|
1813
1847
|
function Module(metadata) {
|
|
1814
1848
|
return (target, context) => {
|
|
@@ -1918,6 +1952,9 @@ var ConflictException = class extends HttpException {
|
|
|
1918
1952
|
}
|
|
1919
1953
|
};
|
|
1920
1954
|
|
|
1955
|
+
// packages/core/src/services/controller-adapter.service.mts
|
|
1956
|
+
import { NaviosException as NaviosException2 } from "@navios/common";
|
|
1957
|
+
|
|
1921
1958
|
// packages/core/src/tokens/application.token.mts
|
|
1922
1959
|
var ApplicationInjectionToken = "ApplicationInjectionToken";
|
|
1923
1960
|
var Application = InjectionToken.create(
|
|
@@ -2061,8 +2098,8 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2061
2098
|
setupController(controller, instance, moduleMetadata) {
|
|
2062
2099
|
const controllerMetadata = extractControllerMetadata(controller);
|
|
2063
2100
|
for (const endpoint of controllerMetadata.endpoints) {
|
|
2064
|
-
const { classMethod, url, httpMethod
|
|
2065
|
-
if (!url
|
|
2101
|
+
const { classMethod, url, httpMethod } = endpoint;
|
|
2102
|
+
if (!url) {
|
|
2066
2103
|
throw new Error(
|
|
2067
2104
|
`[Navios] Malformed Endpoint ${controller.name}:${classMethod}`
|
|
2068
2105
|
);
|
|
@@ -2072,91 +2109,115 @@ var _ControllerAdapterService = class _ControllerAdapterService {
|
|
|
2072
2109
|
controllerMetadata,
|
|
2073
2110
|
endpoint
|
|
2074
2111
|
);
|
|
2075
|
-
const guards = this.guardRunner.makeContext(executionContext);
|
|
2076
|
-
const { querySchema, requestSchema, responseSchema } = config;
|
|
2077
|
-
const schema = {};
|
|
2078
|
-
if (querySchema) {
|
|
2079
|
-
schema.querystring = querySchema;
|
|
2080
|
-
}
|
|
2081
|
-
if (requestSchema) {
|
|
2082
|
-
schema.body = requestSchema;
|
|
2083
|
-
}
|
|
2084
|
-
if (responseSchema) {
|
|
2085
|
-
schema.response = {
|
|
2086
|
-
200: responseSchema
|
|
2087
|
-
};
|
|
2088
|
-
}
|
|
2089
2112
|
instance.withTypeProvider().route({
|
|
2090
2113
|
method: httpMethod,
|
|
2091
2114
|
url: url.replaceAll("$", ":"),
|
|
2092
|
-
schema,
|
|
2093
|
-
preHandler:
|
|
2094
|
-
|
|
2095
|
-
getServiceLocator().registerInstance(Request, request);
|
|
2096
|
-
getServiceLocator().registerInstance(Reply, reply);
|
|
2097
|
-
getServiceLocator().registerInstance(
|
|
2098
|
-
ExecutionContextToken,
|
|
2099
|
-
executionContext
|
|
2100
|
-
);
|
|
2101
|
-
executionContext.provideRequest(request);
|
|
2102
|
-
executionContext.provideReply(reply);
|
|
2103
|
-
const canActivate = await this.guardRunner.runGuards(
|
|
2104
|
-
guards,
|
|
2105
|
-
executionContext
|
|
2106
|
-
);
|
|
2107
|
-
getServiceLocator().removeInstance(Request);
|
|
2108
|
-
getServiceLocator().removeInstance(Reply);
|
|
2109
|
-
getServiceLocator().removeInstance(ExecutionContextToken);
|
|
2110
|
-
if (!canActivate) {
|
|
2111
|
-
return reply;
|
|
2112
|
-
}
|
|
2113
|
-
}
|
|
2114
|
-
},
|
|
2115
|
-
handler: async (request, reply) => {
|
|
2116
|
-
getServiceLocator().registerInstance(Request, request);
|
|
2117
|
-
getServiceLocator().registerInstance(Reply, reply);
|
|
2118
|
-
getServiceLocator().registerInstance(
|
|
2119
|
-
ExecutionContextToken,
|
|
2120
|
-
executionContext
|
|
2121
|
-
);
|
|
2122
|
-
executionContext.provideRequest(request);
|
|
2123
|
-
executionContext.provideReply(reply);
|
|
2124
|
-
const controllerInstance = await inject(controller);
|
|
2125
|
-
try {
|
|
2126
|
-
const { query, params, body } = request;
|
|
2127
|
-
const argument = {};
|
|
2128
|
-
if (query && Object.keys(query).length > 0) {
|
|
2129
|
-
argument.params = query;
|
|
2130
|
-
}
|
|
2131
|
-
if (params && Object.keys(params).length > 0) {
|
|
2132
|
-
argument.urlParams = params;
|
|
2133
|
-
}
|
|
2134
|
-
if (body) {
|
|
2135
|
-
argument.data = body;
|
|
2136
|
-
}
|
|
2137
|
-
const result = await controllerInstance[classMethod](argument);
|
|
2138
|
-
reply.status(200).send(result);
|
|
2139
|
-
} catch (error) {
|
|
2140
|
-
if (error instanceof HttpException) {
|
|
2141
|
-
reply.status(error.statusCode).send(error.response);
|
|
2142
|
-
} else {
|
|
2143
|
-
reply.status(500).send({
|
|
2144
|
-
message: "Internal server error",
|
|
2145
|
-
error: error.message
|
|
2146
|
-
});
|
|
2147
|
-
}
|
|
2148
|
-
} finally {
|
|
2149
|
-
getServiceLocator().removeInstance(Request);
|
|
2150
|
-
getServiceLocator().removeInstance(Reply);
|
|
2151
|
-
getServiceLocator().removeInstance(ExecutionContextToken);
|
|
2152
|
-
}
|
|
2153
|
-
}
|
|
2115
|
+
schema: this.provideSchemaForConfig(endpoint),
|
|
2116
|
+
preHandler: this.providePreHandler(executionContext),
|
|
2117
|
+
handler: this.provideHandler(controller, executionContext, endpoint)
|
|
2154
2118
|
});
|
|
2155
2119
|
this.logger.debug(
|
|
2156
2120
|
`Registered ${httpMethod} ${url} for ${controller.name}:${classMethod}`
|
|
2157
2121
|
);
|
|
2158
2122
|
}
|
|
2159
2123
|
}
|
|
2124
|
+
providePreHandler(executionContext) {
|
|
2125
|
+
const guards = this.guardRunner.makeContext(executionContext);
|
|
2126
|
+
return guards.size > 0 ? async (request, reply) => {
|
|
2127
|
+
getServiceLocator().registerInstance(Request, request);
|
|
2128
|
+
getServiceLocator().registerInstance(Reply, reply);
|
|
2129
|
+
getServiceLocator().registerInstance(
|
|
2130
|
+
ExecutionContextToken,
|
|
2131
|
+
executionContext
|
|
2132
|
+
);
|
|
2133
|
+
executionContext.provideRequest(request);
|
|
2134
|
+
executionContext.provideReply(reply);
|
|
2135
|
+
let canActivate = true;
|
|
2136
|
+
try {
|
|
2137
|
+
canActivate = await this.guardRunner.runGuards(
|
|
2138
|
+
guards,
|
|
2139
|
+
executionContext
|
|
2140
|
+
);
|
|
2141
|
+
} finally {
|
|
2142
|
+
getServiceLocator().removeInstance(Request);
|
|
2143
|
+
getServiceLocator().removeInstance(Reply);
|
|
2144
|
+
getServiceLocator().removeInstance(ExecutionContextToken);
|
|
2145
|
+
}
|
|
2146
|
+
if (!canActivate) {
|
|
2147
|
+
return reply;
|
|
2148
|
+
}
|
|
2149
|
+
} : void 0;
|
|
2150
|
+
}
|
|
2151
|
+
provideSchemaForConfig(endpointMetadata) {
|
|
2152
|
+
if (!endpointMetadata.config) {
|
|
2153
|
+
this.logger.warn(`No config found for endpoint ${endpointMetadata.url}`);
|
|
2154
|
+
return {};
|
|
2155
|
+
}
|
|
2156
|
+
const { querySchema, requestSchema, responseSchema } = endpointMetadata.config;
|
|
2157
|
+
const schema = {};
|
|
2158
|
+
if (querySchema) {
|
|
2159
|
+
schema.querystring = querySchema;
|
|
2160
|
+
}
|
|
2161
|
+
if (requestSchema) {
|
|
2162
|
+
schema.body = requestSchema;
|
|
2163
|
+
}
|
|
2164
|
+
if (responseSchema) {
|
|
2165
|
+
schema.response = {
|
|
2166
|
+
200: responseSchema
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2169
|
+
return schema;
|
|
2170
|
+
}
|
|
2171
|
+
provideHandler(controller, executionContext, endpointMetadata) {
|
|
2172
|
+
switch (endpointMetadata.type) {
|
|
2173
|
+
case "unknown" /* Unknown */:
|
|
2174
|
+
this.logger.error(
|
|
2175
|
+
`Unknown endpoint type ${endpointMetadata.type} for ${controller.name}:${endpointMetadata.classMethod}`
|
|
2176
|
+
);
|
|
2177
|
+
throw new NaviosException2("Unknown endpoint type");
|
|
2178
|
+
case "config" /* Config */:
|
|
2179
|
+
return this.provideHandlerForConfig(
|
|
2180
|
+
controller,
|
|
2181
|
+
executionContext,
|
|
2182
|
+
endpointMetadata
|
|
2183
|
+
);
|
|
2184
|
+
case "handler" /* Handler */:
|
|
2185
|
+
this.logger.error("Not implemented yet");
|
|
2186
|
+
throw new NaviosException2("Not implemented yet");
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
provideHandlerForConfig(controller, executionContext, endpointMetadata) {
|
|
2190
|
+
return async (request, reply) => {
|
|
2191
|
+
getServiceLocator().registerInstance(Request, request);
|
|
2192
|
+
getServiceLocator().registerInstance(Reply, reply);
|
|
2193
|
+
getServiceLocator().registerInstance(
|
|
2194
|
+
ExecutionContextToken,
|
|
2195
|
+
executionContext
|
|
2196
|
+
);
|
|
2197
|
+
executionContext.provideRequest(request);
|
|
2198
|
+
executionContext.provideReply(reply);
|
|
2199
|
+
const controllerInstance = await inject(controller);
|
|
2200
|
+
try {
|
|
2201
|
+
const { query, params, body } = request;
|
|
2202
|
+
const argument = {};
|
|
2203
|
+
if (query && Object.keys(query).length > 0) {
|
|
2204
|
+
argument.params = query;
|
|
2205
|
+
}
|
|
2206
|
+
if (params && Object.keys(params).length > 0) {
|
|
2207
|
+
argument.urlParams = params;
|
|
2208
|
+
}
|
|
2209
|
+
if (body) {
|
|
2210
|
+
argument.data = body;
|
|
2211
|
+
}
|
|
2212
|
+
const result = await controllerInstance[endpointMetadata.classMethod](argument);
|
|
2213
|
+
reply.status(endpointMetadata.successStatusCode).headers(endpointMetadata.headers).send(result);
|
|
2214
|
+
} finally {
|
|
2215
|
+
getServiceLocator().removeInstance(Request);
|
|
2216
|
+
getServiceLocator().removeInstance(Reply);
|
|
2217
|
+
getServiceLocator().removeInstance(ExecutionContextToken);
|
|
2218
|
+
}
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2160
2221
|
};
|
|
2161
2222
|
_init6 = __decoratorStart(null);
|
|
2162
2223
|
_ControllerAdapterService = __decorateElement(_init6, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
|
|
@@ -2313,6 +2374,7 @@ var _NaviosApplication = class _NaviosApplication {
|
|
|
2313
2374
|
}
|
|
2314
2375
|
await this.moduleLoader.loadModules(this.appModule);
|
|
2315
2376
|
this.server = await this.getFastifyInstance(this.options);
|
|
2377
|
+
this.configureFastifyInstance(this.server);
|
|
2316
2378
|
getServiceLocator().registerInstance(Application, this.server);
|
|
2317
2379
|
this.server.setValidatorCompiler(validatorCompiler);
|
|
2318
2380
|
this.server.setSerializerCompiler(serializerCompiler);
|
|
@@ -2320,6 +2382,7 @@ var _NaviosApplication = class _NaviosApplication {
|
|
|
2320
2382
|
await this.server.register(cors, this.corsOptions);
|
|
2321
2383
|
}
|
|
2322
2384
|
await this.initModules();
|
|
2385
|
+
await this.server.ready();
|
|
2323
2386
|
this.logger.debug("Navios application initialized");
|
|
2324
2387
|
}
|
|
2325
2388
|
async getFastifyInstance(rawOptions) {
|
|
@@ -2349,6 +2412,35 @@ var _NaviosApplication = class _NaviosApplication {
|
|
|
2349
2412
|
});
|
|
2350
2413
|
}
|
|
2351
2414
|
}
|
|
2415
|
+
configureFastifyInstance(fastifyInstance) {
|
|
2416
|
+
fastifyInstance.setErrorHandler((error, request, reply) => {
|
|
2417
|
+
if (error instanceof HttpException) {
|
|
2418
|
+
return reply.status(error.statusCode).send(error.response);
|
|
2419
|
+
} else {
|
|
2420
|
+
const statusCode = error.statusCode || 500;
|
|
2421
|
+
const message = error.message || "Internal Server Error";
|
|
2422
|
+
const response = {
|
|
2423
|
+
statusCode,
|
|
2424
|
+
message,
|
|
2425
|
+
error: error.name || "InternalServerError"
|
|
2426
|
+
};
|
|
2427
|
+
this.logger.error(
|
|
2428
|
+
`Error occurred: ${error.message} on ${request.url}`,
|
|
2429
|
+
error
|
|
2430
|
+
);
|
|
2431
|
+
return reply.status(statusCode).send(response);
|
|
2432
|
+
}
|
|
2433
|
+
});
|
|
2434
|
+
fastifyInstance.setNotFoundHandler((req, reply) => {
|
|
2435
|
+
const response = {
|
|
2436
|
+
statusCode: 404,
|
|
2437
|
+
message: "Not Found",
|
|
2438
|
+
error: "NotFound"
|
|
2439
|
+
};
|
|
2440
|
+
this.logger.error(`Route not found: ${req.url}`);
|
|
2441
|
+
return reply.status(404).send(response);
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2352
2444
|
async initModules() {
|
|
2353
2445
|
const modules = this.moduleLoader.getAllModules();
|
|
2354
2446
|
const promises = [];
|
|
@@ -2392,7 +2484,8 @@ var _NaviosApplication = class _NaviosApplication {
|
|
|
2392
2484
|
if (!this.server) {
|
|
2393
2485
|
throw new Error("Server is not initialized. Call init() first.");
|
|
2394
2486
|
}
|
|
2395
|
-
await this.server.listen(options);
|
|
2487
|
+
const res = await this.server.listen(options);
|
|
2488
|
+
this.logger.debug(`Navios is listening on ${res}`);
|
|
2396
2489
|
}
|
|
2397
2490
|
};
|
|
2398
2491
|
_init8 = __decoratorStart(null);
|
|
@@ -2434,6 +2527,7 @@ export {
|
|
|
2434
2527
|
ControllerMetadataKey,
|
|
2435
2528
|
Endpoint,
|
|
2436
2529
|
EndpointMetadataKey,
|
|
2530
|
+
EndpointType,
|
|
2437
2531
|
ErrorsEnum,
|
|
2438
2532
|
EventEmitter,
|
|
2439
2533
|
ExecutionContext2 as ExecutionContext,
|
|
@@ -2442,6 +2536,8 @@ export {
|
|
|
2442
2536
|
FactoryNotFound,
|
|
2443
2537
|
ForbiddenException,
|
|
2444
2538
|
GuardRunnerService,
|
|
2539
|
+
Header,
|
|
2540
|
+
HttpCode,
|
|
2445
2541
|
HttpException,
|
|
2446
2542
|
Injectable,
|
|
2447
2543
|
InjectableScope,
|