Last active
July 1, 2019 13:41
-
-
Save anywhichway/7941a747441c2924cbd141334edc1d75 to your computer and use it in GitHub Desktop.
Cloudflare Workers KV: keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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