Retrieves an association object using a unique object ID

By passing in the appropriate options, you can retrieve an association

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 an association object using a unique object ID
7
 * By passing in the appropriate options, you can retrieve an association
8
 */
9
export async function main(
10
	auth: Xero,
11
	ObjectId: string,
12
	pagesize: string | undefined,
13
	page: string | undefined,
14
	sort: 'Name' | 'CreatedDateUTC' | undefined,
15
	direction: 'ASC' | 'DESC' | undefined,
16
	xero_tenant_id: string
17
) {
18
	const url = new URL(`https://api.xero.com/files.xro/1.0//Associations/${ObjectId}`)
19
	for (const [k, v] of [
20
		['pagesize', pagesize],
21
		['page', page],
22
		['sort', sort],
23
		['direction', direction]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			Accept: 'application/json',
33
			'xero-tenant-id': xero_tenant_id,
34
			Authorization: 'Bearer ' + auth.token
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44