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

@ -24,24 +24,24 @@ export async function moceanApiRequest(this: IHookFunctions | IExecuteFunctions,
if (query === undefined) {
query = {};
}
if (method === 'POST') {
body['mocean-api-key'] = credentials['mocean-api-key'];
}
if (method === 'POST') {
body['mocean-api-key'] = credentials['mocean-api-key'];
body['mocean-api-secret'] = credentials['mocean-api-secret'];
body['mocean-resp-format'] = 'JSON';
} else if(method === 'GET') {
query['mocean-api-key'] = credentials['mocean-api-key'];
} else if (method === 'GET') {
query['mocean-api-key'] = credentials['mocean-api-key'];
query['mocean-api-secret'] = credentials['mocean-api-secret'];
query['mocean-resp-format'] = 'JSON';
}
console.log(body);
}
const options = {
method,
form: body,
qs: query,
uri: `https://rest.moceanapi.com${endpoint}`,
json: true
json: true,
};
try {
@ -49,20 +49,20 @@ export async function moceanApiRequest(this: IHookFunctions | IExecuteFunctions,
} catch (error) {
if (error.statusCode === 401) {
// Return a clear error
throw new Error('Authentication fail.');
throw new Error('Authentication failed.');
}
if (error.response && error.response.body && error.response.body.message) {
// 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) {
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
throw error;
}
}
}

View file

@ -13,10 +13,10 @@ export class Mocean implements INodeType {
description: INodeTypeDescription = {
displayName: 'Mocean',
name: 'mocean',
icon: 'file:logo.png',
icon: 'file:mocean.png',
group: ['transform'],
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: {
name: 'Mocean',
color: '#772244',
@ -25,8 +25,8 @@ export class Mocean implements INodeType {
outputs: ['main'],
credentials: [
{
name: "moceanApi",
required: true
name: 'moceanApi',
required: true,
}
],
properties: [
@ -38,12 +38,12 @@ export class Mocean implements INodeType {
type: 'options',
options:[
{
name: "SMS",
value: "sms"
name: 'SMS',
value: 'sms',
},
{
name: "Voice",
value: "voice"
name: 'Voice',
value: 'voice',
}
],
default: 'sms',
@ -56,7 +56,7 @@ export class Mocean implements INodeType {
show: {
resource: [
'sms',
'voice'
'voice',
],
},
},
@ -118,25 +118,25 @@ export class Mocean implements INodeType {
type: 'options',
options:[
{
name: "English (United States)",
value: "en-US"
name: 'Chinese Mandarin (China)',
value: 'cmn-CN'
},
{
name: "English (United Kingdom)",
value: "en-GB"
name: 'English (United Kingdom)',
value: 'en-GB'
},
{
name: "Chinese Mandarin (China",
value: "cmn-CN"
name: 'English (United States)',
value: 'en-US'
},
{
name: "Japanese (Japan)",
value: "ja-JP"
name: 'Japanese (Japan)',
value: 'ja-JP'
},
{
name: "Korean (Korea)",
value: "ko-KR"
}
name: 'Korean (Korea)',
value: 'ko-KR'
},
],
displayOptions: {
show: {
@ -172,27 +172,25 @@ export class Mocean implements INodeType {
description: 'The message to send',
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
let item: INodeExecutionData;
let returnData: IDataObject[] = [];
const returnData: IDataObject[] = [];
let endpoint: string;
let operation: string;
let requesetMethod: string;
let resource: string;
let text: string;
let dataKey: string;
// For Post
let body: IDataObject;
// For Query string
let qs: IDataObject;
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
body = {};
qs = {};
@ -203,29 +201,39 @@ export class Mocean implements INodeType {
requesetMethod = 'POST';
body['mocean-from'] = this.getNodeParameter('from', itemIndex, '') as string;
body['mocean-to'] = this.getNodeParameter('to', itemIndex, '') as string;
if (resource === 'voice') {
let language: string = this.getNodeParameter("language",itemIndex) as string;
let command:any = [{"action": "say", "language": `${language}`, "text": `${text}`}];
const language: string = this.getNodeParameter('language', itemIndex) as string;
const command = [
{
action: 'say',
language,
text,
}
];
dataKey = 'voice';
body['mocean-command'] = JSON.stringify(command);
endpoint = '/rest/2/voice/dial';
} else if(resource === 'sms') {
dataKey = 'messages';
body['mocean-text'] = text;
endpoint = '/rest/2/sms';
} else {
throw new Error(`Unknown resource ${resource}`);
}
if (operation === "send") {
if (operation === 'send') {
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 {
throw new Error(`Unknown operation ${operation}`);
}
}
return [this.helpers.returnJsonArray(returnData)];

View file

@ -63,14 +63,15 @@
"dist/credentials/LinkFishApi.credentials.js",
"dist/credentials/MailchimpApi.credentials.js",
"dist/credentials/MailgunApi.credentials.js",
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js",
"dist/credentials/MattermostApi.credentials.js",
"dist/credentials/MauticApi.credentials.js",
"dist/credentials/MoceanApi.credentials.js",
"dist/credentials/MongoDb.credentials.js",
"dist/credentials/Msg91Api.credentials.js",
"dist/credentials/MySql.credentials.js",
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/NextCloudApi.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/PayPalApi.credentials.js",
@ -160,17 +161,18 @@
"dist/nodes/Mailchimp/Mailchimp.node.js",
"dist/nodes/Mailchimp/MailchimpTrigger.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/Mattermost/Mattermost.node.js",
"dist/nodes/Mautic/Mautic.node.js",
"dist/nodes/Mautic/MauticTrigger.node.js",
"dist/nodes/Merge.node.js",
"dist/nodes/Mocean/Mocean.node.js",
"dist/nodes/MongoDb/MongoDb.node.js",
"dist/nodes/MoveBinaryData.node.js",
"dist/nodes/Msg91/Msg91.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/NoOp.node.js",
"dist/nodes/OpenWeatherMap.node.js",