@uns-kit/api 3.0.24 → 3.0.26
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/api-interfaces.d.ts +185 -0
- package/dist/api-interfaces.js +311 -0
- package/dist/app.d.ts +48 -0
- package/dist/app.js +148 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/routes/api.d.ts +6 -0
- package/dist/routes/api.js +44 -0
- package/dist/uns-api-plugin.d.ts +9 -0
- package/dist/uns-api-plugin.js +52 -0
- package/dist/uns-api-proxy.d.ts +81 -0
- package/dist/uns-api-proxy.js +846 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
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 };
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
}
|