Retrieves report for balancesheet

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