Retrieves all contacts in a Xero organisation

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Retrieves all contacts in a Xero organisation
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	where: string | undefined,
12
	order: string | undefined,
13
	IDs: string | undefined,
14
	page: string | undefined,
15
	includeArchived: string | undefined,
16
	summaryOnly: string | undefined,
17
	searchTerm: string | undefined,
18
	pageSize: string | undefined,
19
	xero_tenant_id: string,
20
	If_Modified_Since: string
21
) {
22
	const url = new URL(`https://api.xero.com/api.xro/2.0/Contacts`)
23
	for (const [k, v] of [
24
		['where', where],
25
		['order', order],
26
		['IDs', IDs],
27
		['page', page],
28
		['includeArchived', includeArchived],
29
		['summaryOnly', summaryOnly],
30
		['searchTerm', searchTerm],
31
		['pageSize', pageSize]
32
	]) {
33
		if (v !== undefined && v !== '' && k !== undefined) {
34
			url.searchParams.append(k, v)
35
		}
36
	}
37
	const response = await fetch(url, {
38
		method: 'GET',
39
		headers: {
40
			Accept: 'application/json',
41
			'xero-tenant-id': xero_tenant_id,
42
			'If-Modified-Since': If_Modified_Since,
43
			Authorization: 'Bearer ' + auth.token
44
		},
45
		body: undefined
46
	})
47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`${response.status} ${text}`)
50
	}
51
	return await response.json()
52
}
53