From ed4dcbb5ddf3ded5ec9faa16badc128a0fcecf26 Mon Sep 17 00:00:00 2001 From: Jonathan Bennetts Date: Wed, 19 Oct 2022 16:51:09 +0100 Subject: [PATCH] fix(n8n Node): fix resource locator not returning all items (#4248) --- .../nodes-base/nodes/N8n/GenericFunctions.ts | 29 +++++++++++++++++-- .../nodes-base/nodes/N8n/WorkflowLocator.ts | 6 ++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/N8n/GenericFunctions.ts b/packages/nodes-base/nodes/N8n/GenericFunctions.ts index d40bd48490ca6..50bf67e50c845 100644 --- a/packages/nodes-base/nodes/N8n/GenericFunctions.ts +++ b/packages/nodes-base/nodes/N8n/GenericFunctions.ts @@ -24,7 +24,8 @@ export async function apiRequest( endpoint: string, body: object, query?: IDataObject, -): Promise { + // tslint:disable-next-line:no-any +): Promise { query = query || {}; type N8nApiCredentials = { @@ -39,7 +40,7 @@ export async function apiRequest( method, body, qs: query, - uri: `${baseUrl}/${endpoint}`, + uri: `${baseUrl.replace(new RegExp('/$'), '')}/${endpoint}`, json: true, }; @@ -53,6 +54,30 @@ export async function apiRequest( } } +export async function apiRequestAllItems( + this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, + method: string, + endpoint: string, + body: object, + query?: IDataObject, + // tslint:disable-next-line:no-any +): Promise { + query = query || {}; + const returnData: IDataObject[] = []; + + let nextCursor: string | undefined = undefined; + let responseData; + + do { + query.cursor = nextCursor; + query.limit = 100; + responseData = await apiRequest.call(this, method, endpoint, body, query); + returnData.push.apply(returnData, responseData.data); + nextCursor = responseData.nextCursor as string | undefined; + } while (nextCursor); + return returnData; +} + /** * Get a cursor-based paginator to use with n8n 'getAll' type endpoints. * diff --git a/packages/nodes-base/nodes/N8n/WorkflowLocator.ts b/packages/nodes-base/nodes/N8n/WorkflowLocator.ts index ae3b278bf5177..b559d57db2d93 100644 --- a/packages/nodes-base/nodes/N8n/WorkflowLocator.ts +++ b/packages/nodes-base/nodes/N8n/WorkflowLocator.ts @@ -1,5 +1,5 @@ import { ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from 'n8n-workflow'; -import { apiRequest } from './GenericFunctions'; +import { apiRequestAllItems } from './GenericFunctions'; type DataItemsResponse = { data: T[]; @@ -18,7 +18,7 @@ export async function searchWorkflows( this: ILoadOptionsFunctions, query?: string, ): Promise { - const searchResults = (await apiRequest.call( + const searchResults = (await apiRequestAllItems.call( this, 'GET', 'workflows', @@ -27,7 +27,7 @@ export async function searchWorkflows( // Map the workflows list against a simple name/id filter, and sort // with the latest on top. - const workflows = (searchResults?.data as PartialWorkflow[]) + const workflows = (searchResults as unknown as PartialWorkflow[]) .map((w: PartialWorkflow) => ({ name: `${w.name} (#${w.id})`, value: w.id,