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

49 lines
1 KiB
TypeScript
Raw Normal View History

import type { IExecuteFunctions, IDataObject, JsonObject } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
2020-06-23 08:18:01 -07:00
import type { 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
*
2020-06-23 08:18:01 -07:00
*/
export async function SIGNL4ApiRequest(
this: IExecuteFunctions,
method: string,
body: string,
query: IDataObject = {},
option: IDataObject = {},
): Promise<any> {
const credentials = await this.getCredentials('signl4Api');
2021-01-28 21:46:28 -08:00
const teamSecret = credentials?.teamSecret as string;
2020-06-24 21:06:48 -07:00
let options: OptionsWithUri = {
headers: {
Accept: '*/*',
2020-06-24 21:06:48 -07:00
},
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 {
return await this.helpers.request(options);
2020-06-24 21:06:48 -07:00
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
2020-06-23 08:18:01 -07:00
}
}