1 | type Asana = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Submit parallel requests |
6 | * Make multiple requests in parallel to Asana's API. |
7 | */ |
8 | export async function main( |
9 | auth: Asana, |
10 | opt_pretty: string | undefined, |
11 | opt_fields: string | undefined, |
12 | body: { |
13 | data?: { |
14 | actions?: { |
15 | data?: { [k: string]: unknown }; |
16 | method: "get" | "post" | "put" | "delete" | "patch" | "head"; |
17 | options?: { |
18 | fields?: string[]; |
19 | limit?: number; |
20 | offset?: number; |
21 | [k: string]: unknown; |
22 | }; |
23 | relative_path: string; |
24 | [k: string]: unknown; |
25 | }[]; |
26 | [k: string]: unknown; |
27 | }; |
28 | [k: string]: unknown; |
29 | } |
30 | ) { |
31 | const url = new URL(`https://app.asana.com/api/1.0/batch`); |
32 | for (const [k, v] of [ |
33 | ["opt_pretty", opt_pretty], |
34 | ["opt_fields", opt_fields], |
35 | ]) { |
36 | if (v !== undefined && v !== "") { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: "POST", |
42 | headers: { |
43 | "Content-Type": "application/json", |
44 | Authorization: "Bearer " + auth.token, |
45 | }, |
46 | body: JSON.stringify(body), |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|