1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * Uploads a File to the inbox |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | xero_tenant_id: string, |
12 | Idempotency_Key: string, |
13 | body: { body: string; name: string; filename: string; mimeType?: string } |
14 | ) { |
15 | const url = new URL(`https://api.xero.com/files.xro/1.0//Files`) |
16 |
|
17 | const formData = new FormData() |
18 | for (const [k, v] of Object.entries(body)) { |
19 | if (v !== undefined && v !== '') { |
20 | formData.append(k, String(v)) |
21 | } |
22 | } |
23 | const response = await fetch(url, { |
24 | method: 'POST', |
25 | headers: { |
26 | Accept: 'application/json', |
27 | 'xero-tenant-id': xero_tenant_id, |
28 | 'Idempotency-Key': Idempotency_Key, |
29 | Authorization: 'Bearer ' + auth.token |
30 | }, |
31 | body: formData |
32 | }) |
33 | if (!response.ok) { |
34 | const text = await response.text() |
35 | throw new Error(`${response.status} ${text}`) |
36 | } |
37 | return await response.json() |
38 | } |
39 |
|