Retrieves sales invoices or purchase bills

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 sales invoices or purchase bills
7
 *
8
 */
9
export async function main(
10
	auth: Xero,
11
	where: string | undefined,
12
	order: string | undefined,
13
	IDs: string | undefined,
14
	InvoiceNumbers: string | undefined,
15
	ContactIDs: string | undefined,
16
	Statuses: string | undefined,
17
	page: string | undefined,
18
	includeArchived: string | undefined,
19
	createdByMyApp: string | undefined,
20
	unitdp: string | undefined,
21
	summaryOnly: string | undefined,
22
	pageSize: string | undefined,
23
	searchTerm: string | undefined,
24
	xero_tenant_id: string,
25
	If_Modified_Since: string
26
) {
27
	const url = new URL(`https://api.xero.com/api.xro/2.0/Invoices`)
28
	for (const [k, v] of [
29
		['where', where],
30
		['order', order],
31
		['IDs', IDs],
32
		['InvoiceNumbers', InvoiceNumbers],
33
		['ContactIDs', ContactIDs],
34
		['Statuses', Statuses],
35
		['page', page],
36
		['includeArchived', includeArchived],
37
		['createdByMyApp', createdByMyApp],
38
		['unitdp', unitdp],
39
		['summaryOnly', summaryOnly],
40
		['pageSize', pageSize],
41
		['searchTerm', searchTerm]
42
	]) {
43
		if (v !== undefined && v !== '' && k !== undefined) {
44
			url.searchParams.append(k, v)
45
		}
46
	}
47
	const response = await fetch(url, {
48
		method: 'GET',
49
		headers: {
50
			Accept: 'application/json',
51
			'xero-tenant-id': xero_tenant_id,
52
			'If-Modified-Since': If_Modified_Since,
53
			Authorization: 'Bearer ' + auth.token
54
		},
55
		body: undefined
56
	})
57
	if (!response.ok) {
58
		const text = await response.text()
59
		throw new Error(`${response.status} ${text}`)
60
	}
61
	return await response.json()
62
}
63