Skip to content

Add query parameter blacklist option #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/apicache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var url = require('url')
var querystring = require('querystring')
var MemoryCache = require('./memory-cache')
var pkg = require('../package.json')

Expand Down Expand Up @@ -37,6 +38,7 @@ function ApiCache() {
appendKey: [],
jsonp: false,
redisClient: false,
queryParamsBlacklist:[],
headerBlacklist: [],
statusCodes: {
include: [],
Expand Down Expand Up @@ -418,6 +420,26 @@ function ApiCache() {
key = url.parse(key).pathname
}

// Remove blacklisted query params
if (opt.queryParamsBlacklist.length > 0) {
var queryParams = querystring.parse(url.parse(key).query)
if (Object.keys(queryParams).length > 0) {
var validQueryParams = Object.keys(queryParams)
.filter(function(key) {
return globalOptions.queryParamsBlacklist.indexOf(key) === -1
})
.reduce(function(acc, header) {
acc[header] = queryParams[header]
return acc
}, {})

key = url.parse(key).pathname
if (Object.keys(validQueryParams).length > 0) {
key += '?' + querystring.stringify(validQueryParams)
}
}
}

// add appendKey (either custom function or response path)
if (typeof opt.appendKey === 'function') {
key += '$$appendKey=' + opt.appendKey(req, res)
Expand Down