cloudflare/cloudflare-typescript
Publicmirrored from https://github.com/cloudflare/cloudflare-typescriptAvailable
src/resources/cache/cache.ts
495lines · modecode
| 1 | // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | import { APIResource } from '../../core/resource'; |
| 4 | import * as CacheReserveAPI from './cache-reserve'; |
| 5 | import { |
| 6 | BaseCacheReserveResource, |
| 7 | CacheReserve, |
| 8 | CacheReserveClear, |
| 9 | CacheReserveClearParams, |
| 10 | CacheReserveClearResponse, |
| 11 | CacheReserveEditParams, |
| 12 | CacheReserveEditResponse, |
| 13 | CacheReserveGetParams, |
| 14 | CacheReserveGetResponse, |
| 15 | CacheReserveResource, |
| 16 | CacheReserveStatusParams, |
| 17 | CacheReserveStatusResponse, |
| 18 | State, |
| 19 | } from './cache-reserve'; |
| 20 | import * as OriginCloudRegionsAPI from './origin-cloud-regions'; |
| 21 | import { |
| 22 | BaseOriginCloudRegions, |
| 23 | OriginCloudRegion, |
| 24 | OriginCloudRegionBulkDeleteParams, |
| 25 | OriginCloudRegionBulkDeleteResponse, |
| 26 | OriginCloudRegionBulkUpdateParams, |
| 27 | OriginCloudRegionBulkUpdateResponse, |
| 28 | OriginCloudRegionDeleteParams, |
| 29 | OriginCloudRegionDeleteResponse, |
| 30 | OriginCloudRegionGetParams, |
| 31 | OriginCloudRegionListParams, |
| 32 | OriginCloudRegionSupportedRegionsParams, |
| 33 | OriginCloudRegionSupportedRegionsResponse, |
| 34 | OriginCloudRegionUpdateParams, |
| 35 | OriginCloudRegions, |
| 36 | OriginCloudRegionsV4PagePaginationArray, |
| 37 | } from './origin-cloud-regions'; |
| 38 | import * as RegionalTieredCacheAPI from './regional-tiered-cache'; |
| 39 | import { |
| 40 | BaseRegionalTieredCacheResource, |
| 41 | RegionalTieredCache, |
| 42 | RegionalTieredCacheEditParams, |
| 43 | RegionalTieredCacheEditResponse, |
| 44 | RegionalTieredCacheGetParams, |
| 45 | RegionalTieredCacheGetResponse, |
| 46 | RegionalTieredCacheResource, |
| 47 | } from './regional-tiered-cache'; |
| 48 | import * as SmartTieredCacheAPI from './smart-tiered-cache'; |
| 49 | import { |
| 50 | BaseSmartTieredCache, |
| 51 | SmartTieredCache, |
| 52 | SmartTieredCacheCreateParams, |
| 53 | SmartTieredCacheCreateResponse, |
| 54 | SmartTieredCacheDeleteParams, |
| 55 | SmartTieredCacheDeleteResponse, |
| 56 | SmartTieredCacheEditParams, |
| 57 | SmartTieredCacheEditResponse, |
| 58 | SmartTieredCacheGetParams, |
| 59 | SmartTieredCacheGetResponse, |
| 60 | } from './smart-tiered-cache'; |
| 61 | import * as VariantsAPI from './variants'; |
| 62 | import { |
| 63 | BaseVariants, |
| 64 | CacheVariant, |
| 65 | VariantDeleteParams, |
| 66 | VariantDeleteResponse, |
| 67 | VariantEditParams, |
| 68 | VariantEditResponse, |
| 69 | VariantGetParams, |
| 70 | VariantGetResponse, |
| 71 | Variants, |
| 72 | } from './variants'; |
| 73 | import { APIPromise } from '../../core/api-promise'; |
| 74 | import { RequestOptions } from '../../internal/request-options'; |
| 75 | import { path } from '../../internal/utils/path'; |
| 76 | |
| 77 | export class BaseCache extends APIResource { |
| 78 | static override readonly _key: readonly ['cache'] = Object.freeze(['cache'] as const); |
| 79 | |
| 80 | /** |
| 81 | * ### Purge All Cached Content |
| 82 | * |
| 83 | * Removes ALL files from Cloudflare's cache. All tiers can purge everything. |
| 84 | * |
| 85 | * ``` |
| 86 | * {"purge_everything": true} |
| 87 | * ``` |
| 88 | * |
| 89 | * ### Purge Cached Content by URL |
| 90 | * |
| 91 | * Granularly removes one or more files from Cloudflare's cache by specifying URLs. |
| 92 | * All tiers can purge by URL. |
| 93 | * |
| 94 | * To purge files with custom cache keys, include the headers used to compute the |
| 95 | * cache key as in the example. If you have a device type or geo in your cache key, |
| 96 | * you will need to include the CF-Device-Type or CF-IPCountry headers. If you have |
| 97 | * lang in your cache key, you will need to include the Accept-Language header. |
| 98 | * |
| 99 | * **NB:** When including the Origin header, be sure to include the **scheme** and |
| 100 | * **hostname**. The port number can be omitted if it is the default port (80 for |
| 101 | * http, 443 for https), but must be included otherwise. |
| 102 | * |
| 103 | * Single file purge example with files: |
| 104 | * |
| 105 | * ``` |
| 106 | * {"files": ["http://www.example.com/css/styles.css", "http://www.example.com/js/index.js"]} |
| 107 | * ``` |
| 108 | * |
| 109 | * Single file purge example with url and header pairs: |
| 110 | * |
| 111 | * ``` |
| 112 | * {"files": [{"url": "http://www.example.com/cat_picture.jpg", "headers": {"CF-IPCountry": "US", "CF-Device-Type": "desktop", "Accept-Language": "zh-CN"}}, {"url": "http://www.example.com/dog_picture.jpg", "headers": {"CF-IPCountry": "EU", "CF-Device-Type": "mobile", "Accept-Language": "en-US"}}]} |
| 113 | * ``` |
| 114 | * |
| 115 | * ### Purge Cached Content by Tag, Host or Prefix |
| 116 | * |
| 117 | * Granularly removes one or more files from Cloudflare's cache either by |
| 118 | * specifying the host, the associated Cache-Tag, or a Prefix. |
| 119 | * |
| 120 | * Flex purge with tags: |
| 121 | * |
| 122 | * ``` |
| 123 | * {"tags": ["a-cache-tag", "another-cache-tag"]} |
| 124 | * ``` |
| 125 | * |
| 126 | * Flex purge with hosts: |
| 127 | * |
| 128 | * ``` |
| 129 | * {"hosts": ["www.example.com", "images.example.com"]} |
| 130 | * ``` |
| 131 | * |
| 132 | * Flex purge with prefixes: |
| 133 | * |
| 134 | * ``` |
| 135 | * {"prefixes": ["www.example.com/foo", "images.example.com/bar/baz"]} |
| 136 | * ``` |
| 137 | * |
| 138 | * ### Availability and limits |
| 139 | * |
| 140 | * Please refer to |
| 141 | * [purge cache availability and limits documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). |
| 142 | * |
| 143 | * @example |
| 144 | * ```ts |
| 145 | * const response = await client.cache.purge({ |
| 146 | * zone_id: '023e105f4ecef8ad9ca31a8372d0c353', |
| 147 | * }); |
| 148 | * ``` |
| 149 | */ |
| 150 | purge(params: CachePurgeParams, options?: RequestOptions): APIPromise<CachePurgeResponse | null> { |
| 151 | const { zone_id, ...body } = params; |
| 152 | return ( |
| 153 | this._client.post(path`/zones/${zone_id}/purge_cache`, { body, ...options }) as APIPromise<{ |
| 154 | result: CachePurgeResponse | null; |
| 155 | }> |
| 156 | )._thenUnwrap((obj) => obj.result); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Purge cached content scoped to a specific environment. Supports the same purge |
| 161 | * types as the zone-level endpoint (purge everything, by URL, by tag, host, or |
| 162 | * prefix). |
| 163 | * |
| 164 | * ### Availability and limits |
| 165 | * |
| 166 | * Please refer to |
| 167 | * [purge cache availability and limits documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/#availability-and-limits). |
| 168 | * |
| 169 | * @example |
| 170 | * ```ts |
| 171 | * const response = await client.cache.purgeEnvironment( |
| 172 | * '023e105f4ecef8ad9ca31a8372d0c353', |
| 173 | * { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' }, |
| 174 | * ); |
| 175 | * ``` |
| 176 | */ |
| 177 | purgeEnvironment( |
| 178 | environmentID: string, |
| 179 | params: CachePurgeEnvironmentParams, |
| 180 | options?: RequestOptions, |
| 181 | ): APIPromise<CachePurgeEnvironmentResponse | null> { |
| 182 | const { zone_id, ...body } = params; |
| 183 | return ( |
| 184 | this._client.post(path`/zones/${zone_id}/environments/${environmentID}/purge_cache`, { |
| 185 | body, |
| 186 | ...options, |
| 187 | }) as APIPromise<{ result: CachePurgeEnvironmentResponse | null }> |
| 188 | )._thenUnwrap((obj) => obj.result); |
| 189 | } |
| 190 | } |
| 191 | export class Cache extends BaseCache { |
| 192 | cacheReserve: CacheReserveAPI.CacheReserveResource = new CacheReserveAPI.CacheReserveResource(this._client); |
| 193 | smartTieredCache: SmartTieredCacheAPI.SmartTieredCache = new SmartTieredCacheAPI.SmartTieredCache( |
| 194 | this._client, |
| 195 | ); |
| 196 | variants: VariantsAPI.Variants = new VariantsAPI.Variants(this._client); |
| 197 | regionalTieredCache: RegionalTieredCacheAPI.RegionalTieredCacheResource = |
| 198 | new RegionalTieredCacheAPI.RegionalTieredCacheResource(this._client); |
| 199 | originCloudRegions: OriginCloudRegionsAPI.OriginCloudRegions = new OriginCloudRegionsAPI.OriginCloudRegions( |
| 200 | this._client, |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | export interface CachePurgeResponse { |
| 205 | id: string; |
| 206 | } |
| 207 | |
| 208 | export interface CachePurgeEnvironmentResponse { |
| 209 | id: string; |
| 210 | } |
| 211 | |
| 212 | export type CachePurgeParams = |
| 213 | | CachePurgeParams.CachePurgeFlexPurgeByTags |
| 214 | | CachePurgeParams.CachePurgeFlexPurgeByHostnames |
| 215 | | CachePurgeParams.CachePurgeFlexPurgeByPrefixes |
| 216 | | CachePurgeParams.CachePurgeEverything |
| 217 | | CachePurgeParams.CachePurgeSingleFile |
| 218 | | CachePurgeParams.CachePurgeSingleFileWithURLAndHeaders; |
| 219 | |
| 220 | export declare namespace CachePurgeParams { |
| 221 | export interface CachePurgeFlexPurgeByTags { |
| 222 | /** |
| 223 | * Path param |
| 224 | */ |
| 225 | zone_id: string; |
| 226 | |
| 227 | /** |
| 228 | * Body param: For more information on cache tags and purging by tags, please refer |
| 229 | * to |
| 230 | * [purge by cache-tags documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/). |
| 231 | */ |
| 232 | tags?: Array<string>; |
| 233 | } |
| 234 | |
| 235 | export interface CachePurgeFlexPurgeByHostnames { |
| 236 | /** |
| 237 | * Path param |
| 238 | */ |
| 239 | zone_id: string; |
| 240 | |
| 241 | /** |
| 242 | * Body param: For more information purging by hostnames, please refer to |
| 243 | * [purge by hostname documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-hostname/). |
| 244 | */ |
| 245 | hosts?: Array<string>; |
| 246 | } |
| 247 | |
| 248 | export interface CachePurgeFlexPurgeByPrefixes { |
| 249 | /** |
| 250 | * Path param |
| 251 | */ |
| 252 | zone_id: string; |
| 253 | |
| 254 | /** |
| 255 | * Body param: For more information on purging by prefixes, please refer to |
| 256 | * [purge by prefix documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge_by_prefix/). |
| 257 | */ |
| 258 | prefixes?: Array<string>; |
| 259 | } |
| 260 | |
| 261 | export interface CachePurgeEverything { |
| 262 | /** |
| 263 | * Path param |
| 264 | */ |
| 265 | zone_id: string; |
| 266 | |
| 267 | /** |
| 268 | * Body param: For more information, please refer to |
| 269 | * [purge everything documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-everything/). |
| 270 | */ |
| 271 | purge_everything?: boolean; |
| 272 | } |
| 273 | |
| 274 | export interface CachePurgeSingleFile { |
| 275 | /** |
| 276 | * Path param |
| 277 | */ |
| 278 | zone_id: string; |
| 279 | |
| 280 | /** |
| 281 | * Body param: For more information on purging files, please refer to |
| 282 | * [purge by single-file documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-single-file/). |
| 283 | */ |
| 284 | files?: Array<string>; |
| 285 | } |
| 286 | |
| 287 | export interface CachePurgeSingleFileWithURLAndHeaders { |
| 288 | /** |
| 289 | * Path param |
| 290 | */ |
| 291 | zone_id: string; |
| 292 | |
| 293 | /** |
| 294 | * Body param: For more information on purging files with URL and headers, please |
| 295 | * refer to |
| 296 | * [purge by single-file documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-single-file/). |
| 297 | */ |
| 298 | files?: Array<CachePurgeSingleFileWithURLAndHeaders.File>; |
| 299 | } |
| 300 | |
| 301 | export namespace CachePurgeSingleFileWithURLAndHeaders { |
| 302 | export interface File { |
| 303 | headers?: { [key: string]: string }; |
| 304 | |
| 305 | url?: string; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | export type CachePurgeEnvironmentParams = |
| 311 | | CachePurgeEnvironmentParams.CachePurgeFlexPurgeByTags |
| 312 | | CachePurgeEnvironmentParams.CachePurgeFlexPurgeByHostnames |
| 313 | | CachePurgeEnvironmentParams.CachePurgeFlexPurgeByPrefixes |
| 314 | | CachePurgeEnvironmentParams.CachePurgeEverything |
| 315 | | CachePurgeEnvironmentParams.CachePurgeSingleFile |
| 316 | | CachePurgeEnvironmentParams.CachePurgeSingleFileWithURLAndHeaders; |
| 317 | |
| 318 | export declare namespace CachePurgeEnvironmentParams { |
| 319 | export interface CachePurgeFlexPurgeByTags { |
| 320 | /** |
| 321 | * Path param |
| 322 | */ |
| 323 | zone_id: string; |
| 324 | |
| 325 | /** |
| 326 | * Body param: For more information on cache tags and purging by tags, please refer |
| 327 | * to |
| 328 | * [purge by cache-tags documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/). |
| 329 | */ |
| 330 | tags?: Array<string>; |
| 331 | } |
| 332 | |
| 333 | export interface CachePurgeFlexPurgeByHostnames { |
| 334 | /** |
| 335 | * Path param |
| 336 | */ |
| 337 | zone_id: string; |
| 338 | |
| 339 | /** |
| 340 | * Body param: For more information purging by hostnames, please refer to |
| 341 | * [purge by hostname documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-hostname/). |
| 342 | */ |
| 343 | hosts?: Array<string>; |
| 344 | } |
| 345 | |
| 346 | export interface CachePurgeFlexPurgeByPrefixes { |
| 347 | /** |
| 348 | * Path param |
| 349 | */ |
| 350 | zone_id: string; |
| 351 | |
| 352 | /** |
| 353 | * Body param: For more information on purging by prefixes, please refer to |
| 354 | * [purge by prefix documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge_by_prefix/). |
| 355 | */ |
| 356 | prefixes?: Array<string>; |
| 357 | } |
| 358 | |
| 359 | export interface CachePurgeEverything { |
| 360 | /** |
| 361 | * Path param |
| 362 | */ |
| 363 | zone_id: string; |
| 364 | |
| 365 | /** |
| 366 | * Body param: For more information, please refer to |
| 367 | * [purge everything documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-everything/). |
| 368 | */ |
| 369 | purge_everything?: boolean; |
| 370 | } |
| 371 | |
| 372 | export interface CachePurgeSingleFile { |
| 373 | /** |
| 374 | * Path param |
| 375 | */ |
| 376 | zone_id: string; |
| 377 | |
| 378 | /** |
| 379 | * Body param: For more information on purging files, please refer to |
| 380 | * [purge by single-file documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-single-file/). |
| 381 | */ |
| 382 | files?: Array<string>; |
| 383 | } |
| 384 | |
| 385 | export interface CachePurgeSingleFileWithURLAndHeaders { |
| 386 | /** |
| 387 | * Path param |
| 388 | */ |
| 389 | zone_id: string; |
| 390 | |
| 391 | /** |
| 392 | * Body param: For more information on purging files with URL and headers, please |
| 393 | * refer to |
| 394 | * [purge by single-file documentation page](https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-single-file/). |
| 395 | */ |
| 396 | files?: Array<CachePurgeSingleFileWithURLAndHeaders.File>; |
| 397 | } |
| 398 | |
| 399 | export namespace CachePurgeSingleFileWithURLAndHeaders { |
| 400 | export interface File { |
| 401 | headers?: { [key: string]: string }; |
| 402 | |
| 403 | url?: string; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | Cache.CacheReserveResource = CacheReserveResource; |
| 409 | Cache.BaseCacheReserveResource = BaseCacheReserveResource; |
| 410 | Cache.SmartTieredCache = SmartTieredCache; |
| 411 | Cache.BaseSmartTieredCache = BaseSmartTieredCache; |
| 412 | Cache.Variants = Variants; |
| 413 | Cache.BaseVariants = BaseVariants; |
| 414 | Cache.RegionalTieredCacheResource = RegionalTieredCacheResource; |
| 415 | Cache.BaseRegionalTieredCacheResource = BaseRegionalTieredCacheResource; |
| 416 | Cache.OriginCloudRegions = OriginCloudRegions; |
| 417 | Cache.BaseOriginCloudRegions = BaseOriginCloudRegions; |
| 418 | |
| 419 | export declare namespace Cache { |
| 420 | export { |
| 421 | type CachePurgeResponse as CachePurgeResponse, |
| 422 | type CachePurgeEnvironmentResponse as CachePurgeEnvironmentResponse, |
| 423 | type CachePurgeParams as CachePurgeParams, |
| 424 | type CachePurgeEnvironmentParams as CachePurgeEnvironmentParams, |
| 425 | }; |
| 426 | |
| 427 | export { |
| 428 | CacheReserveResource as CacheReserveResource, |
| 429 | BaseCacheReserveResource as BaseCacheReserveResource, |
| 430 | type CacheReserve as CacheReserve, |
| 431 | type CacheReserveClear as CacheReserveClear, |
| 432 | type State as State, |
| 433 | type CacheReserveClearResponse as CacheReserveClearResponse, |
| 434 | type CacheReserveEditResponse as CacheReserveEditResponse, |
| 435 | type CacheReserveGetResponse as CacheReserveGetResponse, |
| 436 | type CacheReserveStatusResponse as CacheReserveStatusResponse, |
| 437 | type CacheReserveClearParams as CacheReserveClearParams, |
| 438 | type CacheReserveEditParams as CacheReserveEditParams, |
| 439 | type CacheReserveGetParams as CacheReserveGetParams, |
| 440 | type CacheReserveStatusParams as CacheReserveStatusParams, |
| 441 | }; |
| 442 | |
| 443 | export { |
| 444 | SmartTieredCache as SmartTieredCache, |
| 445 | BaseSmartTieredCache as BaseSmartTieredCache, |
| 446 | type SmartTieredCacheCreateResponse as SmartTieredCacheCreateResponse, |
| 447 | type SmartTieredCacheDeleteResponse as SmartTieredCacheDeleteResponse, |
| 448 | type SmartTieredCacheEditResponse as SmartTieredCacheEditResponse, |
| 449 | type SmartTieredCacheGetResponse as SmartTieredCacheGetResponse, |
| 450 | type SmartTieredCacheCreateParams as SmartTieredCacheCreateParams, |
| 451 | type SmartTieredCacheDeleteParams as SmartTieredCacheDeleteParams, |
| 452 | type SmartTieredCacheEditParams as SmartTieredCacheEditParams, |
| 453 | type SmartTieredCacheGetParams as SmartTieredCacheGetParams, |
| 454 | }; |
| 455 | |
| 456 | export { |
| 457 | Variants as Variants, |
| 458 | BaseVariants as BaseVariants, |
| 459 | type CacheVariant as CacheVariant, |
| 460 | type VariantDeleteResponse as VariantDeleteResponse, |
| 461 | type VariantEditResponse as VariantEditResponse, |
| 462 | type VariantGetResponse as VariantGetResponse, |
| 463 | type VariantDeleteParams as VariantDeleteParams, |
| 464 | type VariantEditParams as VariantEditParams, |
| 465 | type VariantGetParams as VariantGetParams, |
| 466 | }; |
| 467 | |
| 468 | export { |
| 469 | RegionalTieredCacheResource as RegionalTieredCacheResource, |
| 470 | BaseRegionalTieredCacheResource as BaseRegionalTieredCacheResource, |
| 471 | type RegionalTieredCache as RegionalTieredCache, |
| 472 | type RegionalTieredCacheEditResponse as RegionalTieredCacheEditResponse, |
| 473 | type RegionalTieredCacheGetResponse as RegionalTieredCacheGetResponse, |
| 474 | type RegionalTieredCacheEditParams as RegionalTieredCacheEditParams, |
| 475 | type RegionalTieredCacheGetParams as RegionalTieredCacheGetParams, |
| 476 | }; |
| 477 | |
| 478 | export { |
| 479 | OriginCloudRegions as OriginCloudRegions, |
| 480 | BaseOriginCloudRegions as BaseOriginCloudRegions, |
| 481 | type OriginCloudRegion as OriginCloudRegion, |
| 482 | type OriginCloudRegionDeleteResponse as OriginCloudRegionDeleteResponse, |
| 483 | type OriginCloudRegionBulkDeleteResponse as OriginCloudRegionBulkDeleteResponse, |
| 484 | type OriginCloudRegionBulkUpdateResponse as OriginCloudRegionBulkUpdateResponse, |
| 485 | type OriginCloudRegionSupportedRegionsResponse as OriginCloudRegionSupportedRegionsResponse, |
| 486 | type OriginCloudRegionsV4PagePaginationArray as OriginCloudRegionsV4PagePaginationArray, |
| 487 | type OriginCloudRegionUpdateParams as OriginCloudRegionUpdateParams, |
| 488 | type OriginCloudRegionListParams as OriginCloudRegionListParams, |
| 489 | type OriginCloudRegionDeleteParams as OriginCloudRegionDeleteParams, |
| 490 | type OriginCloudRegionBulkDeleteParams as OriginCloudRegionBulkDeleteParams, |
| 491 | type OriginCloudRegionBulkUpdateParams as OriginCloudRegionBulkUpdateParams, |
| 492 | type OriginCloudRegionGetParams as OriginCloudRegionGetParams, |
| 493 | type OriginCloudRegionSupportedRegionsParams as OriginCloudRegionSupportedRegionsParams, |
| 494 | }; |
| 495 | } |
| 496 | |