@uns-kit/api 2.0.3 → 2.0.5

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/app.d.ts CHANGED
@@ -3,6 +3,11 @@
3
3
  */
4
4
  import { type Router } from "express";
5
5
  import * as http from "http";
6
+ type MountConfig = {
7
+ apiBasePrefix?: string;
8
+ swaggerBasePrefix?: string;
9
+ disableDefaultApiMount?: boolean;
10
+ };
6
11
  export default class App {
7
12
  private expressApplication;
8
13
  server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
@@ -10,6 +15,8 @@ export default class App {
10
15
  router: Router;
11
16
  private processName;
12
17
  private instanceName;
18
+ private apiBasePrefix;
19
+ private swaggerBasePrefix;
13
20
  swaggerSpec: {
14
21
  openapi: string;
15
22
  info: {
@@ -17,9 +24,12 @@ export default class App {
17
24
  version: string;
18
25
  };
19
26
  paths: Record<string, any>;
27
+ servers?: Array<{
28
+ url: string;
29
+ }>;
20
30
  };
21
31
  private swaggerDocs;
22
- constructor(port: number, processName: string, instanceName: string, appContext?: any);
32
+ constructor(port: number, processName: string, instanceName: string, appContext?: any, mountConfig?: MountConfig);
23
33
  static getExternalIPv4(): string | null;
24
34
  getSwaggerSpec(): {
25
35
  openapi: string;
@@ -28,7 +38,11 @@ export default class App {
28
38
  version: string;
29
39
  };
30
40
  paths: Record<string, any>;
41
+ servers?: Array<{
42
+ url: string;
43
+ }>;
31
44
  };
32
45
  registerSwaggerDoc(path: string, doc: Record<string, unknown>): void;
33
46
  start(): Promise<void>;
34
47
  }
48
+ export {};
package/dist/app.js CHANGED
@@ -8,6 +8,15 @@ import cookieParser from "cookie-parser";
8
8
  import { basePath } from "@uns-kit/core/base-path.js";
9
9
  import logger from "@uns-kit/core/logger.js";
10
10
  import os from 'os';
11
+ const normalizeBasePrefix = (value) => {
12
+ if (!value)
13
+ return "";
14
+ const trimmed = value.trim();
15
+ if (!trimmed)
16
+ return "";
17
+ const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
18
+ return withLeading.replace(/\/+$/, "");
19
+ };
11
20
  export default class App {
12
21
  expressApplication;
13
22
  server;
@@ -15,15 +24,22 @@ export default class App {
15
24
  router;
16
25
  processName;
17
26
  instanceName;
27
+ apiBasePrefix;
28
+ swaggerBasePrefix;
18
29
  swaggerSpec;
19
30
  swaggerDocs = new Map();
20
- constructor(port, processName, instanceName, appContext) {
31
+ constructor(port, processName, instanceName, appContext, mountConfig) {
21
32
  this.router = express.Router();
22
33
  this.port = port;
23
34
  this.expressApplication = express();
24
35
  this.server = http.createServer(this.expressApplication);
25
36
  this.processName = processName;
26
37
  this.instanceName = instanceName;
38
+ this.apiBasePrefix =
39
+ normalizeBasePrefix(mountConfig?.apiBasePrefix ?? process.env.UNS_API_BASE_PATH) || "/api";
40
+ this.swaggerBasePrefix =
41
+ normalizeBasePrefix(mountConfig?.swaggerBasePrefix ?? process.env.UNS_SWAGGER_BASE_PATH) ||
42
+ this.apiBasePrefix;
27
43
  // Add context
28
44
  this.expressApplication.use((req, _res, next) => {
29
45
  req.appContext = appContext;
@@ -44,7 +60,9 @@ export default class App {
44
60
  logger.info("Time: ", Date.now());
45
61
  next();
46
62
  });
47
- this.expressApplication.use("/api", this.router);
63
+ if (!mountConfig?.disableDefaultApiMount) {
64
+ this.expressApplication.use(this.apiBasePrefix, this.router);
65
+ }
48
66
  // Swagger specs
49
67
  this.swaggerSpec = {
50
68
  openapi: "3.0.0",
@@ -53,6 +71,7 @@ export default class App {
53
71
  version: "1.0.0",
54
72
  },
55
73
  paths: {},
74
+ servers: this.swaggerBasePrefix ? [{ url: this.swaggerBasePrefix }] : undefined,
56
75
  };
57
76
  }
58
77
  static getExternalIPv4() {
@@ -111,9 +130,10 @@ export default class App {
111
130
  ip = App.getExternalIPv4();
112
131
  port = "";
113
132
  }
114
- logger.info(`API listening on http://${ip}:${port}/api`);
115
- logger.info(`Swagger openAPI on http://${ip}:${port}/${this.processName}/${this.instanceName}/swagger.json`);
116
- this.expressApplication.get(`/${this.processName}/${this.instanceName}/swagger.json`, (req, res) => res.json(this.getSwaggerSpec()));
133
+ logger.info(`API listening on http://${ip}:${port}${this.apiBasePrefix}`);
134
+ const swaggerPath = `${this.swaggerBasePrefix}/${this.processName}/${this.instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
135
+ logger.info(`Swagger openAPI on http://${ip}:${port}${swaggerPath}`);
136
+ this.expressApplication.get(swaggerPath, (req, res) => res.json(this.getSwaggerSpec()));
117
137
  });
118
138
  }
119
139
  }
@@ -11,6 +11,8 @@ export default class UnsApiProxy extends UnsProxy {
11
11
  protected processStatusTopic: string;
12
12
  private app;
13
13
  private options;
14
+ private apiBasePrefix;
15
+ private swaggerBasePrefix;
14
16
  private jwksCache?;
15
17
  private catchAllRouteRegistered;
16
18
  private startedAt;
@@ -13,6 +13,15 @@ import { DataSizeMeasurements, PhysicalMeasurements } from "@uns-kit/core/uns/un
13
13
  import App from "./app.js";
14
14
  const packageJsonPath = path.join(basePath, "package.json");
15
15
  const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
16
+ const normalizeBasePrefix = (value) => {
17
+ if (!value)
18
+ return "";
19
+ const trimmed = value.trim();
20
+ if (!trimmed)
21
+ return "";
22
+ const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
23
+ return withLeading.replace(/\/+$/, "");
24
+ };
16
25
  export default class UnsApiProxy extends UnsProxy {
17
26
  instanceName;
18
27
  topicBuilder;
@@ -20,6 +29,8 @@ export default class UnsApiProxy extends UnsProxy {
20
29
  processStatusTopic;
21
30
  app;
22
31
  options;
32
+ apiBasePrefix;
33
+ swaggerBasePrefix;
23
34
  jwksCache;
24
35
  catchAllRouteRegistered = false;
25
36
  startedAt;
@@ -28,7 +39,15 @@ export default class UnsApiProxy extends UnsProxy {
28
39
  constructor(processName, instanceName, options) {
29
40
  super();
30
41
  this.options = options;
31
- this.app = new App(0, processName, instanceName);
42
+ this.apiBasePrefix =
43
+ normalizeBasePrefix(options.apiBasePath ?? process.env.UNS_API_BASE_PATH) || "/api";
44
+ this.swaggerBasePrefix =
45
+ normalizeBasePrefix(options.swaggerBasePath ?? process.env.UNS_SWAGGER_BASE_PATH) || this.apiBasePrefix;
46
+ this.app = new App(0, processName, instanceName, undefined, {
47
+ apiBasePrefix: this.apiBasePrefix,
48
+ swaggerBasePrefix: this.swaggerBasePrefix,
49
+ disableDefaultApiMount: options.disableDefaultApiMount ?? false,
50
+ });
32
51
  this.app.start();
33
52
  this.startedAt = Date.now();
34
53
  this.instanceName = instanceName;
@@ -54,7 +73,7 @@ export default class UnsApiProxy extends UnsProxy {
54
73
  */
55
74
  async unregister(topic, asset, objectType, objectId, attribute, method) {
56
75
  const fullPath = `/${topic}${attribute}`;
57
- const apiPath = `/api${fullPath}`;
76
+ const apiPath = `${this.apiBasePrefix}${fullPath}`.replace(/\/{2,}/g, "/");
58
77
  const methodKey = method.toLowerCase(); // Express stores method keys in lowercase
59
78
  // Remove route from router
60
79
  if (this.app.router?.stack) {
@@ -88,6 +107,9 @@ export default class UnsApiProxy extends UnsProxy {
88
107
  await new Promise((resolve) => setTimeout(resolve, 100));
89
108
  }
90
109
  const time = UnsPacket.formatToISO8601(new Date());
110
+ const fullPath = `/${topic}${attribute}`;
111
+ const apiPath = `${this.apiBasePrefix}${fullPath}`.replace(/\/{2,}/g, "/");
112
+ const swaggerPath = `${this.swaggerBasePrefix}/${this.processName}/${this.instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
91
113
  try {
92
114
  // Get ip and port from environment variables or defaults
93
115
  const addressInfo = this.app.server.address();
@@ -106,17 +128,16 @@ export default class UnsApiProxy extends UnsProxy {
106
128
  topic: topic,
107
129
  attribute: attribute,
108
130
  apiHost: `http://${ip}:${port}`,
109
- apiEndpoint: `/api/${topic}${attribute}`,
131
+ apiEndpoint: apiPath,
110
132
  apiMethod: "GET",
111
133
  apiQueryParams: options.queryParams,
112
134
  apiDescription: options?.apiDescription,
113
135
  attributeType: UnsAttributeType.Api,
114
- apiSwaggerEndpoint: `/${this.processName}/${this.instanceName}/swagger.json`,
136
+ apiSwaggerEndpoint: swaggerPath,
115
137
  asset,
116
138
  objectType,
117
139
  objectId
118
140
  });
119
- const fullPath = `/${topic}${attribute}`;
120
141
  const handler = (req, res) => {
121
142
  // Query param validation
122
143
  if (options?.queryParams) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uns-kit/api",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Express-powered API gateway plugin for UnsProxyProcess with JWT/JWKS support.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "cookie-parser": "^1.4.7",
36
36
  "express": "^5.1.0",
37
37
  "multer": "^2.0.2",
38
- "@uns-kit/core": "2.0.3"
38
+ "@uns-kit/core": "2.0.5"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jsonwebtoken": "^9.0.10",