From 8b716ca3c49482d94d5bf90269b9f2b7598054d2 Mon Sep 17 00:00:00 2001 From: monkpow Date: Wed, 31 Oct 2018 11:10:04 -0700 Subject: [PATCH] [#374] fixes bug in filter signature (#389) --- README.md | 22 ++++++++++++++++++++-- app/steps/filterUserRequest.js | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a843d170..ac8338f8 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,11 @@ DEPRECATED. See proxyReqPathResolver DEPRECATED. See proxyReqPathResolver -#### filter +#### filter (supports Promises) -The ```filter``` option can be used to limit what requests are proxied. Return ```true``` to execute proxy. +The ```filter``` option can be used to limit what requests are proxied. Return +```true``` to continue to execute proxy; return false-y to skip proxy for this +request. For example, if you only want to proxy get request: @@ -136,6 +138,22 @@ app.use('/proxy', proxy('www.google.com', { })); ``` +Promise form: + +```js + app.use(proxy('localhost:12346', { + filter: function (req, res) { + return new Promise(function (resolve) { + resolve(req.method === 'GET'); + }); + } + })); +``` + +Note that in the previous example, `resolve(false)` will execute the happy path +for filter here (skipping the rest of the proxy, and calling `next()`). +`reject()` will also skip the rest of proxy and call `next()`. + #### userResDecorator (was: intercept) (supports Promise) You can modify the proxy's response before sending it to the client. diff --git a/app/steps/filterUserRequest.js b/app/steps/filterUserRequest.js index 7ebc5a5a..68f60be8 100644 --- a/app/steps/filterUserRequest.js +++ b/app/steps/filterUserRequest.js @@ -10,7 +10,7 @@ function filterUserRequest(container) { var resolverFn = container.options.filter || defaultFilter; return Promise - .resolve(resolverFn(container.proxy.reqBuilder, container.user.req)) + .resolve(resolverFn(container.user.req, container.user.res)) .then(function (shouldIContinue) { if (shouldIContinue) { return Promise.resolve(container);