2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2019-12-21 18:44:56 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2019-12-21 18:44:56 -08:00
|
|
|
IExecuteFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
IExecuteSingleFunctions,
|
2019-12-21 18:44:56 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2019-12-31 12:19:37 -08:00
|
|
|
IPollFunctions,
|
2019-12-21 18:44:56 -08:00
|
|
|
ITriggerFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
import type { IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2019-12-21 18:44:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function togglApiRequest(
|
|
|
|
this:
|
|
|
|
| ITriggerFunctions
|
|
|
|
| IPollFunctions
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('togglApi');
|
2022-08-17 08:50:24 -07:00
|
|
|
const headerWithAuthentication = Object.assign(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
Authorization: ` Basic ${Buffer.from(
|
|
|
|
`${credentials.username}:${credentials.password}`,
|
|
|
|
).toString('base64')}`,
|
|
|
|
},
|
|
|
|
);
|
2019-12-21 18:44:56 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.track.toggl.com/api/v8${resource}`,
|
2019-12-21 18:44:56 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-12-21 18:44:56 -08:00
|
|
|
};
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(options.body as IDataObject).length === 0) {
|
2019-12-21 18:44:56 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2019-12-21 18:44:56 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
|
|
|
}
|