From 0143bca54caed213d1aa62b86d974388f4308e09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Tue, 11 May 2021 15:09:59 +0200 Subject: [PATCH] :zap: Apply Dali's suggestions --- .../nodes-base/nodes/Zoho/GenericFunctions.ts | 27 ++++++++++++++++--- .../nodes-base/nodes/Zoho/ZohoCrm.node.ts | 5 +++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/nodes-base/nodes/Zoho/GenericFunctions.ts b/packages/nodes-base/nodes/Zoho/GenericFunctions.ts index 2efc7fffc6..55c8956695 100644 --- a/packages/nodes-base/nodes/Zoho/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Zoho/GenericFunctions.ts @@ -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 // ---------------------------------------- diff --git a/packages/nodes-base/nodes/Zoho/ZohoCrm.node.ts b/packages/nodes-base/nodes/Zoho/ZohoCrm.node.ts index 4204368c8a..9d4d2f2486 100644 --- a/packages/nodes-base/nodes/Zoho/ZohoCrm.node.ts +++ b/packages/nodes-base/nodes/Zoho/ZohoCrm.node.ts @@ -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); }