Skip to content

Commit

Permalink
fix: use ISO format for Date serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Dec 12, 2024
1 parent bed6210 commit b7c1b4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
6 changes: 5 additions & 1 deletion packages/mailgun/src/models/v1/emails/request/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export const appendFilteredPropertiesToFormData = (
) => {
for (const [key, value] of Object.entries(obj)) {
if (key.startsWith(prefix) && value != null) {
formData.append(key, String(value));
if (value instanceof Date) {
formData.append(key, value.toISOString());
} else {
formData.append(key, String(value));
}
}
}
};
23 changes: 11 additions & 12 deletions packages/mailgun/tests/models/v1/emails/request/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ describe('appendFilteredPropertiesToFormData', () => {
const obj = {
'o:dkim': true,
'o:tracking': 'yes',
// 'v:first_name': 'John',
// 'v:last_name': 'Smith',
// 'v:my_message_id': '123',
// 'v:date1': '2024-06-06T13:42:42',
'v:date2': new Date('2024-06-06T13:42:42'),
'v:first_name': 'John',
'v:last_name': 'Smith',
'v:my_message_id': '123',
'v:date1': '2024-06-06T13:42:42Z',
'v:date2': new Date('2024-06-06T13:42:42Z'),
};

appendFilteredPropertiesToFormData(obj, 'v:', formData);

expect(appendSpy).toHaveBeenCalledTimes(1);
// expect(appendSpy).toHaveBeenCalledWith('v:first_name', 'John');
// expect(appendSpy).toHaveBeenCalledWith('v:last_name', 'Smith');
// expect(appendSpy).toHaveBeenCalledWith('v:my_message_id', '123');
// expect(appendSpy).toHaveBeenCalledWith('v:date1', '2024-06-06T13:42:42');
expect(appendSpy).toHaveBeenCalledWith('v:date2',
'Thu Jun 06 2024 13:42:42 GMT+0200 (Central European Summer Time)');
expect(appendSpy).toHaveBeenCalledTimes(5);
expect(appendSpy).toHaveBeenCalledWith('v:first_name', 'John');
expect(appendSpy).toHaveBeenCalledWith('v:last_name', 'Smith');
expect(appendSpy).toHaveBeenCalledWith('v:my_message_id', '123');
expect(appendSpy).toHaveBeenCalledWith('v:date1', '2024-06-06T13:42:42Z');
expect(appendSpy).toHaveBeenCalledWith('v:date2', '2024-06-06T13:42:42.000Z');
});

it('should not append properties with null or undefined values', () => {
Expand Down

0 comments on commit b7c1b4a

Please sign in to comment.