Skip to content

Commit 856b408

Browse files
authored
feat(account-api): add typing utility + better typing for Account{Wallet,Group}Id (#331)
More utility type which allows for improved typing when using those functions. This can help to not "generalize" the computed ID to be narrowed down to `Account{Wallet,Group}Id` only, using those new generics allows to keep the `AccountWalletType` within the computed typed.
1 parent 8c95e17 commit 856b408

3 files changed

Lines changed: 44 additions & 20 deletions

File tree

packages/account-api/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `Account{Wallet,Group}IdOf` type utility ([#331](https://github.com/MetaMask/accounts/pull/331))
13+
14+
### Changed
15+
16+
- Use generic type for `toAccount{Wallet,Group}Id` ([#331](https://github.com/MetaMask/accounts/pull/331))
17+
1018
## [0.4.0]
1119

1220
### Added

packages/account-api/src/api/group.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { KeyringAccount } from '@metamask/keyring-api';
22

33
// Circular import are allowed when using `import type`.
4-
import type { AccountWallet, AccountWalletId } from './wallet';
4+
import type {
5+
AccountWallet,
6+
AccountWalletId,
7+
AccountWalletIdOf,
8+
AccountWalletType,
9+
} from './wallet';
510

611
/**
712
* Default account group unique ID.
@@ -64,17 +69,24 @@ export type AccountGroup<Account extends KeyringAccount> = {
6469
getAccount(id: Account['id']): Account | undefined;
6570
};
6671

72+
/**
73+
* Type utility to compute a constrained {@link AccountGroupId} type given a
74+
* specifc {@link AccountWalletType}.
75+
*/
76+
export type AccountGroupIdOf<WalletType extends AccountWalletType> =
77+
`${AccountWalletIdOf<WalletType>}/${string}`;
78+
6779
/**
6880
* Convert a wallet ID and a unique ID, to a group ID.
6981
*
7082
* @param walletId - A wallet ID.
7183
* @param id - A unique ID.
7284
* @returns A group ID.
7385
*/
74-
export function toAccountGroupId(
75-
walletId: AccountWalletId,
86+
export function toAccountGroupId<WalletType extends AccountWalletType>(
87+
walletId: AccountWalletIdOf<WalletType>,
7688
id: string,
77-
): AccountGroupId {
89+
): AccountGroupIdOf<WalletType> {
7890
return `${walletId}/${id}`;
7991
}
8092

@@ -84,8 +96,11 @@ export function toAccountGroupId(
8496
* @param walletId - A wallet ID.
8597
* @returns The default group ID.
8698
*/
87-
export function toDefaultAccountGroupId(
88-
walletId: AccountWalletId,
89-
): AccountGroupId {
90-
return toAccountGroupId(walletId, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID);
99+
export function toDefaultAccountGroupId<WalletType extends AccountWalletType>(
100+
walletId: AccountWalletIdOf<WalletType>,
101+
): AccountGroupIdOf<WalletType> {
102+
return toAccountGroupId<WalletType>(
103+
walletId,
104+
DEFAULT_ACCOUNT_GROUP_UNIQUE_ID,
105+
);
91106
}

packages/account-api/src/api/wallet.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@ import type { AccountGroup, AccountGroupId } from './group';
99
* Each wallet types groups accounts using different criterias.
1010
*/
1111
export enum AccountWalletType {
12-
/**
13-
* Wallet grouping accounts based on their entropy source.
14-
*/
12+
/** Wallet grouping accounts based on their entropy source. */
1513
Entropy = 'entropy',
1614

17-
/**
18-
* Wallet grouping accounts based on their keyring's type.
19-
*/
15+
/** Wallet grouping accounts based on their keyring's type. */
2016
Keyring = 'keyring',
2117

22-
/**
23-
* Wallet grouping accounts associated with an account management Snap.
24-
*/
18+
/** Wallet grouping accounts associated with an account management Snap. */
2519
Snap = 'snap',
2620
}
2721

@@ -60,16 +54,23 @@ export type AccountWallet<Account extends KeyringAccount> = {
6054
getAccountGroups(): AccountGroup<Account>[];
6155
};
6256

57+
/**
58+
* Type utility to compute a constrained {@link AccountWalletId} type given a
59+
* specifc {@link AccountWalletType}.
60+
*/
61+
export type AccountWalletIdOf<WalletType extends AccountWalletType> =
62+
`${WalletType}:${string}`;
63+
6364
/**
6465
* Convert a unique ID to a wallet ID for a given type.
6566
*
6667
* @param type - A wallet type.
6768
* @param id - A unique ID.
6869
* @returns A wallet ID.
6970
*/
70-
export function toAccountWalletId(
71-
type: AccountWalletType,
71+
export function toAccountWalletId<WalletType extends AccountWalletType>(
72+
type: WalletType,
7273
id: string,
73-
): AccountWalletId {
74+
): AccountWalletIdOf<WalletType> {
7475
return `${type}:${id}`;
7576
}

0 commit comments

Comments
 (0)