We are developing some command line Google API scripts via node.js, and I'm having a hard time understanding the following:
Given this code to return the max number of our Google Groups in a single call (200):
// auth object, creds, scopes, async, etc. are above and working
const getAllGroups = await googleAdminDir.groups.list({
auth,
domain: "ourDomain.com",
nextPageToken: "nextPage",
});
Say I have a variable number of groups. How would I (sanely) utilize nextPageToken to keep repeating the request until there are no more returned results?
I haven't found yet an API endpoint so far that I could call to find data that says You have *This* many groups in your domain. So my best attempt so far is something like:
const getAllGroups = await googleAdminDir.groups.list({
auth,
domain: "myDomain.com",
nextPageToken: "nextPage",
}).then(resp =>{
let returnData = resp.data.groups;
let pageToken = data.data.nextPageToken;
return googleAdminDir.groups.list({
auth,
domain: "myDomain.com",
nextPageToken: "pageToken",
});
}).then(resp =>{
let returnData = resp.data.groups;
let pageToken = data.data.nextPageToken;
// Create .then() clauses until it errors,
// then create some way to stop it before the error is thrown?
// TODO: Hide that I did it this way
});
The above idea is....bad, and what I need help remedying. I probably missed something simple or have a "Why would you ever do it that way.....?" in here. Please enlighten me if so, I'll be humble :)