Apply Dali's suggestions

This commit is contained in:
Iván Ovejero 2021-05-11 15:09:59 +02:00
parent f5ff3d212b
commit 0143bca54c
2 changed files with 28 additions and 4 deletions

View file

@ -60,7 +60,19 @@ export async function zohoApiRequest(
}
try {
return await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options);
let responseData = await this.helpers.requestOAuth2?.call(this, 'zohoOAuth2Api', options);
if (responseData === undefined) return [];
throwOnErrorStatus.call(this, responseData);
const operation = this.getNodeParameter('operation', 0) as string;
if (['create', 'delete', 'update', 'get'].includes(operation)) {
responseData = responseData.data[0];
}
return responseData;
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
@ -85,9 +97,9 @@ export async function zohoApiRequestAllItems(
do {
responseData = await zohoApiRequest.call(this, method, endpoint, body, qs, uri);
if (!responseData) return [];
if (Array.isArray(responseData) && !responseData.length) return returnData;
returnData.push(...responseData.data);
uri = responseData.info.more_records;
returnData.push.apply(returnData, responseData['data']);
qs.page++;
} while (
responseData.info.more_records !== undefined &&
@ -126,6 +138,15 @@ export function throwOnEmptyUpdate(this: IExecuteFunctions, resource: string) {
);
}
export function throwOnErrorStatus(
this: IExecuteFunctions | IHookFunctions | ILoadOptionsFunctions,
responseData: { data?: Array<{ status: string, message: string }> },
) {
if (responseData?.data?.[0].status === 'error') {
throw new NodeOperationError(this.getNode(), responseData as Error);
}
}
// ----------------------------------------
// required field adjusters
// ----------------------------------------

View file

@ -8,6 +8,8 @@ import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import {
@ -23,6 +25,7 @@ import {
adjustVendorPayload,
handleListing,
throwOnEmptyUpdate,
throwOnErrorStatus,
toLoadOptions,
zohoApiRequest,
zohoApiRequestAllItems,
@ -1001,7 +1004,7 @@ export class ZohoCrm implements INodeType {
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(...responseData.data);
: returnData.push(responseData);
}