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 | * Search Objects by Name |
28 | * Find tasks, projects, users, tags and other objects in a workspace by name, the way Asana's quick search does. An empty query returns recently used objects. |
29 | */ |
30 | export async function main( |
31 | auth: RT.Asana, |
32 | workspace: DynSelect_workspace, |
33 | resource_type: |
34 | | "task" |
35 | | "project" |
36 | | "user" |
37 | | "tag" |
38 | | "team" |
39 | | "portfolio" |
40 | | "goal" |
41 | | "custom_field" |
42 | | "project_template", |
43 | query: string | undefined, |
44 | count: number | undefined, |
45 | opt_fields: string | undefined |
46 | ) { |
47 | const url = new URL( |
48 | `https://app.asana.com/api/1.0/workspaces/${workspace}/typeahead` |
49 | ) |
50 | const params = { resource_type, query, count, opt_fields } |
51 | for (const [key, value] of Object.entries(params)) { |
52 | if (value != null && value !== "") { |
53 | url.searchParams.append(key, String(value)) |
54 | } |
55 | } |
56 |
|
57 | const response = await fetch(url, { |
58 | headers: { |
59 | Authorization: `Bearer ${auth.token}`, |
60 | Accept: "application/json", |
61 | }, |
62 | }) |
63 |
|
64 | if (!response.ok) { |
65 | throw new Error(`${response.status} ${await response.text()}`) |
66 | } |
67 |
|
68 | return await response.json() |
69 | } |
70 |
|