2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-11-24 14:19:41 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject } from 'n8n-workflow';
|
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function spontitApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions | IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('spontitApi');
|
2020-11-24 14:19:41 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'X-Authorization': credentials.apiKey as string,
|
|
|
|
'X-UserId': credentials.username as string,
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `https://api.spontit.com/v3${resource}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers?.request(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-11-24 14:19:41 -08:00
|
|
|
}
|
2020-11-24 14:20:09 -08:00
|
|
|
}
|