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 Teams |
28 | * List the teams in an organization that the token's user can see. 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 | opt_fields: string | undefined, |
34 | limit: number | undefined, |
35 | offset: string | undefined |
36 | ) { |
37 | const url = new URL( |
38 | `https://app.asana.com/api/1.0/workspaces/${workspace}/teams` |
39 | ) |
40 | const params = { opt_fields, limit: limit ?? 100, offset } |
41 | for (const [key, value] of Object.entries(params)) { |
42 | if (value != null && value !== "") { |
43 | url.searchParams.append(key, String(value)) |
44 | } |
45 | } |
46 |
|
47 | const response = await fetch(url, { |
48 | headers: { |
49 | Authorization: `Bearer ${auth.token}`, |
50 | Accept: "application/json", |
51 | }, |
52 | }) |
53 |
|
54 | if (!response.ok) { |
55 | throw new Error(`${response.status} ${await response.text()}`) |
56 | } |
57 |
|
58 | return await response.json() |
59 | } |
60 |
|