1 | |
2 |
|
3 | export type DynSelect_workspace = string |
4 |
|
5 | |
6 | export async function workspace(auth: RT.Asana) { |
7 | const response = await fetch( |
8 | "https://app.asana.com/api/1.0/workspaces?limit=100", |
9 | { |
10 | headers: { |
11 | Authorization: `Bearer ${auth.token}`, |
12 | Accept: "application/json", |
13 | }, |
14 | } |
15 | ) |
16 | if (!response.ok) { |
17 | throw new Error(`${response.status} ${await response.text()}`) |
18 | } |
19 | const { data } = await response.json() |
20 | return data.map((w: { gid: string; name: string }) => ({ |
21 | value: w.gid, |
22 | label: w.name, |
23 | })) |
24 | } |
25 |
|
26 | |
27 | * List My Tasks |
28 | * List the tasks in a user's My Tasks list in a workspace (`me` by default, other users only in organizations you share). Set `completed_since` to `now` to get only incomplete tasks. Pass `next_page.offset` from a response as `offset` to get the next page. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Asana, |
32 | workspace: DynSelect_workspace, |
33 | user: string | undefined, |
34 | completed_since: string | undefined, |
35 | opt_fields: string | undefined, |
36 | limit: number | undefined, |
37 | offset: string | undefined |
38 | ) { |
39 | const headers = { |
40 | Authorization: `Bearer ${auth.token}`, |
41 | Accept: "application/json", |
42 | } |
43 |
|
44 | const listUrl = new URL( |
45 | `https://app.asana.com/api/1.0/users/${user || "me"}/user_task_list` |
46 | ) |
47 | listUrl.searchParams.append("workspace", workspace) |
48 | const listResponse = await fetch(listUrl, { headers }) |
49 | if (!listResponse.ok) { |
50 | throw new Error(`${listResponse.status} ${await listResponse.text()}`) |
51 | } |
52 | const { data: taskList } = await listResponse.json() |
53 |
|
54 | const url = new URL( |
55 | `https://app.asana.com/api/1.0/user_task_lists/${taskList.gid}/tasks` |
56 | ) |
57 | const params = { completed_since, opt_fields, limit: limit ?? 100, offset } |
58 | for (const [key, value] of Object.entries(params)) { |
59 | if (value != null && value !== "") { |
60 | url.searchParams.append(key, String(value)) |
61 | } |
62 | } |
63 |
|
64 | const response = await fetch(url, { headers }) |
65 |
|
66 | if (!response.ok) { |
67 | throw new Error(`${response.status} ${await response.text()}`) |
68 | } |
69 |
|
70 | return await response.json() |
71 | } |
72 |
|