2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-02-22 04:11:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an authenticated API request to Raindrop.
|
|
|
|
*/
|
|
|
|
export async function raindropApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-02-22 04:11:51 -08:00
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
|
|
|
body: IDataObject,
|
|
|
|
option: IDataObject = {},
|
|
|
|
) {
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-02-22 04:11:51 -08:00
|
|
|
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) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-02-22 04:11:51 -08:00
|
|
|
}
|
|
|
|
}
|