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

60 lines
1.3 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
2021-01-20 02:55:25 -08:00
* @param {string} method
* @param {string} contentType
* @param {string} body
* @param {object} query
* @param {string} teamSecret
* @param {object} options
2020-06-23 08:18:01 -07:00
* @returns {Promise<any>}
2021-01-20 03:15:12 -08:00
*
2020-06-23 08:18:01 -07:00
*/
export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string, contentType: string, body: string, query: IDataObject = {}, teamSecret?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
2020-06-24 21:06:48 -07:00
let options: OptionsWithUri = {
headers: {
'Accept': '*/*',
'Content-Type': contentType
2020-06-24 21:06:48 -07:00
},
method,
body,
qs: query,
uri: "https://connect.signl4.com/webhook/" + teamSecret,
json: false,
2020-06-24 21:06:48 -07:00
};
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 {
2021-01-22 01:10:53 -08:00
return JSON.parse(await this.helpers.request!(options));
2020-06-24 21:06:48 -07:00
} 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
}
}