Skip to content

Commit 92e4e8d

Browse files
authored
Convert Date to ISO string in functions (#4887)
1 parent 55ecffe commit 92e4e8d

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

.changeset/brown-snails-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/functions': patch
3+
---
4+
5+
Fix functions to convert Date objects to an ISO string instead of an empty object.

packages-exp/functions-exp/src/serializer.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ describe('Serializer', () => {
8282
expect(decode('hello')).to.equal('hello');
8383
});
8484

85+
it('encodes date to ISO string', () => {
86+
expect(encode(new Date(1620666095891))).to.equal(
87+
'2021-05-10T17:01:35.891Z'
88+
);
89+
});
90+
91+
it('decodes date string without modifying it', () => {
92+
expect(decode('2021-05-10T17:01:35.891Z')).to.equal(
93+
'2021-05-10T17:01:35.891Z'
94+
);
95+
});
96+
8597
// TODO(klimt): Make this test more interesting once we have a complex type
8698
// that can be created in JavaScript.
8799
it('encodes array', () => {
@@ -111,12 +123,14 @@ describe('Serializer', () => {
111123
encode({
112124
foo: 1,
113125
bar: 'hello',
114-
baz: [1, 2, 3]
126+
baz: [1, 2, 3],
127+
date: new Date(1620666095891)
115128
})
116129
).to.deep.equal({
117130
foo: 1,
118131
bar: 'hello',
119-
baz: [1, 2, 3]
132+
baz: [1, 2, 3],
133+
date: '2021-05-10T17:01:35.891Z'
120134
});
121135
});
122136

@@ -132,12 +146,14 @@ describe('Serializer', () => {
132146
value: '1099511627776',
133147
'@type': 'type.googleapis.com/google.protobuf.Int64Value'
134148
}
135-
]
149+
],
150+
date: '2021-05-10T17:01:35.891Z'
136151
})
137152
).to.deep.equal({
138153
foo: 1,
139154
bar: 'hello',
140-
baz: [1, 2, 1099511627776]
155+
baz: [1, 2, 1099511627776],
156+
date: '2021-05-10T17:01:35.891Z'
141157
});
142158
});
143159

packages-exp/functions-exp/src/serializer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export function encode(data: unknown): unknown {
5656
if (Object.prototype.toString.call(data) === '[object String]') {
5757
return data;
5858
}
59+
if (data instanceof Date) {
60+
return data.toISOString();
61+
}
5962
if (Array.isArray(data)) {
6063
return data.map(x => encode(x));
6164
}

packages/functions/src/serializer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export class Serializer {
5454
if (Object.prototype.toString.call(data) === '[object String]') {
5555
return data;
5656
}
57+
if (data instanceof Date) {
58+
return data.toISOString();
59+
}
5760
if (Array.isArray(data)) {
5861
return data.map(x => this.encode(x));
5962
}

packages/functions/test/serializer.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ describe('Serializer', () => {
8888
expect(serializer.decode('hello')).to.equal('hello');
8989
});
9090

91+
it('encodes date to ISO string', () => {
92+
expect(serializer.encode(new Date(1620666095891))).to.equal(
93+
'2021-05-10T17:01:35.891Z'
94+
);
95+
});
96+
97+
it('decodes date string without modifying it', () => {
98+
expect(serializer.decode('2021-05-10T17:01:35.891Z')).to.equal(
99+
'2021-05-10T17:01:35.891Z'
100+
);
101+
});
102+
91103
// TODO(klimt): Make this test more interesting once we have a complex type
92104
// that can be created in JavaScript.
93105
it('encodes array', () => {
@@ -117,12 +129,14 @@ describe('Serializer', () => {
117129
serializer.encode({
118130
foo: 1,
119131
bar: 'hello',
120-
baz: [1, 2, 3]
132+
baz: [1, 2, 3],
133+
date: new Date(1620666095891)
121134
})
122135
).to.deep.equal({
123136
foo: 1,
124137
bar: 'hello',
125-
baz: [1, 2, 3]
138+
baz: [1, 2, 3],
139+
date: '2021-05-10T17:01:35.891Z'
126140
});
127141
});
128142

@@ -138,12 +152,14 @@ describe('Serializer', () => {
138152
value: '1099511627776',
139153
'@type': 'type.googleapis.com/google.protobuf.Int64Value'
140154
}
141-
]
155+
],
156+
date: '2021-05-10T17:01:35.891Z'
142157
})
143158
).to.deep.equal({
144159
foo: 1,
145160
bar: 'hello',
146-
baz: [1, 2, 1099511627776]
161+
baz: [1, 2, 1099511627776],
162+
date: '2021-05-10T17:01:35.891Z'
147163
});
148164
});
149165

0 commit comments

Comments
 (0)