2024-02-14 07:29:09 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
|
|
|
IHttpRequestMethods,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2019-12-13 03:29:33 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to MSG91
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function msg91ApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject,
|
|
|
|
query?: IDataObject,
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('msg91Api');
|
2019-12-13 03:29:33 -08:00
|
|
|
|
|
|
|
if (query === undefined) {
|
|
|
|
query = {};
|
2019-12-20 14:35:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
query.authkey = credentials.authkey as string;
|
2019-12-13 03:29:33 -08:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
method,
|
|
|
|
form: body,
|
|
|
|
qs: query,
|
2019-12-20 14:35:00 -08:00
|
|
|
uri: `https://api.msg91.com/api${endpoint}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-12-13 03:29:33 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request(options);
|
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-12-13 03:29:33 -08:00
|
|
|
}
|
2019-12-20 14:35:00 -08:00
|
|
|
}
|