Edits history of script submission #5781 for ' Update all list items (cloudflare)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Update all list items
     * Removes all existing items from the list and adds the provided items to the list.
    
    This operation is asynchronous. To get current the operation status, invoke the [Get bulk operation status](#lists-get-bulk-operation-status) endpoint with the returned `operation_id`.
     */
    export async function main(
      auth: Cloudflare,
      list_id: string,
      account_identifier: string,
      body: {
        asn?: number;
        comment?: string;
        hostname?: { url_hostname: string; [k: string]: unknown };
        ip?: string;
        redirect?: {
          include_subdomains?: boolean;
          preserve_path_suffix?: boolean;
          preserve_query_string?: boolean;
          source_url: string;
          status_code?: 301 | 302 | 307 | 308;
          subpath_matching?: boolean;
          target_url: string;
          [k: string]: unknown;
        };
        [k: string]: unknown;
      }[]
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/rules/lists/${list_id}/items`
      );
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "X-AUTH-EMAIL": auth.email,
          "X-AUTH-KEY": auth.key,
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 383 days ago

  • nativets
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Update all list items
     * Removes all existing items from the list and adds the provided items to the list.
    
    This operation is asynchronous. To get current the operation status, invoke the [Get bulk operation status](#lists-get-bulk-operation-status) endpoint with the returned `operation_id`.
     */
    export async function main(
      auth: Cloudflare,
      list_id: string,
      account_identifier: string,
      body: {
        asn?: number;
        comment?: string;
        hostname?: { url_hostname: string; [k: string]: unknown };
        ip?: string;
        redirect?: {
          include_subdomains?: boolean;
          preserve_path_suffix?: boolean;
          preserve_query_string?: boolean;
          source_url: string;
          status_code?: 301 | 302 | 307 | 308;
          subpath_matching?: boolean;
          target_url: string;
          [k: string]: unknown;
        };
        [k: string]: unknown;
      }[]
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/rules/lists/${list_id}/items`
      );
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "X-AUTH-EMAIL": auth.email,
          "X-AUTH-KEY": auth.key,
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 920 days ago