2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2019-12-21 18:44:56 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
2019-12-21 18:44:56 -08:00
|
|
|
ILoadOptionsFunctions,
|
2019-12-31 12:19:37 -08:00
|
|
|
IPollFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
2019-12-21 18:44:56 -08:00
|
|
|
ITriggerFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
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
|
|
|
|
| ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
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
|
|
|
) {
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2019-12-21 18:44:56 -08:00
|
|
|
method,
|
|
|
|
qs: query,
|
2024-08-21 02:43:48 -07:00
|
|
|
uri: uri || `https://api.track.toggl.com/api/v9/me${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 {
|
2024-08-21 02:43:48 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'togglApi', 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
|
|
|
}
|
|
|
|
}
|