@hub3js/core 0.58.2-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/CHANGELOG.md +375 -0
  2. package/dist/hub/store/mod.js +2 -0
  3. package/dist/hub/store/mod.js.map +7 -0
  4. package/dist/legacy/mod.js +2 -0
  5. package/dist/legacy/mod.js.map +7 -0
  6. package/dist/mod.js +2 -0
  7. package/dist/mod.js.map +7 -0
  8. package/dist/namespaces/common/mod.js +2 -0
  9. package/dist/namespaces/common/mod.js.map +7 -0
  10. package/dist/namespaces/cosmos/mod.js +2 -0
  11. package/dist/namespaces/cosmos/mod.js.map +7 -0
  12. package/dist/namespaces/evm/mod.js +2 -0
  13. package/dist/namespaces/evm/mod.js.map +7 -0
  14. package/dist/namespaces/solana/mod.js +2 -0
  15. package/dist/namespaces/solana/mod.js.map +7 -0
  16. package/dist/namespaces/starknet/mod.js +2 -0
  17. package/dist/namespaces/starknet/mod.js.map +7 -0
  18. package/dist/namespaces/sui/mod.js +2 -0
  19. package/dist/namespaces/sui/mod.js.map +7 -0
  20. package/dist/namespaces/tron/mod.js +2 -0
  21. package/dist/namespaces/tron/mod.js.map +7 -0
  22. package/dist/namespaces/utxo/mod.js +2 -0
  23. package/dist/namespaces/utxo/mod.js.map +7 -0
  24. package/dist/namespaces/xrpl/mod.js +2 -0
  25. package/dist/namespaces/xrpl/mod.js.map +7 -0
  26. package/dist/utils/mod.js +2 -0
  27. package/dist/utils/mod.js.map +7 -0
  28. package/legacy/package.json +8 -0
  29. package/namespaces/common/package.json +8 -0
  30. package/namespaces/cosmos/package.json +8 -0
  31. package/namespaces/evm/package.json +8 -0
  32. package/namespaces/solana/package.json +8 -0
  33. package/namespaces/starknet/package.json +8 -0
  34. package/namespaces/sui/package.json +8 -0
  35. package/namespaces/tron/package.json +8 -0
  36. package/namespaces/utxo/package.json +8 -0
  37. package/namespaces/xrpl/package.json +8 -0
  38. package/package.json +100 -0
  39. package/readme.md +4 -0
  40. package/src/builders/action.ts +86 -0
  41. package/src/builders/mod.ts +5 -0
  42. package/src/builders/namespace.ts +258 -0
  43. package/src/builders/provider.ts +61 -0
  44. package/src/builders/types.ts +26 -0
  45. package/src/hub/helpers.ts +11 -0
  46. package/src/hub/hub.ts +128 -0
  47. package/src/hub/mod.ts +18 -0
  48. package/src/hub/namespaces/errors.ts +11 -0
  49. package/src/hub/namespaces/mod.ts +10 -0
  50. package/src/hub/namespaces/namespace.test.ts +443 -0
  51. package/src/hub/namespaces/namespace.ts +470 -0
  52. package/src/hub/namespaces/types.ts +58 -0
  53. package/src/hub/provider/mod.ts +10 -0
  54. package/src/hub/provider/provider.test.ts +232 -0
  55. package/src/hub/provider/provider.ts +335 -0
  56. package/src/hub/provider/types.ts +54 -0
  57. package/src/hub/store/events.ts +89 -0
  58. package/src/hub/store/extend.ts +125 -0
  59. package/src/hub/store/hub.ts +18 -0
  60. package/src/hub/store/mod.ts +25 -0
  61. package/src/hub/store/namespaces.ts +177 -0
  62. package/src/hub/store/providers.ts +169 -0
  63. package/src/hub/store/selectors.ts +59 -0
  64. package/src/hub/store/store.test.ts +50 -0
  65. package/src/hub/store/store.ts +30 -0
  66. package/src/legacy/helpers.ts +75 -0
  67. package/src/legacy/mod.ts +44 -0
  68. package/src/legacy/persistor.ts +19 -0
  69. package/src/legacy/types.ts +303 -0
  70. package/src/legacy/utils.ts +20 -0
  71. package/src/legacy/wallet.ts +552 -0
  72. package/src/mod.ts +44 -0
  73. package/src/namespaces/common/actions.ts +12 -0
  74. package/src/namespaces/common/after.ts +8 -0
  75. package/src/namespaces/common/and.ts +42 -0
  76. package/src/namespaces/common/before.ts +9 -0
  77. package/src/namespaces/common/builders.ts +14 -0
  78. package/src/namespaces/common/helpers.ts +10 -0
  79. package/src/namespaces/common/hooks/changeAccountSubscriber.test.ts +173 -0
  80. package/src/namespaces/common/hooks/changeAccountSubscriber.ts +236 -0
  81. package/src/namespaces/common/mod.ts +23 -0
  82. package/src/namespaces/common/or.ts +17 -0
  83. package/src/namespaces/common/types.ts +27 -0
  84. package/src/namespaces/common/utils.ts +43 -0
  85. package/src/namespaces/cosmos/actions.ts +3 -0
  86. package/src/namespaces/cosmos/after.ts +3 -0
  87. package/src/namespaces/cosmos/and.ts +5 -0
  88. package/src/namespaces/cosmos/before.ts +3 -0
  89. package/src/namespaces/cosmos/builders.ts +15 -0
  90. package/src/namespaces/cosmos/constants.ts +1 -0
  91. package/src/namespaces/cosmos/mod.ts +15 -0
  92. package/src/namespaces/cosmos/types.ts +31 -0
  93. package/src/namespaces/cosmos/utils.ts +21 -0
  94. package/src/namespaces/evm/actions.ts +134 -0
  95. package/src/namespaces/evm/after.ts +3 -0
  96. package/src/namespaces/evm/and.ts +5 -0
  97. package/src/namespaces/evm/before.ts +3 -0
  98. package/src/namespaces/evm/builders.ts +52 -0
  99. package/src/namespaces/evm/constants.ts +2 -0
  100. package/src/namespaces/evm/eip1193.ts +1415 -0
  101. package/src/namespaces/evm/hooks.ts +43 -0
  102. package/src/namespaces/evm/mod.ts +10 -0
  103. package/src/namespaces/evm/types.ts +36 -0
  104. package/src/namespaces/evm/utils.ts +106 -0
  105. package/src/namespaces/solana/actions.ts +44 -0
  106. package/src/namespaces/solana/after.ts +3 -0
  107. package/src/namespaces/solana/and.ts +5 -0
  108. package/src/namespaces/solana/before.ts +3 -0
  109. package/src/namespaces/solana/builders.ts +40 -0
  110. package/src/namespaces/solana/constants.ts +2 -0
  111. package/src/namespaces/solana/hooks.ts +10 -0
  112. package/src/namespaces/solana/mod.ts +10 -0
  113. package/src/namespaces/solana/types.ts +25 -0
  114. package/src/namespaces/solana/utils.ts +36 -0
  115. package/src/namespaces/starknet/actions.ts +19 -0
  116. package/src/namespaces/starknet/after.ts +3 -0
  117. package/src/namespaces/starknet/and.ts +5 -0
  118. package/src/namespaces/starknet/before.ts +3 -0
  119. package/src/namespaces/starknet/builders.ts +18 -0
  120. package/src/namespaces/starknet/constants.ts +8 -0
  121. package/src/namespaces/starknet/mod.ts +9 -0
  122. package/src/namespaces/starknet/types.ts +18 -0
  123. package/src/namespaces/starknet/utils.ts +35 -0
  124. package/src/namespaces/sui/actions.ts +27 -0
  125. package/src/namespaces/sui/builders.ts +77 -0
  126. package/src/namespaces/sui/constants.ts +8 -0
  127. package/src/namespaces/sui/hooks.ts +10 -0
  128. package/src/namespaces/sui/mod.ts +8 -0
  129. package/src/namespaces/sui/types.ts +25 -0
  130. package/src/namespaces/sui/utils.ts +56 -0
  131. package/src/namespaces/tron/actions.ts +3 -0
  132. package/src/namespaces/tron/after.ts +3 -0
  133. package/src/namespaces/tron/and.ts +5 -0
  134. package/src/namespaces/tron/before.ts +3 -0
  135. package/src/namespaces/tron/builders.ts +15 -0
  136. package/src/namespaces/tron/constants.ts +8 -0
  137. package/src/namespaces/tron/mod.ts +8 -0
  138. package/src/namespaces/tron/types.ts +14 -0
  139. package/src/namespaces/tron/utils.ts +18 -0
  140. package/src/namespaces/utxo/actions.ts +3 -0
  141. package/src/namespaces/utxo/after.ts +3 -0
  142. package/src/namespaces/utxo/and.ts +5 -0
  143. package/src/namespaces/utxo/before.ts +3 -0
  144. package/src/namespaces/utxo/builders.ts +12 -0
  145. package/src/namespaces/utxo/constants.ts +2 -0
  146. package/src/namespaces/utxo/mod.ts +8 -0
  147. package/src/namespaces/utxo/types.ts +14 -0
  148. package/src/namespaces/utxo/utils.ts +18 -0
  149. package/src/namespaces/xrpl/builders.ts +14 -0
  150. package/src/namespaces/xrpl/constants.ts +7 -0
  151. package/src/namespaces/xrpl/mod.ts +5 -0
  152. package/src/namespaces/xrpl/types.ts +16 -0
  153. package/src/namespaces/xrpl/utils.ts +15 -0
  154. package/src/test-utils/fixtures.ts +13 -0
  155. package/src/types/accounts.ts +12 -0
  156. package/src/types/actions.ts +13 -0
  157. package/src/types/mod.ts +1 -0
  158. package/src/types/utils.ts +7 -0
  159. package/src/utils/mod.ts +8 -0
  160. package/src/utils/versions.test.ts +24 -0
  161. package/src/utils/versions.ts +64 -0
  162. package/store/package.json +8 -0
@@ -0,0 +1,25 @@
1
+ import type { Accounts } from '../../types/accounts.js';
2
+ import type {
3
+ AutoImplementedActionsByRecommended,
4
+ CommonActions,
5
+ } from '../common/types.js';
6
+ import type {
7
+ StandardConnectFeature,
8
+ StandardEventsFeature,
9
+ SuiFeatures,
10
+ WalletWithFeatures,
11
+ } from '@mysten/wallet-standard';
12
+
13
+ export interface SuiActions
14
+ extends AutoImplementedActionsByRecommended, CommonActions {
15
+ connect: () => Promise<Accounts>;
16
+ canEagerConnect: () => Promise<boolean>;
17
+ }
18
+
19
+ export type ProviderAPI = WalletWithFeatures<
20
+ StandardConnectFeature & StandardEventsFeature & SuiFeatures
21
+ >;
22
+
23
+ export interface ChangeAccountSubscriberParams {
24
+ name: string;
25
+ }
@@ -0,0 +1,56 @@
1
+ import type {
2
+ StandardConnectFeature,
3
+ StandardEventsFeature,
4
+ SuiFeatures,
5
+ WalletWithFeatures,
6
+ } from '@mysten/wallet-standard';
7
+
8
+ import { getWallets, SUI_MAINNET_CHAIN } from '@mysten/wallet-standard';
9
+ import { AccountId } from 'caip';
10
+
11
+ import { CAIP_NAMESPACE, CAIP_SUI_CHAIN_ID } from './constants.js';
12
+
13
+ // TODO: StandardFetures doesn't work, so we should add each feature separately
14
+ type SuiWalletStandard = WalletWithFeatures<
15
+ StandardConnectFeature & StandardEventsFeature & SuiFeatures
16
+ >;
17
+
18
+ /**
19
+ * @param name each wallet has a name in WalletStandard. you should pass that value
20
+ */
21
+ export function getInstance(name: string): SuiWalletStandard | undefined {
22
+ const wallet = getWallets()
23
+ .get()
24
+ .find(
25
+ (wallet) =>
26
+ wallet.name === name && wallet.chains.includes(SUI_MAINNET_CHAIN),
27
+ );
28
+
29
+ return wallet as SuiWalletStandard;
30
+ }
31
+
32
+ /**
33
+ * @param name each wallet has a name in WalletStandard. you should pass that value
34
+ */
35
+ export function getInstanceOrThrow(name: string): SuiWalletStandard {
36
+ const wallet = getInstance(name);
37
+
38
+ if (!wallet) {
39
+ throw new Error(
40
+ "We couldn't find the Sui instance on your wallet. It may be fixed by refreshing the page.",
41
+ );
42
+ }
43
+
44
+ return wallet;
45
+ }
46
+ export function formatAccountsToCAIP(accounts: readonly { address: string }[]) {
47
+ return accounts.map((account) => {
48
+ return AccountId.format({
49
+ address: account.address,
50
+ chainId: {
51
+ namespace: CAIP_NAMESPACE,
52
+ reference: CAIP_SUI_CHAIN_ID,
53
+ },
54
+ });
55
+ });
56
+ }
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/actions.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/after.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,5 @@
1
+ import { connectAndUpdateStateForSingleNetwork } from '../common/mod.js';
2
+
3
+ export const recommended = [
4
+ ['connect', connectAndUpdateStateForSingleNetwork] as const,
5
+ ];
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/before.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,15 @@
1
+ import type { TronActions } from './types.js';
2
+
3
+ import { ActionBuilder } from '../../mod.js';
4
+ import { intoConnectionFinished } from '../common/after.js';
5
+ import { connectAndUpdateStateForSingleNetwork } from '../common/and.js';
6
+ import { intoConnecting } from '../common/before.js';
7
+
8
+ export const connect = () =>
9
+ new ActionBuilder<TronActions, 'connect'>('connect')
10
+ .and(connectAndUpdateStateForSingleNetwork)
11
+ .before(intoConnecting)
12
+ .after(intoConnectionFinished);
13
+
14
+ export const canEagerConnect = () =>
15
+ new ActionBuilder<TronActions, 'canEagerConnect'>('canEagerConnect');
@@ -0,0 +1,8 @@
1
+ /*
2
+ * TODO: I couldn't found tron in caip
3
+ * @see https://namespaces.chainagnostic.org/
4
+ */
5
+
6
+ // if the value needs to be change make sure you will update mapCaipNamespaceToLegacyNetworkName as well
7
+ export const CAIP_NAMESPACE = 'tron';
8
+ export const CAIP_TRON_CHAIN_ID = 'tron';
@@ -0,0 +1,8 @@
1
+ export * as actions from './actions.js';
2
+ export * as after from './after.js';
3
+ export * as and from './and.js';
4
+ export * as before from './before.js';
5
+ export * as builders from './builders.js';
6
+ export * as utils from './utils.js';
7
+ export type { ProviderAPI, TronActions } from './types.js';
8
+ export { CAIP_NAMESPACE, CAIP_TRON_CHAIN_ID } from './constants.js';
@@ -0,0 +1,14 @@
1
+ import type { Accounts } from '../../types/accounts.js';
2
+ import type {
3
+ AutoImplementedActionsByRecommended,
4
+ CommonActions,
5
+ } from '../common/types.js';
6
+
7
+ export interface TronActions
8
+ extends AutoImplementedActionsByRecommended, CommonActions {
9
+ connect: () => Promise<Accounts>;
10
+ canEagerConnect: () => Promise<boolean>;
11
+ }
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ export type ProviderAPI = Record<string, any>;
@@ -0,0 +1,18 @@
1
+ import type { CaipAccount } from '../common/mod.js';
2
+
3
+ import { AccountId } from 'caip';
4
+
5
+ import { CAIP_NAMESPACE, CAIP_TRON_CHAIN_ID } from './constants.js';
6
+
7
+ export function formatAccountsToCAIP(accounts: string[]) {
8
+ return accounts.map(
9
+ (account) =>
10
+ AccountId.format({
11
+ address: account.toString(),
12
+ chainId: {
13
+ namespace: CAIP_NAMESPACE,
14
+ reference: CAIP_TRON_CHAIN_ID,
15
+ },
16
+ }) as CaipAccount,
17
+ );
18
+ }
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/actions.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/after.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,5 @@
1
+ import { connectAndUpdateStateForSingleNetwork } from '../common/mod.js';
2
+
3
+ export const recommended = [
4
+ ['connect', connectAndUpdateStateForSingleNetwork] as const,
5
+ ];
@@ -0,0 +1,3 @@
1
+ import { recommended as commonRecommended } from '../common/before.js';
2
+
3
+ export const recommended = [...commonRecommended];
@@ -0,0 +1,12 @@
1
+ import type { UtxoActions } from './types.js';
2
+
3
+ import { ActionBuilder } from '../../mod.js';
4
+ import { intoConnectionFinished } from '../common/after.js';
5
+ import { connectAndUpdateStateForSingleNetwork } from '../common/and.js';
6
+ import { intoConnecting } from '../common/before.js';
7
+
8
+ export const connect = () =>
9
+ new ActionBuilder<UtxoActions, 'connect'>('connect')
10
+ .and(connectAndUpdateStateForSingleNetwork)
11
+ .before(intoConnecting)
12
+ .after(intoConnectionFinished);
@@ -0,0 +1,2 @@
1
+ export const CAIP_NAMESPACE = 'bip122';
2
+ export const CAIP_BITCOIN_CHAIN_ID = '000000000019d6689c085ae165831e93';
@@ -0,0 +1,8 @@
1
+ export * as actions from './actions.js';
2
+ export * as after from './after.js';
3
+ export * as and from './and.js';
4
+ export * as before from './before.js';
5
+ export * as builders from './builders.js';
6
+ export * as utils from './utils.js';
7
+ export type { ProviderAPI, UtxoActions } from './types.js';
8
+ export { CAIP_NAMESPACE, CAIP_BITCOIN_CHAIN_ID } from './constants.js';
@@ -0,0 +1,14 @@
1
+ import type { Accounts } from '../../types/accounts.js';
2
+ import type {
3
+ AutoImplementedActionsByRecommended,
4
+ CommonActions,
5
+ } from '../common/types.js';
6
+
7
+ export interface UtxoActions
8
+ extends AutoImplementedActionsByRecommended, CommonActions {
9
+ connect: () => Promise<Accounts>;
10
+ canEagerConnect: () => Promise<boolean>;
11
+ }
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ export type ProviderAPI = Record<string, any>;
@@ -0,0 +1,18 @@
1
+ import type { CaipAccount } from '../common/mod.js';
2
+
3
+ import { AccountId } from 'caip';
4
+
5
+ import { CAIP_BITCOIN_CHAIN_ID, CAIP_NAMESPACE } from './constants.js';
6
+
7
+ export function formatAccountsToCAIP(accounts: string[]) {
8
+ return accounts.map(
9
+ (account) =>
10
+ AccountId.format({
11
+ address: account.toString(),
12
+ chainId: {
13
+ namespace: CAIP_NAMESPACE,
14
+ reference: CAIP_BITCOIN_CHAIN_ID,
15
+ },
16
+ }) as CaipAccount,
17
+ );
18
+ }
@@ -0,0 +1,14 @@
1
+ import type { XRPLActions } from './types.js';
2
+
3
+ import { ActionBuilder } from '../../mod.js';
4
+ import {
5
+ connectAndUpdateStateForSingleNetwork,
6
+ intoConnecting,
7
+ intoConnectionFinished,
8
+ } from '../common/mod.js';
9
+
10
+ export const connect = () =>
11
+ new ActionBuilder<XRPLActions, 'connect'>('connect')
12
+ .and(connectAndUpdateStateForSingleNetwork)
13
+ .before(intoConnecting)
14
+ .after(intoConnectionFinished);
@@ -0,0 +1,7 @@
1
+ /*
2
+ * @see https://namespaces.chainagnostic.org/xrpl/README
3
+ */
4
+
5
+ // if the value neeeds to be change make sure you will update mapCaipNamespaceToLegacyNetworkName as well
6
+ export const CAIP_NAMESPACE = 'xrpl';
7
+ export const CAIP_XRPL_CHAIN_ID = 'mainnet';
@@ -0,0 +1,5 @@
1
+ export * as builders from './builders.js';
2
+ export type { XRPLActions } from './types.js';
3
+
4
+ export * as utils from './utils.js';
5
+ export { CAIP_XRPL_CHAIN_ID, CAIP_NAMESPACE } from './constants.js';
@@ -0,0 +1,16 @@
1
+ import type { Accounts } from '../common/mod.js';
2
+ import type {
3
+ AutoImplementedActionsByRecommended,
4
+ CommonActions,
5
+ } from '../common/types.js';
6
+ import type { AccountLinesTrustline } from 'xrpl';
7
+
8
+ export interface XRPLActions
9
+ extends AutoImplementedActionsByRecommended, CommonActions {
10
+ connect: () => Promise<Accounts>;
11
+ canEagerConnect: () => Promise<boolean>;
12
+ accountLines: (
13
+ account: string,
14
+ options?: { peer?: string },
15
+ ) => Promise<AccountLinesTrustline[]>;
16
+ }
@@ -0,0 +1,15 @@
1
+ import type { CaipAccount } from '../common/mod.js';
2
+
3
+ import { AccountId } from 'caip';
4
+
5
+ import { CAIP_NAMESPACE, CAIP_XRPL_CHAIN_ID } from './constants.js';
6
+
7
+ export function formatAddressToCAIP(address: string): CaipAccount {
8
+ return AccountId.format({
9
+ address,
10
+ chainId: {
11
+ namespace: CAIP_NAMESPACE,
12
+ reference: CAIP_XRPL_CHAIN_ID,
13
+ },
14
+ }) as CaipAccount;
15
+ }
@@ -0,0 +1,13 @@
1
+ import type { ProviderConfig, ProviderInfo } from '../hub/store/mod.js';
2
+
3
+ export const garbageWalletMetaData: ProviderConfig['metadata'] = {
4
+ name: 'Garbage Wallet',
5
+ icon: 'https://somewhereininternet.com/icon.svg',
6
+ extensions: {
7
+ homepage: 'https://app.rango.exchange',
8
+ },
9
+ };
10
+
11
+ export const garbageWalletInfo: ProviderInfo = {
12
+ metadata: garbageWalletMetaData,
13
+ };
@@ -0,0 +1,12 @@
1
+ type CaipNamespace = string;
2
+ type CaipChainId = string;
3
+ type CaipAccountAddress = string;
4
+
5
+ export type CaipAccount =
6
+ `${CaipNamespace}:${CaipChainId}:${CaipAccountAddress}`;
7
+
8
+ export type Accounts = CaipAccount[];
9
+ export type AccountsWithActiveChain = {
10
+ accounts: Accounts;
11
+ network: string;
12
+ };
@@ -0,0 +1,13 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export type AnyFunction = (...args: any[]) => any;
3
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
+ export type AnyPromiseFunction = (...args: any[]) => Promise<any>;
5
+
6
+ export type AndFunction<
7
+ T extends Record<string, AnyPromiseFunction>,
8
+ K extends keyof T,
9
+ > = (result: Awaited<ReturnType<T[K]>>) => Awaited<ReturnType<T[K]>>;
10
+
11
+ export type FunctionWithContext<T, C> = T extends (...args: infer P) => infer R
12
+ ? (context: C, ...args: P) => R
13
+ : never;
@@ -0,0 +1 @@
1
+ export type { AnyFunction, FunctionWithContext } from './actions.js';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @see https://x.com/mattpocockuk/status/1622730173446557697
3
+ */
4
+ export type Prettify<T> = {
5
+ [K in keyof T]: T[K];
6
+ // todo: should use this comment after configuring rango rules eslint: eslint-disable-next-line @typescript-eslint/ban-types
7
+ } & {};
@@ -0,0 +1,8 @@
1
+ /*
2
+ * It is not a good idea to re-export all of CAIP because if they have a breaking change, we will break as well.
3
+ * It would be better to create an abstraction over them and export our own interface to ensure it is under our control.
4
+ */
5
+ export * as CAIP from 'caip';
6
+
7
+ export { generateStoreId } from '../hub/helpers.js';
8
+ export * from './versions.js';
@@ -0,0 +1,24 @@
1
+ import { describe, expect, test } from 'vitest';
2
+
3
+ import { defineVersions, pickVersion } from './versions.js';
4
+
5
+ describe('Picking versions should work correctly', () => {
6
+ test("Error on picking a version doesn't exist", () => {
7
+ const versions = defineVersions().build();
8
+ // const versions: Versions = [['1.0.0', {}]];
9
+ expect(() => pickVersion(versions, '1.0.0')).toThrowError();
10
+ });
11
+
12
+ test('Pick the correct version if it exist', () => {
13
+ const versions = defineVersions()
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ .version('0.0.0', {} as any)
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ .version('1.0.0', {} as any)
18
+ .build();
19
+
20
+ const target = pickVersion(versions, '1.0.0');
21
+ expect(target).toBeDefined();
22
+ expect(target[0]).toBe('1.0.0');
23
+ });
24
+ });
@@ -0,0 +1,64 @@
1
+ import type { Provider } from '../hub/mod.js';
2
+ import type { LegacyProviderInterface } from '../legacy/mod.js';
3
+
4
+ type LegacyVersioned = ['0.0.0', LegacyProviderInterface];
5
+ type HubVersioned = ['1.0.0', Provider];
6
+ type AvailableVersionedProviders = LegacyVersioned | HubVersioned;
7
+ export type VersionedProviders = AvailableVersionedProviders[];
8
+ export type VersionInterface<T extends AvailableVersionedProviders[]> = T[1];
9
+
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ type SemVer<T extends [string, any]> = T extends [infer U, any] ? U : never;
12
+ type MatchVersion<T extends VersionedProviders, Version> = Extract<
13
+ T[number],
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ [Version, any]
16
+ >;
17
+
18
+ export function pickVersion<
19
+ L extends VersionedProviders,
20
+ V extends SemVer<VersionedProviders[number]>,
21
+ >(list: L, targetVersion: V): MatchVersion<L, V> {
22
+ if (!targetVersion) {
23
+ throw new Error(`You should provide a valid semver, e.g 1.0.0.`);
24
+ }
25
+
26
+ const target = list.find(([version]) => version === targetVersion);
27
+
28
+ if (!target) {
29
+ throw new Error(
30
+ `You target version hasn't been found. Available versions: ${Object.keys(
31
+ list,
32
+ ).join(', ')}`,
33
+ );
34
+ }
35
+ return target as MatchVersion<L, V>;
36
+ }
37
+
38
+ interface DefineVersionsApi {
39
+ version: <T extends SemVer<VersionedProviders[number]>>(
40
+ semver: T,
41
+ value: VersionInterface<MatchVersion<VersionedProviders, T>>,
42
+ ) => DefineVersionsApi;
43
+ build: () => VersionedProviders;
44
+ }
45
+
46
+ export function defineVersions(): DefineVersionsApi {
47
+ const versions: VersionedProviders = [];
48
+ const api: DefineVersionsApi = {
49
+ version: (semver, value) => {
50
+ versions.push([semver, value]);
51
+ return api;
52
+ },
53
+ build: () => {
54
+ return versions;
55
+ },
56
+ };
57
+ return api;
58
+ }
59
+
60
+ export function legacyProviderImportsToVersionsInterface(
61
+ provider: LegacyProviderInterface,
62
+ ): VersionedProviders {
63
+ return defineVersions().version('0.0.0', provider).build();
64
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "@hub3js/core/store",
3
+ "type": "module",
4
+ "main": "../dist/hub/store/mod.js",
5
+ "module": "../dist/hub/store/mod.js",
6
+ "types": "../dist/hub/store/mod.d.ts",
7
+ "sideEffects": false
8
+ }