Skip to content

Commit

Permalink
Merge pull request #574 from stripe/ob-encode-dates
Browse files Browse the repository at this point in the history
Encode Dates as Unix timestamps
  • Loading branch information
ob-stripe authored Mar 11, 2019
2 parents f5a2f49 + d598ca1 commit 4238f12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 4238f12

Please sign in to comment.