2020-05-02 04:09:16 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
2020-04-29 07:51:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to MSG91
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions | IExecuteFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} endpoint
|
|
|
|
* @param {object} form
|
|
|
|
* @param {object | undefined} qs
|
|
|
|
* @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
|
2020-05-02 04:09:16 -07:00
|
|
|
const credentials = this.getCredentials('sms77Api');
|
2020-04-29 07:51:49 -07:00
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
2020-05-02 04:09:16 -07:00
|
|
|
if ('GET' === method) {
|
|
|
|
qs = setPayload(credentials, qs);
|
|
|
|
} else {
|
|
|
|
form = setPayload(credentials, form);
|
|
|
|
}
|
|
|
|
const response = await this.helpers.request({
|
2020-04-29 07:51:49 -07:00
|
|
|
form,
|
|
|
|
json: true,
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
uri: `https://gateway.sms77.io/api/${endpoint}`,
|
|
|
|
});
|
|
|
|
|
2020-05-02 04:09:16 -07:00
|
|
|
if ('100' !== response.success) {
|
2020-04-29 07:51:49 -07:00
|
|
|
throw new Error('Invalid sms77 credentials or API error!');
|
|
|
|
}
|
|
|
|
|
2020-05-02 04:09:16 -07:00
|
|
|
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;
|
2020-04-29 07:51:49 -07:00
|
|
|
}
|