Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions documentation/codegenerator/client/autoform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { HTTP } from 'meteor/http';

AutoForm.addHooks('downloadSDK', {
onSubmit: function (formValues, updateDoc, instance) {
// Prevent form from submitting
this.event.preventDefault();

// Get selected language from dropdown list
const selectedLanguage = formValues.selectLanguage;

// Get index of selected language in global list of languages
const index = _.indexOf(instance.languageList, selectedLanguage);

// Find mask of the language for url
const parameter = instance.urlParameters[index];

const host = 'https://generator.swagger.io';

// Create URL to send request
const url = host + '/api/gen/clients/' + parameter;

// Get path to documentation file
const pathToFile = instance.documentationFileURL;

// Create POST options
const options = {
'swaggerUrl': pathToFile
};

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

// Send POST request
HTTP.post(url, { data: options }, function (error, result) {
// Get information from Swagger API response
let response = JSON.parse(result.content);

if (result.statusCode === 200) {
// Hide modal
Modal.hide('sdkCodeGeneratorModal');

// Go to link and download file
window.location.href = response.link;
} else {
$('button').removeAttr('disabled');

// Otherwise show an error message
FlashMessages.sendError(TAPi18n.__('sdkCodeGeneratorModal_errorText'));
}
// Finish spinner
instance.callRequest.set(false);
});
}
});
34 changes: 34 additions & 0 deletions documentation/codegenerator/client/codegenerator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template name="sdkCodeGeneratorModal">
<div class="modal fade">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h1 class="modal-title">
{{_ "sdkCodeGeneratorModal_Title" }}
</h1>
</div>
<div class="modal-body">
{{#if dataFetched}}

{{> quickForm
id="downloadSDK"
schema=generateSDK
doc=getTemplateInstance
buttonContent="Download" }}

{{else}}
{{> spinner}}
{{/if}}

{{#if statusRequest}}
{{> spinner}}
{{/if}}
</div>
{{> flashMessages}}
</div>
</div>
</div>
</template>
114 changes: 114 additions & 0 deletions documentation/codegenerator/client/codegenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session';

import _ from 'lodash';

import { DocumentationFiles, languageHashName } from '/documentation/collection/collection';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brylie File codegenerator.js
If I do import the variables 'LanguagesList' and 'LanguageParams' from collection.js, this variables wil be undefined.

Template.sdkCodeGeneratorModal.onCreated(function () {
const instance = this;

instance.callRequest = new ReactiveVar(false);
instance.dataReady = new ReactiveVar(false);

// Get documentation file id
const documentationFileId = instance.data.apiBackend.documentationFileId;

// Get documentation file URL
const documentationFileURL = Meteor.absoluteUrl().slice(0, -1) + DocumentationFiles.baseURL + '/id/' + documentationFileId;

// Save documentation file URL
instance.documentationFileURL = documentationFileURL;

/* Get list of an available languages from Codegen server */

// Codegen server url
const url = 'https://generator.swagger.io/api/gen/clients';

// Call GET request
HTTP.get(url, {}, function (error, result) {
// Get information from Swagger API response
const response = JSON.parse(result.content);

// Save response to use it like url parameter in POST request
instance.urlParameters = response;

// Create list of friendly language names
instance.languageList = [];

_.forEach(response, function (language) {
// Check on specific name
let newLanguageName = languageHashName[language];

// Convert name by standard method if it isn't specific name
if (_.isUndefined(newLanguageName)) {
// Split the name into words, ex. 'akka-scala' -> 'akka','scala'
let newLanguageList = language.split('-');
// Do the capital letter for each word
newLanguageList = _.map(newLanguageList, function (word) { return _.capitalize(word); });
// Join this list to string using space
newLanguageName = newLanguageList.join(' ');
}
// Add new name to list of languages which show to user
instance.languageList.push(newLanguageName);

// Finish spinner
instance.dataReady.set(true);
});
});
});

Template.sdkCodeGeneratorModal.helpers({
// Schema for SDK Code Generator form
generateSDK () {
// Get reference to template instance
const instance = Template.instance();

// Create simple schema for sdk modal
const sdkSchema = new SimpleSchema({
selectLanguage: {
type: String,
allowedValues: instance.languageList,
autoform: {
afFieldInput: {
firstOption: '(Language)'
}
}
}
});

return sdkSchema;
},
// Check on ready of data from call GET request
dataFetched () {
// Get reference to template instance
const instance = Template.instance();

return instance.dataReady.get();
},
// Give variable callRequest to template
statusRequest () {
// Get reference to template instance
const instance = Template.instance();

return instance.callRequest.get();
},
// From template AutoForm we don't have access to instance of this template
// getTemplateInstance return object that containts the necessary varaibles
getTemplateInstance () {
// Get reference to template instance
const instance = Template.instance();

// Create object with instance varaibles
const dataToAutoform = {
'apiBackend': instance.data.apiBackend,
'callRequest': instance.callRequest,
'documentationFileURL': instance.documentationFileURL,
'languageList': instance.languageList,
'urlParameters': instance.urlParameters
};

return dataToAutoform;
}
});
21 changes: 19 additions & 2 deletions documentation/collection/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const DocumentationFiles = new FileCollection('DocumentationFiles', {
{
method: 'get',
path: '/id/:_id',
lookup: function(params, query) {
lookup: function (params, query) {
return {
_id: params._id
};
Expand All @@ -14,4 +14,21 @@ const DocumentationFiles = new FileCollection('DocumentationFiles', {
]
});

export { DocumentationFiles };
// The most part of list items can convert to user friendly name by standard method
// The method is to replace dash on space and to do the capital letter of each word
// Some language names can't be convert by standard method.
// Variable 'languageHashName' keeps specific names
const languageHashName = {
Copy link
Copy Markdown
Contributor

@brylie brylie Aug 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to a different file. The only purpose of the collection.js file is to define and export the collection.

This collection is specifically for storing swagger documents, which may be confusing here. Codegen code should only be defined within the codegen module:

documentation/codegenerator/client/codegenerator.js

'dynamic-html': 'Dynamic HTML',
'csharp': 'C#',
'CsharpDotNet2': 'C# .NET 2.0',
'html': 'HTML',
'objc': 'Objective-C',
'php': 'PHP',
'qt5cpp': 'Qt 5 C++',
'swagger': 'Swagger JSON',
'swagger-yaml': 'Swagger YAML'
};

export { DocumentationFiles, languageHashName };

4 changes: 4 additions & 0 deletions documentation/view/client/documentation.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ iframe {
.s-alert-box {
z-index: 10000
}

button#sdk-code-generator {
margin-right: 1em
}
25 changes: 18 additions & 7 deletions documentation/view/client/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
<div class="panel panel-default">
<div class="panel-heading">
{{#if apiBackend.currentUserCanEdit }}
<button
id="manage-api-documentation"
class="btn btn-xs btn-info pull-right">
<i class="fa fa-pencil"></i>&nbsp;
Manage
</button>
{{/if}}
<button
id="manage-api-documentation"
class="btn btn-xs btn-info pull-right">
<i class="fa fa-pencil"></i>&nbsp;
{{_ "documentation_manageDocumentation_buttonText" }}
</button>
{{/if}}

{{#if documentationExists }}
<button
id="sdk-code-generator"
class="btn btn-info btn-xs pull-right"
data-toggle="tooltip"
title="{{_ 'documentation_tooltipSDKGeneratorButton'}}">
{{_ "documentation_sdkGenerateButton" }}
</button>
{{/if}}

<h2 class="panel-title clearfix">
{{_ "documentation_Title" }}
</h2>
Expand Down
6 changes: 6 additions & 0 deletions documentation/view/client/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ Template.documentation.events({
const apiBackend = instance.data.apiBackend
// Show the manage API documentation form
Modal.show('manageApiDocumentationModal', { apiBackend })
},
'click #sdk-code-generator' (event, instance) {
// Get reference to API backend
const apiBackend = instance.data.apiBackend;
// Show the SDK Code generator form
Modal.show('sdkCodeGeneratorModal', { apiBackend });
}
});
5 changes: 5 additions & 0 deletions lib/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@
"documentation_APICall_Warning_Message": "API calls (PUT, POST, or DELETE) from Swagger are real calls.",
"documentation_APICall_Warning_Title": "Warning!",
"documentation_Link_Title": "Link",
"documentation_manageDocumentation_buttonText": "Manage",
"documentation_No_Link_Manager_Message": "You can add one using Manage API feature.",
"documentation_No_Link_Message": "No documentation link is available.",
"documentation_No_Swagger_Manager_Message": "You can add one using Manage API feature.",
"documentation_No_Swagger_Message": "No swagger document is available.",
"documentation_Title": "Documentation",
"documentation_Viewer_Title": "Viewer",
"documentation_sdkGenerateButton": "SDK Generate",
"documentation_tooltipSDKGeneratorButton": "With a valid swagger file, you can download an SDK of your preferred language using this wizard",
"editApiBackendForm-uploadLogo": "Upload logo",
"editApiMetadata_editApiMetadataForm_button": "Edit API Metadata",
"editApiMetadata_editApiMetadataForm_title": "Edit API Metadata",
Expand Down Expand Up @@ -348,6 +351,8 @@
}
}
},
"sdkCodeGeneratorModal_errorText": "Your file is not supported by the generator. Please upload the correct file",
"sdkCodeGeneratorModal_Title": "Generate SDK file",
"searchField_button": "Search",
"searchField_placeholder": "Search...",
"searchPage_addedPrefix": "Added",
Expand Down