Skip to content

Commit

Permalink
pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
vtalas committed Sep 2, 2024
1 parent 389493e commit 43357c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/appmixer/wiz/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ module.exports = {

accountNameFromProfileInfo: context => {

return `${context.clientId.substring(0, 10)} ...`;
const name = context.clientId;
const threshold = 10;
if (name.length > threshold) {
return name.slice(0, 3) + '....' + name.slice(-3);
}
return name;
},

validate: async context => {
Expand Down
49 changes: 31 additions & 18 deletions src/appmixer/wiz/core/FindCloudResources/FindCloudResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const itemSchemaWithTitles = {
module.exports = {

// docs: https://win.wiz.io/reference/pull-cloud-resources
async receive(context) {
async receive(context) {

const { outputType, filter, limit = 10 } = context.messages.in.content;

Expand All @@ -116,28 +116,41 @@ module.exports = {
throw new context.CancelError('Invalid Input: Filter', e);
}
}
const { data } = await lib.makeApiCall({
context,
method: 'POST',
data: {
query,
variables: {
first: limit,
filterBy

let records = [];
let nextPageToken = null;
let totalRecordsCount = 0;
const PAGE_SIZE = 5;

do {
const { data } = await lib.makeApiCall({
context,
method: 'POST',
data: {
query,
variables: {
first: Math.min(PAGE_SIZE, limit - totalRecordsCount),
after: nextPageToken,
filterBy
}
}
});

if (data.errors) {
throw new context.CancelError(data.errors);
}
});

if (data.errors) {
throw new context.CancelError(data.errors);
}
const { pageInfo, nodes: pageRecords } = data.data.cloudResources;

context.log({ stage: 'response', data });
if (pageRecords.length === 0) {
return context.sendJson({ filter: filterBy }, 'notFound');
}

if (data.data.cloudResources.nodes.length === 0) {
return context.sendJson({ filter: filterBy }, 'notFound');
}
records = records.concat(pageRecords);
totalRecordsCount += pageRecords.length;
nextPageToken = pageInfo.endCursor;
} while (nextPageToken && totalRecordsCount < limit);

return lib.sendArrayOutput({ context, records: data.data.cloudResources.nodes, outputType });
return lib.sendArrayOutput({ context, records, outputType });
}
};

0 comments on commit 43357c1

Please sign in to comment.