@uns-kit/api 3.0.23 → 3.0.24
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/package.json +2 -2
- package/dist/api-interfaces.d.ts +0 -185
- package/dist/api-interfaces.js +0 -311
- package/dist/app.d.ts +0 -48
- package/dist/app.js +0 -148
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -5
- package/dist/routes/api.d.ts +0 -6
- package/dist/routes/api.js +0 -44
- package/dist/uns-api-plugin.d.ts +0 -9
- package/dist/uns-api-plugin.js +0 -52
- package/dist/uns-api-proxy.d.ts +0 -81
- package/dist/uns-api-proxy.js +0 -846
package/dist/app.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module dependencies.
|
|
3
|
-
*/
|
|
4
|
-
import express from "express";
|
|
5
|
-
import * as http from "http";
|
|
6
|
-
import * as path from "path";
|
|
7
|
-
import cookieParser from "cookie-parser";
|
|
8
|
-
import { basePath } from "@uns-kit/core/base-path.js";
|
|
9
|
-
import logger from "@uns-kit/core/logger.js";
|
|
10
|
-
import os from 'os';
|
|
11
|
-
import { logRequestContext } from "./request-log.js";
|
|
12
|
-
const normalizeBasePrefix = (value) => {
|
|
13
|
-
if (!value)
|
|
14
|
-
return "";
|
|
15
|
-
const trimmed = value.trim();
|
|
16
|
-
if (!trimmed)
|
|
17
|
-
return "";
|
|
18
|
-
const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
19
|
-
return withLeading.replace(/\/+$/, "");
|
|
20
|
-
};
|
|
21
|
-
const buildSwaggerPath = (base, processName, instanceName) => {
|
|
22
|
-
const processSegment = `/${processName}`;
|
|
23
|
-
let baseWithProcess = base || "/";
|
|
24
|
-
if (!baseWithProcess.endsWith(processSegment)) {
|
|
25
|
-
baseWithProcess = `${baseWithProcess}${processSegment}`;
|
|
26
|
-
}
|
|
27
|
-
return `${baseWithProcess}/${instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
|
|
28
|
-
};
|
|
29
|
-
export default class App {
|
|
30
|
-
expressApplication;
|
|
31
|
-
server;
|
|
32
|
-
port;
|
|
33
|
-
router;
|
|
34
|
-
processName;
|
|
35
|
-
instanceName;
|
|
36
|
-
apiBasePrefix;
|
|
37
|
-
swaggerBasePrefix;
|
|
38
|
-
swaggerSpec;
|
|
39
|
-
swaggerDocs = new Map();
|
|
40
|
-
constructor(port, processName, instanceName, appContext, mountConfig) {
|
|
41
|
-
this.router = express.Router();
|
|
42
|
-
this.port = port;
|
|
43
|
-
this.expressApplication = express();
|
|
44
|
-
this.server = http.createServer(this.expressApplication);
|
|
45
|
-
this.processName = processName;
|
|
46
|
-
this.instanceName = instanceName;
|
|
47
|
-
this.apiBasePrefix =
|
|
48
|
-
normalizeBasePrefix(mountConfig?.apiBasePrefix ?? process.env.UNS_API_BASE_PATH) || "/api";
|
|
49
|
-
const rawSwaggerBase = normalizeBasePrefix(mountConfig?.swaggerBasePrefix ?? process.env.UNS_SWAGGER_BASE_PATH) ||
|
|
50
|
-
this.apiBasePrefix;
|
|
51
|
-
// If someone passed ".../api" as swagger base, strip that to avoid duplicated segments
|
|
52
|
-
this.swaggerBasePrefix = rawSwaggerBase.endsWith("/api")
|
|
53
|
-
? rawSwaggerBase.replace(/\/api\/?$/, "") || "/"
|
|
54
|
-
: rawSwaggerBase;
|
|
55
|
-
// Add context
|
|
56
|
-
this.expressApplication.use((req, _res, next) => {
|
|
57
|
-
req.appContext = appContext;
|
|
58
|
-
next();
|
|
59
|
-
});
|
|
60
|
-
// Body parser (req.body)
|
|
61
|
-
this.expressApplication.use(express.json());
|
|
62
|
-
this.expressApplication.use(express.urlencoded({ extended: false }));
|
|
63
|
-
// Add cookie parser
|
|
64
|
-
this.expressApplication.use(cookieParser());
|
|
65
|
-
// Static / public folder
|
|
66
|
-
const publicHome = process.env.PUBLIC_HOME === null || process.env.PUBLIC_HOME === undefined
|
|
67
|
-
? "public"
|
|
68
|
-
: process.env.PUBLIC_HOME;
|
|
69
|
-
this.expressApplication.use(express.static(path.join(basePath, publicHome)));
|
|
70
|
-
// Map routes
|
|
71
|
-
this.router.use(logRequestContext);
|
|
72
|
-
if (!mountConfig?.disableDefaultApiMount) {
|
|
73
|
-
this.expressApplication.use(this.apiBasePrefix, this.router);
|
|
74
|
-
}
|
|
75
|
-
// Swagger specs
|
|
76
|
-
this.swaggerSpec = {
|
|
77
|
-
openapi: "3.0.0",
|
|
78
|
-
info: {
|
|
79
|
-
title: "UNS API",
|
|
80
|
-
version: "1.0.0",
|
|
81
|
-
},
|
|
82
|
-
paths: {},
|
|
83
|
-
servers: this.swaggerBasePrefix ? [{ url: this.swaggerBasePrefix }] : undefined,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
static getExternalIPv4() {
|
|
87
|
-
const interfaces = os.networkInterfaces();
|
|
88
|
-
for (const name of Object.keys(interfaces)) {
|
|
89
|
-
for (const iface of interfaces[name] || []) {
|
|
90
|
-
if (iface.family === 'IPv4' && !iface.internal) {
|
|
91
|
-
return iface.address;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
getSwaggerSpec() {
|
|
98
|
-
return this.swaggerSpec;
|
|
99
|
-
}
|
|
100
|
-
registerSwaggerDoc(path, doc) {
|
|
101
|
-
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
102
|
-
this.swaggerDocs.set(normalizedPath, doc);
|
|
103
|
-
this.expressApplication.get(normalizedPath, (_req, res) => res.json(doc));
|
|
104
|
-
}
|
|
105
|
-
async start() {
|
|
106
|
-
// Listen on provided port, on all network interfaces.
|
|
107
|
-
this.server.listen(this.port);
|
|
108
|
-
this.server.on("error", (error) => {
|
|
109
|
-
if (error.syscall !== "listen") {
|
|
110
|
-
throw error;
|
|
111
|
-
}
|
|
112
|
-
const bind = typeof this.port === "string"
|
|
113
|
-
? `Pipe ${this.port}`
|
|
114
|
-
: `Port ${this.port}`;
|
|
115
|
-
// handle specific listen errors with friendly messages
|
|
116
|
-
switch (error.code) {
|
|
117
|
-
case "EACCES":
|
|
118
|
-
logger.error(`${bind} requires elevated privileges`);
|
|
119
|
-
process.exit(1);
|
|
120
|
-
break;
|
|
121
|
-
case "EADDRINUSE":
|
|
122
|
-
logger.error(`${bind} is already in use`);
|
|
123
|
-
process.exit(1);
|
|
124
|
-
break;
|
|
125
|
-
default:
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
this.server.on("listening", () => {
|
|
130
|
-
App.bind(this);
|
|
131
|
-
const addressInfo = this.server.address();
|
|
132
|
-
let ip;
|
|
133
|
-
let port;
|
|
134
|
-
if (addressInfo && typeof addressInfo === "object") {
|
|
135
|
-
ip = App.getExternalIPv4();
|
|
136
|
-
port = addressInfo.port;
|
|
137
|
-
}
|
|
138
|
-
else if (typeof addressInfo === "string") {
|
|
139
|
-
ip = App.getExternalIPv4();
|
|
140
|
-
port = "";
|
|
141
|
-
}
|
|
142
|
-
logger.info(`API listening on http://${ip}:${port}${this.apiBasePrefix}`);
|
|
143
|
-
const swaggerPath = buildSwaggerPath(this.swaggerBasePrefix, this.processName, this.instanceName);
|
|
144
|
-
logger.info(`Swagger openAPI on http://${ip}:${port}${swaggerPath}`);
|
|
145
|
-
this.expressApplication.get(swaggerPath, (req, res) => res.json(this.getSwaggerSpec()));
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/routes/api.d.ts
DELETED
package/dist/routes/api.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import multer from "multer";
|
|
3
|
-
import logger from "@uns-kit/core/logger.js";
|
|
4
|
-
export default class Api {
|
|
5
|
-
router;
|
|
6
|
-
upload;
|
|
7
|
-
constructor() {
|
|
8
|
-
const storage = multer.diskStorage({
|
|
9
|
-
destination: function (req, file, cb) {
|
|
10
|
-
cb(null, "tmp/");
|
|
11
|
-
},
|
|
12
|
-
filename: function (req, file, cb) {
|
|
13
|
-
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
|
|
14
|
-
cb(null, file.fieldname + "-" + uniqueSuffix + ".jpg");
|
|
15
|
-
},
|
|
16
|
-
});
|
|
17
|
-
this.upload = multer({ storage: storage });
|
|
18
|
-
this.router = express.Router();
|
|
19
|
-
/**
|
|
20
|
-
* Open for all
|
|
21
|
-
*
|
|
22
|
-
* Example post request
|
|
23
|
-
*/
|
|
24
|
-
this.router.post("/call", async function (req, res) {
|
|
25
|
-
try {
|
|
26
|
-
const appContext = req.appContext;
|
|
27
|
-
res.send("OK");
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
res.status(400).send("Error");
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
/**
|
|
34
|
-
* Open for all
|
|
35
|
-
*
|
|
36
|
-
* Upload files
|
|
37
|
-
*/
|
|
38
|
-
this.router.post("/upload", this.upload.single("file"), function (req, res) {
|
|
39
|
-
const appContext = req.appContext;
|
|
40
|
-
logger.info(req.file);
|
|
41
|
-
res.send("Successfully uploaded files");
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
package/dist/uns-api-plugin.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { IApiProxyOptions } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
2
|
-
import UnsProxyProcess, { type UnsProxyProcessPlugin } from "@uns-kit/core/uns/uns-proxy-process.js";
|
|
3
|
-
import UnsApiProxy from "./uns-api-proxy.js";
|
|
4
|
-
declare const unsApiPlugin: UnsProxyProcessPlugin;
|
|
5
|
-
export default unsApiPlugin;
|
|
6
|
-
export { UnsApiProxy };
|
|
7
|
-
export type UnsProxyProcessWithApi = UnsProxyProcess & {
|
|
8
|
-
createApiProxy(instanceName: string, options: IApiProxyOptions): Promise<UnsApiProxy>;
|
|
9
|
-
};
|
package/dist/uns-api-plugin.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process.js";
|
|
2
|
-
import UnsApiProxy from "./uns-api-proxy.js";
|
|
3
|
-
import { UnsPacket } from "@uns-kit/core/uns/uns-packet.js";
|
|
4
|
-
const apiProxyRegistry = new WeakMap();
|
|
5
|
-
const getApiProxies = (instance) => {
|
|
6
|
-
let proxies = apiProxyRegistry.get(instance);
|
|
7
|
-
if (!proxies) {
|
|
8
|
-
proxies = [];
|
|
9
|
-
apiProxyRegistry.set(instance, proxies);
|
|
10
|
-
}
|
|
11
|
-
return proxies;
|
|
12
|
-
};
|
|
13
|
-
const unsApiPlugin = ({ define }) => {
|
|
14
|
-
define({
|
|
15
|
-
async createApiProxy(instanceName, options) {
|
|
16
|
-
await this.waitForProcessConnection();
|
|
17
|
-
const internals = this;
|
|
18
|
-
const unsApiProxy = new UnsApiProxy(internals.processName, instanceName, options);
|
|
19
|
-
unsApiProxy.event.on("unsProxyProducedTopics", (event) => {
|
|
20
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedTopics));
|
|
21
|
-
});
|
|
22
|
-
unsApiProxy.event.on("unsProxyProducedApiEndpoints", (event) => {
|
|
23
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedApiEndpoints));
|
|
24
|
-
});
|
|
25
|
-
unsApiProxy.event.on("unsProxyProducedServiceEndpoints", (event) => {
|
|
26
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedServiceEndpoints));
|
|
27
|
-
});
|
|
28
|
-
unsApiProxy.event.on("unsProxyProducedDataOfferEndpoints", (event) => {
|
|
29
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedDataOfferEndpoints));
|
|
30
|
-
});
|
|
31
|
-
unsApiProxy.event.on("unsProxyProducedApiCatchAll", (event) => {
|
|
32
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedCatchall));
|
|
33
|
-
});
|
|
34
|
-
unsApiProxy.event.on("unsProxyProducedDataCatalogOffers", (event) => {
|
|
35
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(event.producedDataCatalogOffers));
|
|
36
|
-
});
|
|
37
|
-
unsApiProxy.event.on("mqttProxyStatus", (event) => {
|
|
38
|
-
const time = UnsPacket.formatToISO8601(new Date());
|
|
39
|
-
const unsMessage = { data: { time, value: event.value, uom: event.uom } };
|
|
40
|
-
UnsPacket.unsPacketFromUnsMessage(unsMessage).then((packet) => {
|
|
41
|
-
internals.processMqttProxy.publish(event.statusTopic, JSON.stringify(packet));
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
internals.unsApiProxies.push(unsApiProxy);
|
|
45
|
-
getApiProxies(this).push(unsApiProxy);
|
|
46
|
-
return unsApiProxy;
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
UnsProxyProcess.use(unsApiPlugin);
|
|
51
|
-
export default unsApiPlugin;
|
|
52
|
-
export { UnsApiProxy };
|
package/dist/uns-api-proxy.d.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { UnsAttribute } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
2
|
-
import UnsProxy from "@uns-kit/core/uns/uns-proxy.js";
|
|
3
|
-
import { UnsTopics } from "@uns-kit/core/uns/uns-topics.js";
|
|
4
|
-
import { IApiProxyOptions, IGetEndpointOptions, IPostEndpointOptions } from "@uns-kit/core/uns/uns-interfaces.js";
|
|
5
|
-
import { UnsAsset } from "@uns-kit/core/uns/uns-asset.js";
|
|
6
|
-
import { UnsObjectType, UnsObjectId } from "@uns-kit/core/uns/uns-object.js";
|
|
7
|
-
import type { DataCatalogOfferRegistration } from "./api-interfaces.js";
|
|
8
|
-
export default class UnsApiProxy extends UnsProxy {
|
|
9
|
-
instanceName: string;
|
|
10
|
-
private topicBuilder;
|
|
11
|
-
private processName;
|
|
12
|
-
protected processStatusTopic: string;
|
|
13
|
-
private app;
|
|
14
|
-
private options;
|
|
15
|
-
private apiBasePrefix;
|
|
16
|
-
private swaggerBasePrefix;
|
|
17
|
-
private jwksCache?;
|
|
18
|
-
private catchAllRouteRegistered;
|
|
19
|
-
private startedAt;
|
|
20
|
-
private statusInterval;
|
|
21
|
-
private readonly statusIntervalMs;
|
|
22
|
-
private readonly dataCatalogOffers;
|
|
23
|
-
constructor(processName: string, instanceName: string, options: IApiProxyOptions);
|
|
24
|
-
getProcessName(): string;
|
|
25
|
-
private getPublishedApiHost;
|
|
26
|
-
/**
|
|
27
|
-
* Unregister endpoint
|
|
28
|
-
* @param topic - The API topic
|
|
29
|
-
* @param attribute - The attribute for the topic.
|
|
30
|
-
* @param method - The HTTP method (e.g., "GET", "POST", "PUT", "DELETE").
|
|
31
|
-
*/
|
|
32
|
-
unregister(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, method: "GET" | "POST" | "PUT" | "DELETE"): Promise<void>;
|
|
33
|
-
/**
|
|
34
|
-
* Register a GET endpoint with optional JWT path filter.
|
|
35
|
-
* @param topic - The API topic
|
|
36
|
-
* @param attribute - The attribute for the topic.
|
|
37
|
-
* @param options.description - Optional description.
|
|
38
|
-
* @param options.tags - Optional tags.
|
|
39
|
-
*/
|
|
40
|
-
get(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, options?: IGetEndpointOptions): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* Register a catch-all API mapping for a topic prefix (e.g., "sij/acroni/#").
|
|
43
|
-
* Does not create individual API attribute nodes; the controller treats this as a fallback.
|
|
44
|
-
*
|
|
45
|
-
* This is intended for use by the uns-api-global microservice, which acts as a
|
|
46
|
-
* catch-all gateway for an entire topic namespace. Regular microservices should
|
|
47
|
-
* NOT call this — use registerGetEndpoint() for individual attribute endpoints instead.
|
|
48
|
-
*/
|
|
49
|
-
registerCatchAll(topicPrefix: string, options?: {
|
|
50
|
-
apiBase?: string;
|
|
51
|
-
apiBasePath?: string;
|
|
52
|
-
swaggerPath?: string;
|
|
53
|
-
swaggerDoc?: Record<string, unknown>;
|
|
54
|
-
apiDescription?: string;
|
|
55
|
-
tags?: string[];
|
|
56
|
-
queryParams?: IGetEndpointOptions["queryParams"];
|
|
57
|
-
}): Promise<void>;
|
|
58
|
-
post(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, options?: IPostEndpointOptions): Promise<void>;
|
|
59
|
-
put(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, options?: IPostEndpointOptions): Promise<void>;
|
|
60
|
-
patch(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, options?: IPostEndpointOptions): Promise<void>;
|
|
61
|
-
delete(topic: UnsTopics, asset: UnsAsset, objectType: UnsObjectType, objectId: UnsObjectId, attribute: UnsAttribute, options?: IPostEndpointOptions): Promise<void>;
|
|
62
|
-
private registerMutationEndpoint;
|
|
63
|
-
registerDataOffer(input: DataCatalogOfferRegistration): void;
|
|
64
|
-
private emitStatusMetrics;
|
|
65
|
-
private registerHealthEndpoint;
|
|
66
|
-
private getApiEventName;
|
|
67
|
-
private extractBearerToken;
|
|
68
|
-
private getPublicKeyFromJwks;
|
|
69
|
-
private fetchJwksKeys;
|
|
70
|
-
private certFromX5c;
|
|
71
|
-
stop(): Promise<void>;
|
|
72
|
-
private publishDataCatalogOffer;
|
|
73
|
-
private emitDataCatalogOffers;
|
|
74
|
-
private normalizeDataOfferOperation;
|
|
75
|
-
private normalizeDataOfferParameter;
|
|
76
|
-
private normalizeDataOfferRequestBody;
|
|
77
|
-
private normalizeDataOfferResponse;
|
|
78
|
-
private normalizeDataOfferSchema;
|
|
79
|
-
private normalizeDataOfferSchemaField;
|
|
80
|
-
private resolveOfferSchemas;
|
|
81
|
-
}
|