Edits history of script submission #5611 for ' Add operations to a zone (cloudflare)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Cloudflare = {
      token: string;
      email: string;
      key: string;
    };
    /**
     * Add operations to a zone
     * Add one or more operations to a zone. Endpoints can contain path variables. Host, method, endpoint will be normalized to a canoncial form when creating an operation and must be unique on the zone. Inserting an operation that matches an existing one will return the record of the already existing operation and update its last_updated date.
     */
    export async function main(
      auth: Cloudflare,
      zone_id: string,
      body: {
        endpoint: string;
        host: string;
        method:
          | "GET"
          | "POST"
          | "HEAD"
          | "OPTIONS"
          | "PUT"
          | "DELETE"
          | "CONNECT"
          | "PATCH"
          | "TRACE";
        [k: string]: unknown;
      }[]
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/zones/${zone_id}/api_gateway/operations`
      );
    
      const response = await fetch(url, {
        method: "POST",
        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;
    };
    /**
     * Add operations to a zone
     * Add one or more operations to a zone. Endpoints can contain path variables. Host, method, endpoint will be normalized to a canoncial form when creating an operation and must be unique on the zone. Inserting an operation that matches an existing one will return the record of the already existing operation and update its last_updated date.
     */
    export async function main(
      auth: Cloudflare,
      zone_id: string,
      body: {
        endpoint: string;
        host: string;
        method:
          | "GET"
          | "POST"
          | "HEAD"
          | "OPTIONS"
          | "PUT"
          | "DELETE"
          | "CONNECT"
          | "PATCH"
          | "TRACE";
        [k: string]: unknown;
      }[]
    ) {
      const url = new URL(
        `https://api.cloudflare.com/client/v4/zones/${zone_id}/api_gateway/operations`
      );
    
      const response = await fetch(url, {
        method: "POST",
        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