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

63 lines
1.3 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
/**
* Make an API request to Plivo.
*
* @param {IHookFunctions} this
* @param {string} method
* @param {string} url
* @param {object} body
* @returns {Promise<any>}
*/
export async function plivoApiRequest(
this: IHookFunctions | IExecuteFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const credentials = this.getCredentials('plivoApi') as { authId: string, authToken: string };
if (!credentials) {
throw new Error('No credentials returned!');
}
const options = {
method,
form: body,
qs,
uri: `https://api.plivo.com/v1/Account/${credentials.authId}${endpoint}/`,
auth: {
user: credentials.authId,
pass: credentials.authToken,
},
json: true,
};
try {
return await this.helpers.request(options);
} catch (error) {
if (error.statusCode === 401) {
throw new Error('Invalid Plivo credentials');
}
if (error?.response?.body?.error) {
let errorMessage = `Plivo error response [${error.statusCode}]: ${error.response.body.error}`;
if (error.response.body.more_info) {
errorMessage = `errorMessage (${error.response.body.more_info})`;
}
throw new Error(errorMessage);
}
throw error;
}
}