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