2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-11-10 13:48:20 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-11-10 13:48:20 -08:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { 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:
|
2023-01-19 04:37:19 -08:00
|
|
|
uri ||
|
2022-08-17 08:50:24 -07:00
|
|
|
`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) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-11-10 13:48:20 -08:00
|
|
|
}
|
|
|
|
}
|