superagent-cache
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "superagent-cache", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Superagent with flexible built-in caching.", | ||
@@ -5,0 +5,0 @@ "main": "superagentCache.js", |
@@ -291,3 +291,3 @@ # superagent-cache | ||
This is the second constructor param you handed in when you instantiated `superagent-cache`. If you didn't provide one, then it's an instance of `cacheModule`. | ||
This is the second constructor param you handed in when you instantiated `superagent-cache`. If you didn't provide one, then it's an instance of `cacheModule`. You can assign it or call functions on it at runtime. | ||
@@ -300,2 +300,12 @@ #### Example | ||
## .defaults | ||
This is the third constructor param you handed in when you instantiated `superagent-cache`. If you didn't provide one, then it uses the internal defaults. You can assign it or update it at runtime. | ||
#### Example | ||
```javascript | ||
superagent.defaults... //You can call any function existing on the cache you passed in | ||
``` | ||
# Using Background Refresh | ||
@@ -302,0 +312,0 @@ |
@@ -13,6 +13,6 @@ /** | ||
superagent.cache = (cache) ? cache : new require('cache-service-cache-module')(); | ||
defaults = defaults || {}; | ||
superagent.defaults = defaults || {}; | ||
var Request = superagent.Request; | ||
var props = resetProps(); | ||
var supportedMethods = ['GET', 'HEAD', 'PUT', 'DELETE']; | ||
var props = resetProps(superagent.defaults); | ||
var supportedMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE']; | ||
var cacheableMethods = ['GET', 'HEAD']; | ||
@@ -102,3 +102,3 @@ superagent.patchedBySuperagentCache = true; | ||
Request.prototype._end = function(cb){ | ||
props = resetProps(); | ||
props = resetProps(superagent.defaults); | ||
this.execute(cb); | ||
@@ -113,3 +113,3 @@ } | ||
var curProps = props; | ||
props = resetProps(); | ||
props = resetProps(superagent.defaults); | ||
if(~supportedMethods.indexOf(this.method)){ | ||
@@ -168,3 +168,5 @@ var _this = this; | ||
if(!err && response){ | ||
superagent.cache.del(key, function (){ | ||
var keyGet = key.replace('"method":"' + _this.method + '"', '"method":"GET"'); | ||
var keyHead = key.replace('"method":"' + _this.method + '"', '"method":"HEAD"'); | ||
superagent.cache.del([keyGet, keyHead], function (){ | ||
callbackExecutor(cb, err, response, key); | ||
@@ -314,12 +316,12 @@ }); | ||
*/ | ||
function resetProps(){ | ||
function resetProps(d){ | ||
return { | ||
doQuery: (typeof defaults.doQuery === 'boolean') ? defaults.doQuery : true, | ||
cacheWhenEmpty: (typeof defaults.cacheWhenEmpty === 'boolean') ? defaults.cacheWhenEmpty : true, | ||
prune: defaults.prune, | ||
pruneParams: defaults.pruneParams, | ||
pruneOptions: defaults.pruneOptions, | ||
responseProp: defaults.responseProp, | ||
expiration: defaults.expiration, | ||
backgroundRefresh: defaults.backgroundRefresh | ||
doQuery: (typeof d.doQuery === 'boolean') ? d.doQuery : true, | ||
cacheWhenEmpty: (typeof d.cacheWhenEmpty === 'boolean') ? d.cacheWhenEmpty : true, | ||
prune: d.prune, | ||
pruneParams: d.pruneParams, | ||
pruneOptions: d.pruneOptions, | ||
responseProp: d.responseProp, | ||
expiration: d.expiration, | ||
backgroundRefresh: d.backgroundRefresh | ||
}; | ||
@@ -326,0 +328,0 @@ } |
@@ -262,2 +262,15 @@ var expect = require('expect'); | ||
it('.get() .end() should retrieve and cache response', function (done) { | ||
superagent | ||
.get('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('one'); | ||
superagent.cache.get(key, function (err, response){ | ||
expect(response.body.key).toBe('one'); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
it('.get() ._end() should bypass all caching logic', function (done) { | ||
@@ -278,16 +291,5 @@ superagent | ||
.end(function (err, response, key){ | ||
expect(typeof key).toBe('undefined'); | ||
expect(response.body.key).toBe('post'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('.get() .end() should retrieve and cache response', function (done) { | ||
superagent | ||
.get('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(response.body.key).toBe('one'); | ||
superagent.cache.get(key, function (err, response){ | ||
expect(response.body.key).toBe('one'); | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response).toBe(null); | ||
done(); | ||
@@ -299,3 +301,3 @@ }); | ||
it('.put() .end() should invalidate the generated cache key', function (done) { | ||
it('.get() then .put() should invalidate cache', function (done) { | ||
superagent | ||
@@ -305,3 +307,3 @@ .get('localhost:3000/one') | ||
expect(response.body.key).toBe('one'); | ||
superagent.cache.get(key, function (err, response){ | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response.body.key).toBe('one'); | ||
@@ -311,5 +313,4 @@ superagent | ||
.end(function (err, response, key){ | ||
expect(typeof key).toBe('string'); | ||
expect(response.body.key).toBe('put'); | ||
superagent.cache.get(key, function (err, response){ | ||
superagent.cache.get(key, function (err, response) { | ||
expect(response).toBe(null); | ||
@@ -325,3 +326,3 @@ done(); | ||
it('.del() .end() should invalidate the generated cache key', function (done) { | ||
it('.get() then .del() should invalidate the generated cache key', function (done) { | ||
superagent | ||
@@ -334,6 +335,5 @@ .get('localhost:3000/one') | ||
superagent | ||
.put('localhost:3000/one') | ||
.del('localhost:3000/one') | ||
.end(function (err, response, key){ | ||
expect(typeof key).toBe('string'); | ||
expect(response.body.key).toBe('put'); | ||
expect(response.body.key).toBe('delete'); | ||
superagent.cache.get(key, function (err, response){ | ||
@@ -480,8 +480,5 @@ expect(response).toBe(null); | ||
describe('configurability tests', function () { | ||
//Necessary to eliminate the superagent singleton so we can create another with a defaults object | ||
delete require.cache[require.resolve('superagent')]; | ||
var superagent = require('superagent'); | ||
require('../../superagentCache')(superagent, cacheModule, {doQuery: false, expiration: 1}); | ||
it('Should be able to configure global settings: doQuery', function (done) { | ||
superagent.defaults = {doQuery: false, expiration: 1}; | ||
superagent | ||
@@ -499,2 +496,3 @@ .get('localhost:3000/one') | ||
it('Global settings should be locally overwritten by chainables: doQuery', function (done) { | ||
superagent.defaults = {doQuery: false, expiration: 1}; | ||
superagent | ||
@@ -514,2 +512,3 @@ .get('localhost:3000/one') | ||
it('Should be able to configure global settings: expiration', function (done) { | ||
superagent.defaults = {doQuery: false, expiration: 1}; | ||
superagent | ||
@@ -539,2 +538,3 @@ .get('localhost:3000/one') | ||
it('Global settings should be locally overwritten by chainables: expiration', function (done) { | ||
superagent.defaults = {doQuery: false, expiration: 1}; | ||
superagent | ||
@@ -541,0 +541,0 @@ .get('localhost:3000/one') |
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
47878
853
438