From c922163622831d4418c308764db7ecebfc1583a7 Mon Sep 17 00:00:00 2001 From: Jefiozie Date: Wed, 23 Dec 2020 08:51:07 +0100 Subject: [PATCH] feat: add balance endpoint to messagebird --- .../nodes/MessageBird/GenericFunctions.ts | 4 +- .../nodes/MessageBird/MessageBird.node.ts | 45 ++++++++++++++++--- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/nodes-base/nodes/MessageBird/GenericFunctions.ts b/packages/nodes-base/nodes/MessageBird/GenericFunctions.ts index 5a7eda2210..ae2cbb80e6 100644 --- a/packages/nodes-base/nodes/MessageBird/GenericFunctions.ts +++ b/packages/nodes-base/nodes/MessageBird/GenericFunctions.ts @@ -43,8 +43,10 @@ export async function messageBirdApiRequest( uri: `https://rest.messagebird.com${resource}`, json: true, }; - try { + if (Object.keys(body).length === 0) { + delete options.body; + } return await this.helpers.request(options); } catch (error) { if (error.statusCode === 401) { diff --git a/packages/nodes-base/nodes/MessageBird/MessageBird.node.ts b/packages/nodes-base/nodes/MessageBird/MessageBird.node.ts index b55ef27da5..cb4a7bde8e 100644 --- a/packages/nodes-base/nodes/MessageBird/MessageBird.node.ts +++ b/packages/nodes-base/nodes/MessageBird/MessageBird.node.ts @@ -1,6 +1,6 @@ import { IExecuteFunctions, - } from 'n8n-core'; +} from 'n8n-core'; import { IDataObject, @@ -44,6 +44,10 @@ export class MessageBird implements INodeType { name: 'SMS', value: 'sms', }, + { + name: 'Balance', + value: 'balance', + }, ], default: 'sms', description: 'The resource to operate on.', @@ -69,6 +73,27 @@ export class MessageBird implements INodeType { default: 'send', description: 'The operation to perform.', }, + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'balance', + ], + }, + }, + options: [ + { + name: 'Get', + value: 'get', + description: 'Get the balance', + }, + ], + default: 'get', + description: 'The operation to perform.', + }, // ---------------------------------- // sms:send @@ -269,11 +294,12 @@ export class MessageBird implements INodeType { let resource: string; // For POST - let bodyRequest: IDataObject; + let bodyRequest: IDataObject = {}; // For Query string let qs: IDataObject; let requestMethod; + let requestPath; for (let i = 0; i < items.length; i++) { qs = {}; @@ -289,6 +315,7 @@ export class MessageBird implements INodeType { // ---------------------------------- requestMethod = 'POST'; + requestPath = '/messages'; const originator = this.getNodeParameter('originator', i) as string; const body = this.getNodeParameter('message', i) as string; @@ -337,21 +364,27 @@ export class MessageBird implements INodeType { } const receivers = this.getNodeParameter('recipients', i) as string; - bodyRequest.recipients = receivers.split(',').map(item => { + return parseInt(item, 10); }); - } else { + } + else { throw new Error(`The operation "${operation}" is not known!`); } - } else { + + } else if (resource === 'balance') { + requestMethod = 'GET'; + requestPath = '/balance'; + } + else { throw new Error(`The resource "${resource}" is not known!`); } const responseData = await messageBirdApiRequest.call( this, requestMethod, - '/messages', + requestPath, bodyRequest, qs, );