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
Merged
Show file tree
Hide file tree
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
23 changes: 22 additions & 1 deletion src/http-middlewares/before/add-query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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}`;
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).

}
}
delete req.params;
return req;
Expand Down
99 changes: 99 additions & 0 deletions test/create-request-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,103 @@ describe('request client', () => {
response.status.should.eql(200);
});
});

it('should not omit empty params by default', () => {
const request = createAppRequestClient(input);
return request({
url: 'http://zapier-httpbin.herokuapp.com/get',
params: {
something: '',
cool: 'true'
}
}).then(responseBefore => {
const response = JSON.parse(JSON.stringify(responseBefore));

response.json.args.something.should.eql('');
response.json.args.cool.should.eql('true');
response.status.should.eql(200);

const body = JSON.parse(response.content);
body.url.should.eql(
'http://zapier-httpbin.herokuapp.com/get?something=&cool=true'
);
});
});

it('should not omit empty params when set as false', () => {
const request = createAppRequestClient(input);
return request({
url: 'http://zapier-httpbin.herokuapp.com/get',
params: {
something: '',
cool: 'true'
},
omitEmptyParams: false
}).then(responseBefore => {
const response = JSON.parse(JSON.stringify(responseBefore));

response.json.args.something.should.eql('');
response.json.args.cool.should.eql('true');
response.status.should.eql(200);

const body = JSON.parse(response.content);
body.url.should.eql(
'http://zapier-httpbin.herokuapp.com/get?something=&cool=true'
);
});
});

it('should omit empty params when set as true', () => {
const request = createAppRequestClient(input);
return request({
url: 'http://zapier-httpbin.herokuapp.com/get',
params: {
something: '',
cool: 'false',
foo: null,
bar: undefined,
zzz: '[]',
yyy: '{}',
qqq: ' '
},
omitEmptyParams: true
}).then(responseBefore => {
const response = JSON.parse(JSON.stringify(responseBefore));

should(response.json.args.something).eql(undefined);
should(response.json.args.foo).eql(undefined);
should(response.json.args.bar).eql(undefined);
response.json.args.cool.should.eql('false');
response.json.args.zzz.should.eql('[]');
response.json.args.yyy.should.eql('{}');
response.json.args.qqq.should.eql(' ');
response.status.should.eql(200);

const body = JSON.parse(response.content);
body.url.should.eql(
'http://zapier-httpbin.herokuapp.com/get?cool=false&zzz=[]&yyy={}&qqq= '
);
});
});

it('should not include ? if there are no params after cleaning', () => {
const request = createAppRequestClient(input);
return request({
url: 'http://zapier-httpbin.herokuapp.com/get',
params: {
something: '',
cool: ''
},
omitEmptyParams: true
}).then(responseBefore => {
const response = JSON.parse(JSON.stringify(responseBefore));

should(response.json.args.something).eql(undefined);
should(response.json.args.cool).eql(undefined);
response.status.should.eql(200);

const body = JSON.parse(response.content);
body.url.should.eql('http://zapier-httpbin.herokuapp.com/get');
});
});
});