Skip to content

Commit

Permalink
feat(scw): add custom marshalers for Decimal (#1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Jul 11, 2024
1 parent dc00cb0 commit db39985
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/clients/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
23 changes: 23 additions & 0 deletions packages/clients/src/scw/custom-marshalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
TimeSeries,
TimeSeriesPoint,
} from './custom-types'
import { Decimal } from './custom-types'

/**
* Unmarshals {@link Money}
Expand Down Expand Up @@ -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}.
*
Expand Down Expand Up @@ -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()

0 comments on commit db39985

Please sign in to comment.