|
1 | 1 | import * as formats from "./coins.js";
|
2 | 2 | import { coinTypeMap } from "./consts/coinTypeMap.js";
|
3 | 3 | import type {
|
| 4 | + Coin, |
4 | 5 | CoinName,
|
5 | 6 | CoinType,
|
6 | 7 | CoinTypeInvertedReference,
|
| 8 | + DecoderFunction, |
| 9 | + EncoderFunction, |
7 | 10 | Formats,
|
8 | 11 | } from "./types.js";
|
9 | 12 |
|
10 |
| -export const getCoderByCoinName = <TCoinName extends CoinName>( |
| 13 | +export type { |
| 14 | + Coin, |
| 15 | + CoinName, |
| 16 | + CoinType, |
| 17 | + CoinTypeInvertedReference, |
| 18 | + DecoderFunction, |
| 19 | + EncoderFunction, |
| 20 | + Formats, |
| 21 | +}; |
| 22 | + |
| 23 | +export const getCoderByCoinName = < |
| 24 | + TCoinName extends CoinName | string = string |
| 25 | +>( |
11 | 26 | name: TCoinName
|
12 |
| -): Formats[TCoinName] => { |
13 |
| - const format = formats[name]; |
| 27 | +): TCoinName extends CoinName ? Formats[TCoinName] : Coin => { |
| 28 | + const format = formats[name as keyof typeof formats]; |
14 | 29 | if (!format) {
|
15 | 30 | throw new Error(`Unsupported coin: ${name}`);
|
16 | 31 | }
|
17 |
| - return format; |
| 32 | + return format as TCoinName extends CoinName ? Formats[TCoinName] : Coin; |
18 | 33 | };
|
19 | 34 |
|
20 |
| -export const getCoderByCoinType = <TCoinType extends CoinType>( |
| 35 | +export const getCoderByCoinType = < |
| 36 | + TCoinType extends CoinType | number = number |
| 37 | +>( |
21 | 38 | coinType: TCoinType
|
22 |
| -): CoinTypeInvertedReference[TCoinType] => { |
| 39 | +): TCoinType extends CoinType ? CoinTypeInvertedReference[TCoinType] : Coin => { |
23 | 40 | const name = coinTypeMap[String(coinType) as keyof typeof coinTypeMap];
|
24 | 41 | if (!name) {
|
25 | 42 | throw new Error(`Unsupported coin type: ${coinType}`);
|
26 | 43 | }
|
27 |
| - const format = formats[name] as CoinTypeInvertedReference[TCoinType]; |
| 44 | + const format = formats[name]; |
28 | 45 | if (!format) {
|
29 | 46 | throw new Error(`Unsupported coin type: ${coinType}`);
|
30 | 47 | }
|
31 |
| - return format; |
| 48 | + return format as TCoinType extends CoinType |
| 49 | + ? CoinTypeInvertedReference[TCoinType] |
| 50 | + : Coin; |
32 | 51 | };
|
0 commit comments