From e2d3d4fc93422ba3c05db1491c16744e2bc5d820 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Mon, 25 Jul 2022 10:54:04 +0200 Subject: [PATCH 1/2] fix: unmarshal TimeSeriesPoint from an array --- .../src/scw/__tests__/custom-marshalling.ts | 12 ++------ .../clients/src/scw/custom-marshalling.ts | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/clients/src/scw/__tests__/custom-marshalling.ts b/packages/clients/src/scw/__tests__/custom-marshalling.ts index 26ffb6c5c..aea067967 100644 --- a/packages/clients/src/scw/__tests__/custom-marshalling.ts +++ b/packages/clients/src/scw/__tests__/custom-marshalling.ts @@ -82,10 +82,7 @@ describe('unmarshalScwFile', () => { describe('unmarshalTimeSeriesPoint', () => { it('returns the proper object', () => { expect( - unmarshalTimeSeriesPoint({ - timestamp: '2019-08-08T15:00:00.000Z', - value: 42, - }), + unmarshalTimeSeriesPoint(['2019-08-08T15:00:00.000Z', 42]), ).toStrictEqual({ timestamp: new Date('2019-08-08T15:00:00.000Z'), value: 42, @@ -105,12 +102,7 @@ describe('unmarshalTimeSeries', () => { unmarshalTimeSeries({ metadata: { mattress: 'cloud' }, name: 'sleep', - points: [ - { - timestamp: '2019-08-08T15:00:00.000Z', - value: 8, - }, - ], + points: [['2019-08-08T15:00:00.000Z', 8]], }), ).toStrictEqual({ metadata: { mattress: 'cloud' }, diff --git a/packages/clients/src/scw/custom-marshalling.ts b/packages/clients/src/scw/custom-marshalling.ts index 6a1fcbb32..686d11cdc 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -11,7 +11,9 @@ import type { /** Unmarshals {@link Money} */ export const unmarshalMoney = (data: unknown) => { if (!isJSONObject(data)) { - throw new TypeError(`Expected input isn't a dictionary.`) + throw new TypeError( + `Unmarshalling the type 'Money' failed as data isn't a dictionary.`, + ) } return { @@ -28,7 +30,9 @@ export const unmarshalMoney = (data: unknown) => { */ export const unmarshalServiceInfo = (data: unknown) => { if (!isJSONObject(data)) { - throw new TypeError(`Expected input isn't a dictionary.`) + throw new TypeError( + `Unmarshalling the type 'ServiceInfo' failed as data isn't a dictionary.`, + ) } return { @@ -46,7 +50,9 @@ export const unmarshalServiceInfo = (data: unknown) => { */ export const unmarshalScwFile = (data: unknown) => { if (!isJSONObject(data)) { - throw new TypeError(`Expected input isn't a dictionary.`) + throw new TypeError( + `Unmarshalling the type 'ScwFile' failed as data isn't a dictionary.`, + ) } return { @@ -59,16 +65,22 @@ export const unmarshalScwFile = (data: unknown) => { /** * Unmarshals {@link TimeSeriesPoint} * + * @remarks To optimize the size of this message, + * the JSON is compressed in an array instead of a dictionary. + * Example: `["2019-08-08T15:00:00Z", 0.2]`. + * * @internal */ export const unmarshalTimeSeriesPoint = (data: unknown) => { - if (!isJSONObject(data)) { - throw new TypeError(`Expected input isn't a dictionary.`) + if (!Array.isArray(data)) { + throw new TypeError( + `Unmarshalling the type 'TimeSeriesPoint' failed as data isn't an array.`, + ) } return { - timestamp: unmarshalDate(data.timestamp), - value: data.value, + timestamp: unmarshalDate(data[0]), + value: data[1] as number, } as TimeSeriesPoint } @@ -79,7 +91,9 @@ export const unmarshalTimeSeriesPoint = (data: unknown) => { */ export const unmarshalTimeSeries = (data: unknown) => { if (!isJSONObject(data)) { - throw new TypeError(`Expected input isn't a dictionary.`) + throw new TypeError( + `Unmarshalling the type 'ServeTimeSeriesrOption' failed as data isn't a dictionary.`, + ) } return { From aa19ea90722e0a714502d7da3c87fc82afa895d2 Mon Sep 17 00:00:00 2001 From: Vincent Germain Date: Mon, 25 Jul 2022 11:22:21 +0200 Subject: [PATCH 2/2] chore: typo --- packages/clients/src/scw/custom-marshalling.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/clients/src/scw/custom-marshalling.ts b/packages/clients/src/scw/custom-marshalling.ts index 686d11cdc..841e01d71 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -92,7 +92,7 @@ export const unmarshalTimeSeriesPoint = (data: unknown) => { export const unmarshalTimeSeries = (data: unknown) => { if (!isJSONObject(data)) { throw new TypeError( - `Unmarshalling the type 'ServeTimeSeriesrOption' failed as data isn't a dictionary.`, + `Unmarshalling the type 'TimeSeries' failed as data isn't a dictionary.`, ) }