Retrieves report for aged payables by contact

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