n8n/packages/nodes-base/nodes/Signl4/GenericFunctions.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-06-23 08:18:01 -07:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
2020-06-24 21:06:48 -07:00
import {
OptionsWithUri,
} from 'request';
2020-06-23 08:18:01 -07:00
/**
2020-06-24 21:06:48 -07:00
* Make an API request to SIGNL4
2020-06-23 08:18:01 -07:00
*
* @param {IHookFunctions | IExecuteFunctions} this
* @param {object} message
* @returns {Promise<any>}
*/
2020-06-24 21:06:48 -07:00
export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
let options: OptionsWithUri = {
headers: {
'Accept': '*/*',
},
method,
body,
qs: query,
uri: uri || ``,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
2020-06-23 08:18:01 -07:00
}
2020-06-24 21:06:48 -07:00
if (!Object.keys(query).length) {
delete options.qs;
2020-06-23 08:18:01 -07:00
}
2020-06-24 21:06:48 -07:00
options = Object.assign({}, options, option);
2020-06-23 08:18:01 -07:00
2020-06-24 21:06:48 -07:00
try {
return await this.helpers.request!(options);
} catch (error) {
2020-06-23 08:18:01 -07:00
2020-06-24 21:06:48 -07:00
if (error.response && error.response.body && error.response.body.details) {
throw new Error(`SIGNL4 error response [${error.statusCode}]: ${error.response.body.details}`);
}
2020-06-23 08:18:01 -07:00
2020-06-24 21:06:48 -07:00
throw error;
2020-06-23 08:18:01 -07:00
}
}