0
Get a custom field
One script reply has been approved by the moderators Verified

Get the complete definition of a custom field’s metadata.

Since custom fields can be defined for one of a number of types, and these types have different data and behaviors, there are fields that are relevant to a particular type. For instance, as noted above, enum_options is only relevant for the enum type and defines the set of choices that the enum could represent. The examples below show some of these type-specific custom field definitions.

Created by hugo697 313 days ago Viewed 8959 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 313 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Get a custom field
6
 * Get the complete definition of a custom field’s metadata.
7

8
Since custom fields can be defined for one of a number of types, and
9
these types have different data and behaviors, there are fields that are
10
relevant to a particular type. For instance, as noted above, enum_options
11
is only relevant for the enum type and defines the set of choices that
12
the enum could represent. The examples below show some of these
13
type-specific custom field definitions.
14
 */
15
export async function main(
16
  auth: Asana,
17
  custom_field_gid: string,
18
  opt_pretty: string | undefined,
19
  opt_fields: string | undefined
20
) {
21
  const url = new URL(
22
    `https://app.asana.com/api/1.0/custom_fields/${custom_field_gid}`
23
  );
24
  for (const [k, v] of [
25
    ["opt_pretty", opt_pretty],
26
    ["opt_fields", opt_fields],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45