Skip to content

Commit d8dab67

Browse files
committed
Paste user api-key to special field in Api key authorization
1 parent 092d53b commit d8dab67

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

documentation/client/swaggerUI/swaggerUiContent/swaggerUiContent.js

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
import SwaggerUi from 'swagger-ui-browserify';
21
import { Apis } from '/apis/collection';
3-
import { ProxyBackends } from '/proxy_backends/collection'
4-
import { Proxies } from '/proxies/collection'
5-
2+
import { ApiKeys } from '/api_keys/collection';
3+
import { ProxyBackends } from '/proxy_backends/collection';
4+
import { Proxies } from '/proxies/collection';
65

76
import _ from 'lodash';
7+
import SwaggerUi from 'swagger-ui-browserify';
8+
import SwaggerClient from 'swagger-client';
89

910
Template.swaggerUiContent.onCreated(function () {
1011
const instance = this;
11-
12+
1213
// Get URL of api documentation
1314
const documentationURL = this.data.apiDoc;
14-
15+
1516
// Get proxy
1617
const proxy = Proxies.findOne();
17-
18+
1819
// Get proxy backend
19-
const proxyBackend = ProxyBackends.findOne({"apiId": instance.data.api._id});
20-
20+
const proxyBackend = ProxyBackends.findOne({ 'apiId': instance.data.api._id });
21+
2122
// Get proxy host if it exists
2223
let proxyHost = proxy ? proxy.apiUmbrella.url : '';
23-
24+
2425
// Get values of proxy apiUmbrella
2526
const apiUmbrellaSettings = proxyBackend ? proxyBackend.apiUmbrella.url_matches : false;
26-
27+
2728
// Get proxy base path if it exists
2829
let proxyBasePath = apiUmbrellaSettings ? apiUmbrellaSettings[0].frontend_prefix : '';
2930

@@ -46,6 +47,15 @@ Template.swaggerUiContent.onCreated(function () {
4647
proxyBasePath = proxyBasePath.slice(0, -1);
4748
}
4849

50+
// Get api key collection
51+
const apiKey = ApiKeys.findOne({ proxyId: proxy._id, userId: Meteor.userId() });
52+
53+
// Check if api-key exists
54+
const apiKeyValue = apiKey ? apiKey.apiUmbrella.apiKey : 'instagram';
55+
56+
// Create object to save information about property of api-key authorization
57+
const infoAuth = {};
58+
4959
// Create Swagger UI object
5060
const swagger = new SwaggerUi({
5161
url: documentationURL,
@@ -55,7 +65,7 @@ Template.swaggerUiContent.onCreated(function () {
5565
apisSorter: 'alpha',
5666
operationsSorter: 'alpha',
5767
docExpansion: 'none',
58-
onComplete: function () {
68+
onComplete () {
5969
if (proxyHost) {
6070
// Replace Swagger host to proxy host
6171
swagger.api.setHost(proxyHost);
@@ -65,21 +75,38 @@ Template.swaggerUiContent.onCreated(function () {
6575
// Replace Swagger base path to proxy base path
6676
swagger.api.setBasePath(proxyBasePath);
6777
}
68-
console.log('swagger host',swagger.api.host , 'swagger basePath', swagger.api.basePath)
69-
}
78+
79+
// If api-key exists, add it to authorization form
80+
if (apiKeyValue) {
81+
// Search information information about property of api-key authorization
82+
_.forEach(swagger.api.auths, (authorization) => {
83+
if (authorization.type === 'apiKey') {
84+
infoAuth.title = authorization.name;
85+
infoAuth.keyName = authorization.value.name;
86+
infoAuth.keyIn = authorization.value.in;
87+
}
88+
});
89+
90+
// Create Authorization Object for swagger client
91+
const authz = new SwaggerClient.ApiKeyAuthorization(infoAuth.keyName, apiKeyValue, infoAuth.keyIn);
92+
93+
// Input user api-key in field
94+
swagger.api.clientAuthorizations.add(infoAuth.title, authz);
95+
}
96+
},
7097
});
71-
98+
7299
// Subscribe to api collection
73100
instance.autorun(() => {
74101
// Get relevant api collection
75102
instance.subscribe('apiBackend', instance.data.api._id);
76-
103+
77104
// Get api
78105
const api = Apis.findOne(instance.data.api._id);
79-
106+
80107
// Set selected methods in Swagger
81108
swagger.setOption('supportedSubmitMethods', api.submit_methods);
82-
109+
83110
// Load Swagger UI
84111
swagger.load();
85112
});

0 commit comments

Comments
 (0)