Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

superagent-cache

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

superagent-cache - npm Package Compare versions

Comparing version 1.0.6 to 1.1.0

2

package.json
{
"name": "superagent-cache",
"version": "1.0.6",
"version": "1.1.0",
"description": "Superagent with flexible built-in caching.",

@@ -5,0 +5,0 @@ "main": "superagentCache.js",

@@ -76,3 +76,3 @@ # superagent-cache

To use a custom configuraiton, take advantage of the the two optional params you can hand to `superagent-cache`'s [`require` command](#user-content-requiresuperagent-cachesuperagent-cache) as follows:
To use a custom configuraiton, take advantage of the the three optional params you can hand to `superagent-cache`'s [`require` command](#user-content-requiresuperagent-cachesuperagent-cache) (`superagent`, `cache`, and `defaults`) as follows:

@@ -84,11 +84,27 @@ ```javascript

var redisCache = new redisModule({redisEnv: 'REDISCLOUD_URL'});
var defaults = {cacheWhenEmpty: false, expiration: 900};
//Patch my superagent instance and pass in my redis cache
require('superagent-cache')(superagent, redisCache);
require('superagent-cache')(superagent, redisCache, defaults);
```
This example allows you to provide your own instance of `superagent` to be patched as well as allowing you to pass in your own, pre-instantiated cache. Here's a list of [supported caches](#supported-caches).
This example allows you to provide your own instance of `superagent` to be patched as well as allowing you to pass in your own, pre-instantiated cache and some defaults for superagent-cache to use with all queries. Here's a list of [supported caches](#supported-caches).
For more information on `require` command params usage, see [this section](#various-ways-of-requiring-superagentcache).
All data passed in the `defaults` object will apply to all queries made with superagent-cache unless overwritten with chainables. See the [Available Configuration Options](#available-configuration-options) section for a list of all options you can pass.
For more information on `require` command params usage, see [this section](#various-ways-of-requiring-superagent-cache).
# Available Configuration Options
All options that can be passed to the `defaults` `require` param can be overwritten with chainables of the same name. All of the below options are detailed in the [API section](#api).
* responseProp
* prune
* pruneParams
* pruneOptions
* expiration
* cacheWhenEmpty
* doQuery
* backgroundRefresh
# Supported Caches

@@ -159,5 +175,5 @@

## .prune(callback (response))
## .prune(callback (response, gutFunction))
> Caution: if you use this function, `supergent-cache` [will not gut](#what-exactly-gets-cached) the `response` object for you. Be sure that the result of your `.prune()` callback function will never be circular and is not larger than it needs to be.
> Caution: if you use this function, `supergent-cache` [will not gut](#what-exactly-gets-cached) the `response` object for you. Be sure that the result of your `.prune()` callback function will never be circular and is not larger than it needs to be. If you are simply checking for the existence of an attribute and still want superagent-cache to gut the response for you, use the `gutFunction` param as shown in example 2 below.

@@ -173,4 +189,4 @@ If you need to dig several layers into superagent's response, you can do so by passing a function to `.prune()`. Your prune function will receive superagent's response and should return a truthy value or null. The benefit of using this function is that you can cache only what you need.

```javascript
var prune = funtion(r){
return (r && r.ok && r.body && r.body.user) ? r.body.user : null;
var prune = function(r, gut){
if(r && r.ok && r.body && r.body.user) ? r.body.user : null;
}

@@ -189,2 +205,22 @@

#### Example 2
```javascript
var prune = funtion(r, gut){
if(r && r.ok && r.body && r.body.user){
return gut(r);
}
return null;
}
//response will now be gutted by superagent-cache
superagent
.get(uri)
.prune(prune)
.end(function (error, response){
// handle response
}
);
```
## .pruneParams(params)

@@ -408,2 +444,6 @@

#### With `defaults`
The `defaults` object can be passed as the third param at any time. It does not affect the `superagent` or `cache` params. You can see a brief demo [here](#how-do-i-use-a-custom-configuration) and a list of all the options you can pass in the `defaults` object [here](#available-configuration-options).
# Breaking Change History

@@ -410,0 +450,0 @@

@@ -7,17 +7,11 @@ /**

*/
module.exports = function(agent, cache){
module.exports = function(agent, cache, defaults){
var superagent = (agent) ? agent : require('superagent');
if(cache){
superagent.cache = cache;
}
else{
var cModule = require('cache-service-cache-module');
superagent.cache = new cModule();
}
if(!superagent.patchedBySuperagentCache){
superagent.cache = (cache) ? cache : new require('cache-service-cache-module')();
defaults = defaults || {};
var Request = superagent.Request;
var props = {doQuery: true, cacheWhenEmpty: true};
var props = resetProps();
var supportedMethods = ['GET', 'HEAD', 'PUT', 'DELETE'];

@@ -108,3 +102,3 @@ var cacheableMethods = ['GET', 'HEAD'];

Request.prototype._end = function(cb){
resetProps();
props = resetProps();
this.execute(cb);

@@ -119,3 +113,3 @@ }

var curProps = props;
resetProps();
props = resetProps();
if(~supportedMethods.indexOf(this.method)){

@@ -320,3 +314,12 @@ var _this = this;

function resetProps(){
props = {doQuery: true, cacheWhenEmpty: true};
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
};
}

@@ -362,23 +365,9 @@

}
else if(cb.length === 2){
cb(err, response);
}
else if(cb.length === 3){
else if(cb.length > 1){
cb(err, response, key);
}
else{
throw new exception('UnsupportedCallbackException', 'You must have 1, 2, or 3 callback params in your .end() callback argument list.');
throw new Error('UnsupportedCallbackException: Your .end() callback must pass at least one argument.');
}
}
/**
* Instantates an exception to be thrown
* @param {string} name
* @param {string} message
* @return {exception}
*/
function exception(name, message){
this.name = name;
this.message = message;
}
}

@@ -385,0 +374,0 @@

@@ -7,2 +7,3 @@ var expect = require('expect');

require('../../superagentCache')(superagent, cacheModule);
//To make sure requiring a second time won't break anything
require('../../superagentCache')(superagent, cacheModule);

@@ -16,2 +17,6 @@

app.get('/four', function(req, res){
res.send(400, {key: 'one'});
});
app.post('/one', function(req, res){

@@ -43,3 +48,3 @@ res.send(200, {key: 'post'});

describe('Array', function(){
describe('superagentCache', function(){

@@ -50,3 +55,3 @@ beforeEach(function(){

describe('superagentCache API tests', function () {
describe('API tests', function () {

@@ -226,5 +231,36 @@ it('.end() should not require the \'err\' callback param', function (done) {

it('.get() .cacheWhenEmpty(false) .prune(function) should only cache responses with status code 2xx', function (done) {
var prune = function(r){
if(r && r.statusCode && r.statusCode.toString()[0] === '2'){
return r.statusCode;
}
return null;
}
superagent
.get('localhost:3000/four')
.cacheWhenEmpty(false)
.prune(prune)
.end(function (err, response, key) {
superagent.cache.get(key, function (err, response){
expect(response).toBe(null);
superagent
.get('localhost:3000/one')
.cacheWhenEmpty(false)
.prune(prune)
.end(function (err, response, key) {
superagent.cache.get(key, function (err, response){
expect(response).toBe(200);
done();
});
}
);
});
}
);
});
});
describe('superagentCache caching tests', function () {
describe('caching tests', function () {

@@ -314,3 +350,3 @@ it('.get() ._end() should bypass all caching logic', function (done) {

describe('superagentCache background refresh tests', function () {
describe('background refresh tests', function () {

@@ -352,3 +388,3 @@ it('.get() .expiration() .end() background refresh should not work if the chainable is not used', function (done) {

it('.get() .query(string&string) .expiration() .end() background refresh should not work if the chainable is not used', function (done) {
it('.get() .query(string&string) .end() background refresh should not work if the chainable is not used', function (done) {
superagent

@@ -373,3 +409,3 @@ .get('localhost:3000/params')

it('.get() .query(string&string) .expiration() .backgroundRefresh(true) .end() background refresh should refresh a key shortly before expiration', function (done) {
it('.get() .query(string&string) .backgroundRefresh(true) .end() background refresh should refresh a key shortly before expiration', function (done) {
superagent

@@ -444,2 +480,86 @@ .get('localhost:3000/params')

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
.get('localhost:3000/one')
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toBe(null);
done();
});
}
);
});
it('Global settings should be locally overwritten by chainables: doQuery', function (done) {
superagent
.get('localhost:3000/one')
.doQuery(true)
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toNotBe(null);
expect(response.body.key).toBe('one');
done();
});
}
);
});
it('Should be able to configure global settings: expiration', function (done) {
superagent
.get('localhost:3000/one')
.doQuery(true)
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toNotBe(null);
expect(response.body.key).toBe('one');
setTimeout(function(){
superagent
.get('localhost:3000/one')
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toBe(null);
done();
});
}
);
}, 1000);
});
}
);
});
it('Global settings should be locally overwritten by chainables: expiration', function (done) {
superagent
.get('localhost:3000/one')
.doQuery(true)
.expiration(2)
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toNotBe(null);
expect(response.body.key).toBe('one');
setTimeout(function(){
superagent
.get('localhost:3000/one')
.end(function (err, response, key){
superagent.cache.get(key, function (err, response) {
expect(response).toNotBe(null);
expect(response.body.key).toBe('one');
done();
});
}
);
}, 1000);
});
}
);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc