Skip to content

Commit db39985

Browse files
authored
feat(scw): add custom marshalers for Decimal (#1362)
1 parent dc00cb0 commit db39985

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/clients/src/bridge.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ export type {
1010
ScwFile,
1111
TimeSeries,
1212
} from './scw/custom-types'
13+
export { Decimal } from './scw/custom-types'
1314
export {
1415
marshalScwFile,
1516
marshalMoney,
1617
marshalTimeSeries,
18+
marshalDecimal,
1719
unmarshalMoney,
1820
unmarshalScwFile,
1921
unmarshalServiceInfo,
2022
unmarshalTimeSeries,
2123
unmarshalTimeSeriesPoint,
24+
unmarshalDecimal,
2225
} from './scw/custom-marshalling'
2326
export { enrichForPagination } from './scw/fetch/resource-paginator'
2427
export type { Region, Zone } from './scw/locality'

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
TimeSeries,
88
TimeSeriesPoint,
99
} from './custom-types'
10+
import { Decimal } from './custom-types'
1011

1112
/**
1213
* Unmarshals {@link Money}
@@ -107,6 +108,21 @@ export const unmarshalTimeSeries = (data: unknown) => {
107108
} as TimeSeries
108109
}
109110

111+
/**
112+
* Unmarshals {@link Decimal}
113+
*
114+
* @internal
115+
*/
116+
export const unmarshalDecimal = (data: unknown) => {
117+
if (!(typeof data === 'string')) {
118+
throw new TypeError(
119+
`Unmarshalling the type 'Decimal' failed as data isn't a string.`,
120+
)
121+
}
122+
123+
return new Decimal(data)
124+
}
125+
110126
/**
111127
* Marshals {@link ScwFile}.
112128
*
@@ -153,3 +169,10 @@ export const marshalTimeSeries = (
153169
name: obj.name,
154170
points: obj.points.map(elt => marshalTimeSeriesPoint(elt)),
155171
})
172+
173+
/**
174+
* Marshals {@link Decimal}
175+
*
176+
* @internal
177+
*/
178+
export const marshalDecimal = (obj: Decimal): string => obj.toString()

0 commit comments

Comments
 (0)