n8n/packages/nodes-base/nodes/Plivo/GenericFunctions.ts
Iván Ovejero cdcbc3d256
Add Plivo Node (#1563)
* add Plivo Node

* fixed package.json

* added Git user in CLI to validate CLA

* 🎨 Replace PNG with SVG icon

*  Ajust per codebase conventions

* 🔨 Fix operation ID

* ✏️ Specify geo-restriction for MMS

*  Improvements

*  Fix node name and operation order

Co-authored-by: Nixon <nixon@Nixons-MacBook-Pro.local>
Co-authored-by: Nixon <nixon@plivo.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2021-03-23 23:49:08 +01:00

63 lines
1.3 KiB
TypeScript

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;
}
}