Update a workspace

A specific, existing workspace can be updated by making a PUT request on the URL for that workspace. Only the fields provided in the data block will be updated; any unspecified fields will remain unchanged. Currently the only field that can be modified for a workspace is its name. Returns the complete, updated workspace record.

Script asana Verified

by hugo697 ยท 10/31/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Update a workspace
6
 * A specific, existing workspace can be updated by making a PUT request on the URL for that workspace. Only the fields provided in the data block will be updated; any unspecified fields will remain unchanged.
7
Currently the only field that can be modified for a workspace is its name.
8
Returns the complete, updated workspace record.
9
 */
10
export async function main(
11
  auth: Asana,
12
  workspace_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  body: {
16
    data?: { gid?: string; resource_type?: string; [k: string]: unknown } & {
17
      name?: string;
18
      [k: string]: unknown;
19
    };
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}`
25
  );
26
  for (const [k, v] of [
27
    ["opt_pretty", opt_pretty],
28
    ["opt_fields", opt_fields],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "PUT",
36
    headers: {
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48