Skip to content

Commit 8eba61f

Browse files
authored
Merge pull request #1468 from apinf/feature/add-setting-for-sdk
Add a setting for SDK download
2 parents 9b1e237 + ab14585 commit 8eba61f

File tree

8 files changed

+117
-41
lines changed

8 files changed

+117
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ node_modules/
1313
packages
1414
node_modules
1515
npm-debug.log
16+
.eslintrc.json
Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { HTTP } from 'meteor/http';
22

3+
import _ from 'lodash';
4+
35
AutoForm.addHooks('downloadSDK', {
4-
onSubmit: function (formValues, updateDoc, instance) {
6+
onSubmit (formValues, updateDoc, instance) {
57
// Prevent form from submitting
68
this.event.preventDefault();
79

@@ -14,7 +16,13 @@ AutoForm.addHooks('downloadSDK', {
1416
// Find mask of the language for url
1517
const parameter = instance.urlParameters[index];
1618

17-
const host = 'https://generator.swagger.io';
19+
// Get host of code generator server
20+
let host = instance.codegenServer;
21+
22+
// Delete last forward slash if it exists
23+
if (_.endsWith(host, '/')) {
24+
host = host.slice(0, -1);
25+
}
1826

1927
// Create URL to send request
2028
const url = host + '/api/gen/clients/' + parameter;
@@ -24,31 +32,35 @@ AutoForm.addHooks('downloadSDK', {
2432

2533
// Create POST options
2634
const options = {
27-
'swaggerUrl': pathToFile
35+
'swaggerUrl': pathToFile,
2836
};
2937

3038
// Start spinner when send request
3139
instance.callRequest.set(true);
3240

3341
// Send POST request
3442
HTTP.post(url, { data: options }, function (error, result) {
35-
// Get information from Swagger API response
36-
let response = JSON.parse(result.content);
37-
38-
if (result.statusCode === 200) {
39-
// Hide modal
40-
Modal.hide('sdkCodeGeneratorModal');
41-
42-
// Go to link and download file
43-
window.location.href = response.link;
43+
// If url is incorrect
44+
if (result === undefined) {
45+
FlashMessages.sendError(TAPi18n.__('sdkCodeGeneratorModal_errorTextInvalidHost'));
4446
} else {
45-
$('button').removeAttr('disabled');
47+
// Get information from Swagger API response
48+
const response = JSON.parse(result.content);
49+
50+
if (result.statusCode === 200) {
51+
// Hide modal
52+
Modal.hide('sdkCodeGeneratorModal');
4653

47-
// Otherwise show an error message
48-
FlashMessages.sendError(TAPi18n.__('sdkCodeGeneratorModal_errorText'));
54+
// Go to link and download file
55+
window.location.href = response.link;
56+
} else {
57+
// Otherwise show an error message
58+
FlashMessages.sendError(TAPi18n.__('sdkCodeGeneratorModal_errorText'));
59+
}
4960
}
61+
$('button').removeAttr('disabled');
5062
// Finish spinner
5163
instance.callRequest.set(false);
5264
});
53-
}
65+
},
5466
});

documentation/client/codegenerator/codegenerator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ Template.sdkCodeGeneratorModal.helpers({
9898

9999
// Create object with instance varaibles
100100
return {
101-
'apiBackend': instance.data.apiBackend,
102-
'callRequest': instance.callRequest,
103-
'documentationFileURL': instance.documentationFileURL,
104-
'languageList': instance.languageList,
105-
'urlParameters': instance.urlParameters
101+
codegenServer: instance.data.host,
102+
callRequest: instance.callRequest,
103+
documentationFileURL: instance.documentationFileURL,
104+
languageList: instance.languageList,
105+
urlParameters: instance.urlParameters
106106
};
107107
}
108108
});

documentation/client/view/documentation.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
</button>
1111
{{/if}}
1212

13-
{{#if documentationExists }}
13+
{{#if Template.subscriptionsReady }}
14+
{{#if codegenServerExists }}
1415
<button
1516
id="sdk-code-generator"
1617
class="btn btn-info btn-xs pull-right"
@@ -19,7 +20,7 @@
1920
{{_ "documentation_sdkGenerateButton" }}
2021
</button>
2122
{{/if}}
22-
23+
{{/if}}
2324
<h2 class="panel-title clearfix">
2425
{{_ "documentation_Title" }}
2526
</h2>
@@ -61,18 +62,18 @@ <h2 class="panel-title">{{_ "documentation_Link_Title" }}</h2>
6162
<h2 class="panel-title">{{_ "documentation_Viewer_Title" }}</h2>
6263
</div>
6364
<div class="panel-body">
64-
{{ # if Template.subscriptionsReady }}
65-
{{ # if documentationExists }}
65+
{{#if Template.subscriptionsReady }}
66+
{{#if documentationExists }}
6667
<iframe src="/swagger/index.html?url={{uploadedDocumentationLink}}" width="100%" id="documentation"></iframe>
6768
{{ else }}
6869
<i>
6970
{{_ "documentation_No_Swagger_Message" }}
70-
{{ # if apiBackend.currentUserCanEdit }}
71+
{{#if apiBackend.currentUserCanEdit }}
7172
{{_ "documentation_No_Swagger_Manager_Message" }}
72-
{{ / if}}
73+
{{/if}}
7374
</i>
74-
{{ / if}}
75-
{{ / if }}
75+
{{/if}}
76+
{{/if }}
7677
</div>
7778
</div>
7879
</div>
Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { DocumentationFiles } from '/documentation/collection/collection';
22

3-
Template.documentation.onCreated(function(){
3+
Template.documentation.onCreated(function () {
44
const instance = this;
55

66
// Run subscription in autorun
77
instance.autorun(() => {
8-
98
// Get current documentation file Id
109
const documentationFileId = Template.currentData().apiBackend.documentationFileId;
1110

@@ -14,15 +13,17 @@ Template.documentation.onCreated(function(){
1413
instance.subscribe('singleDocumentationFile', documentationFileId);
1514
}
1615
});
16+
17+
// Subscribe to code generator settings
18+
instance.subscribe('singleSetting', 'sdkCodeGenerator');
1719
});
1820

1921
Template.documentation.onRendered(function () {
2022
$('[data-toggle="popover"]').popover();
2123
});
2224

2325
Template.documentation.helpers({
24-
uploadedDocumentationLink: function() {
25-
26+
uploadedDocumentationLink () {
2627
const currentDocumentationFileId = this.apiBackend.documentationFileId;
2728

2829
// Convert to Mongo ObjectID
@@ -37,36 +38,58 @@ Template.documentation.helpers({
3738
const currentDocumentationFileId = currentDocumentationFile._id;
3839

3940
// Get documentation file URL
40-
return Meteor.absoluteUrl().slice(0, -1) + DocumentationFiles.baseURL + "/id/" + currentDocumentationFileId;
41+
return Meteor.absoluteUrl().slice(0, -1) + DocumentationFiles.baseURL + '/id/' + currentDocumentationFileId;
4142
}
4243
},
43-
documentationLink: function() {
44+
documentationLink () {
4445
// get documentation link
4546
const documentationLink = this.apiBackend.documentation_link;
4647
// check if exists
4748
if (documentationLink) {
48-
return documentationLink
49+
return documentationLink;
4950
}
5051
},
51-
documentationExists: function () {
52+
documentationExists () {
5253
const currentApiBackend = this.apiBackend;
5354
if (currentApiBackend.documentationFileId) {
5455
return true;
5556
}
57+
},
58+
codegenServerExists () {
59+
// Get template instance
60+
const instance = Template.instance();
61+
62+
// Get documentation file
63+
const apiDocumentation = this.apiBackend.documentationFileId;
64+
65+
// Get settings
66+
const settings = Settings.findOne();
67+
68+
// Check documentation exists, generator is enabled and host setting exists
69+
if (settings && apiDocumentation && settings.sdkCodeGenerator.host && settings.sdkCodeGenerator.enabled) {
70+
// Get code generator host
71+
instance.codegenServer = settings.sdkCodeGenerator.host;
72+
73+
// Generator is enabled and has host setting, return true
74+
return true;
75+
}
5676
}
77+
5778
});
5879

5980
Template.documentation.events({
6081
'click #manage-api-documentation' (event, instance) {
6182
// Get reference to API backend
62-
const apiBackend = instance.data.apiBackend
83+
const apiBackend = instance.data.apiBackend;
6384
// Show the manage API documentation form
64-
Modal.show('manageApiDocumentationModal', { apiBackend })
85+
Modal.show('manageApiDocumentationModal', { apiBackend });
6586
},
6687
'click #sdk-code-generator' (event, instance) {
6788
// Get reference to API backend
6889
const apiBackend = instance.data.apiBackend;
90+
// Get reference to Code Generator host
91+
const host = instance.codegenServer;
6992
// Show the SDK Code generator form
70-
Modal.show('sdkCodeGeneratorModal', { apiBackend });
93+
Modal.show('sdkCodeGeneratorModal', { apiBackend, host });
7194
}
7295
});

lib/i18n/en.i18n.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@
422422
}
423423
},
424424
"sdkCodeGeneratorModal_errorText": "Your file is not supported by the generator. Please upload the correct file",
425+
"sdkCodeGeneratorModal_errorTextInvalidHost": "Code Generator server is incorrect. Please report to administrator about this problem.",
425426
"sdkCodeGeneratorModal_Title": "Generate SDK file",
426427
"searchField_button": "Search",
427428
"searchField_placeholder": "Search...",
@@ -438,6 +439,7 @@
438439
"settings_save": "Save",
439440
"settings_apiUmbrella": "API Umbrella",
440441
"settings_elasticsearch": "Elasticsearch",
442+
"settings_sdkCode_generator": "SDK Code Generator",
441443
"settingsWizard_APIDocumentation_Editor": "Api Documentation Editor",
442444
"settingsWizard_APIUmbrella_settings": "Settings for API Umbrella",
443445
"settingsWizard_Finish": "Finish",

settings/client/settings.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="container">
33
<div class="row" id="champion-messages">
44
<div class="col-md-3 settings-menu">
5-
{{ > settingsMenu }}
5+
{{> settingsMenu }}
66
</div>
77
{{#autoForm collection="Settings" doc=editDoc id="settings" type=formType}}
88
<div class="col-md-6 settings-fields">
@@ -17,6 +17,17 @@
1717
{{/if}}
1818
</div>
1919
</div>
20+
<div class="panel panel-default">
21+
<div class="panel-heading">
22+
<span class="panel-title">{{_ "settings_sdkCode_generator"}}</span>
23+
<span class="pull-right">{{> afQuickField name='sdkCodeGenerator.enabled'}}</span>
24+
</div>
25+
<div class="panel-body">
26+
{{#if afFieldValueIs name="sdkCodeGenerator.enabled" value=true}}
27+
{{> afQuickField name='sdkCodeGenerator.host'}}
28+
{{/if}}
29+
</div>
30+
</div>
2031
<div class="panel panel-default">
2132
<div class="panel-heading">
2233
<span class="panel-title">{{_ "settings_mail"}}</span>

settings/collection/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ Schemas.SettingsSchema = new SimpleSchema({
155155
initialSetupComplete: {
156156
type: Boolean,
157157
optional: true
158+
},
159+
sdkCodeGenerator: {
160+
type: Object,
161+
optional: true
162+
},
163+
"sdkCodeGenerator.enabled": {
164+
type: Boolean,
165+
optional: true
166+
},
167+
"sdkCodeGenerator.host": {
168+
type: String,
169+
regEx: SimpleSchema.RegEx.Url,
170+
label: "Host",
171+
optional: true,
172+
autoform: {
173+
placeholder: 'https://generator.example.com/'
174+
},
175+
custom: function () {
176+
let sdkCodeGeneratorEnabled = this.field("sdkCodeGenerator.enabled").value;
177+
let sdkCodeGeneratorHost = this.value;
178+
179+
// Require code generator host if sdkCodeGenerator.enabled is checked
180+
if (sdkCodeGeneratorEnabled === true && !sdkCodeGeneratorHost) {
181+
return "required";
182+
}
183+
}
158184
}
159185
});
160186

0 commit comments

Comments
 (0)