1 | |
2 | type Brex = { |
3 | token: string; |
4 | }; |
5 | |
6 | * List expenses |
7 | * List expenses under the same account. Admin and bookkeeper have access to any expense, and regular users can only access their own. |
8 | */ |
9 | export async function main( |
10 | auth: Brex, |
11 | expand__: string | undefined, |
12 | user_id__: string | undefined, |
13 | parent_expense_id__: string | undefined, |
14 | budget_id__: string | undefined, |
15 | spending_entity_id__: string | undefined, |
16 | expense_type__: string | undefined, |
17 | status__: string | undefined, |
18 | payment_status__: string | undefined, |
19 | purchased_at_start: string | undefined, |
20 | purchased_at_end: string | undefined, |
21 | updated_at_start: string | undefined, |
22 | updated_at_end: string | undefined, |
23 | load_custom_fields: string | undefined, |
24 | cursor: string | undefined, |
25 | limit: string | undefined, |
26 | ) { |
27 | const url = new URL(`https://platform.brexapis.com/v1/expenses`); |
28 | for (const [k, v] of [ |
29 | ["expand[]", expand__], |
30 | ["user_id[]", user_id__], |
31 | ["parent_expense_id[]", parent_expense_id__], |
32 | ["budget_id[]", budget_id__], |
33 | ["spending_entity_id[]", spending_entity_id__], |
34 | ["expense_type[]", expense_type__], |
35 | ["status[]", status__], |
36 | ["payment_status[]", payment_status__], |
37 | ["purchased_at_start", purchased_at_start], |
38 | ["purchased_at_end", purchased_at_end], |
39 | ["updated_at_start", updated_at_start], |
40 | ["updated_at_end", updated_at_end], |
41 | ["load_custom_fields", load_custom_fields], |
42 | ["cursor", cursor], |
43 | ["limit", limit], |
44 | ]) { |
45 | if (v !== undefined && v !== "" && k !== undefined) { |
46 | url.searchParams.append(k, v); |
47 | } |
48 | } |
49 | const response = await fetch(url, { |
50 | method: "GET", |
51 | headers: { |
52 | Authorization: "Bearer " + auth.token, |
53 | }, |
54 | body: undefined, |
55 | }); |
56 | if (!response.ok) { |
57 | const text = await response.text(); |
58 | throw new Error(`${response.status} ${text}`); |
59 | } |
60 | return await response.json(); |
61 | } |
62 |
|