1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * searches fixed asset |
7 | * By passing in the appropriate options, you can search for available fixed asset in the system |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | status: 'DRAFT' | 'REGISTERED' | 'DISPOSED' | undefined, |
12 | page: string | undefined, |
13 | pageSize: string | undefined, |
14 | orderBy: |
15 | | 'AssetType' |
16 | | 'AssetName' |
17 | | 'AssetNumber' |
18 | | 'PurchaseDate' |
19 | | 'PurchasePrice' |
20 | | 'DisposalDate' |
21 | | 'DisposalPrice' |
22 | | undefined, |
23 | sortDirection: 'asc' | 'desc' | undefined, |
24 | filterBy: string | undefined, |
25 | xero_tenant_id: string |
26 | ) { |
27 | const url = new URL(`https://api.xero.com/assets.xro/1.0/Assets`) |
28 | for (const [k, v] of [ |
29 | ['status', status], |
30 | ['page', page], |
31 | ['pageSize', pageSize], |
32 | ['orderBy', orderBy], |
33 | ['sortDirection', sortDirection], |
34 | ['filterBy', filterBy] |
35 | ]) { |
36 | if (v !== undefined && v !== '' && k !== undefined) { |
37 | url.searchParams.append(k, v) |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: 'GET', |
42 | headers: { |
43 | Accept: 'application/json', |
44 | 'xero-tenant-id': xero_tenant_id, |
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 |
|