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

62 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>}
*
2020-06-23 08:18:01 -07:00
*/
2021-01-28 21:46:28 -08:00
export async function SIGNL4ApiRequest(this: IExecuteFunctions, method: string, body: string, query: IDataObject = {}, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('signl4Api');
const teamSecret = credentials?.teamSecret as string;
2020-06-24 21:06:48 -07:00
let options: OptionsWithUri = {
headers: {
'Accept': '*/*',
},
method,
body,
qs: query,
2021-01-28 21:46:28 -08:00
uri: `https://connect.signl4.com/webhook/${teamSecret}`,
json: true,
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-28 21:46:28 -08:00
return 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
}
}