Retrieves linked transactions (billable expenses)

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 linked transactions (billable expenses)
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	page: string | undefined,
12
	LinkedTransactionID: string | undefined,
13
	SourceTransactionID: string | undefined,
14
	ContactID: string | undefined,
15
	Status: string | undefined,
16
	TargetTransactionID: string | undefined,
17
	xero_tenant_id: string
18
) {
19
	const url = new URL(`https://api.xero.com/api.xro/2.0/LinkedTransactions`)
20
	for (const [k, v] of [
21
		['page', page],
22
		['LinkedTransactionID', LinkedTransactionID],
23
		['SourceTransactionID', SourceTransactionID],
24
		['ContactID', ContactID],
25
		['Status', Status],
26
		['TargetTransactionID', TargetTransactionID]
27
	]) {
28
		if (v !== undefined && v !== '' && k !== undefined) {
29
			url.searchParams.append(k, v)
30
		}
31
	}
32
	const response = await fetch(url, {
33
		method: 'GET',
34
		headers: {
35
			Accept: 'application/json',
36
			'xero-tenant-id': xero_tenant_id,
37
			Authorization: 'Bearer ' + auth.token
38
		},
39
		body: undefined
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47