Load a preview of the file

Script windmill Verified

by hugo697 ยท 3/6/2024

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 370 days ago
1
/**
2
 * Load a preview of the file
3
 *
4
 */
5
export async function main(
6
  workspace: string,
7
  file_key: string | undefined,
8
  file_size_in_bytes: string | undefined,
9
  file_mime_type: string | undefined,
10
  csv_separator: string | undefined,
11
  csv_has_header: string | undefined,
12
  read_bytes_from: string | undefined,
13
  read_bytes_length: string | undefined
14
) {
15
  const url = new URL(
16
    `${BASE_URL}/api/w/${workspace}/job_helpers/load_file_preview`
17
  );
18
  for (const [k, v] of [
19
    ["file_key", file_key],
20
    ["file_size_in_bytes", file_size_in_bytes],
21
    ["file_mime_type", file_mime_type],
22
    ["csv_separator", csv_separator],
23
    ["csv_has_header", csv_has_header],
24
    ["read_bytes_from", read_bytes_from],
25
    ["read_bytes_length", read_bytes_length],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + WM_TOKEN,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44