@uns-kit/api 2.0.57 → 2.0.59

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.
Files changed (3) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +157 -157
  3. package/package.json +2 -2
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Aljoša Vister
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Aljoša Vister
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,115 +1,115 @@
1
- # @uns-kit/api
2
-
3
- `@uns-kit/api` exposes Express-based HTTP endpoints for UNS deployments. The plugin attaches a `createApiProxy` method to `UnsProxyProcess`, handles JWT/JWKS access control, automatically publishes API metadata back into the Unified Namespace, and serves a Swagger UI for every registered endpoint.
4
-
5
- Note: Apps built with uns-kit are intended to be managed by the **UNS Datahub controller**.
6
- For the MQTT topic registry published by the API plugin, see `../../docs/uns-topics.md`.
7
-
8
- ## uns-kit in context
9
-
10
- | Package | Description |
11
- | --- | --- |
12
- | [`@uns-kit/core`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-core) | Base runtime (UnsProxyProcess, MQTT helpers, config tooling, gRPC gateway). |
13
- | [`@uns-kit/api`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-api) | Express plugin — HTTP endpoints, JWT/JWKS auth, Swagger, UNS metadata. |
14
- | [`@uns-kit/cron`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-cron) | Cron-driven scheduler that emits UNS events on a fixed cadence. |
15
- | [`@uns-kit/cli`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-cli) | CLI for scaffolding new UNS applications. |
16
-
17
- ## Installation
18
-
19
- ```bash
20
- pnpm add @uns-kit/api
21
- # or
22
- npm install @uns-kit/api
23
- ```
24
-
25
- `@uns-kit/core` must also be present — the plugin augments its `UnsProxyProcess` type.
26
-
27
- ## How it works
28
-
29
- 1. Import `@uns-kit/api` as a side-effect to register the plugin.
30
- 2. Call `process.createApiProxy(instanceName, options)` to start an Express server.
31
- 3. Register endpoints with `api.get(...)` or `api.post(...)`.
32
- 4. Listen to `apiGetEvent` / `apiPostEvent` to handle incoming requests.
33
-
34
- Every registered endpoint is:
35
- - Automatically secured with JWT/JWKS or a shared secret.
36
- - Published to the UNS controller as an API metadata record (topic, host, path, method).
37
- - Added to the Swagger spec served at `/<processName>/<instanceName>/swagger.json`.
38
-
39
- ## GET endpoint example
40
-
41
- ```ts
42
- import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process";
43
- import type { UnsEvents } from "@uns-kit/core";
44
- import "@uns-kit/api";
45
- import { type UnsProxyProcessWithApi } from "@uns-kit/api";
46
-
47
- const config = await ConfigFile.loadConfig();
48
- const proc = new UnsProxyProcess(config.infra.host!, { processName: config.uns.processName }) as UnsProxyProcessWithApi;
49
-
50
- const api = await proc.createApiProxy("my-service", {
51
- jwks: { wellKnownJwksUrl: config.uns.jwksWellKnownUrl },
52
- // or for simple/dev deployments: jwtSecret: "CHANGEME"
53
- });
54
-
55
- // Register a GET endpoint
56
- // Signature: api.get(topic, asset, objectType, objectId, attribute, options?)
57
- await api.get(
58
- "enterprise/site/area/line/",
59
- "line-3-furnace",
60
- "energy-resource",
61
- "main-bus",
62
- "current",
63
- {
64
- tags: ["Energy"],
65
- apiDescription: "Current reading for line-3-furnace main-bus",
66
- queryParams: [
67
- { name: "from", type: "string", required: false, description: "Start of time range (ISO 8601)", chatCanonical: "from" },
68
- { name: "to", type: "string", required: false, description: "End of time range (ISO 8601)", chatCanonical: "to" },
69
- { name: "limit", type: "number", required: false, description: "Maximum records", chatCanonical: "limit", defaultValue: 100 },
70
- ],
71
- chatDefaults: { limit: 100 },
72
- }
73
- );
74
-
75
- // Handle incoming GET requests
76
- api.event.on("apiGetEvent", (event: UnsEvents["apiGetEvent"]) => {
77
- const { from, to, limit } = event.req.query;
78
- // fetch your data here
79
- event.res.json({ status: "ok", data: [] });
80
- });
81
- ```
82
-
1
+ # @uns-kit/api
2
+
3
+ `@uns-kit/api` exposes Express-based HTTP endpoints for UNS deployments. The plugin attaches a `createApiProxy` method to `UnsProxyProcess`, handles JWT/JWKS access control, automatically publishes API metadata back into the Unified Namespace, and serves a Swagger UI for every registered endpoint.
4
+
5
+ Note: Apps built with uns-kit are intended to be managed by the **UNS Datahub controller**.
6
+ For the MQTT topic registry published by the API plugin, see `../../docs/uns-topics.md`.
7
+
8
+ ## uns-kit in context
9
+
10
+ | Package | Description |
11
+ | --- | --- |
12
+ | [`@uns-kit/core`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-core) | Base runtime (UnsProxyProcess, MQTT helpers, config tooling, gRPC gateway). |
13
+ | [`@uns-kit/api`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-api) | Express plugin — HTTP endpoints, JWT/JWKS auth, Swagger, UNS metadata. |
14
+ | [`@uns-kit/cron`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-cron) | Cron-driven scheduler that emits UNS events on a fixed cadence. |
15
+ | [`@uns-kit/cli`](https://github.com/uns-datahub/uns-kit/tree/main/packages/uns-cli) | CLI for scaffolding new UNS applications. |
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ pnpm add @uns-kit/api
21
+ # or
22
+ npm install @uns-kit/api
23
+ ```
24
+
25
+ `@uns-kit/core` must also be present — the plugin augments its `UnsProxyProcess` type.
26
+
27
+ ## How it works
28
+
29
+ 1. Import `@uns-kit/api` as a side-effect to register the plugin.
30
+ 2. Call `process.createApiProxy(instanceName, options)` to start an Express server.
31
+ 3. Register endpoints with `api.get(...)` or `api.post(...)`.
32
+ 4. Listen to `apiGetEvent` / `apiPostEvent` to handle incoming requests.
33
+
34
+ Every registered endpoint is:
35
+ - Automatically secured with JWT/JWKS or a shared secret.
36
+ - Published to the UNS controller as an API metadata record (topic, host, path, method).
37
+ - Added to the Swagger spec served at `/<processName>/<instanceName>/swagger.json`.
38
+
39
+ ## GET endpoint example
40
+
41
+ ```ts
42
+ import UnsProxyProcess from "@uns-kit/core/uns/uns-proxy-process";
43
+ import type { UnsEvents } from "@uns-kit/core";
44
+ import "@uns-kit/api";
45
+ import { type UnsProxyProcessWithApi } from "@uns-kit/api";
46
+
47
+ const config = await ConfigFile.loadConfig();
48
+ const proc = new UnsProxyProcess(config.infra.host!, { processName: config.uns.processName }) as UnsProxyProcessWithApi;
49
+
50
+ const api = await proc.createApiProxy("my-service", {
51
+ jwks: { wellKnownJwksUrl: config.uns.jwksWellKnownUrl },
52
+ // or for simple/dev deployments: jwtSecret: "CHANGEME"
53
+ });
54
+
55
+ // Register a GET endpoint
56
+ // Signature: api.get(topic, asset, objectType, objectId, attribute, options?)
57
+ await api.get(
58
+ "enterprise/site/area/line/",
59
+ "line-3-furnace",
60
+ "energy-resource",
61
+ "main-bus",
62
+ "current",
63
+ {
64
+ tags: ["Energy"],
65
+ apiDescription: "Current reading for line-3-furnace main-bus",
66
+ queryParams: [
67
+ { name: "from", type: "string", required: false, description: "Start of time range (ISO 8601)", chatCanonical: "from" },
68
+ { name: "to", type: "string", required: false, description: "End of time range (ISO 8601)", chatCanonical: "to" },
69
+ { name: "limit", type: "number", required: false, description: "Maximum records", chatCanonical: "limit", defaultValue: 100 },
70
+ ],
71
+ chatDefaults: { limit: 100 },
72
+ }
73
+ );
74
+
75
+ // Handle incoming GET requests
76
+ api.event.on("apiGetEvent", (event: UnsEvents["apiGetEvent"]) => {
77
+ const { from, to, limit } = event.req.query;
78
+ // fetch your data here
79
+ event.res.json({ status: "ok", data: [] });
80
+ });
81
+ ```
82
+
83
83
  ## POST endpoint example
84
84
 
85
85
  ```ts
86
86
  // Register a POST endpoint
87
- // Signature: api.post(topic, asset, objectType, objectId, attribute, options?)
88
- await api.post(
89
- "enterprise/site/area/line/",
90
- "line-3-furnace",
91
- "energy-resource",
92
- "main-bus",
93
- "setpoint",
94
- {
95
- tags: ["Energy"],
96
- apiDescription: "Write a new setpoint for line-3-furnace main-bus",
97
- requestBody: {
98
- description: "Setpoint payload",
99
- required: true,
100
- schema: {
101
- type: "object",
102
- required: ["value"],
103
- properties: {
104
- value: { type: "number", description: "Target setpoint value" },
105
- unit: { type: "string", description: "Unit of measurement, e.g. A" },
106
- },
107
- },
108
- },
109
- }
110
- );
111
-
112
- // Handle incoming POST requests — body is pre-parsed JSON
87
+ // Signature: api.post(topic, asset, objectType, objectId, attribute, options?)
88
+ await api.post(
89
+ "enterprise/site/area/line/",
90
+ "line-3-furnace",
91
+ "energy-resource",
92
+ "main-bus",
93
+ "setpoint",
94
+ {
95
+ tags: ["Energy"],
96
+ apiDescription: "Write a new setpoint for line-3-furnace main-bus",
97
+ requestBody: {
98
+ description: "Setpoint payload",
99
+ required: true,
100
+ schema: {
101
+ type: "object",
102
+ required: ["value"],
103
+ properties: {
104
+ value: { type: "number", description: "Target setpoint value" },
105
+ unit: { type: "string", description: "Unit of measurement, e.g. A" },
106
+ },
107
+ },
108
+ },
109
+ }
110
+ );
111
+
112
+ // Handle incoming POST requests — body is pre-parsed JSON
113
113
  api.event.on("apiPostEvent", (event: UnsEvents["apiPostEvent"]) => {
114
114
  const { value, unit } = event.req.body;
115
115
  // write/command logic here
@@ -266,52 +266,52 @@ await registerApiCatalog(api, {
266
266
 
267
267
  ```
268
268
  api.get(topic, asset, objectType, objectId, attribute, options?)
269
- api.post(topic, asset, objectType, objectId, attribute, options?)
270
- ```
271
-
272
- | Parameter | Type | Description |
273
- |---|---|---|
274
- | `topic` | `UnsTopics` | UNS topic path prefix (e.g. `"enterprise/site/area/line/"`) |
275
- | `asset` | `UnsAsset` | Asset identifier (e.g. `"line-3-furnace"`) |
276
- | `objectType` | `UnsObjectType` | UNS object type (e.g. `"energy-resource"`) |
277
- | `objectId` | `UnsObjectId` | Object instance id (e.g. `"main-bus"`) |
278
- | `attribute` | `UnsAttribute` | Attribute name (e.g. `"current"`) |
279
- | `options` | `IGetEndpointOptions` / `IPostEndpointOptions` | Tags, description, query params / request body |
280
-
281
- ## Auth options
282
-
283
- | Option | When to use |
284
- |---|---|
285
- | `jwks.wellKnownJwksUrl` | Production — verifies RS256 tokens from the UNS controller JWKS endpoint |
286
- | `jwks.activeKidUrl` | Optional companion to JWKS — narrows which key ID is active |
287
- | `jwtSecret` | Development / simple deployments — symmetric secret |
288
-
289
- Requests that fail auth return `401 Unauthorized`. Requests whose token `accessRules` do not match the endpoint path return `403 Forbidden`.
290
-
291
- ## Unregistering an endpoint
292
-
293
- ```ts
294
- await api.unregister(topic, asset, objectType, objectId, attribute, "GET");
295
- await api.unregister(topic, asset, objectType, objectId, attribute, "POST");
296
- ```
297
-
298
- This removes the route from Express, the Swagger spec, and the internal UNS endpoint registry.
299
-
300
- ## registerCatchAll (uns-api-global only)
301
-
302
- `api.registerCatchAll()` is reserved for the **uns-api-global** microservice, which acts as a
303
- catch-all gateway for an entire topic namespace. **Regular microservices must not call this** —
304
- use `api.get()` / `api.post()` for per-attribute endpoints instead.
305
-
306
- ## Scripts
307
-
308
- ```bash
309
- pnpm run typecheck
310
- pnpm run build
311
- ```
312
-
313
- `build` emits both JavaScript and type declarations to `dist/`.
314
-
315
- ## License
316
-
317
- MIT © Aljoša Vister
269
+ api.post(topic, asset, objectType, objectId, attribute, options?)
270
+ ```
271
+
272
+ | Parameter | Type | Description |
273
+ |---|---|---|
274
+ | `topic` | `UnsTopics` | UNS topic path prefix (e.g. `"enterprise/site/area/line/"`) |
275
+ | `asset` | `UnsAsset` | Asset identifier (e.g. `"line-3-furnace"`) |
276
+ | `objectType` | `UnsObjectType` | UNS object type (e.g. `"energy-resource"`) |
277
+ | `objectId` | `UnsObjectId` | Object instance id (e.g. `"main-bus"`) |
278
+ | `attribute` | `UnsAttribute` | Attribute name (e.g. `"current"`) |
279
+ | `options` | `IGetEndpointOptions` / `IPostEndpointOptions` | Tags, description, query params / request body |
280
+
281
+ ## Auth options
282
+
283
+ | Option | When to use |
284
+ |---|---|
285
+ | `jwks.wellKnownJwksUrl` | Production — verifies RS256 tokens from the UNS controller JWKS endpoint |
286
+ | `jwks.activeKidUrl` | Optional companion to JWKS — narrows which key ID is active |
287
+ | `jwtSecret` | Development / simple deployments — symmetric secret |
288
+
289
+ Requests that fail auth return `401 Unauthorized`. Requests whose token `accessRules` do not match the endpoint path return `403 Forbidden`.
290
+
291
+ ## Unregistering an endpoint
292
+
293
+ ```ts
294
+ await api.unregister(topic, asset, objectType, objectId, attribute, "GET");
295
+ await api.unregister(topic, asset, objectType, objectId, attribute, "POST");
296
+ ```
297
+
298
+ This removes the route from Express, the Swagger spec, and the internal UNS endpoint registry.
299
+
300
+ ## registerCatchAll (uns-api-global only)
301
+
302
+ `api.registerCatchAll()` is reserved for the **uns-api-global** microservice, which acts as a
303
+ catch-all gateway for an entire topic namespace. **Regular microservices must not call this** —
304
+ use `api.get()` / `api.post()` for per-attribute endpoints instead.
305
+
306
+ ## Scripts
307
+
308
+ ```bash
309
+ pnpm run typecheck
310
+ pnpm run build
311
+ ```
312
+
313
+ `build` emits both JavaScript and type declarations to `dist/`.
314
+
315
+ ## License
316
+
317
+ MIT © Aljoša Vister
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uns-kit/api",
3
- "version": "2.0.57",
3
+ "version": "2.0.59",
4
4
  "description": "Express-powered API gateway plugin for UnsProxyProcess with JWT/JWKS support.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "multer": "^2.0.2",
38
38
  "parquets": "^0.10.10",
39
39
  "uuid": "^14.0.0",
40
- "@uns-kit/core": "2.0.57"
40
+ "@uns-kit/core": "2.0.59"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/cookie-parser": "^1.4.9",