2020-02-12 07:04:43 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError
|
2020-02-12 07:04:43 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function zohoApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body: {
|
|
|
|
data: [
|
|
|
|
body,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
qs,
|
|
|
|
uri: uri || `https://www.zohoapis.com/crm/v2${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-02-12 07:04:43 -08:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
//@ts-ignore
|
2020-06-01 17:42:38 -07:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'zohoOAuth2Api', options);
|
2020-02-12 07:04:43 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-02-12 07:04:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function zohoApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string ,method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
let uri: string | undefined;
|
2020-02-15 16:23:22 -08:00
|
|
|
query.per_page = 200;
|
|
|
|
query.page = 0;
|
2020-02-12 07:04:43 -08:00
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await zohoApiRequest.call(this, method, endpoint, body, query, uri);
|
2020-02-15 16:23:22 -08:00
|
|
|
uri = responseData.info.more_records;
|
2020-02-12 07:04:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
2020-02-15 16:23:22 -08:00
|
|
|
query.page++;
|
2020-02-12 07:04:43 -08:00
|
|
|
} while (
|
2020-02-15 16:23:22 -08:00
|
|
|
responseData.info.more_records !== undefined &&
|
|
|
|
responseData.info.more_records === true
|
2020-02-12 07:04:43 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|