Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(n8n Node): fix resource locator not returning all items #4248

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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