0

Scroll over all companies

by
Published Dec 20, 2024

The `list all companies` functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply.

Script intercom Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Intercom = {
3
	apiVersion: string
4
	token: string
5
}
6
/**
7
 * Scroll over all companies
8
 *       The `list all companies` functionality does not work well for huge datasets, and can result in errors and performance problems when paging deeply.
9
 */
10
export async function main(auth: Intercom, scroll_param: string | undefined) {
11
	const url = new URL(`https://api.intercom.io/companies/scroll`)
12
	for (const [k, v] of [['scroll_param', scroll_param]]) {
13
		if (v !== undefined && v !== '' && k !== undefined) {
14
			url.searchParams.append(k, v)
15
		}
16
	}
17
	const response = await fetch(url, {
18
		method: 'GET',
19
		headers: {
20
			'Intercom-Version': auth.apiVersion,
21
			Authorization: 'Bearer ' + auth.token
22
		},
23
		body: undefined
24
	})
25
	if (!response.ok) {
26
		const text = await response.text()
27
		throw new Error(`${response.status} ${text}`)
28
	}
29
	return await response.json()
30
}
31