Skip to content

Commit

Permalink
Merge pull request #10 from jpodwys/prune-add-gut-param
Browse files Browse the repository at this point in the history
Add gutResponse as second param to prune callback
  • Loading branch information
jpodwys authored Dec 6, 2019
2 parents bc65a0d + d38c680 commit 2ba8105
Show file tree
Hide file tree
Showing 5 changed files with 728 additions and 5 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ superagent
);
```

## .prune(callback (response))
## .prune(callback (response, gutResponse))

> Caution: if you use this function, `supergent-cache-plugin` [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-plugin` [will not automatically gut](#what-exactly-gets-cached) the `response` object for you (although you can use the `gutResponse` param to do so manually--more on that below). 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 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.

Your prune function will optionally receive `gutResponse` as its second param. This is the method that `superagent-cache-plugin` calls internally to simplify responses. If, based on some processing within the function you pass to `.prune`, you decide that no custom return value is necessary, or you would like to take advantage of the default behavior plus some of your own, you can call `gutResponse(response)` to get the value that `superagent-cache-plugin` would have returned without a call to `.prune`.

#### Arguments

* callback: a function that accepts superagent's response object and returns a truthy value or null
* callback: a function that accepts superagent's response object and the gutResponse function and returns a truthy value or null

#### Example

Expand All @@ -200,6 +202,27 @@ superagent
);
```

#### Example Using `gutResponse`

```javascript
var prune = function(r, gutResponse){
var output = gutResponse(r);
output.customValue = getCustomValue();
return output;
}

//response will now be superagent-cache-plugin's default output plus `customValue`
//all of which will be cached rather than the entire superagent response
superagent
.get(uri)
.use(superagentCache)
.prune(prune)
.end(function (error, response){
// handle response
}
);
```

## .pruneQuery(params)

In the event that you need certain query params to execute a query but cannot have those params as part of your cache key (useful when security or time-related params are sent), use `.pruneQuery()` to remove those properties. Pass `.pruneQuery()` an array containing the param keys you want omitted from the cache key.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = function(cache, defaults){
else if(!err && response){
response.redirects = _Request.scRedirectsList;
if(props.prune){
response = props.prune(response);
response = props.prune(response, utils.gutResponse);
}
else if(props.responseProp){
response = response[props.responseProp] || null;
Expand Down
Loading

0 comments on commit 2ba8105

Please sign in to comment.