Skip to content
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

[#437] dynamic value generation of option parseReqBody #457

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@ app.use('/proxy', proxy('www.google.com', {
parseReqBody: false
}));
```
You can use function instead of boolean value for dynamic value generation based on request

```js
app.use('/proxy', proxy('www.google.com', {
parseReqBody: function (proxyReq) {
if (proxyReq.headers["content-type"] === "application/json") {
return true;
} else {
return false;
}
}
v-electrolux marked this conversation as resolved.
Show resolved Hide resolved
}));
```

#### reqAsBuffer

Expand Down
3 changes: 2 additions & 1 deletion app/steps/buildProxyReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function buildProxyReq(Container) {
var options = Container.options;
var host = Container.proxy.host;

var parseBody = (!options.parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(req, res, options);
var parseReqBody = (typeof options.parseReqBody === 'function') ? options.parseReqBody(req) : options.parseReqBody;
v-electrolux marked this conversation as resolved.
Show resolved Hide resolved
var parseBody = (!parseReqBody) ? Promise.resolve(null) : requestOptions.bodyContent(req, res, options);
var createReqOptions = requestOptions.create(req, res, options, host);

return Promise
Expand Down
3 changes: 2 additions & 1 deletion app/steps/sendProxyRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function sendProxyRequest(Container) {

proxyReq.on('error', reject);

var parseReqBody = (typeof options.parseReqBody === 'function') ? options.parseReqBody(req) : options.parseReqBody;
// this guy should go elsewhere, down the chain
if (options.parseReqBody) {
if (parseReqBody) {
// We are parsing the body ourselves so we need to write the body content
// and then manually end the request.

Expand Down
1 change: 1 addition & 0 deletions lib/requestOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function createRequestOptions(req, res, options) {

function bodyContent(req, res, options) {
var parseReqBody = isUnset(options.parseReqBody) ? true : options.parseReqBody;
parseReqBody = (typeof parseReqBody === 'function') ? parseReqBody(req) : options.parseReqBody;

function maybeParseBody(req, limit) {
if (req.body) {
Expand Down