Retrieves purchase orders

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 purchase orders
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	Status: 'DRAFT' | 'SUBMITTED' | 'AUTHORISED' | 'BILLED' | 'DELETED' | undefined,
12
	DateFrom: string | undefined,
13
	DateTo: string | undefined,
14
	order: string | undefined,
15
	page: string | undefined,
16
	pageSize: string | undefined,
17
	xero_tenant_id: string,
18
	If_Modified_Since: string
19
) {
20
	const url = new URL(`https://api.xero.com/api.xro/2.0/PurchaseOrders`)
21
	for (const [k, v] of [
22
		['Status', Status],
23
		['DateFrom', DateFrom],
24
		['DateTo', DateTo],
25
		['order', order],
26
		['page', page],
27
		['pageSize', pageSize]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33
	const response = await fetch(url, {
34
		method: 'GET',
35
		headers: {
36
			Accept: 'application/json',
37
			'xero-tenant-id': xero_tenant_id,
38
			'If-Modified-Since': If_Modified_Since,
39
			Authorization: 'Bearer ' + auth.token
40
		},
41
		body: undefined
42
	})
43
	if (!response.ok) {
44
		const text = await response.text()
45
		throw new Error(`${response.status} ${text}`)
46
	}
47
	return await response.json()
48
}
49