Skip to content

Commit 207b4aa

Browse files
authored
fix: unmarshal TimeSeriesPoint from an array (#25)
1 parent b097ed4 commit 207b4aa

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

packages/clients/src/scw/__tests__/custom-marshalling.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ describe('unmarshalScwFile', () => {
8282
describe('unmarshalTimeSeriesPoint', () => {
8383
it('returns the proper object', () => {
8484
expect(
85-
unmarshalTimeSeriesPoint({
86-
timestamp: '2019-08-08T15:00:00.000Z',
87-
value: 42,
88-
}),
85+
unmarshalTimeSeriesPoint(['2019-08-08T15:00:00.000Z', 42]),
8986
).toStrictEqual({
9087
timestamp: new Date('2019-08-08T15:00:00.000Z'),
9188
value: 42,
@@ -105,12 +102,7 @@ describe('unmarshalTimeSeries', () => {
105102
unmarshalTimeSeries({
106103
metadata: { mattress: 'cloud' },
107104
name: 'sleep',
108-
points: [
109-
{
110-
timestamp: '2019-08-08T15:00:00.000Z',
111-
value: 8,
112-
},
113-
],
105+
points: [['2019-08-08T15:00:00.000Z', 8]],
114106
}),
115107
).toStrictEqual({
116108
metadata: { mattress: 'cloud' },

packages/clients/src/scw/custom-marshalling.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import type {
1111
/** Unmarshals {@link Money} */
1212
export const unmarshalMoney = (data: unknown) => {
1313
if (!isJSONObject(data)) {
14-
throw new TypeError(`Expected input isn't a dictionary.`)
14+
throw new TypeError(
15+
`Unmarshalling the type 'Money' failed as data isn't a dictionary.`,
16+
)
1517
}
1618

1719
return {
@@ -28,7 +30,9 @@ export const unmarshalMoney = (data: unknown) => {
2830
*/
2931
export const unmarshalServiceInfo = (data: unknown) => {
3032
if (!isJSONObject(data)) {
31-
throw new TypeError(`Expected input isn't a dictionary.`)
33+
throw new TypeError(
34+
`Unmarshalling the type 'ServiceInfo' failed as data isn't a dictionary.`,
35+
)
3236
}
3337

3438
return {
@@ -46,7 +50,9 @@ export const unmarshalServiceInfo = (data: unknown) => {
4650
*/
4751
export const unmarshalScwFile = (data: unknown) => {
4852
if (!isJSONObject(data)) {
49-
throw new TypeError(`Expected input isn't a dictionary.`)
53+
throw new TypeError(
54+
`Unmarshalling the type 'ScwFile' failed as data isn't a dictionary.`,
55+
)
5056
}
5157

5258
return {
@@ -59,16 +65,22 @@ export const unmarshalScwFile = (data: unknown) => {
5965
/**
6066
* Unmarshals {@link TimeSeriesPoint}
6167
*
68+
* @remarks To optimize the size of this message,
69+
* the JSON is compressed in an array instead of a dictionary.
70+
* Example: `["2019-08-08T15:00:00Z", 0.2]`.
71+
*
6272
* @internal
6373
*/
6474
export const unmarshalTimeSeriesPoint = (data: unknown) => {
65-
if (!isJSONObject(data)) {
66-
throw new TypeError(`Expected input isn't a dictionary.`)
75+
if (!Array.isArray(data)) {
76+
throw new TypeError(
77+
`Unmarshalling the type 'TimeSeriesPoint' failed as data isn't an array.`,
78+
)
6779
}
6880

6981
return {
70-
timestamp: unmarshalDate(data.timestamp),
71-
value: data.value,
82+
timestamp: unmarshalDate(data[0]),
83+
value: data[1] as number,
7284
} as TimeSeriesPoint
7385
}
7486

@@ -79,7 +91,9 @@ export const unmarshalTimeSeriesPoint = (data: unknown) => {
7991
*/
8092
export const unmarshalTimeSeries = (data: unknown) => {
8193
if (!isJSONObject(data)) {
82-
throw new TypeError(`Expected input isn't a dictionary.`)
94+
throw new TypeError(
95+
`Unmarshalling the type 'TimeSeries' failed as data isn't a dictionary.`,
96+
)
8397
}
8498

8599
return {

0 commit comments

Comments
 (0)