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 | * Create Task |
28 | * Create a task in a workspace, optionally in projects or as a subtask of `parent`. Dates: `due_on`/`start_on` as YYYY-MM-DD, or `due_at` as an ISO 8601 date-time (set one of due_on/due_at). `custom_fields` maps custom field gids to values, e.g. {"1234": "High"}; enum fields take the enum option gid. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Asana, |
32 | workspace: DynSelect_workspace, |
33 | name: string, |
34 | notes: string | undefined, |
35 | html_notes: string | undefined, |
36 | assignee: string | undefined, |
37 | projects: string[] | undefined, |
38 | parent: string | undefined, |
39 | due_on: string | undefined, |
40 | due_at: string | undefined, |
41 | start_on: string | undefined, |
42 | tags: string[] | undefined, |
43 | followers: string[] | undefined, |
44 | assignee_section: string | undefined, |
45 | completed: boolean | undefined, |
46 | custom_fields: { [key: string]: any } | undefined |
47 | ) { |
48 | const data = { |
49 | workspace, |
50 | name, |
51 | notes: notes || undefined, |
52 | html_notes: html_notes || undefined, |
53 | assignee: assignee || undefined, |
54 | projects: projects?.length ? projects : undefined, |
55 | parent: parent || undefined, |
56 | due_on: due_on || undefined, |
57 | due_at: due_at || undefined, |
58 | start_on: start_on || undefined, |
59 | tags: tags?.length ? tags : undefined, |
60 | followers: followers?.length ? followers : undefined, |
61 | assignee_section: assignee_section || undefined, |
62 | completed: completed || undefined, |
63 | custom_fields: |
64 | custom_fields && Object.keys(custom_fields).length |
65 | ? custom_fields |
66 | : undefined, |
67 | } |
68 |
|
69 | const response = await fetch("https://app.asana.com/api/1.0/tasks", { |
70 | method: "POST", |
71 | headers: { |
72 | Authorization: `Bearer ${auth.token}`, |
73 | "Content-Type": "application/json", |
74 | Accept: "application/json", |
75 | }, |
76 | body: JSON.stringify({ data }), |
77 | }) |
78 |
|
79 | if (!response.ok) { |
80 | throw new Error(`${response.status} ${await response.text()}`) |
81 | } |
82 |
|
83 | return await response.json() |
84 | } |
85 |
|