11import SwaggerUi from 'swagger-ui-browserify' ;
22import { Apis } from '/apis/collection' ;
3+ import { ProxyBackends } from '/proxy_backends/collection'
4+ import { Proxies } from '/proxies/collection'
5+
6+
7+ import _ from 'lodash' ;
38
49Template . 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