Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat(resources): support Workers KV
Browse files Browse the repository at this point in the history
Add support for the Workers KV API endpoints.

Signed-off-by: Terin Stock <terin@cloudflare.com>
  • Loading branch information
tmayr authored and terinjokes committed Sep 10, 2019
1 parent fbce975 commit f3a9131
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const resources = {
dnsRecords: require('./lib/resources/DNSRecords'),
enterpriseZoneWorkersScripts: require('./lib/resources/EnterpriseZoneWorkersScripts'),
enterpriseZoneWorkersRoutes: require('./lib/resources/EnterpriseZoneWorkersRoutes'),
enterpriseZoneWorkersKVNamespaces: require('./lib/resources/EnterpriseZoneWorkersKVNamespaces'),
enterpriseZoneWorkersKV: require('./lib/resources/EnterpriseZoneWorkersKV'),
ips: require('./lib/resources/IPs'),
zones: require('./lib/resources/Zones'),
zoneSettings: require('./lib/resources/ZoneSettings'),
Expand Down
5 changes: 4 additions & 1 deletion lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ module.exports = prototypal({
options.body = data;
}

if (options.body && isPlainObject(options.body)) {
if (
options.body &&
(isPlainObject(options.body) || Array.isArray(options.body))
) {
options.body = JSON.stringify(options.body);
}

Expand Down
126 changes: 126 additions & 0 deletions lib/resources/EnterpriseZoneWorkersKV.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2014-present Cloudflare, Inc.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

'use strict';

const prototypal = require('es-class');
const auto = require('autocreate');

const Resource = require('../Resource');
const method = require('../method');

/**
* EnterpriseZoneWorkersKV represents the accounts/:accountId/storage/kv/namespaces API endpoint.
*
* @class EnterpriseZoneWorkersKV
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'accounts/:accountId/storage/kv/namespaces/:namespaceId',

/**
* browse allows for listing all the keys of a namespace
*
* @function browse
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @returns {Promise<Object>} The KV response object.
*/
browse: method({
method: 'GET',
path: 'keys',
}),
/**
* add allows for creating a key-value pair in a namespace
*
* @function add
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @param {string} value - The value to store
* @returns {Promise<Object>} The KV response object
*/
add: method({
method: 'PUT',
path: 'values/:id',
}),
/**
* read allows for reading the contents of key in a namespace
*
* @function read
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @param {string} key_name - The key name
* @returns {Promise<Object>} The KV response object
*/
read: method({
method: 'GET',
path: 'values/:id',
json: false,
contentType: 'text/plain',
}),
/**
* del allows for deleting a key and its contents in a namespace
*
* @function del
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @param {string} key_name - The key name
* @returns {Promise<Object>} The KV response object
*/
del: method({
method: 'DELETE',
path: 'values/:id',
}),
/**
* addMulti allows for creating multiple key-value pairs in a namespace
*
* @function addMulti
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @param {Array<Object>} data - An array containing key-vaue pair Objects to add
* @returns {Promise<Object>} The KV response object
*/
addMulti: method({
method: 'PUT',
path: 'bulk',
}),
/**
* delMulti allows for deleting multiple key-value pairs in a namespace
*
* @function delMulti
* @memberof EnterpriseZoneWorkersKV
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} namespace_id - The namespace ID
* @param {Array<String>} data - The array with keys to delete
* @returns {Promise<Object>} The KV response object
*/
delMulti: method({
method: 'DELETE',
path: 'bulk',
}),
})
);
79 changes: 79 additions & 0 deletions lib/resources/EnterpriseZoneWorkersKVNamespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2014-present Cloudflare, Inc.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

'use strict';

const prototypal = require('es-class');
const auto = require('autocreate');

const Resource = require('../Resource');
const method = require('../method');

/**
* EnterpriseZoneWorkersKVNamespaces represents the accounts/:accountId/storage/kv/namespaces API endpoint.
*
* @class EnterpriseZoneWorkersKVNamespaces
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'accounts/:accountId/storage/kv/namespaces',

includeBasic: ['browse', 'add', 'del'],

/**
* browse allows for listing all of a zone's workers namespaces
*
* @function browse
* @memberof EnterpriseZoneWorkersKVNamespaces
* @instance
* @async
* @param {string} account_id - The account ID
* @returns {Promise<Object>} The namespace response object.
*/
/**
* add allows for creating a workers namespace
*
* @function add
* @memberof EnterpriseZoneWorkersKVNamespaces
* @instance
* @async
* @param {string} account_id - The account ID
* @param {Object} config - The namespace object
* @returns {Promise<Object>} The namespace response object.
*/
/**
* del allows for deleting a workers namespace
*
* @function del
* @memberof EnterpriseZoneWorkersKVNamespaces
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} id - The namespace id
* @returns {Promise<Object>} The namespace response object.
*/
/**
* edit allows for renaming a workers namespace
*
* @function edit
* @memberof EnterpriseZoneWorkersKVNamespaces
* @instance
* @async
* @param {string} account_id - The account ID
* @param {string} id - The namespace id
* @param {Object} config - The namespace object
* @returns {Promise<Object>} The namespace response object.
*/
edit: method({
method: 'PUT',
path: ':id',
}),
})
);

0 comments on commit f3a9131

Please sign in to comment.