2020-06-11 17:40:58 -07:00
|
|
|
import {
|
2020-06-12 00:39:56 -07:00
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
2020-06-11 17:40:58 -07:00
|
|
|
import {
|
2020-06-12 00:39:56 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-06-11 17:40:58 -07:00
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2020-06-12 00:39:56 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-06-11 15:57:32 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Message Bird
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function messageBirdApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject,
|
2020-10-22 09:00:28 -07:00
|
|
|
query: IDataObject = {},
|
2020-06-12 00:39:56 -07:00
|
|
|
): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('messageBirdApi');
|
2020-06-11 15:57:32 -07:00
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials returned!');
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
2020-06-11 17:40:58 -07:00
|
|
|
Authorization: `AccessKey ${credentials.accessKey}`,
|
2020-06-11 15:57:32 -07:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs: query,
|
|
|
|
body,
|
|
|
|
uri: `https://rest.messagebird.com${resource}`,
|
2020-06-12 00:39:56 -07:00
|
|
|
json: true,
|
2020-06-11 15:57:32 -07:00
|
|
|
};
|
|
|
|
try {
|
2020-12-22 23:51:07 -08:00
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2020-06-11 15:57:32 -07:00
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-11 15:57:32 -07:00
|
|
|
}
|
|
|
|
}
|