2020-06-23 08:18:01 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2020-06-23 08:18:01 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-06-24 21:06:48 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
2021-01-31 11:07:32 -08:00
|
|
|
} 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-31 11:07:32 -08:00
|
|
|
*
|
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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-23 08:18:01 -07:00
|
|
|
}
|
|
|
|
}
|