2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-11-24 05:15:47 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-01-28 06:46:38 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
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';
|
2020-01-28 06:46:38 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function harvestApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
2022-12-02 12:54:28 -08:00
|
|
|
qs: IDataObject,
|
2022-08-17 08:50:24 -07:00
|
|
|
path: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2020-01-28 06:46:38 -08:00
|
|
|
let options: OptionsWithUri = {
|
2020-11-24 05:15:47 -08:00
|
|
|
headers: {
|
|
|
|
'Harvest-Account-Id': `${this.getNodeParameter('accountId', 0)}`,
|
|
|
|
'User-Agent': 'Harvest App',
|
2022-08-17 08:50:24 -07:00
|
|
|
Authorization: '',
|
2020-11-24 05:15:47 -08:00
|
|
|
},
|
2020-01-28 06:46:38 -08:00
|
|
|
method,
|
|
|
|
body,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://api.harvestapp.com/v2/${path}`,
|
2020-11-24 05:15:47 -08:00
|
|
|
qs,
|
2020-01-28 06:46:38 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(options.body as IDataObject).length === 0) {
|
2020-01-28 06:46:38 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
2020-11-24 05:15:47 -08:00
|
|
|
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
2020-02-03 04:55:21 -08:00
|
|
|
|
2020-01-28 06:46:38 -08:00
|
|
|
try {
|
2020-11-24 05:15:47 -08:00
|
|
|
if (authenticationMethod === 'accessToken') {
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('harvestApi');
|
2020-01-28 23:57:54 -08:00
|
|
|
|
2020-11-24 05:15:47 -08:00
|
|
|
//@ts-ignore
|
2022-12-02 12:54:28 -08:00
|
|
|
options.headers.Authorization = `Bearer ${credentials.accessToken}`;
|
2020-11-24 05:15:47 -08:00
|
|
|
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2020-11-24 05:15:47 -08:00
|
|
|
} else {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'harvestOAuth2Api', options);
|
2020-11-24 05:15:47 -08:00
|
|
|
}
|
2020-01-28 06:46:38 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2020-01-28 06:46:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to paginated flow endpoint
|
|
|
|
* and return all results
|
|
|
|
*/
|
|
|
|
export async function harvestApiRequestAllItems(
|
2020-11-24 05:15:47 -08:00
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
2022-12-02 12:54:28 -08:00
|
|
|
qs: IDataObject,
|
2020-11-24 05:15:47 -08:00
|
|
|
uri: string,
|
|
|
|
resource: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
option: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2020-01-28 06:46:38 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
2020-11-24 05:15:47 -08:00
|
|
|
do {
|
|
|
|
responseData = await harvestApiRequest.call(this, method, qs, uri, body, option);
|
|
|
|
qs.page = responseData.next_page;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[resource] as IDataObject[]);
|
2020-11-24 05:15:47 -08:00
|
|
|
} while (responseData.next_page);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fetch All resource using paginated calls
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getAllResource(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
resource: string,
|
|
|
|
i: number,
|
|
|
|
) {
|
2020-11-24 05:15:47 -08:00
|
|
|
const endpoint = resource;
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
const requestMethod = 'GET';
|
|
|
|
|
|
|
|
qs.per_page = 100;
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('filters', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-11-24 05:15:47 -08:00
|
|
|
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
let responseData: IDataObject = {};
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData[resource] = await harvestApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
requestMethod,
|
|
|
|
qs,
|
|
|
|
endpoint,
|
|
|
|
resource,
|
|
|
|
);
|
2020-11-24 05:15:47 -08:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2020-11-24 05:15:47 -08:00
|
|
|
qs.per_page = limit;
|
|
|
|
responseData = await harvestApiRequest.call(this, requestMethod, qs, endpoint);
|
2020-01-28 06:46:38 -08:00
|
|
|
}
|
2020-11-24 05:15:47 -08:00
|
|
|
return responseData[resource] as IDataObject[];
|
2020-01-28 06:46:38 -08:00
|
|
|
}
|