Skip to content

Instantly share code, notes, and snippets.

@anywhichway
Last active July 1, 2019 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anywhichway/7941a747441c2924cbd141334edc1d75 to your computer and use it in GitHub Desktop.
Save anywhichway/7941a747441c2924cbd141334edc1d75 to your computer and use it in GitHub Desktop.
Cloudflare Workers KV: keys
(function() {
// getKeys makes the URL based REST call for keys
function getKeys(prefix,limit,cursor) {
const endpoint = "https://api.cloudflare.com/client/v4",
zone = <ZONE_ID>,
ns = <NAMESPACE_ID>,
ctxt = `${cursor ? "&cursor="+cursor : ""}`,
ptxt = `${prefix ? "&prefix="+prefix : ""}`,
email = <ACCOUNT_EMAIL>,
authkey = <ACCOUNT_AUTH_KEY>;
return fetch(`${endpoint}/accounts/${zone}/storage/kv/namespaces/${ns}>/keys?limit=${limit}${ctxt}${ptxt},
{headers:{"X-Auth-Email":"${email}","X-Auth-Key":"${authkey"}})
.then((result) => result.json())
}
module.exports = async function keys({prefix="",limit=1000,cursor}) {
// so long as the cursor is not the empty string, get more keys
if(cursor!=="") {
let {result,result_info} = await getKeys(prefix,limit,cursor);
// process the results to get the names of the keys
result = result.map((item) => item.name);
// make the last element in the results be the cursor
// per Cloudflare, this will be an empty string if there is no cursor
result.push(result_info.cursor);
return result;
}
// otherwise return an empty array
return [];
}
}).call(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment