Skip to content

Commit 092d53b

Browse files
committed
replace swagger host to proxy host
1 parent 6d824db commit 092d53b

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

apis/client/view/details/details.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ Template.viewApiBackendDetails.helpers({
3333
// Get Proxy host
3434
const host = proxyBackend.apiUmbrella.frontend_host;
3535

36+
3637
// Get proxy base path
37-
const basePath = proxyBackend.apiUmbrella.url_matches[0].frontend_prefix;
38+
let basePath = ''
39+
40+
// It can be moment when proxyBackend exists but url_matches isn't
41+
if (proxyBackend.apiUmbrella.url_matches) {
42+
basePath = proxyBackend.apiUmbrella.url_matches[0].frontend_prefix
43+
}
3844

3945
// Construct the URL from host and base path
4046
url = host + basePath;

documentation/client/swaggerUI/swaggerUiContent/swaggerUiContent.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
import SwaggerUi from 'swagger-ui-browserify';
22
import { Apis } from '/apis/collection';
3+
import { ProxyBackends } from '/proxy_backends/collection'
4+
import { Proxies } from '/proxies/collection'
5+
6+
7+
import _ from 'lodash';
38

49
Template.swaggerUiContent.onCreated(function () {
510
const instance = this;
611

712
// Get URL of api documentation
813
const documentationURL = this.data.apiDoc;
914

15+
// Get proxy
16+
const proxy = Proxies.findOne();
17+
18+
// Get proxy backend
19+
const proxyBackend = ProxyBackends.findOne({"apiId": instance.data.api._id});
20+
21+
// Get proxy host if it exists
22+
let proxyHost = proxy ? proxy.apiUmbrella.url : '';
23+
24+
// Get values of proxy apiUmbrella
25+
const apiUmbrellaSettings = proxyBackend ? proxyBackend.apiUmbrella.url_matches : false;
26+
27+
// Get proxy base path if it exists
28+
let proxyBasePath = apiUmbrellaSettings ? apiUmbrellaSettings[0].frontend_prefix : '';
29+
30+
// Delete 'http://' prefix
31+
if (_.startsWith(proxyHost, 'http://')) {
32+
proxyHost = proxyHost.slice(7, proxyHost.length);
33+
}
34+
// Delete 'https://' prefix
35+
else {
36+
proxyHost = proxyHost.slice(8, proxyHost.length);
37+
}
38+
39+
// Delete last forward slash if it exists
40+
if (_.endsWith(proxyHost, '/')) {
41+
proxyHost = proxyHost.slice(0, -1);
42+
}
43+
44+
// Delete last forward slash if it exists
45+
if (_.endsWith(proxyBasePath, '/')) {
46+
proxyBasePath = proxyBasePath.slice(0, -1);
47+
}
48+
1049
// Create Swagger UI object
1150
const swagger = new SwaggerUi({
1251
url: documentationURL,
@@ -16,6 +55,18 @@ Template.swaggerUiContent.onCreated(function () {
1655
apisSorter: 'alpha',
1756
operationsSorter: 'alpha',
1857
docExpansion: 'none',
58+
onComplete: function () {
59+
if (proxyHost) {
60+
// Replace Swagger host to proxy host
61+
swagger.api.setHost(proxyHost);
62+
}
63+
64+
if (proxyBasePath) {
65+
// Replace Swagger base path to proxy base path
66+
swagger.api.setBasePath(proxyBasePath);
67+
}
68+
console.log('swagger host',swagger.api.host , 'swagger basePath', swagger.api.basePath)
69+
}
1970
});
2071

2172
// Subscribe to api collection

0 commit comments

Comments
 (0)