From d601d1b6efa5472ef2c1d2b07b9e7d34e5635d8f Mon Sep 17 00:00:00 2001 From: Ryosuke Iwakura Date: Tue, 19 Nov 2024 18:10:11 +0900 Subject: [PATCH] use checkpoint pagination to get all organizations --- src/tools/auth0/client.ts | 23 +++++++++++++---------- src/tools/auth0/handlers/organizations.ts | 2 +- src/types.ts | 4 ++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/tools/auth0/client.ts b/src/tools/auth0/client.ts index 8ec35be6..3a553c7e 100644 --- a/src/tools/auth0/client.ts +++ b/src/tools/auth0/client.ts @@ -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 @@ -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; } } @@ -175,7 +178,7 @@ export default function pagedClient(client: ManagementClient): Auth0APIClient { // eslint-disable-next-line no-unused-vars export async function paginate( fetchFunc: (...paginateArgs: any) => any, - args: PagePaginationParams + args: PagePaginationParams | CheckpointPaginationParams ): Promise { // override default .getAll() behaviour using pagedClient const allItems = (await fetchFunc(args)) as unknown as T[]; diff --git a/src/tools/auth0/handlers/organizations.ts b/src/tools/auth0/handlers/organizations.ts index 6c11e3b0..aa3cfd24 100644 --- a/src/tools/auth0/handlers/organizations.ts +++ b/src/tools/auth0/handlers/organizations.ts @@ -321,7 +321,7 @@ export default class OrganizationsHandler extends DefaultHandler { try { const [organizations, clients] = await Promise.all([ paginate(this.client.organizations.getAll, { - paginate: true, + checkpoint: true, include_totals: true, }), paginate(this.client.clients.getAll, { diff --git a/src/types.ts b/src/types.ts index 08ac88bb..6dd8a761 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,8 +22,8 @@ type SharedPaginationParams = { }; export type CheckpointPaginationParams = SharedPaginationParams & { - from: string; - take: number; + from?: string; + take?: number; }; export type PagePaginationParams = SharedPaginationParams & {