From 2f15e0e7675702c5f1cce54a217bd7f2e32aadc2 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Thu, 18 Apr 2024 16:20:51 -0700 Subject: [PATCH] Add pagination to getAppsVercel --- .../integration-auth/integration-app-list.ts | 61 ++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/backend/src/services/integration-auth/integration-app-list.ts b/backend/src/services/integration-auth/integration-app-list.ts index 17c0051c8..9cb0d822c 100644 --- a/backend/src/services/integration-auth/integration-app-list.ts +++ b/backend/src/services/integration-auth/integration-app-list.ts @@ -129,26 +129,55 @@ const getAppsHeroku = async ({ accessToken }: { accessToken: string }) => { * Return list of names of apps for Vercel integration */ const getAppsVercel = async ({ accessToken, teamId }: { teamId?: string | null; accessToken: string }) => { - const res = ( - await request.get<{ projects: { name: string; id: string }[] }>(`${IntegrationUrls.VERCEL_API_URL}/v9/projects`, { + const apps: Array<{ name: string; appId: string }> = []; + + const limit = "20"; + let hasMorePages = true; + let next: number | null = null; + + interface Response { + projects: { name: string; id: string }[]; + pagination: { + count: number; + next: number | null; + prev: number; + }; + } + + while (hasMorePages) { + const params: { [key: string]: string } = { + limit + }; + + if (teamId) { + params.teamId = teamId; + } + + if (next) { + params.until = String(next); + } + + const { data } = await request.get(`${IntegrationUrls.VERCEL_API_URL}/v9/projects`, { + params: new URLSearchParams(params), headers: { Authorization: `Bearer ${accessToken}`, "Accept-Encoding": "application/json" - }, - ...(teamId - ? { - params: { - teamId - } - } - : {}) - }) - ).data; + } + }); - const apps = res.projects.map((a) => ({ - name: a.name, - appId: a.id - })); + data.projects.forEach((a) => { + apps.push({ + name: a.name, + appId: a.id + }); + }); + + next = data.pagination.next; + + if (data.pagination.next === null) { + hasMorePages = false; + } + } return apps; };