Retrieves sales quotes

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