Edits history of script submission #24097 for ' Update URL list (netskope)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update URL list
     * Replace a URL list's entries or append entries to it. The change stays pending until applied with Apply URL list changes.
     */
    export async function main(
      auth: RT.Netskope,
      url_list_id: number,
      mode: "replace" | "append",
      urls: string[],
      list_type: "exact" | "regex" | undefined
    ) {
      if (!urls?.length) {
        throw new Error("urls must contain at least one entry")
      }
      const action = mode === "append" ? "append" : "replace"
      const listUrl = `${auth.tenant_url.replace(/\/+$/, "")}/api/v2/policy/urllist/${url_list_id}`
      const headers = {
        "Netskope-Api-Token": auth.token,
        Accept: "application/json",
      }
    
      // The body always carries a type: keep the list's own when none is given, so an
      // untouched list_type never turns a regex list into an exact one.
      let type = list_type
      if (!type) {
        const current = await fetch(listUrl, { headers })
        if (!current.ok) {
          throw new Error(`${current.status} ${await current.text()}`)
        }
        type =
          ((await current.json()) as { data?: { type?: "exact" | "regex" } }).data
            ?.type || "exact"
      }
    
      const url = new URL(`${listUrl}/${action}`)
    
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          "Netskope-Api-Token": auth.token,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ data: { type, urls } }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 7 hours ago