@rango-dev/wallets-core 0.38.0 → 0.38.1-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 (193) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/builders/action.d.ts +22 -0
  3. package/dist/builders/action.d.ts.map +1 -0
  4. package/dist/builders/mod.d.ts +5 -0
  5. package/dist/builders/mod.d.ts.map +1 -0
  6. package/dist/builders/namespace.d.ts +47 -0
  7. package/dist/builders/namespace.d.ts.map +1 -0
  8. package/dist/builders/provider.d.ts +13 -0
  9. package/dist/builders/provider.d.ts.map +1 -0
  10. package/dist/builders/types.d.ts +21 -0
  11. package/dist/builders/types.d.ts.map +1 -0
  12. package/dist/hub/helpers.d.ts +6 -0
  13. package/dist/hub/helpers.d.ts.map +1 -0
  14. package/dist/hub/hub.d.ts +28 -0
  15. package/dist/hub/hub.d.ts.map +1 -0
  16. package/dist/hub/mod.d.ts +7 -0
  17. package/dist/hub/mod.d.ts.map +1 -0
  18. package/dist/hub/namespaces/errors.d.ts +4 -0
  19. package/dist/hub/namespaces/errors.d.ts.map +1 -0
  20. package/dist/hub/namespaces/mod.d.ts +3 -0
  21. package/dist/hub/namespaces/mod.d.ts.map +1 -0
  22. package/dist/hub/namespaces/namespace.d.ts +146 -0
  23. package/dist/hub/namespaces/namespace.d.ts.map +1 -0
  24. package/dist/hub/namespaces/namespace.test.d.ts +2 -0
  25. package/dist/hub/namespaces/namespace.test.d.ts.map +1 -0
  26. package/dist/hub/namespaces/types.d.ts +33 -0
  27. package/dist/hub/namespaces/types.d.ts.map +1 -0
  28. package/dist/hub/provider/mod.d.ts +3 -0
  29. package/dist/hub/provider/mod.d.ts.map +1 -0
  30. package/dist/hub/provider/provider.d.ts +136 -0
  31. package/dist/hub/provider/provider.d.ts.map +1 -0
  32. package/dist/hub/provider/provider.test.d.ts +2 -0
  33. package/dist/hub/provider/provider.test.d.ts.map +1 -0
  34. package/dist/hub/provider/types.d.ts +28 -0
  35. package/dist/hub/provider/types.d.ts.map +1 -0
  36. package/dist/hub/store/hub.d.ts +11 -0
  37. package/dist/hub/store/hub.d.ts.map +1 -0
  38. package/dist/hub/store/mod.d.ts +6 -0
  39. package/dist/hub/store/mod.d.ts.map +1 -0
  40. package/dist/hub/store/namespaces.d.ts +34 -0
  41. package/dist/hub/store/namespaces.d.ts.map +1 -0
  42. package/dist/hub/store/providers.d.ts +45 -0
  43. package/dist/hub/store/providers.d.ts.map +1 -0
  44. package/dist/hub/store/selectors.d.ts +18 -0
  45. package/dist/hub/store/selectors.d.ts.map +1 -0
  46. package/dist/hub/store/store.d.ts +13 -0
  47. package/dist/hub/store/store.d.ts.map +1 -0
  48. package/dist/hub/store/store.test.d.ts +2 -0
  49. package/dist/hub/store/store.test.d.ts.map +1 -0
  50. package/dist/mod.d.ts +6 -1
  51. package/dist/mod.d.ts.map +1 -1
  52. package/dist/mod.js +1 -0
  53. package/dist/mod.js.map +4 -4
  54. package/dist/namespaces/common/actions.d.ts +4 -0
  55. package/dist/namespaces/common/actions.d.ts.map +1 -0
  56. package/dist/namespaces/common/after.d.ts +4 -0
  57. package/dist/namespaces/common/after.d.ts.map +1 -0
  58. package/dist/namespaces/common/and.d.ts +6 -0
  59. package/dist/namespaces/common/and.d.ts.map +1 -0
  60. package/dist/namespaces/common/before.d.ts +4 -0
  61. package/dist/namespaces/common/before.d.ts.map +1 -0
  62. package/dist/namespaces/common/builders.d.ts +4 -0
  63. package/dist/namespaces/common/builders.d.ts.map +1 -0
  64. package/dist/namespaces/common/helpers.d.ts +2 -0
  65. package/dist/namespaces/common/helpers.d.ts.map +1 -0
  66. package/dist/namespaces/common/mod.d.ts +6 -0
  67. package/dist/namespaces/common/mod.d.ts.map +1 -0
  68. package/dist/namespaces/common/mod.js +2 -0
  69. package/dist/namespaces/common/mod.js.map +7 -0
  70. package/dist/namespaces/common/types.d.ts +7 -0
  71. package/dist/namespaces/common/types.d.ts.map +1 -0
  72. package/dist/namespaces/cosmos/mod.d.ts +2 -0
  73. package/dist/namespaces/cosmos/mod.d.ts.map +1 -0
  74. package/dist/namespaces/cosmos/types.d.ts +4 -0
  75. package/dist/namespaces/cosmos/types.d.ts.map +1 -0
  76. package/dist/namespaces/evm/actions.d.ts +8 -0
  77. package/dist/namespaces/evm/actions.d.ts.map +1 -0
  78. package/dist/namespaces/evm/after.d.ts +2 -0
  79. package/dist/namespaces/evm/after.d.ts.map +1 -0
  80. package/dist/namespaces/evm/and.d.ts +3 -0
  81. package/dist/namespaces/evm/and.d.ts.map +1 -0
  82. package/dist/namespaces/evm/before.d.ts +2 -0
  83. package/dist/namespaces/evm/before.d.ts.map +1 -0
  84. package/dist/namespaces/evm/builders.d.ts +4 -0
  85. package/dist/namespaces/evm/builders.d.ts.map +1 -0
  86. package/dist/namespaces/evm/constants.d.ts +3 -0
  87. package/dist/namespaces/evm/constants.d.ts.map +1 -0
  88. package/dist/namespaces/evm/eip1193.d.ts +1228 -0
  89. package/dist/namespaces/evm/eip1193.d.ts.map +1 -0
  90. package/dist/namespaces/evm/mod.d.ts +9 -0
  91. package/dist/namespaces/evm/mod.d.ts.map +1 -0
  92. package/dist/namespaces/evm/mod.js +2 -0
  93. package/dist/namespaces/evm/mod.js.map +7 -0
  94. package/dist/namespaces/evm/types.d.ts +10 -0
  95. package/dist/namespaces/evm/types.d.ts.map +1 -0
  96. package/dist/namespaces/evm/utils.d.ts +9 -0
  97. package/dist/namespaces/evm/utils.d.ts.map +1 -0
  98. package/dist/namespaces/solana/actions.d.ts +6 -0
  99. package/dist/namespaces/solana/actions.d.ts.map +1 -0
  100. package/dist/namespaces/solana/after.d.ts +2 -0
  101. package/dist/namespaces/solana/after.d.ts.map +1 -0
  102. package/dist/namespaces/solana/and.d.ts +3 -0
  103. package/dist/namespaces/solana/and.d.ts.map +1 -0
  104. package/dist/namespaces/solana/before.d.ts +2 -0
  105. package/dist/namespaces/solana/before.d.ts.map +1 -0
  106. package/dist/namespaces/solana/builders.d.ts +4 -0
  107. package/dist/namespaces/solana/builders.d.ts.map +1 -0
  108. package/dist/namespaces/solana/constants.d.ts +3 -0
  109. package/dist/namespaces/solana/constants.d.ts.map +1 -0
  110. package/dist/namespaces/solana/mod.d.ts +8 -0
  111. package/dist/namespaces/solana/mod.d.ts.map +1 -0
  112. package/dist/namespaces/solana/mod.js +2 -0
  113. package/dist/namespaces/solana/mod.js.map +7 -0
  114. package/dist/namespaces/solana/types.d.ts +7 -0
  115. package/dist/namespaces/solana/types.d.ts.map +1 -0
  116. package/dist/test-utils/fixtures.d.ts +3 -0
  117. package/dist/test-utils/fixtures.d.ts.map +1 -0
  118. package/dist/types/accounts.d.ts +11 -0
  119. package/dist/types/accounts.d.ts.map +1 -0
  120. package/dist/types/actions.d.ts +5 -0
  121. package/dist/types/actions.d.ts.map +1 -0
  122. package/dist/types/utils.d.ts +7 -0
  123. package/dist/types/utils.d.ts.map +1 -0
  124. package/dist/utils/mod.d.ts +4 -0
  125. package/dist/utils/mod.d.ts.map +1 -0
  126. package/dist/utils/mod.js +2 -0
  127. package/dist/utils/mod.js.map +7 -0
  128. package/dist/utils/versions.d.ts +21 -0
  129. package/dist/utils/versions.d.ts.map +1 -0
  130. package/dist/utils/versions.test.d.ts +2 -0
  131. package/dist/utils/versions.test.d.ts.map +1 -0
  132. package/dist/wallets-core.build.json +1 -1
  133. package/package.json +25 -4
  134. package/src/builders/action.ts +86 -0
  135. package/src/builders/mod.ts +5 -0
  136. package/src/builders/namespace.ts +229 -0
  137. package/src/builders/provider.ts +61 -0
  138. package/src/builders/types.ts +29 -0
  139. package/src/hub/helpers.ts +11 -0
  140. package/src/hub/hub.ts +122 -0
  141. package/src/hub/mod.ts +10 -0
  142. package/src/hub/namespaces/errors.ts +8 -0
  143. package/src/hub/namespaces/mod.ts +9 -0
  144. package/src/hub/namespaces/namespace.test.ts +333 -0
  145. package/src/hub/namespaces/namespace.ts +443 -0
  146. package/src/hub/namespaces/types.ts +50 -0
  147. package/src/hub/provider/mod.ts +9 -0
  148. package/src/hub/provider/provider.test.ts +231 -0
  149. package/src/hub/provider/provider.ts +330 -0
  150. package/src/hub/provider/types.ts +37 -0
  151. package/src/hub/store/hub.ts +18 -0
  152. package/src/hub/store/mod.ts +8 -0
  153. package/src/hub/store/namespaces.ts +90 -0
  154. package/src/hub/store/providers.ts +97 -0
  155. package/src/hub/store/selectors.ts +59 -0
  156. package/src/hub/store/store.test.ts +32 -0
  157. package/src/hub/store/store.ts +26 -0
  158. package/src/mod.ts +35 -1
  159. package/src/namespaces/common/actions.ts +11 -0
  160. package/src/namespaces/common/after.ts +8 -0
  161. package/src/namespaces/common/and.ts +42 -0
  162. package/src/namespaces/common/before.ts +9 -0
  163. package/src/namespaces/common/builders.ts +19 -0
  164. package/src/namespaces/common/helpers.ts +10 -0
  165. package/src/namespaces/common/mod.ts +12 -0
  166. package/src/namespaces/common/types.ts +7 -0
  167. package/src/namespaces/cosmos/mod.ts +1 -0
  168. package/src/namespaces/cosmos/types.ts +10 -0
  169. package/src/namespaces/evm/actions.ts +97 -0
  170. package/src/namespaces/evm/after.ts +3 -0
  171. package/src/namespaces/evm/and.ts +5 -0
  172. package/src/namespaces/evm/before.ts +3 -0
  173. package/src/namespaces/evm/builders.ts +10 -0
  174. package/src/namespaces/evm/constants.ts +2 -0
  175. package/src/namespaces/evm/eip1193.ts +1414 -0
  176. package/src/namespaces/evm/mod.ts +9 -0
  177. package/src/namespaces/evm/types.ts +18 -0
  178. package/src/namespaces/evm/utils.ts +52 -0
  179. package/src/namespaces/solana/actions.ts +70 -0
  180. package/src/namespaces/solana/after.ts +3 -0
  181. package/src/namespaces/solana/and.ts +5 -0
  182. package/src/namespaces/solana/before.ts +3 -0
  183. package/src/namespaces/solana/builders.ts +10 -0
  184. package/src/namespaces/solana/constants.ts +2 -0
  185. package/src/namespaces/solana/mod.ts +8 -0
  186. package/src/namespaces/solana/types.ts +20 -0
  187. package/src/test-utils/fixtures.ts +9 -0
  188. package/src/types/accounts.ts +12 -0
  189. package/src/types/actions.ts +11 -0
  190. package/src/types/utils.ts +7 -0
  191. package/src/utils/mod.ts +8 -0
  192. package/src/utils/versions.test.ts +22 -0
  193. package/src/utils/versions.ts +63 -0
@@ -0,0 +1,9 @@
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 utils from './utils.js';
6
+ export * as builders from './builders.js';
7
+
8
+ export type { EvmActions, ProviderAPI } from './types.js';
9
+ export { CAIP_NAMESPACE, CAIP_ETHEREUM_CHAIN_ID } from './constants.js';
@@ -0,0 +1,18 @@
1
+ import type { AddEthereumChainParameter } from './eip1193.js';
2
+ import type { AccountsWithActiveChain } from '../../types/accounts.js';
3
+ import type {
4
+ AutoImplementedActionsByRecommended,
5
+ CommonActions,
6
+ } from '../common/types.js';
7
+
8
+ export interface EvmActions
9
+ extends AutoImplementedActionsByRecommended,
10
+ CommonActions {
11
+ connect: (chain?: Chain | ChainId) => Promise<AccountsWithActiveChain>;
12
+ }
13
+
14
+ export type { EIP1193Provider as ProviderAPI } from './eip1193.js';
15
+
16
+ // A 0x-prefixed hexadecimal string
17
+ export type ChainId = string;
18
+ export type Chain = AddEthereumChainParameter;
@@ -0,0 +1,52 @@
1
+ import type { Chain, ChainId, ProviderAPI } from './types.js';
2
+
3
+ export async function getAccounts(provider: ProviderAPI) {
4
+ const [accounts, chainId] = await Promise.all([
5
+ provider.request({ method: 'eth_requestAccounts' }),
6
+ provider.request({ method: 'eth_chainId' }),
7
+ ]);
8
+
9
+ return {
10
+ accounts,
11
+ chainId,
12
+ };
13
+ }
14
+
15
+ export async function suggestNetwork(instance: ProviderAPI, chain: Chain) {
16
+ return await instance.request({
17
+ method: 'wallet_addEthereumChain',
18
+ params: [chain],
19
+ });
20
+ }
21
+
22
+ export async function switchNetwork(instance: ProviderAPI, chainId: ChainId) {
23
+ return await instance.request({
24
+ method: 'wallet_switchEthereumChain',
25
+ params: [{ chainId: chainId }],
26
+ });
27
+ }
28
+
29
+ export async function switchOrAddNetwork(
30
+ instance: ProviderAPI,
31
+ chain: ChainId | Chain
32
+ ) {
33
+ try {
34
+ const chainId = typeof chain === 'string' ? chain : chain.chainId;
35
+ await switchNetwork(instance, chainId);
36
+ } catch (switchError) {
37
+ const error = switchError as { code: number };
38
+
39
+ const NOT_FOUND_CHAIN_ERROR_CODE = 4902;
40
+ if (
41
+ typeof chain !== 'string' &&
42
+ (error.code === NOT_FOUND_CHAIN_ERROR_CODE || !error.code)
43
+ ) {
44
+ /*
45
+ * Note: on WalletConnect `code` is undefined so we have to use !switchError.code as fallback.
46
+ * This error code indicates that the chain has not been added to wallet.
47
+ */
48
+ await suggestNetwork(instance, chain);
49
+ }
50
+ throw switchError;
51
+ }
52
+ }
@@ -0,0 +1,70 @@
1
+ import type { ProviderAPI, SolanaActions } from './types.js';
2
+ import type { Subscriber } from '../../hub/namespaces/mod.js';
3
+ import type { SubscriberCleanUp } from '../../hub/namespaces/types.js';
4
+ import type { AnyFunction } from '../../types/actions.js';
5
+
6
+ import { AccountId } from 'caip';
7
+
8
+ import { recommended as commonRecommended } from '../common/actions.js';
9
+
10
+ import { CAIP_NAMESPACE, CAIP_SOLANA_CHAIN_ID } from './constants.js';
11
+
12
+ export const recommended = [...commonRecommended];
13
+
14
+ export function changeAccountSubscriber(
15
+ instance: () => ProviderAPI | undefined
16
+ ): [Subscriber<SolanaActions>, SubscriberCleanUp<SolanaActions>] {
17
+ let eventCallback: AnyFunction;
18
+
19
+ // subscriber can be passed to `or`, it will get the error and should rethrow error to pass the error to next `or` or throw error.
20
+ return [
21
+ (context, err) => {
22
+ const solanaInstance = instance();
23
+
24
+ if (!solanaInstance) {
25
+ throw new Error(
26
+ 'Trying to subscribe to your Solana wallet, but seems its instance is not available.'
27
+ );
28
+ }
29
+
30
+ const [, setState] = context.state();
31
+
32
+ eventCallback = (publicKey) => {
33
+ /*
34
+ * In Phantom, when user is switching to an account which is not connected to dApp yet, it returns a null.
35
+ * So null means we don't have access to account and we 0 need to disconnect and let the user connect the account.
36
+ */
37
+ if (!publicKey) {
38
+ context.action('disconnect');
39
+ return;
40
+ }
41
+
42
+ setState('accounts', [
43
+ AccountId.format({
44
+ address: publicKey.toString(),
45
+ chainId: {
46
+ namespace: CAIP_NAMESPACE,
47
+ reference: CAIP_SOLANA_CHAIN_ID,
48
+ },
49
+ }),
50
+ ]);
51
+ };
52
+ solanaInstance.on('accountChanged', eventCallback);
53
+
54
+ if (err instanceof Error) {
55
+ throw err;
56
+ }
57
+ },
58
+ (_context, err) => {
59
+ const solanaInstance = instance();
60
+
61
+ if (eventCallback && solanaInstance) {
62
+ solanaInstance.off('accountChanged', eventCallback);
63
+ }
64
+
65
+ if (err instanceof Error) {
66
+ throw err;
67
+ }
68
+ },
69
+ ];
70
+ }
@@ -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,10 @@
1
+ import type { SolanaActions } 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
+
7
+ export const connect = () =>
8
+ new ActionBuilder<SolanaActions, 'connect'>('connect')
9
+ .and(connectAndUpdateStateForSingleNetwork)
10
+ .after(intoConnectionFinished);
@@ -0,0 +1,2 @@
1
+ export const CAIP_NAMESPACE = 'solana';
2
+ export const CAIP_SOLANA_CHAIN_ID = '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
@@ -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
+
7
+ export type { ProviderAPI, SolanaActions } from './types.js';
8
+ export { CAIP_NAMESPACE, CAIP_SOLANA_CHAIN_ID } from './constants.js';
@@ -0,0 +1,20 @@
1
+ import type { Accounts } from '../../types/accounts.js';
2
+ import type {
3
+ AutoImplementedActionsByRecommended,
4
+ CommonActions,
5
+ } from '../common/types.js';
6
+
7
+ export interface SolanaActions
8
+ extends AutoImplementedActionsByRecommended,
9
+ CommonActions {
10
+ connect: () => Promise<Accounts>;
11
+ }
12
+
13
+ /*
14
+ *
15
+ * TODO: That would be better to define a type for Solana injected wallets.
16
+ * They have something called [Wallet Standard](https://github.com/wallet-standard/wallet-standard) but not sure all the Solana wallets support that (Phantom do).
17
+ * If Phantom's interface is what Solana wallets are supporting, another option would be define that type here.
18
+ *
19
+ */
20
+ export type ProviderAPI = Record<string, any>;
@@ -0,0 +1,9 @@
1
+ import type { ProviderConfig } from '../hub/store/mod.js';
2
+
3
+ export const garbageWalletInfo: ProviderConfig['info'] = {
4
+ name: 'Garbage Wallet',
5
+ icon: 'https://somewhereininternet.com/icon.svg',
6
+ extensions: {
7
+ homepage: 'https://app.rango.exchange',
8
+ },
9
+ };
@@ -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,11 @@
1
+ export type AnyFunction = (...args: any[]) => any;
2
+ export type AnyPromiseFunction = (...args: any[]) => Promise<any>;
3
+
4
+ export type AndFunction<
5
+ T extends Record<string, AnyPromiseFunction>,
6
+ K extends keyof T
7
+ > = (result: Awaited<ReturnType<T[K]>>) => Awaited<ReturnType<T[K]>>;
8
+
9
+ export type FunctionWithContext<T, C> = T extends (...args: infer P) => infer R
10
+ ? (context: C, ...args: P) => R
11
+ : never;
@@ -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
+ // 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,22 @@
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
+ .version('0.0.0', {} as any)
15
+ .version('1.0.0', {} as any)
16
+ .build();
17
+
18
+ const target = pickVersion(versions, '1.0.0');
19
+ expect(target).toBeDefined();
20
+ expect(target[0]).toBe('1.0.0');
21
+ });
22
+ });
@@ -0,0 +1,63 @@
1
+ import type { Provider } from '../hub/mod.js';
2
+ import type { LegacyProviderInterface } from '../legacy/mod.js';
3
+
4
+ type VersionedVLegacy = ['0.0.0', LegacyProviderInterface];
5
+ type VersionedV1 = ['1.0.0', Provider];
6
+ type AvailableVersions = VersionedVLegacy | VersionedV1;
7
+ export type Versions = AvailableVersions[];
8
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
9
+ export type VersionInterface<T extends AvailableVersions[]> = T[1];
10
+
11
+ type SemVer<T extends [string, any]> = T extends [infer U, any] ? U : never;
12
+ type MatchVersion<T extends Versions, Version> = Extract<
13
+ T[number],
14
+ [Version, any]
15
+ >;
16
+
17
+ export function pickVersion<
18
+ L extends Versions,
19
+ V extends SemVer<Versions[number]>
20
+ >(list: L, targetVersion: V): MatchVersion<L, V> {
21
+ if (!targetVersion) {
22
+ throw new Error(`You should provide a valid semver, e.g 1.0.0.`);
23
+ }
24
+
25
+ const target = list.find(([version]) => version === targetVersion);
26
+
27
+ if (!target) {
28
+ throw new Error(
29
+ `You target version hasn't been found. Available versions: ${Object.keys(
30
+ list
31
+ ).join(', ')}`
32
+ );
33
+ }
34
+ return target as MatchVersion<L, V>;
35
+ }
36
+
37
+ interface DefineVersionsApi {
38
+ version: <T extends SemVer<Versions[number]>>(
39
+ semver: T,
40
+ value: VersionInterface<MatchVersion<Versions, T>>
41
+ ) => DefineVersionsApi;
42
+ build: () => Versions;
43
+ }
44
+
45
+ export function defineVersions(): DefineVersionsApi {
46
+ const versions: Versions = [];
47
+ const api: DefineVersionsApi = {
48
+ version: (semver, value) => {
49
+ versions.push([semver, value]);
50
+ return api;
51
+ },
52
+ build: () => {
53
+ return versions;
54
+ },
55
+ };
56
+ return api;
57
+ }
58
+
59
+ export function legacyProviderImportsToVersionsInterface(
60
+ provider: LegacyProviderInterface
61
+ ): Versions {
62
+ return defineVersions().version('0.0.0', provider).build();
63
+ }