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

Commit

Permalink
Merge pull request #121 from zapier/feature/PDE-476-handle-omit-empty…
Browse files Browse the repository at this point in the history
…-params

PDE-476: Handling omitEmptyParams
  • Loading branch information
Bruno Bernardino authored Oct 16, 2018
2 parents b410301 + 0277e76 commit d97c3f8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
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 =>
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}`;
}
}
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');
});
});
});

0 comments on commit d97c3f8

Please sign in to comment.