nope/modules/xetics-lean-connector/helper/node-rest-client.handlebars
2020-12-04 19:10:33 +01:00

88 lines
3.3 KiB
Handlebars

// Import the Node-Rest-Client Library
import { Client } from 'node-rest-client';
import { getNopeLogger } from '../../../../lib/logger/getLogger';
import * as Logger from "js-logger";
{{#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 = '';
public client = new Client();
protected _logger = getNopeLogger('xetics.{{operationGroup.operationsGroupName}}');
{{#each operationGroup.operations}}
// Checkout the Original Link at {{getDoku operationName}}
public {{operationName}}({{#joinList operationParams}} {{paramDisplayName}}?:{{paramType}} {{/joinList}}):Promise<{{responsesType}}>{
const _this = this;
// Define the Parameters and Path Object
let _data: any = {};
const _path: any = {};
{{#if operationParams}}
{{!-- {{#each operationParams}}
///////////////////////////////////////////
// OP =>
// Inbody: {{this.inBody}}
// InPath: {{this.inPath}}
// Parameter-Name: {{paramName}}
// Display-Name: {{paramDisplayName}}
{{/each}} --}}
{{!-- Update Path Variable --}}
{{#some operationParams "op=>(!op.inBody)"}}
// Path Variables
{{#joinList this "\n" "op=>(!op.inBody)"}}
_path["{{paramName}}"] = {{paramDisplayName}};{{/joinList}}
{{/some}}
{{!-- Update Parameter --}}
{{#some operationParams "op=>(op.inBody && !op.inPath)"}}
// Update Parameters
_data = Object.assign({},{{#joinList this "\n" "op=>(op.inBody && !op.inPath)"}}{{paramDisplayName}}{{#unless @last}},{{/unless}}{{/joinList}});
{{/some}}
{{/if}}
// Define the Callback
return new Promise<{{responsesType}}>((resolve, reject) => {
this.client.{{httpVerb}}(this.uri+`{{updateURL url}}`{{#some operationParams "op=>(!op.inBody && !op.inPath)"}}{{#joinList this " " "op=>(!op.inBody && !op.inPath)"}}+ ({{paramDisplayName}} !== undefined ? `?{{paramDisplayName}}=${ {{paramDisplayName}} }` : ''){{/joinList}}{{/some}},{
data: _data,
headers: this.token ? {
'Content-Type': 'application/json',
'Authorization': this.token
} : {
'Content-Type': 'application/json'
},
path: _path,
}, function(data, response) {
{{!-- console.log(data,response) --}}
if (response.statusCode !== 200) {
_this._logger.error('rest-error: ' + response.statusCode);
const error = new Error('Failed with '+ response.statusCode);
reject(error);
} else {
if (_this._logger.enabledFor(Logger.DEBUG)) {
_this._logger.debug('request {{operationGroup.operationsGroupName}}.{{operationName}} sucessfull');
}
resolve(data as {{responsesType}});
}
});
});
}
{{/each}}
}