MCPcopy Index your code
hub / github.com/simstudioai/sim / drainGooglePagedList

Function drainGooglePagedList

apps/sim/lib/oauth/google-pagination.ts:65–108  ·  view source on GitHub ↗
(
  options: DrainGooglePagedListOptions<T, R>
)

Source from the content-addressed store, hash-verified

63 * their existing error-response shapes.
64 */
65export async function drainGooglePagedList<T, R>(
66 options: DrainGooglePagedListOptions<T, R>
67): Promise<GoogleDrainResult<T>> {
68 const {
69 buildUrl,
70 fetch: fetchPage,
71 parseError,
72 getItems,
73 getNextPageToken,
74 maxPages,
75 label,
76 } = options
77
78 const items: T[] = []
79 let pageToken: string | undefined
80 let truncated = false
81
82 for (let page = 0; page < maxPages; page++) {
83 const response = await fetchPage(buildUrl(pageToken))
84
85 if (!response.ok) {
86 throw new GooglePageError(response.status, await parseError(response))
87 }
88
89 const body = (await response.json()) as R
90
91 const pageItems = getItems(body)
92 if (pageItems?.length) {
93 items.push(...pageItems)
94 }
95
96 pageToken = getNextPageToken(body)?.trim() || undefined
97 if (!pageToken) {
98 return { items, truncated }
99 }
100
101 if (page === maxPages - 1) {
102 truncated = true
103 logger.warn(`${label}: hit pagination cap of ${maxPages} pages; results may be incomplete`)
104 }
105 }
106
107 return { items, truncated }
108}

Callers

nothing calls this directly

Calls 4

getItemsFunction · 0.85
warnMethod · 0.65
buildUrlFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected