@uns-kit/api 3.0.0 → 3.0.2

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.
@@ -22,6 +22,7 @@ export default class UnsApiProxy extends UnsProxy {
22
22
  private readonly dataCatalogOffers;
23
23
  constructor(processName: string, instanceName: string, options: IApiProxyOptions);
24
24
  getProcessName(): string;
25
+ private getPublishedApiHost;
25
26
  /**
26
27
  * Unregister endpoint
27
28
  * @param topic - The API topic
@@ -95,6 +95,26 @@ export default class UnsApiProxy extends UnsProxy {
95
95
  getProcessName() {
96
96
  return this.processName;
97
97
  }
98
+ getPublishedApiHost() {
99
+ const addressInfo = this.app.server.address();
100
+ const port = addressInfo && typeof addressInfo === "object" ? addressInfo.port : undefined;
101
+ const configuredHost = this.options.publishedApiHost?.trim() || process.env.UNS_PUBLISHED_API_HOST?.trim();
102
+ if (configuredHost) {
103
+ const origin = new URL(/^[a-z][a-z\d+.-]*:\/\//i.test(configuredHost)
104
+ ? configuredHost
105
+ : `http://${configuredHost}`);
106
+ if (!origin.port && port !== undefined) {
107
+ origin.port = String(port);
108
+ }
109
+ origin.pathname = "";
110
+ origin.search = "";
111
+ origin.hash = "";
112
+ return origin.origin;
113
+ }
114
+ return port === undefined
115
+ ? `http://${App.getExternalIPv4()}`
116
+ : `http://${App.getExternalIPv4()}:${port}`;
117
+ }
98
118
  /**
99
119
  * Unregister endpoint
100
120
  * @param topic - The API topic
@@ -141,18 +161,6 @@ export default class UnsApiProxy extends UnsProxy {
141
161
  const apiPath = `${this.apiBasePrefix}${fullPath}`.replace(/\/{2,}/g, "/");
142
162
  const swaggerPath = buildSwaggerPath(this.swaggerBasePrefix, this.processName, this.instanceName);
143
163
  try {
144
- // Get ip and port from environment variables or defaults
145
- const addressInfo = this.app.server.address();
146
- let ip;
147
- let port;
148
- if (addressInfo && typeof addressInfo === "object") {
149
- ip = App.getExternalIPv4();
150
- port = addressInfo.port;
151
- }
152
- else if (typeof addressInfo === "string") {
153
- ip = App.getExternalIPv4();
154
- port = "";
155
- }
156
164
  this.registerApiEndpoint({
157
165
  timestamp: time,
158
166
  topic: topic,
@@ -160,10 +168,10 @@ export default class UnsApiProxy extends UnsProxy {
160
168
  routeOnly: options?.routeOnly === true,
161
169
  registryTopic: options?.registryTopic ??
162
170
  "api-endpoints",
163
- apiHost: `http://${ip}:${port}`,
171
+ apiHost: this.getPublishedApiHost(),
164
172
  apiEndpoint: apiPath,
165
173
  apiMethod: "GET",
166
- apiQueryParams: options.queryParams,
174
+ apiQueryParams: options?.queryParams ?? [],
167
175
  apiDescription: options?.apiDescription,
168
176
  serviceApi: options?.serviceApi ?? null,
169
177
  attributeType: UnsAttributeType.Api,
@@ -330,20 +338,9 @@ export default class UnsApiProxy extends UnsProxy {
330
338
  }
331
339
  const finalOptions = options ?? {};
332
340
  const topicNormalized = topicPrefix.endsWith("/") ? topicPrefix : `${topicPrefix}`;
333
- const addressInfo = this.app.server.address();
334
- let ip;
335
- let port;
336
- if (addressInfo && typeof addressInfo === "object") {
337
- ip = App.getExternalIPv4();
338
- port = addressInfo.port;
339
- }
340
- else if (typeof addressInfo === "string") {
341
- ip = App.getExternalIPv4();
342
- port = "";
343
- }
344
341
  const apiBase = typeof finalOptions?.apiBase === "string" && finalOptions.apiBase.length
345
342
  ? finalOptions.apiBase
346
- : `http://${ip}:${port}`;
343
+ : this.getPublishedApiHost();
347
344
  const apiBasePath = typeof finalOptions?.apiBasePath === "string" && finalOptions.apiBasePath.length
348
345
  ? finalOptions.apiBasePath
349
346
  : "/api";
@@ -428,17 +425,6 @@ export default class UnsApiProxy extends UnsProxy {
428
425
  const swaggerPath = buildSwaggerPath(this.swaggerBasePrefix, this.processName, this.instanceName);
429
426
  const methodKey = method.toLowerCase();
430
427
  try {
431
- const addressInfo = this.app.server.address();
432
- let ip;
433
- let port;
434
- if (addressInfo && typeof addressInfo === "object") {
435
- ip = App.getExternalIPv4();
436
- port = addressInfo.port;
437
- }
438
- else if (typeof addressInfo === "string") {
439
- ip = App.getExternalIPv4();
440
- port = "";
441
- }
442
428
  this.registerApiEndpoint({
443
429
  timestamp: time,
444
430
  topic,
@@ -446,7 +432,7 @@ export default class UnsApiProxy extends UnsProxy {
446
432
  routeOnly: options?.routeOnly === true,
447
433
  registryTopic: options?.registryTopic ??
448
434
  "api-endpoints",
449
- apiHost: `http://${ip}:${port}`,
435
+ apiHost: this.getPublishedApiHost(),
450
436
  apiEndpoint: apiPath,
451
437
  apiMethod: method,
452
438
  apiQueryParams: [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uns-kit/api",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
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": "3.0.0"
40
+ "@uns-kit/core": "3.0.2"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/cookie-parser": "^1.4.9",