0
Create an Input for future use in a script or flow
One script reply has been approved by the moderators Verified
Created by admin 263 days ago Viewed 5393 times
0
Submitted by admin Typescript (fetch-only)
Verified 263 days ago
1
/**
2
 * Create an Input for future use in a script or flow
3
 *
4
 */
5
export async function main(
6
  workspace: string,
7
  runnable_id: string | undefined,
8
  runnable_type: "ScriptHash" | "ScriptPath" | "FlowPath" | undefined,
9
  body: { name: string; args: { [k: string]: unknown }; [k: string]: unknown }
10
) {
11
  const url = new URL(`${BASE_URL}/api/w/${workspace}/inputs/create`);
12
  for (const [k, v] of [
13
    ["runnable_id", runnable_id],
14
    ["runnable_type", runnable_type],
15
  ]) {
16
    if (v !== undefined && v !== "") {
17
      url.searchParams.append(k, v);
18
    }
19
  }
20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      "Content-Type": "application/json",
24
      Authorization: "Bearer " + WM_TOKEN,
25
    },
26
    body: JSON.stringify(body),
27
  });
28
  if (!response.ok) {
29
    const text = await response.text();
30
    throw new Error(`${response.status} ${text}`);
31
  }
32
  return await response.text();
33
}
34