Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions packages/clients/src/scw/__tests__/custom-marshalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' },
Expand Down
30 changes: 22 additions & 8 deletions packages/clients/src/scw/custom-marshalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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 'TimeSeries' failed as data isn't a dictionary.`,
)
}

return {
Expand Down