Skip to content

Commit

Permalink
update the pagination logic and eliminate any unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stamsy committed Dec 11, 2024
1 parent ceec48e commit aef9f83
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 82 deletions.
52 changes: 20 additions & 32 deletions packages/nodes-base/nodes/Aws/IAM/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ import type {
import { ApplicationError, NodeApiError, NodeOperationError } from 'n8n-workflow';

/* Function which helps while developing the node */
// ToDo: Remove before completing the pull request
export async function presendTest(
this: IExecuteSingleFunctions,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
console.log('requestOptions', requestOptions);
return requestOptions;
}

/*
* Helper function which stringifies the body before sending the request.
Expand Down Expand Up @@ -145,13 +137,16 @@ export async function processGroupsResponse(
items: INodeExecutionData[],
response: IN8nHttpFullResponse,
): Promise<INodeExecutionData[]> {
const responseBody = response.body as { Groups: IDataObject[] };
const responseBody = response.body as {
ListGroupsResponse: { ListGroupsResult: { Groups: IDataObject[] } };
};
const data = responseBody.ListGroupsResponse.ListGroupsResult.Groups;

if (!responseBody || !Array.isArray(responseBody.Groups)) {
if (!responseBody || !Array.isArray(data)) {
throw new ApplicationError('Unexpected response format: No groups found.');
}

const executionData: INodeExecutionData[] = responseBody.Groups.map((group) => ({
const executionData: INodeExecutionData[] = data.map((group) => ({
json: group,
}));

Expand Down Expand Up @@ -179,8 +174,6 @@ export async function processUsersResponse(
return executionData;
}

const possibleRootProperties = ['Users', 'Groups'];
// ToDo: Test if pagination works
export async function handlePagination(
this: IExecutePaginationFunctions,
resultOptions: DeclarativeRestApiSettings.ResultOptions,
Expand All @@ -189,7 +182,6 @@ export async function handlePagination(
let nextPageToken: string | undefined;
const returnAll = this.getNodeParameter('returnAll') as boolean;
let limit = 60;
console.log('Entered pagination!!!!');
// Update limit if 'returnAll' is not selected
if (!returnAll) {
limit = this.getNodeParameter('limit') as number;
Expand All @@ -208,39 +200,35 @@ export async function handlePagination(
...body,
PaginationToken: nextPageToken,
} as IDataObject;
console.log('Updated request body with PaginationToken:', resultOptions.options.body);
}

// Make the request
console.log('Sending request with options:', resultOptions);
const responseData = await this.makeRoutingRequest(resultOptions);

// Process response data
for (const page of responseData) {
console.log('Processing page:', page.json);

// Iterate over possible root properties (e.g., "Users")
for (const prop of possibleRootProperties) {
if (page.json[prop]) {
const currentData = page.json[prop] as IDataObject[];
console.log(`Extracted data from property "${prop}":`, currentData);
aggregatedResult.push(...currentData);
if (responseData && Array.isArray(responseData)) {
for (const page of responseData) {
aggregatedResult.push(page.json);

// Check if the limit has been reached
if (!returnAll && aggregatedResult.length >= limit) {
return aggregatedResult.slice(0, limit).map((item) => ({ json: item }));
}

// Update the nextPageToken for the next request
nextPageToken = page.json.PaginationToken as string | undefined;
}
} else if (responseData && typeof responseData === 'object') {
aggregatedResult.push(responseData as IDataObject);

nextPageToken = (responseData as IDataObject).PaginationToken as string | undefined;

// Check if the limit has been reached
if (!returnAll && aggregatedResult.length >= limit) {
console.log('Limit reached. Returning results.');
return aggregatedResult.slice(0, limit).map((item) => ({ json: item }));
}

// Update the nextPageToken for the next request
nextPageToken = page.json.PaginationToken as string | undefined;
console.log('Next Page Token:', nextPageToken);
}
} while (nextPageToken);

console.log('Final Aggregated Results:', aggregatedResult);
return aggregatedResult.map((item) => ({ json: item }));
}

Expand Down
38 changes: 19 additions & 19 deletions packages/nodes-base/nodes/Aws/IAM/descriptions/GroupDescription.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { IExecuteSingleFunctions, IHttpRequestOptions, INodeProperties } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import type { INodeProperties } from 'n8n-workflow';

import { handleErrorPostReceive, presendTest, validatePath } from '../GenericFunctions';
import {
handleErrorPostReceive,
handlePagination,
processGroupsResponse,
validatePath,
} from '../GenericFunctions';

export const groupOperations: INodeProperties[] = [
{
Expand Down Expand Up @@ -37,9 +41,6 @@ export const groupOperations: INodeProperties[] = [
value: 'delete',
description: 'Delete an existing group',
routing: {
send: {
preSend: [presendTest], // ToDo: Remove this line before completing the pull request
},
request: {
method: 'POST',
url: '=?Action=DeleteGroup&Version=2010-05-08&GroupName={{$parameter["GroupName"]}}',
Expand All @@ -64,9 +65,6 @@ export const groupOperations: INodeProperties[] = [
value: 'get',
description: 'Retrieve details of an existing group',
routing: {
// send: {
// preSend: [presendTest], // ToDo: Remove this line before completing the pull request
// },
request: {
method: 'POST',
url: '=?Action=GetGroup&Version=2010-05-08&GroupName={{$parameter["GroupName"]}}',
Expand All @@ -83,13 +81,23 @@ export const groupOperations: INodeProperties[] = [
value: 'getAll',
description: 'Retrieve a list of groups',
routing: {
send: {
paginate: true,
},
operations: {
pagination: handlePagination,
},
request: {
method: 'POST',
url: '=/?Action=ListGroups&Version=2010-05-08',
qs: {
pageSize:
'={{ $parameter["limit"] ? ($parameter["limit"] < 60 ? $parameter["limit"] : 60) : 60 }}',
},
ignoreHttpStatusErrors: true,
},
output: {
postReceive: [handleErrorPostReceive],
postReceive: [handleErrorPostReceive, processGroupsResponse],
},
},
action: 'Get many groups',
Expand All @@ -105,15 +113,7 @@ export const groupOperations: INodeProperties[] = [
ignoreHttpStatusErrors: true,
},
output: {
postReceive: [
handleErrorPostReceive,
// {
// type: 'set',
// properties: {
// value: '={{ { "updated": true } }}',
// },
// },
],
postReceive: [handleErrorPostReceive],
},
},
action: 'Update group',
Expand Down
44 changes: 13 additions & 31 deletions packages/nodes-base/nodes/Aws/IAM/descriptions/UserDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { INodeProperties } from 'n8n-workflow';

import {
handleErrorPostReceive,
handlePagination,
presendFields,
presendTest,
processUsersResponse,
validatePath,
} from '../GenericFunctions';
Expand All @@ -28,10 +28,7 @@ export const userOperations: INodeProperties[] = [
action: 'Add user to group',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand All @@ -50,10 +47,7 @@ export const userOperations: INodeProperties[] = [
action: 'Create user',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand All @@ -72,10 +66,7 @@ export const userOperations: INodeProperties[] = [
action: 'Delete user',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand All @@ -94,10 +85,7 @@ export const userOperations: INodeProperties[] = [
action: 'Get user',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand All @@ -114,16 +102,17 @@ export const userOperations: INodeProperties[] = [
value: 'getAll',
description: 'Retrieve a list of users',
routing: {
// send: {
// paginate: true,
// preSend: [presendFields]
// },
send: {
preSend: [presendTest], // ToDo: Remove this line before completing the pull request
paginate: true,
},
operations: { pagination: handlePagination },
request: {
method: 'POST',
url: '/?Action=ListUsers&Version=2010-05-08',
qs: {
pageSize:
'={{ $parameter["limit"] ? ($parameter["limit"] < 60 ? $parameter["limit"] : 60) : 60 }}',
},
ignoreHttpStatusErrors: true,
},
output: {
Expand All @@ -139,10 +128,7 @@ export const userOperations: INodeProperties[] = [
action: 'Remove user from group',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand All @@ -161,10 +147,7 @@ export const userOperations: INodeProperties[] = [
action: 'Update user',
routing: {
send: {
preSend: [
presendTest, // ToDo: Remove this line before completing the pull request
presendFields,
],
preSend: [presendFields],
},
request: {
method: 'POST',
Expand Down Expand Up @@ -353,7 +336,6 @@ const getAllFields: INodeProperties[] = [
property: 'Limit',
type: 'query',
value: '={{ $value }}',
preSend: [presendTest],
},
},
type: 'number',
Expand Down

0 comments on commit aef9f83

Please sign in to comment.