1 | |
2 |
|
3 | |
4 | * Update URL list |
5 | * Replace a URL list's entries or append entries to it. The change stays pending until applied with Apply URL list changes. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Netskope, |
9 | url_list_id: number, |
10 | mode: "replace" | "append", |
11 | urls: string[], |
12 | list_type: "exact" | "regex" | undefined |
13 | ) { |
14 | if (!urls?.length) { |
15 | throw new Error("urls must contain at least one entry") |
16 | } |
17 | const action = mode === "append" ? "append" : "replace" |
18 | const listUrl = `${auth.tenant_url.replace(/\/+$/, "")}/api/v2/policy/urllist/${url_list_id}` |
19 | const headers = { |
20 | "Netskope-Api-Token": auth.token, |
21 | Accept: "application/json", |
22 | } |
23 |
|
24 | |
25 | |
26 | let type = list_type |
27 | if (!type) { |
28 | const current = await fetch(listUrl, { headers }) |
29 | if (!current.ok) { |
30 | throw new Error(`${current.status} ${await current.text()}`) |
31 | } |
32 | type = |
33 | ((await current.json()) as { data?: { type?: "exact" | "regex" } }).data |
34 | ?.type || "exact" |
35 | } |
36 |
|
37 | const url = new URL(`${listUrl}/${action}`) |
38 |
|
39 | const response = await fetch(url, { |
40 | method: "PATCH", |
41 | headers: { |
42 | "Netskope-Api-Token": auth.token, |
43 | "Content-Type": "application/json", |
44 | Accept: "application/json", |
45 | }, |
46 | body: JSON.stringify({ data: { type, urls } }), |
47 | }) |
48 |
|
49 | if (!response.ok) { |
50 | throw new Error(`${response.status} ${await response.text()}`) |
51 | } |
52 |
|
53 | return await response.json() |
54 | } |
55 |
|