Skip to content

Instantly share code, notes, and snippets.

@atnartur
Last active January 21, 2018 09:36
Show Gist options
  • Save atnartur/32c5699f196d3d83677b53a062f361eb to your computer and use it in GitHub Desktop.
Save atnartur/32c5699f196d3d83677b53a062f361eb to your computer and use it in GitHub Desktop.
Template for swagger-js-codegen with separated arguments in functions
/**
* {{&summary}}
* @method
* @name {{&className}}#{{&methodName}}
{{#parameters}}
{{^isSingleton}} * @param {{=<% %>=}}{<%&type%>}<%={{ }}=%> {{&camelCaseName}} - {{&description}}{{/isSingleton}}
{{/parameters}}
*/
{{&className}}.prototype.{{&methodName}} = function({{#parameters.length}}{{#parameters}} {{&camelCaseName}}{{^isLast}}, {{/isLast}}{{/parameters}}{{/parameters.length}}){
{{#isES6}}let{{/isES6}}{{^isES6}}var{{/isES6}} parameters = {};
{{#isES6}}let{{/isES6}}{{^isES6}}var{{/isES6}} deferred = {{#isNode}}Q{{/isNode}}{{^isNode}}$q{{/isNode}}.defer();
{{#isES6}}let{{/isES6}}{{^isES6}}var{{/isES6}} domain = this.domain, path = '{{&path}}';
{{#isES6}}let{{/isES6}}{{^isES6}}var{{/isES6}} requestBody = {}, queryParameters = {}, headers = {}, form = {};
{{#isSecure}}
headers = this.setAuthHeaders(headers);
{{/isSecure}}
{{#headers}}
headers['{{&name}}'] = [{{&value}}];
{{/headers}}
{{#parameters}}
{{#isQueryParameter}}
{{#isSingleton}}
queryParameters['{{&name}}'] = {{&singleton}};
{{/isSingleton}}
{{^isSingleton}}
{{#isPatternType}}
Object.keys(parameters).forEach(function(parameterName) {
if(new RegExp('{{&pattern}}').test(parameterName)){
queryParameters[parameterName] = parameters[parameterName];
}
});
{{/isPatternType}}
{{#default}}
/** set default value **/
queryParameters['{{&name}}'] = '{{&default}}';
{{/default}}
{{^isPatternType}}
if({{&camelCaseName}} !== undefined){
queryParameters['{{&name}}'] = {{&camelCaseName}};
}
{{/isPatternType}}
{{/isSingleton}}
{{/isQueryParameter}}
{{#isPathParameter}}
path = path.replace('{{=<% %>=}}{<%&name%>}<%={{ }}=%>', parameters['{{&camelCaseName}}']);
{{/isPathParameter}}
{{#isHeaderParameter}}
{{#isSingleton}}
headers['{{&name}}'] = '{{&singleton}}';
{{/isSingleton}}
{{^isSingleton}}
if({{&camelCaseName}} !== undefined){
headers['{{&name}}'] = parameters['{{&camelCaseName}}'];
}
{{/isSingleton}}
{{/isHeaderParameter}}
{{#isBodyParameter}}
if({{&camelCaseName}} !== undefined){
requestBody = {{&camelCaseName}};
}
{{/isBodyParameter}}
{{#isFormParameter}}
{{#isSingleton}}
form['{{&name}}'] = '{{&singleton}}';
{{/isSingleton}}
{{^isSingleton}}
if({{&camelCaseName}} !== undefined){
form['{{&name}}'] = {{&camelCaseName}};
}
{{/isSingleton}}
{{/isFormParameter}}
{{#required}}
if({{&camelCaseName}} === undefined){
deferred.reject(new Error('Missing required {{&paramType}} parameter: {{&camelCaseName}}'));
return deferred.promise;
}
{{/required}}
{{/parameters}}
queryParameters = mergeQueryParams(parameters, queryParameters);
this.request('{{method}}', domain + path, parameters, requestBody, headers, queryParameters, form, deferred);
return deferred.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment