Small changes to Sms77 node

This commit is contained in:
Jan Oberhauser
2020-05-02 13:09:16 +02:00
parent 58d0cda0bf
commit ae853ce602
5 changed files with 43 additions and 31 deletions

View File

@@ -1,5 +1,12 @@
import {IExecuteFunctions, IHookFunctions,} from 'n8n-core';
import {IDataObject,} from 'n8n-workflow';
import {
IExecuteFunctions,
IHookFunctions,
} from 'n8n-core';
import {
ICredentialDataDecryptedObject,
IDataObject,
} from 'n8n-workflow';
/**
* Make an API request to MSG91
@@ -12,26 +19,17 @@ import {IDataObject,} from 'n8n-workflow';
* @returns {Promise<any>}
*/
export async function sms77ApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, form: IDataObject, qs?: IDataObject): Promise<any> { // tslint:disable-line:no-any
const setPayload = (o?: IDataObject) => {
if (!o) {
o = {};
}
o.p = credentials!.apiKey as string;
o.json = 1;
o.sendwith = 'n8n';
return o;
};
const credentials = this.getCredentials('Sms77');
const credentials = this.getCredentials('sms77Api');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
'GET' === method ? qs = setPayload(qs) : form = setPayload(form);
const res = await this.helpers.request({
if ('GET' === method) {
qs = setPayload(credentials, qs);
} else {
form = setPayload(credentials, form);
}
const response = await this.helpers.request({
form,
json: true,
method,
@@ -39,9 +37,22 @@ export async function sms77ApiRequest(this: IHookFunctions | IExecuteFunctions,
uri: `https://gateway.sms77.io/api/${endpoint}`,
});
if ('100' !== res.success) {
if ('100' !== response.success) {
throw new Error('Invalid sms77 credentials or API error!');
}
return res;
return response;
}
function setPayload(credentials: ICredentialDataDecryptedObject, o?: IDataObject) {
if (!o) {
o = {};
}
o.p = credentials!.apiKey as string;
o.json = 1;
o.sendwith = 'n8n';
return o;
}