Retrieves files

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 files
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	pagesize: string | undefined,
12
	page: string | undefined,
13
	sort: 'Name' | 'Size' | 'CreatedDateUTC' | undefined,
14
	xero_tenant_id: string
15
) {
16
	const url = new URL(`https://api.xero.com/files.xro/1.0//Files`)
17
	for (const [k, v] of [
18
		['pagesize', pagesize],
19
		['page', page],
20
		['sort', sort]
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