2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, ILoadOptionsFunctions } from 'n8n-workflow';
|
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an authenticated API request to Raindrop.
|
|
|
|
*/
|
|
|
|
export async function raindropApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
body: IDataObject,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'n8n',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
uri: `https://api.raindrop.io/rest/v1${endpoint}`,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'raindropOAuth2Api', options, {
|
2022-11-22 04:43:28 -08:00
|
|
|
includeCredentialsOnRefreshOnBody: true,
|
|
|
|
});
|
2021-02-22 04:11:51 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-02-22 04:11:51 -08:00
|
|
|
}
|
|
|
|
}
|