Skip to content

Instantly share code, notes, and snippets.

@linux-china
Last active August 26, 2020 20:49
Show Gist options
  • Save linux-china/ef7ef71d63dab3bcdc0d771d77fe79b0 to your computer and use it in GitHub Desktop.
Save linux-china/ef7ef71d63dab3bcdc0d771d77fe79b0 to your computer and use it in GitHub Desktop.
CloudFlare KV JavaScript API with jsdoc support
/**
* cloudflare KV Namespace
*/
class KVNameSpace {
/**
* Write key-value pairs
* @param {string} key - key
* @param {(string|ArrayBuffer|ReadableStream)} value - value
* @param {{[expiration]: number, [expirationTtl]: number, [metadata]: {}}} [options] - putting options
* @return {Promise}
*/
put(key, value, options) {
}
/**
* Read key-value pairs
* @param {string} key - key
* @param {('text'|'json'|'arrayBuffer'|'stream')} [type] - result type, such as text(default), json, arrayBuffer, stream
* @return {Promise<(string|ArrayBuffer|ReadableStream)>} value
*/
get(key, type) {
}
/**
* Read key-value pairs with metadata
* @param {string} key - key
* @return {Promise<ValueWithMetadata>}
*/
getWithMetadata(key) {
}
/**
* List keys
* @param {{[prefix]: string, [limit]: number, [cursor]: string}} [options] - list options
* @return {Promise<ListResult>}
*/
list(options) {
}
/**
* Delete key-value pairs
* @param {string} key - key
* @return {Promise}
*/
delete(key) {
}
}
class ListResult {
/**
* keys
* @type {KeyMetadata[]}
*/
keys = []
/**
* list completed?
* @type {boolean}
*/
list_complete = true
/**
* cursor
* @type {string}
*/
cursor = 'xxx'
}
class KeyMetadata {
/**
* key name
* @type {string}
*/
name = ''
/**
* absolute expired timestamp
* @type {number}
*/
expiration = 1000
/**
* key metadata
* @type {{}}
*/
metadata = {}
}
class ValueWithMetadata {
/**
*
* @type {(string|ArrayBuffer|ReadableStream)}
*/
value = ''
/**
* key metadata
* @type {KeyMetadata}
*/
metadata = new KeyMetadata()
}
const KV_MVNSEARCH = new KVNameSpace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment