2024-02-14 07:29:09 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
|
|
|
} 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,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
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';
|
2024-02-14 07:29:09 -08:00
|
|
|
let options: IRequestOptions = {
|
2021-11-10 13:48:20 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|