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

Encode Dates as Unix timestamps #574

Merged
merged 1 commit into from
Mar 11, 2019
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
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var utils = module.exports = {
* (forming the conventional key 'parent[child]=value')
*/
stringifyRequestData: function(data) {
return qs.stringify(data)
return qs.stringify(data, {serializeDate: function (d) { return Math.floor(d.getTime() / 1000); }})
// Don't use strict form encoding by changing the square bracket control
// characters back to their literals. This is fine by the server, and
// makes these parameter strings easier to read.
Expand Down
14 changes: 14 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ describe('utils', function() {
})).to.equal('a=1&b=foo');
});

it('Handles Dates', function() {
expect(utils.stringifyRequestData({
date: new Date('2009-02-13T23:31:30Z'),
created: {
gte: new Date('2009-02-13T23:31:30Z'),
lt: new Date('2044-05-01T01:28:21Z'),
},
})).to.equal([
'date=1234567890',
'created[gte]=1234567890',
'created[lt]=2345678901'
].join('&'));
});

it('Handles deeply nested object', function() {
expect(utils.stringifyRequestData({
a: {
Expand Down