Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f3a9131

Browse files
tmayrterinjokes
authored andcommitted
feat(resources): support Workers KV
Add support for the Workers KV API endpoints. Signed-off-by: Terin Stock <terin@cloudflare.com>
1 parent fbce975 commit f3a9131

4 files changed

Lines changed: 211 additions & 1 deletion

File tree

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const resources = {
1818
dnsRecords: require('./lib/resources/DNSRecords'),
1919
enterpriseZoneWorkersScripts: require('./lib/resources/EnterpriseZoneWorkersScripts'),
2020
enterpriseZoneWorkersRoutes: require('./lib/resources/EnterpriseZoneWorkersRoutes'),
21+
enterpriseZoneWorkersKVNamespaces: require('./lib/resources/EnterpriseZoneWorkersKVNamespaces'),
22+
enterpriseZoneWorkersKV: require('./lib/resources/EnterpriseZoneWorkersKV'),
2123
ips: require('./lib/resources/IPs'),
2224
zones: require('./lib/resources/Zones'),
2325
zoneSettings: require('./lib/resources/ZoneSettings'),

lib/Client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ module.exports = prototypal({
7575
options.body = data;
7676
}
7777

78-
if (options.body && isPlainObject(options.body)) {
78+
if (
79+
options.body &&
80+
(isPlainObject(options.body) || Array.isArray(options.body))
81+
) {
7982
options.body = JSON.stringify(options.body);
8083
}
8184

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (C) 2014-present Cloudflare, Inc.
3+
4+
* This software may be modified and distributed under the terms
5+
* of the MIT license. See the LICENSE file for details.
6+
*/
7+
8+
'use strict';
9+
10+
const prototypal = require('es-class');
11+
const auto = require('autocreate');
12+
13+
const Resource = require('../Resource');
14+
const method = require('../method');
15+
16+
/**
17+
* EnterpriseZoneWorkersKV represents the accounts/:accountId/storage/kv/namespaces API endpoint.
18+
*
19+
* @class EnterpriseZoneWorkersKV
20+
* @hideconstructor
21+
* @extends Resource
22+
*/
23+
module.exports = auto(
24+
prototypal({
25+
extends: Resource,
26+
path: 'accounts/:accountId/storage/kv/namespaces/:namespaceId',
27+
28+
/**
29+
* browse allows for listing all the keys of a namespace
30+
*
31+
* @function browse
32+
* @memberof EnterpriseZoneWorkersKV
33+
* @instance
34+
* @async
35+
* @param {string} account_id - The account ID
36+
* @param {string} namespace_id - The namespace ID
37+
* @returns {Promise<Object>} The KV response object.
38+
*/
39+
browse: method({
40+
method: 'GET',
41+
path: 'keys',
42+
}),
43+
/**
44+
* add allows for creating a key-value pair in a namespace
45+
*
46+
* @function add
47+
* @memberof EnterpriseZoneWorkersKV
48+
* @instance
49+
* @async
50+
* @param {string} account_id - The account ID
51+
* @param {string} namespace_id - The namespace ID
52+
* @param {string} value - The value to store
53+
* @returns {Promise<Object>} The KV response object
54+
*/
55+
add: method({
56+
method: 'PUT',
57+
path: 'values/:id',
58+
}),
59+
/**
60+
* read allows for reading the contents of key in a namespace
61+
*
62+
* @function read
63+
* @memberof EnterpriseZoneWorkersKV
64+
* @instance
65+
* @async
66+
* @param {string} account_id - The account ID
67+
* @param {string} namespace_id - The namespace ID
68+
* @param {string} key_name - The key name
69+
* @returns {Promise<Object>} The KV response object
70+
*/
71+
read: method({
72+
method: 'GET',
73+
path: 'values/:id',
74+
json: false,
75+
contentType: 'text/plain',
76+
}),
77+
/**
78+
* del allows for deleting a key and its contents in a namespace
79+
*
80+
* @function del
81+
* @memberof EnterpriseZoneWorkersKV
82+
* @instance
83+
* @async
84+
* @param {string} account_id - The account ID
85+
* @param {string} namespace_id - The namespace ID
86+
* @param {string} key_name - The key name
87+
* @returns {Promise<Object>} The KV response object
88+
*/
89+
del: method({
90+
method: 'DELETE',
91+
path: 'values/:id',
92+
}),
93+
/**
94+
* addMulti allows for creating multiple key-value pairs in a namespace
95+
*
96+
* @function addMulti
97+
* @memberof EnterpriseZoneWorkersKV
98+
* @instance
99+
* @async
100+
* @param {string} account_id - The account ID
101+
* @param {string} namespace_id - The namespace ID
102+
* @param {Array<Object>} data - An array containing key-vaue pair Objects to add
103+
* @returns {Promise<Object>} The KV response object
104+
*/
105+
addMulti: method({
106+
method: 'PUT',
107+
path: 'bulk',
108+
}),
109+
/**
110+
* delMulti allows for deleting multiple key-value pairs in a namespace
111+
*
112+
* @function delMulti
113+
* @memberof EnterpriseZoneWorkersKV
114+
* @instance
115+
* @async
116+
* @param {string} account_id - The account ID
117+
* @param {string} namespace_id - The namespace ID
118+
* @param {Array<String>} data - The array with keys to delete
119+
* @returns {Promise<Object>} The KV response object
120+
*/
121+
delMulti: method({
122+
method: 'DELETE',
123+
path: 'bulk',
124+
}),
125+
})
126+
);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2014-present Cloudflare, Inc.
3+
4+
* This software may be modified and distributed under the terms
5+
* of the MIT license. See the LICENSE file for details.
6+
*/
7+
8+
'use strict';
9+
10+
const prototypal = require('es-class');
11+
const auto = require('autocreate');
12+
13+
const Resource = require('../Resource');
14+
const method = require('../method');
15+
16+
/**
17+
* EnterpriseZoneWorkersKVNamespaces represents the accounts/:accountId/storage/kv/namespaces API endpoint.
18+
*
19+
* @class EnterpriseZoneWorkersKVNamespaces
20+
* @hideconstructor
21+
* @extends Resource
22+
*/
23+
module.exports = auto(
24+
prototypal({
25+
extends: Resource,
26+
path: 'accounts/:accountId/storage/kv/namespaces',
27+
28+
includeBasic: ['browse', 'add', 'del'],
29+
30+
/**
31+
* browse allows for listing all of a zone's workers namespaces
32+
*
33+
* @function browse
34+
* @memberof EnterpriseZoneWorkersKVNamespaces
35+
* @instance
36+
* @async
37+
* @param {string} account_id - The account ID
38+
* @returns {Promise<Object>} The namespace response object.
39+
*/
40+
/**
41+
* add allows for creating a workers namespace
42+
*
43+
* @function add
44+
* @memberof EnterpriseZoneWorkersKVNamespaces
45+
* @instance
46+
* @async
47+
* @param {string} account_id - The account ID
48+
* @param {Object} config - The namespace object
49+
* @returns {Promise<Object>} The namespace response object.
50+
*/
51+
/**
52+
* del allows for deleting a workers namespace
53+
*
54+
* @function del
55+
* @memberof EnterpriseZoneWorkersKVNamespaces
56+
* @instance
57+
* @async
58+
* @param {string} account_id - The account ID
59+
* @param {string} id - The namespace id
60+
* @returns {Promise<Object>} The namespace response object.
61+
*/
62+
/**
63+
* edit allows for renaming a workers namespace
64+
*
65+
* @function edit
66+
* @memberof EnterpriseZoneWorkersKVNamespaces
67+
* @instance
68+
* @async
69+
* @param {string} account_id - The account ID
70+
* @param {string} id - The namespace id
71+
* @param {Object} config - The namespace object
72+
* @returns {Promise<Object>} The namespace response object.
73+
*/
74+
edit: method({
75+
method: 'PUT',
76+
path: ':id',
77+
}),
78+
})
79+
);

0 commit comments

Comments
 (0)