0
update variable
One script reply has been approved by the moderators Verified
Created by admin 480 days ago Viewed 20418 times
0
Submitted by admin Typescript (fetch-only)
Verified 480 days ago
1
/**
2
 * update variable
3
 *
4
 */
5
export async function main(
6
  workspace: string,
7
  path: string,
8
  already_encrypted: string | undefined,
9
  body: {
10
    path?: string;
11
    value?: string;
12
    is_secret?: boolean;
13
    description?: string;
14
    [k: string]: unknown;
15
  }
16
) {
17
  const url = new URL(
18
    `${BASE_URL}/api/w/${workspace}/variables/update/${path}`
19
  );
20
  for (const [k, v] of [["already_encrypted", already_encrypted]]) {
21
    if (v !== undefined && v !== "") {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + WM_TOKEN,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.text();
38
}
39