2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-11-10 13:48:20 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-11-10 13:48:20 -08:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2021-11-10 13:48:20 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function oneSimpleApiRequest(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
2021-11-10 13:48:20 -08:00
|
|
|
const credentials = await this.getCredentials('oneSimpleApi');
|
|
|
|
|
|
|
|
const outputFormat = 'json';
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2022-08-17 08:50:24 -07:00
|
|
|
uri:
|
|
|
|
uri ||
|
|
|
|
`https://onesimpleapi.com/api${resource}?token=${credentials.apiToken}&output=${outputFormat}`,
|
2021-11-10 13:48:20 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const responseData = await this.helpers.request(options);
|
|
|
|
return responseData;
|
|
|
|
} catch (error) {
|
|
|
|
throw new NodeApiError(this.getNode(), error);
|
|
|
|
}
|
|
|
|
}
|