scratch-www/bin/lib/fastly-extended.js

114 lines
4.1 KiB
JavaScript
Raw Normal View History

var Fastly = require('fastly');
2016-04-16 13:46:03 -04:00
/*
* Fastly library extended to allow configuration for a particular service
* and some helper methods.
*
* @param {string} API key
* @param {string} Service id
*/
module.exports = function (apiKey, serviceId) {
var fastly = Fastly(apiKey);
fastly.serviceId = serviceId;
2016-04-16 13:46:03 -04:00
/*
* Helper method for constructing Fastly API urls
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {string} Service id
* @param {number} Version
*
* @return {string}
*/
fastly.getFastlyAPIPrefix = function (serviceId, version) {
return '/service/' + encodeURIComponent(serviceId) + '/version/' + version;
};
2016-04-16 13:46:03 -04:00
/*
* getLatestVersion: Get the most recent version for the configured service
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {callback} Callback with signature *err, latestVersion)
*/
fastly.getLatestVersion = function (cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
return cb('Failed to get latest version. No serviceId configured');
}
var url = '/service/'+ encodeURIComponent(this.serviceId) +'/version';
this.request('GET', url, function (err, versions) {
if (err) {
2016-04-19 10:47:33 -04:00
return cb('Failed to fetch versions: ' + err);
}
var latestVersion = versions.reduce(function (latestVersion, version) {
if (!latestVersion) return version;
if (version.number > latestVersion.number) return version;
return latestVersion;
});
return cb(null, latestVersion);
2016-04-19 10:47:33 -04:00
});
};
2016-04-16 13:46:03 -04:00
/*
* setCondition: Upsert a Fastly condition entry
* Attempts to PUT and POSTs if the PUT request is a 404
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {number} Version number
* @param {object} Condition object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setCondition = function (version, condition, cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
return cb('Failed to set condition. No serviceId configured');
}
var name = condition.name;
var putUrl = this.getFastlyAPIPrefix(this.serviceId, version) + '/condition/' + encodeURIComponent(name);
var postUrl = this.getFastlyAPIPrefix(this.serviceId, version) + '/condition';
return this.request('PUT', putUrl, condition, function (err, response) {
if (err && err.statusCode === 404) {
this.request('POST', postUrl, condition, function (err, response) {
if (err) {
2016-04-19 10:47:33 -04:00
return cb('Failed while inserting condition: ' + err);
}
return cb(null, response);
});
return;
}
if (err) {
2016-04-19 10:47:33 -04:00
return cb('Failed to update condition: ' + err);
}
return cb(null, response);
}.bind(this));
};
2016-04-16 13:46:03 -04:00
/*
* setFastlyHeader: Upsert a Fastly header entry
* Attempts to PUT and POSTs if the PUT request is a 404
2016-04-19 10:47:33 -04:00
*
2016-04-16 13:46:03 -04:00
* @param {number} Version number
* @param {object} Header object sent to the API
* @param {callback} Callback for fastly.request
*/
fastly.setFastlyHeader = function (version, header, cb) {
if (!this.serviceId) {
2016-04-19 10:47:33 -04:00
cb('Failed to set header. No serviceId configured');
}
var name = header.name;
var putUrl = this.getFastlyAPIPrefix(this.serviceId, version) + '/header/' + encodeURIComponent(name);
var postUrl = this.getFastlyAPIPrefix(this.serviceId, version) + '/header';
return this.request('PUT', putUrl, header, function (err, response) {
if (err && err.statusCode === 404) {
this.request('POST', postUrl, header, function (err, response) {
if (err) {
2016-04-19 10:47:33 -04:00
return cb('Failed to insert header: ' + err);
}
return cb(null, response);
});
return;
}
if (err) {
2016-04-19 10:47:33 -04:00
return cb('Failed to update header: ' + err);
}
return cb(null, response);
}.bind(this));
};
return fastly;
};