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

This commit is contained in:
Jonathan Bennetts 2022-10-19 16:51:09 +01:00 committed by GitHub
parent 971c2c0aed
commit ed4dcbb5dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View file

@ -24,7 +24,8 @@ export async function apiRequest(
endpoint: string, endpoint: string,
body: object, body: object,
query?: IDataObject, query?: IDataObject,
): Promise<unknown> { // tslint:disable-next-line:no-any
): Promise<any> {
query = query || {}; query = query || {};
type N8nApiCredentials = { type N8nApiCredentials = {
@ -39,7 +40,7 @@ export async function apiRequest(
method, method,
body, body,
qs: query, qs: query,
uri: `${baseUrl}/${endpoint}`, uri: `${baseUrl.replace(new RegExp('/$'), '')}/${endpoint}`,
json: true, 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<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. * Get a cursor-based paginator to use with n8n 'getAll' type endpoints.
* *

View file

@ -1,5 +1,5 @@
import { ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from 'n8n-workflow'; import { ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from 'n8n-workflow';
import { apiRequest } from './GenericFunctions'; import { apiRequestAllItems } from './GenericFunctions';
type DataItemsResponse<T> = { type DataItemsResponse<T> = {
data: T[]; data: T[];
@ -18,7 +18,7 @@ export async function searchWorkflows(
this: ILoadOptionsFunctions, this: ILoadOptionsFunctions,
query?: string, query?: string,
): Promise<INodeListSearchResult> { ): Promise<INodeListSearchResult> {
const searchResults = (await apiRequest.call( const searchResults = (await apiRequestAllItems.call(
this, this,
'GET', 'GET',
'workflows', 'workflows',
@ -27,7 +27,7 @@ export async function searchWorkflows(
// Map the workflows list against a simple name/id filter, and sort // Map the workflows list against a simple name/id filter, and sort
// with the latest on top. // with the latest on top.
const workflows = (searchResults?.data as PartialWorkflow[]) const workflows = (searchResults as unknown as PartialWorkflow[])
.map((w: PartialWorkflow) => ({ .map((w: PartialWorkflow) => ({
name: `${w.name} (#${w.id})`, name: `${w.name} (#${w.id})`,
value: w.id, value: w.id,