Retrieves currencies for your Xero organisation

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