From bd194f5cc2ad224ac14cfc90a586dd0289e4335f Mon Sep 17 00:00:00 2001 From: Jules Casteran Date: Thu, 11 Jul 2024 14:14:45 +0200 Subject: [PATCH 1/2] feat(scw): add custom marshalers for Decimal --- .../clients/src/scw/custom-marshalling.ts | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/clients/src/scw/custom-marshalling.ts b/packages/clients/src/scw/custom-marshalling.ts index 364047268..2c229da0a 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -5,7 +5,9 @@ import type { ScwFile, ServiceInfo, TimeSeries, - TimeSeriesPoint, + TimeSeriesPoint} from './custom-types'; +import { + Decimal } from './custom-types' /** @@ -107,6 +109,21 @@ export const unmarshalTimeSeries = (data: unknown) => { } as TimeSeries } +/** + * Unmarshals {@link Decimal} + * + * @internal + */ +export const unmarshalDecimal = (data: unknown) => { + if (!(typeof data === 'string')) { + throw new TypeError( + `Unmarshalling the type 'Decimal' failed as data isn't a string.`, + ) + } + + return new Decimal(data) +} + /** * Marshals {@link ScwFile}. * @@ -153,3 +170,10 @@ export const marshalTimeSeries = ( name: obj.name, points: obj.points.map(elt => marshalTimeSeriesPoint(elt)), }) + +/** + * Marshals {@link Decimal} + * + * @internal + */ +export const marshalDecimal = (obj: Decimal): string => obj.toString() From 575104f80ff6f817c1d4cc34d9c2c90f518bbecb Mon Sep 17 00:00:00 2001 From: Jules Casteran Date: Thu, 11 Jul 2024 14:19:43 +0200 Subject: [PATCH 2/2] correct imports --- packages/clients/src/bridge.ts | 3 +++ packages/clients/src/scw/custom-marshalling.ts | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/clients/src/bridge.ts b/packages/clients/src/bridge.ts index 692155d26..2f48c0919 100644 --- a/packages/clients/src/bridge.ts +++ b/packages/clients/src/bridge.ts @@ -10,15 +10,18 @@ export type { ScwFile, TimeSeries, } from './scw/custom-types' +export { Decimal } from './scw/custom-types' export { marshalScwFile, marshalMoney, marshalTimeSeries, + marshalDecimal, unmarshalMoney, unmarshalScwFile, unmarshalServiceInfo, unmarshalTimeSeries, unmarshalTimeSeriesPoint, + unmarshalDecimal, } from './scw/custom-marshalling' export { enrichForPagination } from './scw/fetch/resource-paginator' export type { Region, Zone } from './scw/locality' diff --git a/packages/clients/src/scw/custom-marshalling.ts b/packages/clients/src/scw/custom-marshalling.ts index 2c229da0a..28f0dc64c 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -5,10 +5,9 @@ import type { ScwFile, ServiceInfo, TimeSeries, - TimeSeriesPoint} from './custom-types'; -import { - Decimal + TimeSeriesPoint, } from './custom-types' +import { Decimal } from './custom-types' /** * Unmarshals {@link Money}