Files
cypress/scripts/binary/util/purge-cloudflare-cache.ts
T
Devon 1ce9575315 Updated Cloudflare cache purge to use API Tokens (#5250)
* Updated cypress/scripts/binary/util/upload.coffee to use curl and API Tokens as the cfcli command currently *ONLY* supports API Keys.

* Update purge cache to use request-promise

* Cleanup & remove sync method

* Remove unused cloudflare-cli dep

* update appveyor encrypted secrets for cloudflare

* add missing deps


Co-authored-by: Zach Bloomquist <github@chary.us>
Co-authored-by: Brian Mann <brian.mann86@gmail.com>
2019-10-11 21:30:32 -04:00

44 lines
1.1 KiB
TypeScript

import _ from 'lodash'
import check from 'check-more-types'
import la from 'lazy-ass'
import rp from 'request-promise'
function hasCloudflareEnvironmentVars () {
return _.chain([process.env.CF_TOKEN, process.env.CF_ZONEID])
.map(check.unemptyString)
.every()
.value()
}
export function purgeCloudflareCache (url) {
la(
hasCloudflareEnvironmentVars(),
'Cannot purge Cloudflare cache without credentials. Ensure that the CF_TOKEN and CF_ZONEID environment variables are set.'
)
la(check.webUrl(url), 'Missing url to purge from Cloudflare.')
console.log(`Found CF_TOKEN and CF_ZONEID, purging Cloudflare cache for ${url}`)
const { CF_TOKEN, CF_ZONEID } = process.env
return rp({
body: {
files: [url],
},
headers: {
'Authorization': `Bearer ${CF_TOKEN}`,
},
json: true,
method: 'POST',
url: `https://api.cloudflare.com/client/v4/zones/${CF_ZONEID}/purge_cache`,
})
.promise()
.tap(() => {
console.log('Cloudflare cache successfully purged.')
})
.tapCatch((e) => {
console.error(`Could not purge ${url}. Error: ${e.message}`)
})
}