From db3998570b33dcfdd457f594badb70ffcbb2b882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Cast=C3=A9ran?= Date: Thu, 11 Jul 2024 14:41:26 +0200 Subject: [PATCH] feat(scw): add custom marshalers for Decimal (#1362) --- packages/clients/src/bridge.ts | 3 +++ .../clients/src/scw/custom-marshalling.ts | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) 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 364047268..28f0dc64c 100644 --- a/packages/clients/src/scw/custom-marshalling.ts +++ b/packages/clients/src/scw/custom-marshalling.ts @@ -7,6 +7,7 @@ import type { TimeSeries, TimeSeriesPoint, } from './custom-types' +import { Decimal } from './custom-types' /** * Unmarshals {@link Money} @@ -107,6 +108,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 +169,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()