0

Retrieve all statements

by
Published Dec 20, 2024

By passing in parameters, you can search for matching statements

Script xero Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Retrieve all statements
7
 * By passing in parameters, you can search for matching statements
8
 */
9
export async function main(
10
	auth: Xero,
11
	page: string | undefined,
12
	pageSize: string | undefined,
13
	Xero_Tenant_Id: string,
14
	Xero_Application_Id: string,
15
	Xero_User_Id: string
16
) {
17
	const url = new URL(`https://api.xero.com/bankfeeds.xro/1.0/Statements`)
18
	for (const [k, v] of [
19
		['page', page],
20
		['pageSize', pageSize]
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
			'Xero-Application-Id': Xero_Application_Id,
32
			'Xero-User-Id': Xero_User_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