Skip to content

Commit

Permalink
fix(n8n Node): fix resource locator not returning all items (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joffcom authored Oct 19, 2022
1 parent 971c2c0 commit ed4dcbb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
29 changes: 27 additions & 2 deletions packages/nodes-base/nodes/N8n/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export async function apiRequest(
endpoint: string,
body: object,
query?: IDataObject,
): Promise<unknown> {
// tslint:disable-next-line:no-any
): Promise<any> {
query = query || {};

type N8nApiCredentials = {
Expand All @@ -39,7 +40,7 @@ export async function apiRequest(
method,
body,
qs: query,
uri: `${baseUrl}/${endpoint}`,
uri: `${baseUrl.replace(new RegExp('/$'), '')}/${endpoint}`,
json: true,
};

Expand All @@ -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<any> {
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.
*
Expand Down
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/N8n/WorkflowLocator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from 'n8n-workflow';
import { apiRequest } from './GenericFunctions';
import { apiRequestAllItems } from './GenericFunctions';

type DataItemsResponse<T> = {
data: T[];
Expand All @@ -18,7 +18,7 @@ export async function searchWorkflows(
this: ILoadOptionsFunctions,
query?: string,
): Promise<INodeListSearchResult> {
const searchResults = (await apiRequest.call(
const searchResults = (await apiRequestAllItems.call(
this,
'GET',
'workflows',
Expand All @@ -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,
Expand Down

0 comments on commit ed4dcbb

Please sign in to comment.