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: Support limit flag for Box Search #323

Merged
merged 8 commits into from
May 4, 2022
8 changes: 6 additions & 2 deletions src/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ class SearchCommand extends BoxCommand {

let results = await this.client.search.query(args.query || null, options);

// Hard limit the search results to avoid slamming the API
// Limit the search results according to the --limit flag value (if specified) or RESULTS_LIMIT value
const itemsLimit = flags.limit || RESULTS_LIMIT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you also need to modify line 30 where options.limit is set to use flags.limit instead of the hardcoded RESULTS_LIMIT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no. We don't want to use limit parameter the same as API understands that. For API limit means number of items per page, but CLI makes pagination for us. So in our case limit means total number of items returned by CLI. You can also read this thread: https://box.slack.com/archives/C6KSD7C91/p1651243355904129

let limitedResults = [];
for await (let result of { [Symbol.asyncIterator]: () => results }) {
let numResults = limitedResults.push(result);
if (numResults >= RESULTS_LIMIT) {
if (numResults >= itemsLimit) {
break;
}
}
Expand Down Expand Up @@ -284,6 +285,9 @@ SearchCommand.flags = {
'desc'
]
}),
limit: flags.integer({
description: 'Defines the maximum number of items to return. Default value is 100.',
}),
'include-recent-shared-links': flags.boolean({
description: 'Returns shared links that the user recently accessed'
}),
Expand Down