0

Retrieves folders

by
Published Dec 20, 2024

By passing in the appropriate options, you can search for available folders

Script xero Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Xero = {
3
	token: string
4
}
5
/**
6
 * Retrieves folders
7
 * By passing in the appropriate options, you can search for available folders
8
 */
9
export async function main(
10
	auth: Xero,
11
	sort: 'Name' | 'Size' | 'CreatedDateUTC' | undefined,
12
	xero_tenant_id: string
13
) {
14
	const url = new URL(`https://api.xero.com/files.xro/1.0//Folders`)
15
	for (const [k, v] of [['sort', sort]]) {
16
		if (v !== undefined && v !== '' && k !== undefined) {
17
			url.searchParams.append(k, v)
18
		}
19
	}
20
	const response = await fetch(url, {
21
		method: 'GET',
22
		headers: {
23
			Accept: 'application/json',
24
			'xero-tenant-id': xero_tenant_id,
25
			Authorization: 'Bearer ' + auth.token
26
		},
27
		body: undefined
28
	})
29
	if (!response.ok) {
30
		const text = await response.text()
31
		throw new Error(`${response.status} ${text}`)
32
	}
33
	return await response.json()
34
}
35