//native
export type DynSelect_namespace_id = string
// Dropdown of the account's KV namespaces, by title.
export async function namespace_id(auth: RT.Cloudflare) {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/storage/kv/namespaces?per_page=100`,
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { result } = (await response.json()) as {
result: { id: string; title: string }[]
}
return result.map((n) => ({ value: n.id, label: n.title }))
}
/**
* List KV Keys
* List the keys in a KV namespace, optionally by prefix. Pass the returned result_info.cursor to get the next page.
*/
export async function main(
auth: RT.Cloudflare,
namespace_id: DynSelect_namespace_id,
prefix: string | undefined,
limit: number | undefined,
cursor: string | undefined
) {
const url = new URL(
`https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/storage/kv/namespaces/${namespace_id}/keys`
)
for (const [k, v] of Object.entries({ prefix, limit, cursor })) {
if (v != null && v !== "") url.searchParams.append(k, String(v))
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 4 hours ago