2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2022-08-01 13:47:55 -07:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IPollFunctions,
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
2022-08-01 13:47:55 -07:00
|
|
|
|
|
|
|
export async function clockifyApiRequest(
|
|
|
|
this: ILoadOptionsFunctions | IPollFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
2022-11-08 06:28:21 -08:00
|
|
|
_option: IDataObject = {},
|
2022-08-01 13:47:55 -07:00
|
|
|
): Promise<any> {
|
2020-02-21 02:41:08 -08:00
|
|
|
const BASE_URL = 'https://api.clockify.me/api/v1';
|
2020-02-03 14:11:35 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
uri: `${BASE_URL}/${resource}`,
|
2020-10-04 07:28:05 -07:00
|
|
|
json: true,
|
|
|
|
useQuerystring: true,
|
2020-02-03 14:11:35 -08:00
|
|
|
};
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'clockifyApi', options);
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function clockifyApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | IPollFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-10-04 07:28:05 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query['page-size'] = 50;
|
|
|
|
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await clockifyApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2023-03-30 04:59:59 -07:00
|
|
|
const limit = query.limit as number | undefined;
|
|
|
|
if (limit && returnData.length >= limit) {
|
2020-10-04 07:28:05 -07:00
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.page++;
|
2022-08-01 13:47:55 -07:00
|
|
|
} while (responseData.length !== 0);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|