Improvements and fixes on Mocean-Node

This commit is contained in:
Jan Oberhauser 2020-02-14 21:39:53 -08:00
parent 98d4049741
commit 77521bb387
3 changed files with 63 additions and 53 deletions

View file

@ -30,18 +30,18 @@ export async function moceanApiRequest(this: IHookFunctions | IExecuteFunctions,
body['mocean-api-key'] = credentials['mocean-api-key']; body['mocean-api-key'] = credentials['mocean-api-key'];
body['mocean-api-secret'] = credentials['mocean-api-secret']; body['mocean-api-secret'] = credentials['mocean-api-secret'];
body['mocean-resp-format'] = 'JSON'; body['mocean-resp-format'] = 'JSON';
} else if(method === 'GET') { } else if (method === 'GET') {
query['mocean-api-key'] = credentials['mocean-api-key']; query['mocean-api-key'] = credentials['mocean-api-key'];
query['mocean-api-secret'] = credentials['mocean-api-secret']; query['mocean-api-secret'] = credentials['mocean-api-secret'];
query['mocean-resp-format'] = 'JSON'; query['mocean-resp-format'] = 'JSON';
} }
console.log(body);
const options = { const options = {
method, method,
form: body, form: body,
qs: query, qs: query,
uri: `https://rest.moceanapi.com${endpoint}`, uri: `https://rest.moceanapi.com${endpoint}`,
json: true json: true,
}; };
try { try {
@ -49,17 +49,17 @@ export async function moceanApiRequest(this: IHookFunctions | IExecuteFunctions,
} catch (error) { } catch (error) {
if (error.statusCode === 401) { if (error.statusCode === 401) {
// Return a clear error // Return a clear error
throw new Error('Authentication fail.'); throw new Error('Authentication failed.');
} }
if (error.response && error.response.body && error.response.body.message) { if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier // Try to return the error prettier
let errorMessage = `Twilio error response [${error.statusCode}]: ${error.response.body.message}`; let errorMessage = error.response.body.message;
if (error.response.body.more_info) { if (error.response.body.more_info) {
errorMessage = `errorMessage (${error.response.body.more_info})`; errorMessage += `errorMessage (${error.response.body.more_info})`;
} }
throw new Error(errorMessage); throw new Error(`Mocean error response [${error.statusCode}]: ${errorMessage}`);
} }
// If that data does not exist for some reason return the actual error // If that data does not exist for some reason return the actual error

View file

@ -13,10 +13,10 @@ export class Mocean implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Mocean', displayName: 'Mocean',
name: 'mocean', name: 'mocean',
icon: 'file:logo.png', icon: 'file:mocean.png',
group: ['transform'], group: ['transform'],
version: 1, version: 1,
description: 'This is the official node created from Mocean<https://moceanapi.com>', description: 'Send SMS & voice messages via Mocean (https://moceanapi.com)',
defaults: { defaults: {
name: 'Mocean', name: 'Mocean',
color: '#772244', color: '#772244',
@ -25,8 +25,8 @@ export class Mocean implements INodeType {
outputs: ['main'], outputs: ['main'],
credentials: [ credentials: [
{ {
name: "moceanApi", name: 'moceanApi',
required: true required: true,
} }
], ],
properties: [ properties: [
@ -38,12 +38,12 @@ export class Mocean implements INodeType {
type: 'options', type: 'options',
options:[ options:[
{ {
name: "SMS", name: 'SMS',
value: "sms" value: 'sms',
}, },
{ {
name: "Voice", name: 'Voice',
value: "voice" value: 'voice',
} }
], ],
default: 'sms', default: 'sms',
@ -56,7 +56,7 @@ export class Mocean implements INodeType {
show: { show: {
resource: [ resource: [
'sms', 'sms',
'voice' 'voice',
], ],
}, },
}, },
@ -118,25 +118,25 @@ export class Mocean implements INodeType {
type: 'options', type: 'options',
options:[ options:[
{ {
name: "English (United States)", name: 'Chinese Mandarin (China)',
value: "en-US" value: 'cmn-CN'
}, },
{ {
name: "English (United Kingdom)", name: 'English (United Kingdom)',
value: "en-GB" value: 'en-GB'
}, },
{ {
name: "Chinese Mandarin (China", name: 'English (United States)',
value: "cmn-CN" value: 'en-US'
}, },
{ {
name: "Japanese (Japan)", name: 'Japanese (Japan)',
value: "ja-JP" value: 'ja-JP'
}, },
{ {
name: "Korean (Korea)", name: 'Korean (Korea)',
value: "ko-KR" value: 'ko-KR'
} },
], ],
displayOptions: { displayOptions: {
show: { show: {
@ -177,22 +177,20 @@ export class Mocean implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
let item: INodeExecutionData; const returnData: IDataObject[] = [];
let returnData: IDataObject[] = [];
let endpoint: string; let endpoint: string;
let operation: string; let operation: string;
let requesetMethod: string; let requesetMethod: string;
let resource: string; let resource: string;
let text: string; let text: string;
let dataKey: string;
// For Post // For Post
let body: IDataObject; let body: IDataObject;
// For Query string // For Query string
let qs: IDataObject; let qs: IDataObject;
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
body = {}; body = {};
qs = {}; qs = {};
@ -204,28 +202,38 @@ export class Mocean implements INodeType {
body['mocean-from'] = this.getNodeParameter('from', itemIndex, '') as string; body['mocean-from'] = this.getNodeParameter('from', itemIndex, '') as string;
body['mocean-to'] = this.getNodeParameter('to', itemIndex, '') as string; body['mocean-to'] = this.getNodeParameter('to', itemIndex, '') as string;
if (resource === 'voice') { if (resource === 'voice') {
let language: string = this.getNodeParameter("language",itemIndex) as string; const language: string = this.getNodeParameter('language', itemIndex) as string;
let command:any = [{"action": "say", "language": `${language}`, "text": `${text}`}]; const command = [
{
action: 'say',
language,
text,
}
];
dataKey = 'voice';
body['mocean-command'] = JSON.stringify(command); body['mocean-command'] = JSON.stringify(command);
endpoint = '/rest/2/voice/dial'; endpoint = '/rest/2/voice/dial';
} else if(resource === 'sms') { } else if(resource === 'sms') {
dataKey = 'messages';
body['mocean-text'] = text; body['mocean-text'] = text;
endpoint = '/rest/2/sms'; endpoint = '/rest/2/sms';
} else { } else {
throw new Error(`Unknown resource ${resource}`); throw new Error(`Unknown resource ${resource}`);
} }
if (operation === "send") { if (operation === 'send') {
const responseData = await moceanApiRequest.call(this,requesetMethod,endpoint,body,qs); const responseData = await moceanApiRequest.call(this,requesetMethod,endpoint,body,qs);
returnData.push(responseData as IDataObject);
for (const item of responseData[dataKey] as IDataObject[]) {
item.type = resource;
returnData.push(item);
}
} else { } else {
throw new Error(`Unknown operation ${operation}`); throw new Error(`Unknown operation ${operation}`);
} }
} }
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];

View file

@ -63,14 +63,15 @@
"dist/credentials/LinkFishApi.credentials.js", "dist/credentials/LinkFishApi.credentials.js",
"dist/credentials/MailchimpApi.credentials.js", "dist/credentials/MailchimpApi.credentials.js",
"dist/credentials/MailgunApi.credentials.js", "dist/credentials/MailgunApi.credentials.js",
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js", "dist/credentials/MandrillApi.credentials.js",
"dist/credentials/MattermostApi.credentials.js", "dist/credentials/MattermostApi.credentials.js",
"dist/credentials/MauticApi.credentials.js", "dist/credentials/MauticApi.credentials.js",
"dist/credentials/MoceanApi.credentials.js",
"dist/credentials/MongoDb.credentials.js", "dist/credentials/MongoDb.credentials.js",
"dist/credentials/Msg91Api.credentials.js", "dist/credentials/Msg91Api.credentials.js",
"dist/credentials/MySql.credentials.js", "dist/credentials/MySql.credentials.js",
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/NextCloudApi.credentials.js", "dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/PayPalApi.credentials.js", "dist/credentials/PayPalApi.credentials.js",
@ -160,17 +161,18 @@
"dist/nodes/Mailchimp/Mailchimp.node.js", "dist/nodes/Mailchimp/Mailchimp.node.js",
"dist/nodes/Mailchimp/MailchimpTrigger.node.js", "dist/nodes/Mailchimp/MailchimpTrigger.node.js",
"dist/nodes/Mailgun/Mailgun.node.js", "dist/nodes/Mailgun/Mailgun.node.js",
"dist/nodes/Mailjet/Mailjet.node.js",
"dist/nodes/Mailjet/MailjetTrigger.node.js",
"dist/nodes/Mandrill/Mandrill.node.js", "dist/nodes/Mandrill/Mandrill.node.js",
"dist/nodes/Mattermost/Mattermost.node.js", "dist/nodes/Mattermost/Mattermost.node.js",
"dist/nodes/Mautic/Mautic.node.js", "dist/nodes/Mautic/Mautic.node.js",
"dist/nodes/Mautic/MauticTrigger.node.js", "dist/nodes/Mautic/MauticTrigger.node.js",
"dist/nodes/Merge.node.js", "dist/nodes/Merge.node.js",
"dist/nodes/Mocean/Mocean.node.js",
"dist/nodes/MongoDb/MongoDb.node.js", "dist/nodes/MongoDb/MongoDb.node.js",
"dist/nodes/MoveBinaryData.node.js", "dist/nodes/MoveBinaryData.node.js",
"dist/nodes/Msg91/Msg91.node.js", "dist/nodes/Msg91/Msg91.node.js",
"dist/nodes/MySql/MySql.node.js", "dist/nodes/MySql/MySql.node.js",
"dist/nodes/Mailjet/Mailjet.node.js",
"dist/nodes/Mailjet/MailjetTrigger.node.js",
"dist/nodes/NextCloud/NextCloud.node.js", "dist/nodes/NextCloud/NextCloud.node.js",
"dist/nodes/NoOp.node.js", "dist/nodes/NoOp.node.js",
"dist/nodes/OpenWeatherMap.node.js", "dist/nodes/OpenWeatherMap.node.js",