2020-05-02 04:09:16 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-05-02 04:09:16 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-04-29 07:51:49 -07:00
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
2020-04-29 07:51:49 -07:00
|
|
|
/**
|
2021-09-05 03:25:28 -07:00
|
|
|
* Make an API request to Sms77
|
2020-04-29 07:51:49 -07:00
|
|
|
*
|
|
|
|
* @param {IHookFunctions | IExecuteFunctions} this
|
|
|
|
* @param {string} method
|
2021-09-05 03:25:28 -07:00
|
|
|
* @param {Endpoint} endpoint
|
|
|
|
* @param {object | undefined} data
|
2020-04-29 07:51:49 -07:00
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
2021-09-05 03:25:28 -07:00
|
|
|
export async function sms77ApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: IDataObject, qs: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('sms77Api');
|
2020-04-29 07:51:49 -07:00
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2020-04-29 07:51:49 -07:00
|
|
|
}
|
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
SentWith: 'n8n',
|
|
|
|
'X-Api-Key': credentials.apiKey,
|
|
|
|
},
|
|
|
|
qs,
|
|
|
|
uri: `https://gateway.sms77.io/api${endpoint}`,
|
2020-04-29 07:51:49 -07:00
|
|
|
json: true,
|
|
|
|
method,
|
2021-09-05 03:25:28 -07:00
|
|
|
};
|
2020-04-29 07:51:49 -07:00
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
if (Object.keys(body).length) {
|
|
|
|
options.form = body;
|
|
|
|
body.json = 1;
|
2020-04-29 07:51:49 -07:00
|
|
|
}
|
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
const response = await this.helpers.request(options);
|
2020-05-02 04:09:16 -07:00
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
if (response.success !== '100') {
|
|
|
|
throw new NodeApiError(this.getNode(), response, { message: 'Invalid sms77 credentials or API error!' });
|
2020-05-02 04:09:16 -07:00
|
|
|
}
|
|
|
|
|
2021-09-05 03:25:28 -07:00
|
|
|
return response;
|
2020-04-29 07:51:49 -07:00
|
|
|
}
|