good-guy-http
Advanced tools
Comparing version 1.7.4 to 1.8.0
@@ -6,6 +6,11 @@ var _ = require('underscore'); | ||
/** | ||
* Checks whether a request is cacheable. Currenlty this depends only on the HTTP method used. | ||
* Checks whether a request is cacheable or retryable. | ||
* It checks if Idempotence was user defined or fallback to default depending on the HTTP method used. | ||
* @param req the request in question | ||
*/ | ||
function isIdempotent(req) { | ||
if (typeof req.idempotent === 'boolean') { | ||
return req.idempotent; | ||
} | ||
return _.contains(IDEM_METHODS, req.method); | ||
@@ -12,0 +17,0 @@ } |
@@ -74,3 +74,4 @@ var _ = require('underscore'); | ||
'mockTimer' | ||
'mockTimer', | ||
'idempotent' | ||
]; | ||
@@ -77,0 +78,0 @@ |
{ | ||
"name": "good-guy-http", | ||
"version": "1.7.4", | ||
"version": "1.8.0", | ||
"description": "The opinionated sane HTTP client with a good guy approach.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -75,3 +75,6 @@ # good-guy-http | ||
errorThreshold: 50 // good-guy stops sending them and periodically checks if the situation improves | ||
} // you can set 'circuitBreaking: false' to turn this off | ||
}, // you can set 'circuitBreaking: false' to turn this off | ||
idempotent: ... // request idempotence (boolean). By default it's automatically selected | ||
// depending on request method | ||
}); | ||
@@ -122,2 +125,30 @@ ``` | ||
Only idempotent requests are cached | ||
#### Idempotence | ||
By default only HEAD, GET and OPTIONS request are treated as idempotent. | ||
That means only requests mentioned above could be cached or retried as they, in short terms, do not modify state | ||
(for full explanation of idempotence follow the [wikipedia description](https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning) on the topic). | ||
That behaviour could be changed using `idempotent` key in options. | ||
For some services does that are not REST-ish (e.g. RPC) marking request as idempotent is quite useful. | ||
For example this way failed ElasticSearch query will be retried up to 5 times: | ||
```javascript | ||
goodGuy({ | ||
url: 'http://elasticsearch-server.io/_search', | ||
method: 'POST', | ||
maxRetries: 5, | ||
idempotent: true, | ||
json: true, | ||
body: { | ||
query: { | ||
match_all: {} | ||
} | ||
} | ||
}) | ||
.then(...); | ||
``` | ||
### Circuit breaker | ||
@@ -139,3 +170,3 @@ | ||
var goodGuy = goodGuyLib({ | ||
circuitBreaking: { errorThreshold: 75 }; // only break the circuit when 75% or more requests fail | ||
circuitBreaking: { errorThreshold: 75 } // only break the circuit when 75% or more requests fail | ||
}); | ||
@@ -142,0 +173,0 @@ var goodGuy = goodGuyLib({circuitBreaking: false}); // no circuit breaking, please |
@@ -5,3 +5,3 @@ var assert = require('assert'); | ||
describe("isIdempotent()", function() { | ||
it('should report true for read-only HTTP methods', function() { | ||
it('should report true for read-only HTTP methods if idempotence was not defined', function() { | ||
var methods = ['HEAD', 'GET', 'OPTIONS']; | ||
@@ -15,3 +15,3 @@ var requests = methods.map(function(method) { | ||
it('should report false for non-idempotent and mutating HTTP methods', function() { | ||
it('should report false for non-idempotent and mutating HTTP methods if idempotence was not defined', function() { | ||
// PUT is idempotent, but for our purposes it's better treated as if it isn't | ||
@@ -25,2 +25,22 @@ var methods = ['POST', 'PUT', 'PATCH', 'DELETE']; | ||
}); | ||
it('should report false for read-only HTTP methods if idempotence was defined to be false', function() { | ||
var methods = ['HEAD', 'GET', 'OPTIONS']; | ||
var requests = methods.map(function(method) { | ||
return {url: 'http://blah.org', method: method, idempotent: false}; | ||
}); | ||
assert.deepEqual(requests.map(isIdempotent), [false, false, false]); | ||
}); | ||
it('should report true for non-idempotent and mutating HTTP methods if idempotence was defined to be true', | ||
function() { | ||
// PUT is idempotent, but for our purposes it's better treated as if it isn't | ||
var methods = ['POST', 'PUT', 'PATCH', 'DELETE']; | ||
var requests = methods.map(function(method) { | ||
return {url: 'http://blah.org', method: method, idempotent: true}; | ||
}); | ||
assert.deepEqual(requests.map(isIdempotent), [true, true, true, true]); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93328
2163
180