// File generated from our OpenAPI spec by Stainless.

import * as Core from 'cloudflare/core';
import { APIResource } from 'cloudflare/resource';
import * as PurgeCachesAPI from 'cloudflare/resources/purge-caches';

export class PurgeCaches extends APIResource {
  /**
   * ### Purge All Cached Content
   *
   * Removes ALL files from Cloudflare's cache. All tiers can purge everything.
   *
   * ### Purge Cached Content by URL
   *
   * Granularly removes one or more files from Cloudflare's cache by specifying URLs.
   * All tiers can purge by URL.
   *
   * To purge files with custom cache keys, include the headers used to compute the
   * cache key as in the example. If you have a device type or geo in your cache key,
   * you will need to include the CF-Device-Type or CF-IPCountry headers. If you have
   * lang in your cache key, you will need to include the Accept-Language header.
   *
   * **NB:** When including the Origin header, be sure to include the **scheme** and
   * **hostname**. The port number can be omitted if it is the default port (80 for
   * http, 443 for https), but must be included otherwise.
   *
   * ### Purge Cached Content by Tag, Host or Prefix
   *
   * Granularly removes one or more files from Cloudflare's cache either by
   * specifying the host, the associated Cache-Tag, or a Prefix. Only Enterprise
   * customers are permitted to purge by Tag, Host or Prefix.
   *
   * **NB:** Cache-Tag, host, and prefix purging each have a rate limit of 30,000
   * purge API calls in every 24 hour period. You may purge up to 30 tags, hosts, or
   * prefixes in one API call. This rate limit can be raised for customers who need
   * to purge at higher volume.
   */
  zonePurge(
    identifier: string,
    body: PurgeCachZonePurgeParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<PurgeCachZonePurgeResponse | null> {
    return (
      this._client.post(`/zones/${identifier}/purge_cache`, { body, ...options }) as Core.APIPromise<{
        result: PurgeCachZonePurgeResponse | null;
      }>
    )._thenUnwrap((obj) => obj.result);
  }
}

export interface PurgeCachZonePurgeResponse {
  /**
   * Identifier
   */
  id: string;
}

export type PurgeCachZonePurgeParams =
  | PurgeCachZonePurgeParams.GJq47JoyFlex
  | PurgeCachZonePurgeParams.GJq47JoyEverything
  | PurgeCachZonePurgeParams.GJq47JoyFiles;

export namespace PurgeCachZonePurgeParams {
  export interface GJq47JoyFlex {
    hosts?: Array<string>;

    prefixes?: Array<string>;

    tags?: Array<string>;
  }

  export interface GJq47JoyEverything {
    purge_everything?: boolean;
  }

  export interface GJq47JoyFiles {
    files?: Array<string | PurgeCachZonePurgeParams.GJq47JoyFiles.GJq47JoyURLAndHeaders>;
  }

  export namespace GJq47JoyFiles {
    export interface GJq47JoyURLAndHeaders {
      headers?: unknown;

      url?: string;
    }
  }
}

export namespace PurgeCaches {
  export import PurgeCachZonePurgeResponse = PurgeCachesAPI.PurgeCachZonePurgeResponse;
  export import PurgeCachZonePurgeParams = PurgeCachesAPI.PurgeCachZonePurgeParams;
}
