2020-02-03 14:11:35 -08:00
|
|
|
import {
|
2020-10-04 07:28:05 -07:00
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-02-03 14:11:35 -08:00
|
|
|
ILoadOptionsFunctions,
|
2020-10-04 07:28:05 -07:00
|
|
|
IPollFunctions,
|
2020-02-21 02:41:08 -08:00
|
|
|
} from 'n8n-core';
|
2020-02-03 14:11:35 -08:00
|
|
|
|
2020-10-04 07:28:05 -07:00
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function clockifyApiRequest(this: ILoadOptionsFunctions | IPollFunctions | IExecuteFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
2020-02-03 14:11:35 -08:00
|
|
|
|
|
|
|
const credentials = this.getCredentials('clockifyApi');
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
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',
|
|
|
|
'X-Api-Key': credentials.apiKey as string,
|
|
|
|
},
|
|
|
|
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
|
|
|
};
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
try {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
return await this.helpers.request!(options);
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
let errorMessage = error.message;
|
2020-10-04 07:28:05 -07:00
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
if (error.response.body && error.response.body.message) {
|
2020-10-04 07:28:05 -07:00
|
|
|
|
|
|
|
errorMessage = `[${error.statusCode}] ${error.response.body.message}`;
|
|
|
|
|
2020-02-03 14:11:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Clockify Error: ' + errorMessage);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 07:28:05 -07:00
|
|
|
|
|
|
|
export async function clockifyApiRequestAllItems(this: IExecuteFunctions | IPollFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
query['page-size'] = 50;
|
|
|
|
|
|
|
|
query.page = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await clockifyApiRequest.call(this, method, endpoint, body, query);
|
|
|
|
|
|
|
|
returnData.push.apply(returnData, responseData);
|
|
|
|
|
|
|
|
if (query.limit && (returnData.length >= query.limit)) {
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.page++;
|
|
|
|
|
|
|
|
} while (
|
|
|
|
responseData.length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|