Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

PDE-476: Handling omitEmptyParams #121

Merged
merged 2 commits into from
Oct 16, 2018

Conversation

BrunoBernardino
Copy link
Contributor

@BrunoBernardino BrunoBernardino commented Oct 15, 2018

Includes tests and has been tested in a CLI app in production with a modified core.

Requires a schema PR: zapier/zapier-platform-schema#57

Fixes PDE-476.

Includes tests and has been tested in a CLI app in production with a modified core.

Requires a schema PR.
Copy link
Member

@eliangcs eliangcs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests look very solid. Thanks for that! Only have a style suggestion (not a blocker). Feel free to merge on your call.

// 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 === true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be if (req.omitEmptyParams)?

Copy link
Contributor

@xavdid xavdid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Love the tests. Had some thoughts about url parsing but nothing showstopping.

One last thing- do we have tests that cover the splitter code? It seems like all of the tests (here and previously) call /get, but none with /get?a=1. I may be missing it, but I don’t think anyging uses & as the splitter.

@@ -2,13 +2,34 @@

const querystring = require('querystring');

// Mutates the object (it'll be deleted later anyway)
const removeEmptyParams = params =>
Copy link
Contributor

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

Copy link
Contributor Author

@BrunoBernardino BrunoBernardino Oct 16, 2018

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.

const stringifiedParams = querystring.stringify(req.params);

if (stringifiedParams) {
req.url += `${splitter}${stringifiedParams}`;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 splitter now by not adding ? (it was in the case the params existed but where then removed).

@BrunoBernardino BrunoBernardino merged commit d97c3f8 into master Oct 16, 2018
@BrunoBernardino BrunoBernardino deleted the feature/PDE-476-handle-omit-empty-params branch October 16, 2018 06:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants