🔀 Merge branch 'Jefiozie-feat/messagebird_add_balance'

This commit is contained in:
Jan Oberhauser 2020-12-23 12:59:57 +01:00
commit b01dd54a42
2 changed files with 52 additions and 7 deletions

View file

@ -43,8 +43,10 @@ export async function messageBirdApiRequest(
uri: `https://rest.messagebird.com${resource}`, uri: `https://rest.messagebird.com${resource}`,
json: true, json: true,
}; };
try { try {
if (Object.keys(body).length === 0) {
delete options.body;
}
return await this.helpers.request(options); return await this.helpers.request(options);
} catch (error) { } catch (error) {
if (error.statusCode === 401) { if (error.statusCode === 401) {

View file

@ -1,6 +1,6 @@
import { import {
IExecuteFunctions, IExecuteFunctions,
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject, IDataObject,
@ -44,6 +44,10 @@ export class MessageBird implements INodeType {
name: 'SMS', name: 'SMS',
value: 'sms', value: 'sms',
}, },
{
name: 'Balance',
value: 'balance',
},
], ],
default: 'sms', default: 'sms',
description: 'The resource to operate on.', description: 'The resource to operate on.',
@ -69,6 +73,27 @@ export class MessageBird implements INodeType {
default: 'send', default: 'send',
description: 'The operation to perform.', 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 // sms:send
@ -133,6 +158,16 @@ export class MessageBird implements INodeType {
displayName: 'Additional Fields', displayName: 'Additional Fields',
name: 'additionalFields', name: 'additionalFields',
type: 'collection', type: 'collection',
displayOptions: {
show: {
operation: [
'send',
],
resource: [
'sms',
],
},
},
placeholder: 'Add Fields', placeholder: 'Add Fields',
default: {}, default: {},
options: [ options: [
@ -269,11 +304,12 @@ export class MessageBird implements INodeType {
let resource: string; let resource: string;
// For POST // For POST
let bodyRequest: IDataObject; let bodyRequest: IDataObject = {};
// For Query string // For Query string
let qs: IDataObject; let qs: IDataObject;
let requestMethod; let requestMethod;
let requestPath;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
qs = {}; qs = {};
@ -289,6 +325,7 @@ export class MessageBird implements INodeType {
// ---------------------------------- // ----------------------------------
requestMethod = 'POST'; requestMethod = 'POST';
requestPath = '/messages';
const originator = this.getNodeParameter('originator', i) as string; const originator = this.getNodeParameter('originator', i) as string;
const body = this.getNodeParameter('message', i) as string; const body = this.getNodeParameter('message', i) as string;
@ -337,21 +374,27 @@ export class MessageBird implements INodeType {
} }
const receivers = this.getNodeParameter('recipients', i) as string; const receivers = this.getNodeParameter('recipients', i) as string;
bodyRequest.recipients = receivers.split(',').map(item => { bodyRequest.recipients = receivers.split(',').map(item => {
return parseInt(item, 10); return parseInt(item, 10);
}); });
} else { }
else {
throw new Error(`The operation "${operation}" is not known!`); 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!`); throw new Error(`The resource "${resource}" is not known!`);
} }
const responseData = await messageBirdApiRequest.call( const responseData = await messageBirdApiRequest.call(
this, this,
requestMethod, requestMethod,
'/messages', requestPath,
bodyRequest, bodyRequest,
qs, qs,
); );