-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move getAllUsagePlans to a "shared" file
- Loading branch information
Alex Chew
committed
Jun 26, 2019
1 parent
5bf6b1c
commit 6cdc771
Showing
3 changed files
with
69 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Fetches all usage plans, combining all pages into a single array. | ||
* | ||
* @param apiGateway | ||
* an instance of `AWS.APIGateway` to use for API calls | ||
* | ||
* @param paramOverrides | ||
* a parameter object passed in calls to `APIGateway.getUsagePlans` | ||
* | ||
* @returns | ||
* a Promise resolving with an array of items returned from | ||
* `APIGateway.getUsagePlans` calls | ||
*/ | ||
async function getAllUsagePlans(apiGateway, paramOverrides = {}) { | ||
// The maximum allowed value of `limit` is 500 according to | ||
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property | ||
const defaultParams = {limit: 500, ...paramOverrides} | ||
|
||
console.log('Fetching first page of usage plans') | ||
let response = await apiGateway.getUsagePlans(defaultParams).promise() | ||
const usagePlans = response.items | ||
|
||
while (response.position) { | ||
console.log(`Fetching next page of usage plans, at position=[${response.position}]`) | ||
const nextParams = {...defaultParams, position: response.position} | ||
response = await apiGateway.getUsagePlans(nextParams).promise() | ||
usagePlans.push(...response.items) | ||
} | ||
|
||
return usagePlans | ||
} | ||
|
||
module.exports = { getAllUsagePlans } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Fetches all usage plans, combining all pages into a single array. | ||
* | ||
* @param apiGateway | ||
* an instance of `AWS.APIGateway` to use for API calls | ||
* | ||
* @param paramOverrides | ||
* a parameter object passed in calls to `APIGateway.getUsagePlans` | ||
* | ||
* @returns | ||
* a Promise resolving with an array of items returned from | ||
* `APIGateway.getUsagePlans` calls | ||
*/ | ||
async function getAllUsagePlans(apiGateway, paramOverrides = {}) { | ||
// The maximum allowed value of `limit` is 500 according to | ||
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#getUsagePlans-property | ||
const defaultParams = {limit: 500, ...paramOverrides} | ||
|
||
console.log('Fetching first page of usage plans') | ||
let response = await apiGateway.getUsagePlans(defaultParams).promise() | ||
const usagePlans = response.items | ||
|
||
while (response.position) { | ||
console.log(`Fetching next page of usage plans, at position=[${response.position}]`) | ||
const nextParams = {...defaultParams, position: response.position} | ||
response = await apiGateway.getUsagePlans(nextParams).promise() | ||
usagePlans.push(...response.items) | ||
} | ||
|
||
return usagePlans | ||
} | ||
|
||
module.exports = { getAllUsagePlans } |