Skip to content

Commit 4e1e04b

Browse files
authored
TatumUtils better error messages and types (#984)
1 parent 6790295 commit 4e1e04b

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [4.1.7] - 2023.10.15
2+
### Changed
3+
- `TatumUtils` chainId <-> `Network` mappings always return usable value or throw error for ease of use.
4+
15
## [4.1.6] - 2023.10.15
26
### Added
37
- `TatumUtils` added with access to chainId <-> `Network` mapping

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tatumio/tatum",
3-
"version": "4.1.6",
3+
"version": "4.1.7",
44
"description": "Tatum JS SDK",
55
"author": "Tatum",
66
"repository": "https://github.com/tatumio/tatum-js",

β€Žsrc/e2e/tatum.spec.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Network to chainId mapping check', () => {
5151
const chainId = TatumUtils.getChainId(network)
5252

5353
expect(chainId).toBeGreaterThan(0)
54-
expect(TatumUtils.getNetwork(chainId as number)).toBe(network)
54+
expect(TatumUtils.getNetwork(chainId)).toBe(network)
5555
})
5656
}
5757
})

β€Žsrc/util/tatum.utils.tsβ€Ž

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import { Network } from '../dto'
22
import { Constant } from './constant'
33

44
export const TatumUtils = {
5-
getChainId: (network: Network): number | undefined => {
5+
getChainId: (network: Network): number => {
66
if (network in Constant.NETWORK.ChainId) {
77
return Constant.NETWORK.ChainId[network as keyof typeof Constant.NETWORK.ChainId]
88
}
9-
return undefined
9+
throw new Error(`Tatum Network to ChainId for network ${network} does not exist`)
1010
},
11-
getNetwork: (chainId: number): keyof typeof Constant.NETWORK.ChainId | undefined => {
11+
getNetwork: (chainId: number): Network => {
1212
if (Object.keys(chainIdToNetworkCache).length === 0) {
1313
buildChainIdToNetworkCache()
1414
}
15-
return chainIdToNetworkCache[chainId] || undefined
15+
const network = chainIdToNetworkCache[chainId]
16+
if (!network) {
17+
throw new Error(`ChainId to Tatum Network for chainId ${chainId} does not exist`)
18+
}
19+
return network
1620
},
1721
}
1822

0 commit comments

Comments
Β (0)