run script preview

Script windmill Verified

by admin ยท 8/13/2023

The script

Submitted by admin Typescript (fetch-only)
Verified 370 days ago
1
/**
2
 * run script preview
3
 *
4
 */
5
export async function main(
6
  workspace: string,
7
  include_header: string | undefined,
8
  invisible_to_owner: string | undefined,
9
  job_id: string | undefined,
10
  body: {
11
    content?: string;
12
    path?: string;
13
    args: { [k: string]: unknown };
14
    language?:
15
      | "python3"
16
      | "deno"
17
      | "go"
18
      | "bash"
19
      | "powershell"
20
      | "postgresql"
21
      | "mysql"
22
      | "bigquery"
23
      | "snowflake"
24
      | "mssql"
25
      | "graphql"
26
      | "nativets"
27
      | "bun";
28
    tag?: string;
29
    kind?: "code" | "identity" | "http";
30
    dedicated_worker?: boolean;
31
    lock?: string;
32
    [k: string]: unknown;
33
  }
34
) {
35
  const url = new URL(`${BASE_URL}/api/w/${workspace}/jobs/run/preview`);
36
  for (const [k, v] of [
37
    ["include_header", include_header],
38
    ["invisible_to_owner", invisible_to_owner],
39
    ["job_id", job_id],
40
  ]) {
41
    if (v !== undefined && v !== "") {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "POST",
47
    headers: {
48
      "Content-Type": "application/json",
49
      Authorization: "Bearer " + WM_TOKEN,
50
    },
51
    body: JSON.stringify(body),
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.text();
58
}
59