This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
PDE-476: Handling omitEmptyParams #121
Merged
BrunoBernardino
merged 2 commits into
master
from
feature/PDE-476-handle-omit-empty-params
Oct 16, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,34 @@ | |
|
||
const querystring = require('querystring'); | ||
|
||
// Mutates the object (it'll be deleted later anyway) | ||
const removeEmptyParams = params => | ||
Object.keys(params).map(param => { | ||
if ( | ||
params[param] === '' || | ||
params[param] === null || | ||
typeof params[param] === 'undefined' | ||
) { | ||
delete params[param]; | ||
} | ||
}); | ||
|
||
// Take params off of req.params and append to url - "?a=1&b=2"". | ||
// This middleware should run *after* custom middlewares, because | ||
// custom middlewares might add params. | ||
const addQueryParams = req => { | ||
if (Object.keys(req.params || {}).length) { | ||
const splitter = req.url.indexOf('?') === -1 ? '?' : '&'; | ||
req.url += splitter + querystring.stringify(req.params); | ||
|
||
if (req.omitEmptyParams) { | ||
removeEmptyParams(req.params); | ||
} | ||
|
||
const stringifiedParams = querystring.stringify(req.params); | ||
|
||
if (stringifiedParams) { | ||
req.url += `${splitter}${stringifiedParams}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine as is, but I think we’d be better off parsing the url with the stdlib, pulling out the query, merging with new params, and adding the whole thing rather than guessing the splitter and adding it that way. Again, it’s fine, but using established tools feels a little more above the board. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I just didn't want to change too much here, honestly. We only test the |
||
} | ||
} | ||
delete req.params; | ||
return req; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t love that this mutates, but given that it’s local and commented, it’s probably fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, but since the variable was being mutated/removed before, I went with the less code option here.