2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, ILoadOptionsFunctions, NodeApiError } from 'n8n-workflow';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { 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-11-22 04:43:28 -08:00
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'raindropOAuth2Api', options, {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|