Retrieve a list of budgets

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