Skip to content

Commit

Permalink
feat: Workers API
Browse files Browse the repository at this point in the history
This change adds support for the Cloudflare Workers API endpoints,
allowing users to upload and manage their worker scripts and routes.
Two set of endpoints are provided, one for non-Enterprise zones, and
another for Enterprise zones.

Internally, endpoints can now specify an alternative Content-Type for
the request body and disable automatic JSON parsing of the response.

[terinjokes@gmail.com: dropped non-source changes and added commit msg]
Signed-off-by: Terin Stock <terinjokes@gmail.com>
  • Loading branch information
hankjacobs authored and terinjokes committed Jul 24, 2018
1 parent 7d3d1b8 commit 92af8b0
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 3 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ const proxy = require('./lib/proxy');
/* eslint-disable global-require */
const resources = {
dnsRecords: require('./lib/resources/DNSRecords'),
enterpriseZoneWorkersScripts: require('./lib/resources/EnterpriseZoneWorkersScripts'),
enterpriseZoneWorkersRoutes: require('./lib/resources/EnterpriseZoneWorkersRoutes'),
ips: require('./lib/resources/IPs'),
zones: require('./lib/resources/Zones'),
zoneSettings: require('./lib/resources/ZoneSettings'),
zoneCustomHostNames: require('./lib/resources/ZoneCustomHostNames'),
zoneWorkers: require('./lib/resources/ZoneWorkers'),
zoneWorkersScript: require('./lib/resources/ZoneWorkersScript'),
zoneWorkersRoutes: require('./lib/resources/ZoneWorkersRoutes'),
user: require('./lib/resources/User'),
};
/* eslint-enable global-require */
Expand Down
4 changes: 2 additions & 2 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ module.exports = prototypal({
const uri = `https://api.cloudflare.com/client/v4/${requestPath}`;

const options = {
json: true,
json: opts.json !== false,
timeout: opts.timeout || 1e4,
retries: opts.retries,
method: requestMethod,
headers: {
'user-agent': `cloudflare/${pkg.version} node/${process.versions.node}`,
'Content-Type': 'application/json',
'Content-Type': opts.contentType || 'application/json',
Accept: 'application/json',
'X-Cloudflare-Client-User-Agent': USER_AGENT,
},
Expand Down
7 changes: 6 additions & 1 deletion lib/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const isOptionsHash = function isOptionsHash(obj) {

const getDataFromArgs = function getDataFromArgs(args) {
if (args.length > 0) {
if (isPlainObject(args[0]) && !isOptionsHash(args[0])) {
if (!isOptionsHash(args[0])) {
return args.shift();
}
}
Expand Down Expand Up @@ -75,6 +75,8 @@ const identity = function identity(x) {
module.exports = function(spec) {
const requestMethod = (spec.method || 'GET').toUpperCase();
const encode = spec.encode || identity;
const json = spec.json !== false;
const contentType = spec.contentType || 'application/json';

return function() {
const fullPath = this.createFullPath(spec.path);
Expand Down Expand Up @@ -107,6 +109,9 @@ module.exports = function(spec) {
const data = encode(getDataFromArgs(args));
const opts = getOptionsFromArgs(args);

opts.json = json;
opts.contentType = contentType;

if (args.length !== 0) {
err = new Error(
`Cloudflare: Unknown arguments (${args}). Did you mean to pass an options object? (on API request to ${requestMethod} ${fullPath})`
Expand Down
85 changes: 85 additions & 0 deletions lib/resources/EnterpriseZoneWorkersRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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');

/**
* EnterpriseZoneWorkersRoutes represents the zones/:zoneId/workers/routes API endpoint.
*
* @class EnterpriseZoneWorkersRoutes
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'zones/:zoneId/workers/routes',

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

/**
* browse allows for listing all of a zone's workers routes
*
* @function browse
* @memberof EnterpriseZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @returns {Promise<Object>} The route browse response object.
*/
/**
* read allows for retrieving a specific zone's workers route
*
* @function read
* @memberof EnterpriseZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID
* @returns {Promise<Object>} The route response object.
*/
/**
* edit allows for modifying a specific zone's workers
*
* @function edit
* @memberof EnterpriseZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID
* @param {Object} config - The modified route object
* @returns {Promise<Object>} The custom hostname response object.
*/
/**
* add allows for creating a workers route
*
* @function add
* @memberof EnterpriseZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {Object} config - The new route object
* @returns {Promise<Object>} The custom route response object.
*/
/**
* del allows for removing a workers routes
*
* @function del
* @memberof EnterpriseZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID to delete
* @returns {Promise<Object>} The custom route response object.
*/
})
);
88 changes: 88 additions & 0 deletions lib/resources/EnterpriseZoneWorkersScripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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');

/**
* EnterpriseZoneWorkersScripts represents the accounts/:accountId/workers/scripts API endpoint.
*
* @class EnterpriseZoneWorkersScripts
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'accounts/:accountId/workers/scripts',

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

/**
* read retrieves a single workers script
*
* @function read
* @memberof EnterpriseZoneWorkersScripts
* @instance
* @async
* @param {string} account_id - The enterprise account ID
* @param {string} name - The script name
* @returns {Promise<Object>} The workers script response object.
*/
read: method({
method: 'GET',
path: ':name',
json: false,
}),

/**
* edit uploads a new version of a workers script
*
* @function edit
* @memberof EnterpriseZoneWorkersScripts
* @instance
* @async
* @param {string} account_id - The enterprise account ID
* @param {string} name - The script name
* @param {string} script - The script
* @returns {Promise<Object>} The response object
*/
edit: method({
method: 'PUT',
path: ':name',
contentType: 'application/javascript',
}),

/**
* browse allows for listing all the workers scripts
*
* @function browse
* @memberof EnterpriseZoneWorkersScripts
* @instance
* @async
* @param {string} account_id - The enterprise account ID
* @param {string} name - The script name
* @returns {Promise<Object>} The zone workers script response object.
*/
/**
* del allows for deleting the specified workers script
*
* @function del
* @memberof EnterpriseZoneWorkersScripts
* @instance
* @async
* @param {string} account_id - The enterprise account ID
* @param {string} name - The script name
* @returns {Promise<Object>} The deleted zone workers script response object.
*/
})
);
45 changes: 45 additions & 0 deletions lib/resources/ZoneWorkers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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');

/**
* ZoneWorkers represents the /zones/:zoneId/workers API endpoint.
*
* @class ZoneWorkers
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'zones/:zoneId/workers',

/**
* validate allows for validating a workers script
*
* @function validate
* @memberof ZoneWorkers
* @instance
* @async
* @param {string} zoneId - The zone ID
* @param {string} script - The worker script
* @returns {Promise<Object>} The validate response object.
*/
validate: method({
method: 'PUT',
path: 'validate',
contentType: 'application/javascript',
}),
})
);
91 changes: 91 additions & 0 deletions lib/resources/ZoneWorkersRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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');

/**
* ZoneWorkersRoutes represents the zones/:zoneId/workers/filters API endpoint.
*
* @class ZoneWorkersRoutes
* @hideconstructor
* @extends Resource
*/
module.exports = auto(
prototypal({
extends: Resource,
path: 'zones/:zoneId/workers/filters',

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

/**
* edit allows for modifying a specific zone's workers route
*
* @function edit
* @memberof ZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID
* @param {Object} config - The modified route object
* @returns {Promise<Object>} The custom hostname response object.
*/
edit: method({
method: 'PUT',
path: ':id',
}),

/**
* browse allows for listing all of a zone's workers routes
*
* @function browse
* @memberof ZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @returns {Promise<Object>} The route browse response object.
*/
/**
* read allows for retrieving a specific zone's workers route
*
* @function read
* @memberof ZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID
* @returns {Promise<Object>} The route response object.
*/
/**
* add allows for creating a workers route
*
* @function add
* @memberof ZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {Object} config - The new route object
* @returns {Promise<Object>} The custom route response object.
*/
/**
* del allows for removing a workers route
*
* @function del
* @memberof ZoneWorkersRoutes
* @instance
* @async
* @param {string} zone_id - The zone ID
* @param {string} id - The route ID to delete
* @returns {Promise<Object>} The custom route response object.
*/
})
);
Loading

0 comments on commit 92af8b0

Please sign in to comment.