@uns-kit/api 2.0.5 → 2.0.7

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.js CHANGED
@@ -17,6 +17,14 @@ const normalizeBasePrefix = (value) => {
17
17
  const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
18
18
  return withLeading.replace(/\/+$/, "");
19
19
  };
20
+ const buildSwaggerPath = (base, processName, instanceName) => {
21
+ const processSegment = `/${processName}`;
22
+ let baseWithProcess = base || "/";
23
+ if (!baseWithProcess.endsWith(processSegment)) {
24
+ baseWithProcess = `${baseWithProcess}${processSegment}`;
25
+ }
26
+ return `${baseWithProcess}/${instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
27
+ };
20
28
  export default class App {
21
29
  expressApplication;
22
30
  server;
@@ -37,9 +45,12 @@ export default class App {
37
45
  this.instanceName = instanceName;
38
46
  this.apiBasePrefix =
39
47
  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;
48
+ const rawSwaggerBase = normalizeBasePrefix(mountConfig?.swaggerBasePrefix ?? process.env.UNS_SWAGGER_BASE_PATH) ||
49
+ this.apiBasePrefix;
50
+ // If someone passed ".../api" as swagger base, strip that to avoid duplicated segments
51
+ this.swaggerBasePrefix = rawSwaggerBase.endsWith("/api")
52
+ ? rawSwaggerBase.replace(/\/api\/?$/, "") || "/"
53
+ : rawSwaggerBase;
43
54
  // Add context
44
55
  this.expressApplication.use((req, _res, next) => {
45
56
  req.appContext = appContext;
@@ -131,7 +142,7 @@ export default class App {
131
142
  port = "";
132
143
  }
133
144
  logger.info(`API listening on http://${ip}:${port}${this.apiBasePrefix}`);
134
- const swaggerPath = `${this.swaggerBasePrefix}/${this.processName}/${this.instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
145
+ const swaggerPath = buildSwaggerPath(this.swaggerBasePrefix, this.processName, this.instanceName);
135
146
  logger.info(`Swagger openAPI on http://${ip}:${port}${swaggerPath}`);
136
147
  this.expressApplication.get(swaggerPath, (req, res) => res.json(this.getSwaggerSpec()));
137
148
  });
@@ -22,6 +22,14 @@ const normalizeBasePrefix = (value) => {
22
22
  const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
23
23
  return withLeading.replace(/\/+$/, "");
24
24
  };
25
+ const buildSwaggerPath = (base, processName, instanceName) => {
26
+ const processSegment = `/${processName}`;
27
+ let baseWithProcess = base || "/";
28
+ if (!baseWithProcess.endsWith(processSegment)) {
29
+ baseWithProcess = `${baseWithProcess}${processSegment}`;
30
+ }
31
+ return `${baseWithProcess}/${instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
32
+ };
25
33
  export default class UnsApiProxy extends UnsProxy {
26
34
  instanceName;
27
35
  topicBuilder;
@@ -41,8 +49,10 @@ export default class UnsApiProxy extends UnsProxy {
41
49
  this.options = options;
42
50
  this.apiBasePrefix =
43
51
  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;
52
+ const rawSwaggerBase = normalizeBasePrefix(options.swaggerBasePath ?? process.env.UNS_SWAGGER_BASE_PATH) || this.apiBasePrefix;
53
+ this.swaggerBasePrefix = rawSwaggerBase.endsWith("/api")
54
+ ? rawSwaggerBase.replace(/\/api\/?$/, "") || "/"
55
+ : rawSwaggerBase;
46
56
  this.app = new App(0, processName, instanceName, undefined, {
47
57
  apiBasePrefix: this.apiBasePrefix,
48
58
  swaggerBasePrefix: this.swaggerBasePrefix,
@@ -109,7 +119,7 @@ export default class UnsApiProxy extends UnsProxy {
109
119
  const time = UnsPacket.formatToISO8601(new Date());
110
120
  const fullPath = `/${topic}${attribute}`;
111
121
  const apiPath = `${this.apiBasePrefix}${fullPath}`.replace(/\/{2,}/g, "/");
112
- const swaggerPath = `${this.swaggerBasePrefix}/${this.processName}/${this.instanceName}/swagger.json`.replace(/\/{2,}/g, "/");
122
+ const swaggerPath = buildSwaggerPath(this.swaggerBasePrefix, this.processName, this.instanceName);
113
123
  try {
114
124
  // Get ip and port from environment variables or defaults
115
125
  const addressInfo = this.app.server.address();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uns-kit/api",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
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.5"
38
+ "@uns-kit/core": "2.0.7"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jsonwebtoken": "^9.0.10",