UNPKG

cloudflare

Version:

The official TypeScript library for the Cloudflare API

244 lines • 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cache = exports.BaseCache = void 0; const tslib_1 = require("../../internal/tslib.js"); // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. const resource_1 = require("../../core/resource.js"); const CacheReserveAPI = tslib_1.__importStar(require("./cache-reserve.js")); const cache_reserve_1 = require("./cache-reserve.js"); const OriginCloudRegionsAPI = tslib_1.__importStar(require("./origin-cloud-regions.js")); const origin_cloud_regions_1 = require("./origin-cloud-regions.js"); const RegionalTieredCacheAPI = tslib_1.__importStar(require("./regional-tiered-cache.js")); const regional_tiered_cache_1 = require("./regional-tiered-cache.js"); const SmartTieredCacheAPI = tslib_1.__importStar(require("./smart-tiered-cache.js")); const smart_tiered_cache_1 = require("./smart-tiered-cache.js"); const VariantsAPI = tslib_1.__importStar(require("./variants.js")); const variants_1 = require("./variants.js"); const path_1 = require("../../internal/utils/path.js"); var BaseCache = /* @__PURE__ */ (() => { class BaseCache extends resource_1.APIResource { /** * Marks cached content as stale in every Cloudflare data center and cache tier, * including Cache Reserve. The content stays in cache. The next request for it * makes Cloudflare revalidate it with your origin, using the `ETag` and * `Last-Modified` values it was cached with: * * - If your origin answers `304 Not Modified`, Cloudflare serves the cached copy * without downloading it again, and `CF-Cache-Status` is `REVALIDATED`. * - If your origin sends a full response, Cloudflare serves and caches the new * content, and `CF-Cache-Status` is `EXPIRED`. * * With Tiered Cache, each tier revalidates with the tier above it, so a visitor * can see `EXPIRED` even when your origin answered `304`. * * Until content is revalidated, your `stale-while-revalidate` and `stale-if-error` * directives still apply, counted from the time you invalidated it. For example, * if your origin fails during revalidation, Cloudflare can keep serving the stale * copy for the `stale-if-error` window. * * ### Invalidate or purge? * * - **Invalidate** when content may not have changed, for example after a deploy. * Unchanged content costs your origin a `304` instead of a full response. That * saving needs an origin that sends `ETag` or `Last-Modified` and answers * conditional requests. Otherwise, every revalidation downloads the full * response. * - **Purge**, with `POST /zones/{zone_id}/purge_cache`, when content must not be * served again, for example content you removed for legal or security reasons. * * Invalidating takes the same request bodies as purging, needs the same * permission, and counts against the same rate limits. After a broad invalidation, * such as `purge_everything`, expect more conditional requests to your origin * while visitors request the invalidated content again. * * ### Choose what to invalidate * * Send one of these fields in the request body: * * - `files`: specific URLs. If your cache key includes request headers, send each * URL with the header values it was cached with. * - `tags`: all content whose `Cache-Tag` response header contains one of the * tags. * - `hosts`: all content cached for the hostnames. * - `prefixes`: all content whose URL starts with one of the prefixes. * - `purge_everything`: all cached content in the zone. * * ### Check the result * * A `200` response with `success: true` means Cloudflare accepted the request. To * check, request an invalidated URL and confirm that the `CF-Cache-Status` * response header is `REVALIDATED` or `EXPIRED`. * * ### Availability and limits * * Rate limits and the number of items you can send in one request depend on your * plan. See * [Purge cache: availability and limits](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). * * @example * ```ts * const response = await client.cache.invalidate({ * zone_id: '023e105f4ecef8ad9ca31a8372d0c353', * tags: ['product-1234', 'homepage'], * }); * ``` */ invalidate(params, options) { const { zone_id, ...body } = params; return this._client.post((0, path_1.path) `/zones/${zone_id}/invalidate_cache`, { body, ...options })._thenUnwrap((obj) => obj.result); } /** * Marks cached content as stale for one environment of the zone. Content cached * for the zone's other environments, including production, is not affected. * Otherwise this works like `POST /zones/{zone_id}/invalidate_cache`: the next * request for invalidated content makes Cloudflare revalidate it with your origin, * and the request body takes the same fields. * * Environments are part of * [Version Management](https://developers.cloudflare.com/version-management/). To * delete the content instead, use * `POST /zones/{zone_id}/environments/{environment_id}/purge_cache`. * * Invalidating by URL (`files`) does not work for environments that select * requests by IP address, country, ASN, or threat score, and fails with error * `1136`. Use `tags`, `hosts`, `prefixes`, or `purge_everything` for those * environments. * * ### Availability and limits * * Rate limits and the number of items you can send in one request depend on your * plan. See * [Purge cache: availability and limits](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). * * @example * ```ts * const response = await client.cache.invalidateEnvironment( * '023e105f4ecef8ad9ca31a8372d0c353', * { * zone_id: '023e105f4ecef8ad9ca31a8372d0c353', * tags: ['product-1234', 'homepage'], * }, * ); * ``` */ invalidateEnvironment(environmentID, params, options) { const { zone_id, ...body } = params; return this._client.post((0, path_1.path) `/zones/${zone_id}/environments/${environmentID}/invalidate_cache`, { body, ...options, })._thenUnwrap((obj) => obj.result); } /** * Deletes cached content in every Cloudflare data center and cache tier, including * Cache Reserve. The next request for purged content is a cache `MISS`: Cloudflare * fetches the full response from your origin and caches it again. Cloudflare does * not serve purged content from cache again, even if your origin is unavailable. * * To keep content cached and have Cloudflare revalidate it with your origin * instead, use `POST /zones/{zone_id}/invalidate_cache`. * * ### Choose what to purge * * Send one of these fields in the request body: * * - `files`: specific URLs. If your cache key includes request headers, send each * URL with the header values it was cached with. * - `tags`: all content whose `Cache-Tag` response header contains one of the * tags. * - `hosts`: all content cached for the hostnames. * - `prefixes`: all content whose URL starts with one of the prefixes. * - `purge_everything`: all cached content in the zone. * * ### Check the result * * A `200` response with `success: true` means Cloudflare accepted the request. It * does not confirm that any content was cached or removed. To check, request a * purged URL and confirm that the `CF-Cache-Status` response header is `MISS`. * * ### Availability and limits * * Rate limits and the number of items you can send in one request depend on your * plan. See * [Purge cache: availability and limits](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). * * @example * ```ts * const response = await client.cache.purge({ * zone_id: '023e105f4ecef8ad9ca31a8372d0c353', * tags: ['product-1234', 'homepage'], * }); * ``` */ purge(params, options) { const { zone_id, ...body } = params; return this._client.post((0, path_1.path) `/zones/${zone_id}/purge_cache`, { body, ...options })._thenUnwrap((obj) => obj.result); } /** * Deletes cached content for one environment of the zone. Content cached for the * zone's other environments, including production, is not affected. Otherwise this * works like `POST /zones/{zone_id}/purge_cache`: the next request for purged * content is a cache `MISS`, and the request body takes the same fields. * * Environments are part of * [Version Management](https://developers.cloudflare.com/version-management/). To * keep content cached and have Cloudflare revalidate it instead, use * `POST /zones/{zone_id}/environments/{environment_id}/invalidate_cache`. * * Purging by URL (`files`) does not work for environments that select requests by * IP address, country, ASN, or threat score, and fails with error `1136`. Use * `tags`, `hosts`, `prefixes`, or `purge_everything` for those environments. * * ### Availability and limits * * Rate limits and the number of items you can send in one request depend on your * plan. See * [Purge cache: availability and limits](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). * * @example * ```ts * const response = await client.cache.purgeEnvironment( * '023e105f4ecef8ad9ca31a8372d0c353', * { * zone_id: '023e105f4ecef8ad9ca31a8372d0c353', * tags: ['product-1234', 'homepage'], * }, * ); * ``` */ purgeEnvironment(environmentID, params, options) { const { zone_id, ...body } = params; return this._client.post((0, path_1.path) `/zones/${zone_id}/environments/${environmentID}/purge_cache`, { body, ...options, })._thenUnwrap((obj) => obj.result); } } BaseCache._key = Object.freeze(['cache']); return BaseCache; })(); exports.BaseCache = BaseCache; var Cache = /* @__PURE__ */ (() => { class Cache extends BaseCache { constructor() { super(...arguments); this.cacheReserve = new CacheReserveAPI.CacheReserveResource(this._client); this.smartTieredCache = new SmartTieredCacheAPI.SmartTieredCache(this._client); this.variants = new VariantsAPI.Variants(this._client); this.regionalTieredCache = new RegionalTieredCacheAPI.RegionalTieredCacheResource(this._client); this.originCloudRegions = new OriginCloudRegionsAPI.OriginCloudRegions(this._client); } } Cache.CacheReserveResource = cache_reserve_1.CacheReserveResource; Cache.BaseCacheReserveResource = cache_reserve_1.BaseCacheReserveResource; Cache.SmartTieredCache = smart_tiered_cache_1.SmartTieredCache; Cache.BaseSmartTieredCache = smart_tiered_cache_1.BaseSmartTieredCache; Cache.Variants = variants_1.Variants; Cache.BaseVariants = variants_1.BaseVariants; Cache.RegionalTieredCacheResource = regional_tiered_cache_1.RegionalTieredCacheResource; Cache.BaseRegionalTieredCacheResource = regional_tiered_cache_1.BaseRegionalTieredCacheResource; Cache.OriginCloudRegions = origin_cloud_regions_1.OriginCloudRegions; Cache.BaseOriginCloudRegions = origin_cloud_regions_1.BaseOriginCloudRegions; return Cache; })(); exports.Cache = Cache; //# sourceMappingURL=cache.js.map