Skip to content
Browse files

Merge pull request #3 from isaacs/master

Make purging by url actually purge by url, and catch failures properly
  • Loading branch information...
2 parents 8a4eed0 + 6ecee95 commit 9350b63317972039b8a1ef17118625c85c380ff9 @thisandagain committed
Showing with 15 additions and 16 deletions.
  1. +1 −1 README.md
  2. +14 −15 lib/index.js
View
2 README.md
@@ -52,4 +52,4 @@ The fastly module also includes a few limited "helper" methods that make working
### Testing
```bash
npm test
-```
+```
View
29 lib/index.js
@@ -22,32 +22,31 @@ function Fastly (apikey) {
*
* @param {string} Method
* @param {string} URL
- * @param {string, optional} Host
*
* @return {Object}
*/
-Fastly.prototype.request = function (method, url, host, callback) {
+Fastly.prototype.request = function (method, url, callback) {
var self = this;
- // Handle params
- if (typeof callback === 'undefined') {
- callback = host;
- host = false;
- }
-
// Construct headers
- var headers = { 'X-Fastly-Key': self.apikey };
- if (host) headers.Host = host;
+ var headers = { 'x-fastly-key': self.apikey };
// HTTP request
request({
method: method,
url: 'https://api.fastly.com' + url,
- headers: headers,
- json: {}
+ headers: headers
}, function (err, response, body) {
if (err) return callback(err);
- if (response.code < 200 || response.code > 302) return callback(body);
+ if (response.statusCode < 200 || response.statusCode > 302)
+ return callback(body);
+ if (response.headers['content-type'] === 'application/json') {
+ try {
+ body = JSON.parse(body);
+ } catch (er) {
+ return callback(er);
+ }
+ }
callback(null, body);
});
};
@@ -55,7 +54,7 @@ Fastly.prototype.request = function (method, url, host, callback) {
// -------------------------------------------------------
Fastly.prototype.purge = function (host, url, callback) {
- this.request('PURGE', url, host, callback);
+ this.request('POST', '/purge/' + host + url, callback);
};
Fastly.prototype.purgeAll = function (service, callback) {
@@ -78,4 +77,4 @@ Fastly.prototype.stats = function (service, callback) {
*/
module.exports = function (apikey) {
return new Fastly(apikey);
-};
+};

0 comments on commit 9350b63

Please sign in to comment.
Something went wrong with that request. Please try again.