Skip to content

Commit

Permalink
use checkpoint pagination to get all organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
tc3-iwakura committed Nov 20, 2024
1 parent c0e372f commit d601d1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
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

0 comments on commit d601d1b

Please sign in to comment.