Retrieves all project tasks

Allows you to retrieve a specific project

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 all project tasks
7
 * Allows you to retrieve a specific project
8
 */
9
export async function main(
10
	auth: Xero,
11
	projectId: string,
12
	page: string | undefined,
13
	pageSize: string | undefined,
14
	taskIds: string | undefined,
15
	chargeType: 'TIME' | 'FIXED' | 'NON_CHARGEABLE' | undefined,
16
	Xero_Tenant_Id: string
17
) {
18
	const url = new URL(`https://api.xero.com/projects.xro/2.0/Projects/${projectId}/Tasks`)
19
	for (const [k, v] of [
20
		['page', page],
21
		['pageSize', pageSize],
22
		['taskIds', taskIds],
23
		['chargeType', chargeType]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			Accept: 'application/json',
33
			'Xero-Tenant-Id': Xero_Tenant_Id,
34
			Authorization: 'Bearer ' + auth.token
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44