nope/modules/xetics-lean-connector/helper/superagent-client.handlebars

56 lines
1.8 KiB
Handlebars
Raw Normal View History

2020-11-11 16:06:25 +00:00
// Import the Node-Rest-Client Library
const request = require('superagent');
{{#if imports.types}}
import { {{#joinList imports.types}}{{this}}{{/joinList}} } from "{{imports.path}}"
{{/if}}
{{#if tag}}
{{#each tag}}
{{this}}
{{/each}}
{{/if}}
export class {{operationGroup.operationsGroupName}} {
// Default URI which will be used to combine the Requests
public uri = '';
public token = '';
{{#each operationGroup.operations}}
public {{operationName}}({{#joinList operationParams}} {{paramDisplayName}}:{{paramType}} {{/joinList}}):Promise<{{responsesType}}>{
{{#if operationParams}}
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
const params = {
{{#joinList this ",\n" "op=>(!op.inBody && !op.inPath)"}}
"{{paramName}}":{{paramDisplayName}}{{/joinList}}
};
{{/some}}
{{/if}}
// Define the Callback
return new Promise<{{responsesType}}>((resolve, reject) => {
request
.{{httpVerb}}(this.uri+'{{updateURL url}}'{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}{{#joinList this " " "op=>(!op.inBody && !op.inPath)"}}+ ({{paramDisplayName}} ? '?{{paramDisplayName}}=${ {{paramDisplayName}} }' : ''){{/joinList}}{{/some}})
{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}
.query(params)
{{/some}}
.set('Authorization', this.token)
{{#filterList operationParams "op=>op.inBody" "1"}}
.send({{paramDisplayName}})
{{/filterList}}
.end(function(error, res){
if(error){
reject(error);
}else{
resolve(res.body as {{responsesType}});
}
});
});
}
{{/each}}
}