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

Use checkpoint pagination to get all organizations #984

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/tools/auth0/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ function checkpointPaginator(
const { checkpoint, ...newArgs } = _.cloneDeep(args[0]);

// fetch the total to validate records match
const { total } = await client.pool
.addSingleTask({
data: newArgs,
generator: (requestArgs) => target[name](requestArgs),
})
.promise();
const total =
(
await client.pool
.addSingleTask({
data: newArgs,
generator: (requestArgs) => target[name](requestArgs),
})
.promise()
).data?.total || 0;

let done = false;
// use checkpoint pagination to allow fetching 1000+ results
Expand All @@ -57,11 +60,11 @@ function checkpointPaginator(
})
.promise();

data.push(...getEntity(rsp));
if (!rsp.next) {
data.push(...getEntity(rsp.data));
if (!rsp.data.next) {
done = true;
} else {
newArgs.from = rsp.next;
newArgs.from = rsp.data.next;
}
}

Expand Down Expand Up @@ -175,7 +178,7 @@ export default function pagedClient(client: ManagementClient): Auth0APIClient {
// eslint-disable-next-line no-unused-vars
export async function paginate<T>(
fetchFunc: (...paginateArgs: any) => any,
args: PagePaginationParams
args: PagePaginationParams | CheckpointPaginationParams
): Promise<T[]> {
// override default <T>.getAll() behaviour using pagedClient
const allItems = (await fetchFunc(args)) as unknown as T[];
Expand Down
2 changes: 1 addition & 1 deletion src/tools/auth0/handlers/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export default class OrganizationsHandler extends DefaultHandler {
try {
const [organizations, clients] = await Promise.all([
paginate<GetOrganizations200ResponseOneOfInner>(this.client.organizations.getAll, {
paginate: true,
checkpoint: true,
include_totals: true,
}),
paginate<Client>(this.client.clients.getAll, {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type SharedPaginationParams = {
};

export type CheckpointPaginationParams = SharedPaginationParams & {
from: string;
take: number;
from?: string;
take?: number;
};

export type PagePaginationParams = SharedPaginationParams & {
Expand Down
Loading
Loading