@metamask/core-backend 5.0.0 → 5.1.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 (159) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/README.md +252 -1
  3. package/dist/AccountActivityService.cjs +1 -1
  4. package/dist/AccountActivityService.cjs.map +1 -1
  5. package/dist/AccountActivityService.mjs +1 -1
  6. package/dist/AccountActivityService.mjs.map +1 -1
  7. package/dist/BackendWebSocketService.cjs +5 -3
  8. package/dist/BackendWebSocketService.cjs.map +1 -1
  9. package/dist/BackendWebSocketService.d.cts +2 -2
  10. package/dist/BackendWebSocketService.d.cts.map +1 -1
  11. package/dist/BackendWebSocketService.d.mts +2 -2
  12. package/dist/BackendWebSocketService.d.mts.map +1 -1
  13. package/dist/BackendWebSocketService.mjs +5 -3
  14. package/dist/BackendWebSocketService.mjs.map +1 -1
  15. package/dist/api/ApiPlatformClient.cjs +177 -0
  16. package/dist/api/ApiPlatformClient.cjs.map +1 -0
  17. package/dist/api/ApiPlatformClient.d.cts +127 -0
  18. package/dist/api/ApiPlatformClient.d.cts.map +1 -0
  19. package/dist/api/ApiPlatformClient.d.mts +127 -0
  20. package/dist/api/ApiPlatformClient.d.mts.map +1 -0
  21. package/dist/api/ApiPlatformClient.mjs +172 -0
  22. package/dist/api/ApiPlatformClient.mjs.map +1 -0
  23. package/dist/api/accounts/client.cjs +492 -0
  24. package/dist/api/accounts/client.cjs.map +1 -0
  25. package/dist/api/accounts/client.d.cts +213 -0
  26. package/dist/api/accounts/client.d.cts.map +1 -0
  27. package/dist/api/accounts/client.d.mts +213 -0
  28. package/dist/api/accounts/client.d.mts.map +1 -0
  29. package/dist/api/accounts/client.mjs +488 -0
  30. package/dist/api/accounts/client.mjs.map +1 -0
  31. package/dist/api/accounts/index.cjs +9 -0
  32. package/dist/api/accounts/index.cjs.map +1 -0
  33. package/dist/api/accounts/index.d.cts +6 -0
  34. package/dist/api/accounts/index.d.cts.map +1 -0
  35. package/dist/api/accounts/index.d.mts +6 -0
  36. package/dist/api/accounts/index.d.mts.map +1 -0
  37. package/dist/api/accounts/index.mjs +5 -0
  38. package/dist/api/accounts/index.mjs.map +1 -0
  39. package/dist/api/accounts/types.cjs +7 -0
  40. package/dist/api/accounts/types.cjs.map +1 -0
  41. package/dist/api/accounts/types.d.cts +195 -0
  42. package/dist/api/accounts/types.d.cts.map +1 -0
  43. package/dist/api/accounts/types.d.mts +195 -0
  44. package/dist/api/accounts/types.d.mts.map +1 -0
  45. package/dist/api/accounts/types.mjs +6 -0
  46. package/dist/api/accounts/types.mjs.map +1 -0
  47. package/dist/api/base-client.cjs +149 -0
  48. package/dist/api/base-client.cjs.map +1 -0
  49. package/dist/api/base-client.d.cts +54 -0
  50. package/dist/api/base-client.d.cts.map +1 -0
  51. package/dist/api/base-client.d.mts +54 -0
  52. package/dist/api/base-client.d.mts.map +1 -0
  53. package/dist/api/base-client.mjs +143 -0
  54. package/dist/api/base-client.mjs.map +1 -0
  55. package/dist/api/index.cjs +35 -0
  56. package/dist/api/index.cjs.map +1 -0
  57. package/dist/api/index.d.cts +18 -0
  58. package/dist/api/index.d.cts.map +1 -0
  59. package/dist/api/index.d.mts +18 -0
  60. package/dist/api/index.d.mts.map +1 -0
  61. package/dist/api/index.mjs +18 -0
  62. package/dist/api/index.mjs.map +1 -0
  63. package/dist/api/prices/client.cjs +521 -0
  64. package/dist/api/prices/client.cjs.map +1 -0
  65. package/dist/api/prices/client.d.cts +235 -0
  66. package/dist/api/prices/client.d.cts.map +1 -0
  67. package/dist/api/prices/client.d.mts +235 -0
  68. package/dist/api/prices/client.d.mts.map +1 -0
  69. package/dist/api/prices/client.mjs +517 -0
  70. package/dist/api/prices/client.mjs.map +1 -0
  71. package/dist/api/prices/index.cjs +9 -0
  72. package/dist/api/prices/index.cjs.map +1 -0
  73. package/dist/api/prices/index.d.cts +6 -0
  74. package/dist/api/prices/index.d.cts.map +1 -0
  75. package/dist/api/prices/index.d.mts +6 -0
  76. package/dist/api/prices/index.d.mts.map +1 -0
  77. package/dist/api/prices/index.mjs +5 -0
  78. package/dist/api/prices/index.mjs.map +1 -0
  79. package/dist/api/prices/types.cjs +7 -0
  80. package/dist/api/prices/types.cjs.map +1 -0
  81. package/dist/api/prices/types.d.cts +61 -0
  82. package/dist/api/prices/types.d.cts.map +1 -0
  83. package/dist/api/prices/types.d.mts +61 -0
  84. package/dist/api/prices/types.d.mts.map +1 -0
  85. package/dist/api/prices/types.mjs +6 -0
  86. package/dist/api/prices/types.mjs.map +1 -0
  87. package/dist/api/shared-types.cjs +93 -0
  88. package/dist/api/shared-types.cjs.map +1 -0
  89. package/dist/api/shared-types.d.cts +131 -0
  90. package/dist/api/shared-types.d.cts.map +1 -0
  91. package/dist/api/shared-types.d.mts +131 -0
  92. package/dist/api/shared-types.d.mts.map +1 -0
  93. package/dist/api/shared-types.mjs +87 -0
  94. package/dist/api/shared-types.mjs.map +1 -0
  95. package/dist/api/test-utils.cjs +70 -0
  96. package/dist/api/test-utils.cjs.map +1 -0
  97. package/dist/api/test-utils.d.cts +30 -0
  98. package/dist/api/test-utils.d.cts.map +1 -0
  99. package/dist/api/test-utils.d.mts +30 -0
  100. package/dist/api/test-utils.d.mts.map +1 -0
  101. package/dist/api/test-utils.mjs +64 -0
  102. package/dist/api/test-utils.mjs.map +1 -0
  103. package/dist/api/token/client.cjs +342 -0
  104. package/dist/api/token/client.cjs.map +1 -0
  105. package/dist/api/token/client.d.cts +193 -0
  106. package/dist/api/token/client.d.cts.map +1 -0
  107. package/dist/api/token/client.d.mts +193 -0
  108. package/dist/api/token/client.d.mts.map +1 -0
  109. package/dist/api/token/client.mjs +338 -0
  110. package/dist/api/token/client.mjs.map +1 -0
  111. package/dist/api/token/index.cjs +9 -0
  112. package/dist/api/token/index.cjs.map +1 -0
  113. package/dist/api/token/index.d.cts +6 -0
  114. package/dist/api/token/index.d.cts.map +1 -0
  115. package/dist/api/token/index.d.mts +6 -0
  116. package/dist/api/token/index.d.mts.map +1 -0
  117. package/dist/api/token/index.mjs +5 -0
  118. package/dist/api/token/index.mjs.map +1 -0
  119. package/dist/api/token/types.cjs +7 -0
  120. package/dist/api/token/types.cjs.map +1 -0
  121. package/dist/api/token/types.d.cts +75 -0
  122. package/dist/api/token/types.d.cts.map +1 -0
  123. package/dist/api/token/types.d.mts +75 -0
  124. package/dist/api/token/types.d.mts.map +1 -0
  125. package/dist/api/token/types.mjs +6 -0
  126. package/dist/api/token/types.mjs.map +1 -0
  127. package/dist/api/tokens/client.cjs +91 -0
  128. package/dist/api/tokens/client.cjs.map +1 -0
  129. package/dist/api/tokens/client.d.cts +45 -0
  130. package/dist/api/tokens/client.d.cts.map +1 -0
  131. package/dist/api/tokens/client.d.mts +45 -0
  132. package/dist/api/tokens/client.d.mts.map +1 -0
  133. package/dist/api/tokens/client.mjs +87 -0
  134. package/dist/api/tokens/client.mjs.map +1 -0
  135. package/dist/api/tokens/index.cjs +9 -0
  136. package/dist/api/tokens/index.cjs.map +1 -0
  137. package/dist/api/tokens/index.d.cts +6 -0
  138. package/dist/api/tokens/index.d.cts.map +1 -0
  139. package/dist/api/tokens/index.d.mts +6 -0
  140. package/dist/api/tokens/index.d.mts.map +1 -0
  141. package/dist/api/tokens/index.mjs +5 -0
  142. package/dist/api/tokens/index.mjs.map +1 -0
  143. package/dist/api/tokens/types.cjs +7 -0
  144. package/dist/api/tokens/types.cjs.map +1 -0
  145. package/dist/api/tokens/types.d.cts +83 -0
  146. package/dist/api/tokens/types.d.cts.map +1 -0
  147. package/dist/api/tokens/types.d.mts +83 -0
  148. package/dist/api/tokens/types.d.mts.map +1 -0
  149. package/dist/api/tokens/types.mjs +6 -0
  150. package/dist/api/tokens/types.mjs.map +1 -0
  151. package/dist/index.cjs +33 -4
  152. package/dist/index.cjs.map +1 -1
  153. package/dist/index.d.cts +6 -7
  154. package/dist/index.d.cts.map +1 -1
  155. package/dist/index.d.mts +6 -7
  156. package/dist/index.d.mts.map +1 -1
  157. package/dist/index.mjs +21 -5
  158. package/dist/index.mjs.map +1 -1
  159. package/package.json +8 -11
@@ -0,0 +1,127 @@
1
+ /**
2
+ * ApiPlatformClient - MetaMask API Platform Client
3
+ *
4
+ * A comprehensive API client that uses @tanstack/query-core directly for:
5
+ * - Automatic request deduplication
6
+ * - Intelligent caching
7
+ * - Automatic retries with exponential backoff
8
+ *
9
+ * Provides unified access to all MetaMask backend APIs:
10
+ * - Accounts API (accounts.api.cx.metamask.io)
11
+ * - Price API (price.api.cx.metamask.io)
12
+ * - Token API (token.api.cx.metamask.io)
13
+ * - Tokens API (tokens.api.cx.metamask.io)
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const client = new ApiPlatformClient({
18
+ * clientProduct: 'metamask-extension',
19
+ * getBearerToken: async () => token,
20
+ * });
21
+ *
22
+ * // Access API methods through sub-clients
23
+ * const networks = await client.accounts.fetchV2SupportedNetworks();
24
+ * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);
25
+ * const prices = await client.prices.fetchV3SpotPrices(assetIds);
26
+ * const tokenList = await client.token.fetchTokenList(1);
27
+ * const assets = await client.tokens.fetchV3Assets(assetIds);
28
+ *
29
+ * // Cache management
30
+ * await client.invalidateAll(); // Invalidate all caches
31
+ * await client.invalidateAuthToken(); // Invalidate auth token
32
+ * await client.accounts.invalidateBalances(); // Domain-specific via sub-client
33
+ * await client.prices.invalidatePrices(); // Domain-specific via sub-client
34
+ * ```
35
+ */
36
+ import { QueryClient } from "@tanstack/query-core";
37
+ import type { QueryKey } from "@tanstack/query-core";
38
+ import { AccountsApiClient } from "./accounts/index.cjs";
39
+ import { PricesApiClient } from "./prices/index.cjs";
40
+ import type { ApiPlatformClientOptions } from "./shared-types.cjs";
41
+ import { TokenApiClient } from "./token/index.cjs";
42
+ import { TokensApiClient } from "./tokens/index.cjs";
43
+ /**
44
+ * MetaMask API Platform Client with TanStack Query caching.
45
+ * Provides cached access to all MetaMask backend APIs through a unified interface.
46
+ *
47
+ * Access API methods through the sub-clients:
48
+ * - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)
49
+ * - `client.prices` - Prices API (spot prices, exchange rates, historical prices)
50
+ * - `client.token` - Token API (token metadata, trending, top gainers)
51
+ * - `client.tokens` - Tokens API (bulk asset operations, supported networks)
52
+ */
53
+ export declare class ApiPlatformClient {
54
+ #private;
55
+ /**
56
+ * Accounts API client.
57
+ * Provides methods for balances, transactions, relationships, NFTs, and token discovery.
58
+ */
59
+ readonly accounts: AccountsApiClient;
60
+ /**
61
+ * Prices API client.
62
+ * Provides methods for spot prices, exchange rates, and historical prices.
63
+ */
64
+ readonly prices: PricesApiClient;
65
+ /**
66
+ * Token API client.
67
+ * Provides methods for token metadata, networks, trending tokens, and top assets.
68
+ */
69
+ readonly token: TokenApiClient;
70
+ /**
71
+ * Tokens API client.
72
+ * Provides methods for bulk asset operations and supported networks.
73
+ */
74
+ readonly tokens: TokensApiClient;
75
+ constructor(options: ApiPlatformClientOptions);
76
+ /**
77
+ * Get the underlying QueryClient (for advanced usage).
78
+ *
79
+ * @returns The underlying QueryClient instance.
80
+ */
81
+ get queryClient(): QueryClient;
82
+ /**
83
+ * Get cached data for a query key.
84
+ *
85
+ * @param queryKey - The query key to look up.
86
+ * @returns The cached data or undefined.
87
+ */
88
+ getCachedData<CachedData>(queryKey: QueryKey): CachedData | undefined;
89
+ /**
90
+ * Set cached data for a query key.
91
+ *
92
+ * @param queryKey - The query key to set data for.
93
+ * @param data - The data to cache.
94
+ */
95
+ setCachedData<CachedData>(queryKey: QueryKey, data: CachedData): void;
96
+ /**
97
+ * Check if a query is currently fetching.
98
+ *
99
+ * @param queryKey - The query key to check.
100
+ * @returns True if the query is currently fetching.
101
+ */
102
+ isFetching(queryKey: QueryKey): boolean;
103
+ /**
104
+ * Clear all cached data across all sub-clients.
105
+ */
106
+ clear(): void;
107
+ /**
108
+ * Invalidate all queries across all sub-clients.
109
+ */
110
+ invalidateAll(): Promise<void>;
111
+ /**
112
+ * Invalidate the cached auth token.
113
+ * Call this when the user logs out or the token expires.
114
+ *
115
+ * Uses resetQueries() instead of invalidateQueries() to completely remove
116
+ * the cached value, ensuring the next request fetches a fresh token immediately.
117
+ */
118
+ invalidateAuthToken(): Promise<void>;
119
+ }
120
+ /**
121
+ * Factory function to create an ApiPlatformClient.
122
+ *
123
+ * @param options - Configuration options for the client.
124
+ * @returns A new ApiPlatformClient instance.
125
+ */
126
+ export declare function createApiPlatformClient(options: ApiPlatformClientOptions): ApiPlatformClient;
127
+ //# sourceMappingURL=ApiPlatformClient.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiPlatformClient.d.cts","sourceRoot":"","sources":["../../src/api/ApiPlatformClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,WAAW,EAAE,6BAA6B;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAE,6BAA6B;AAGrD,OAAO,EAAE,iBAAiB,EAAE,6BAAmB;AAE/C,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAO3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,2BAAuB;AAC/D,OAAO,EAAE,cAAc,EAAE,0BAAgB;AACzC,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAM3C;;;;;;;;;GASG;AACH,qBAAa,iBAAiB;;IAC5B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;gBAOrB,OAAO,EAAE,wBAAwB;IAiC7C;;;;OAIG;IACH,IAAI,WAAW,IAAI,WAAW,CAE7B;IAED;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS;IAIrE;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI;IAIrE;;;;;OAKG;IACH,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAIvC;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAIpC;;;;;;OAMG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;CAK3C;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,iBAAiB,CAEnB"}
@@ -0,0 +1,127 @@
1
+ /**
2
+ * ApiPlatformClient - MetaMask API Platform Client
3
+ *
4
+ * A comprehensive API client that uses @tanstack/query-core directly for:
5
+ * - Automatic request deduplication
6
+ * - Intelligent caching
7
+ * - Automatic retries with exponential backoff
8
+ *
9
+ * Provides unified access to all MetaMask backend APIs:
10
+ * - Accounts API (accounts.api.cx.metamask.io)
11
+ * - Price API (price.api.cx.metamask.io)
12
+ * - Token API (token.api.cx.metamask.io)
13
+ * - Tokens API (tokens.api.cx.metamask.io)
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const client = new ApiPlatformClient({
18
+ * clientProduct: 'metamask-extension',
19
+ * getBearerToken: async () => token,
20
+ * });
21
+ *
22
+ * // Access API methods through sub-clients
23
+ * const networks = await client.accounts.fetchV2SupportedNetworks();
24
+ * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);
25
+ * const prices = await client.prices.fetchV3SpotPrices(assetIds);
26
+ * const tokenList = await client.token.fetchTokenList(1);
27
+ * const assets = await client.tokens.fetchV3Assets(assetIds);
28
+ *
29
+ * // Cache management
30
+ * await client.invalidateAll(); // Invalidate all caches
31
+ * await client.invalidateAuthToken(); // Invalidate auth token
32
+ * await client.accounts.invalidateBalances(); // Domain-specific via sub-client
33
+ * await client.prices.invalidatePrices(); // Domain-specific via sub-client
34
+ * ```
35
+ */
36
+ import { QueryClient } from "@tanstack/query-core";
37
+ import type { QueryKey } from "@tanstack/query-core";
38
+ import { AccountsApiClient } from "./accounts/index.mjs";
39
+ import { PricesApiClient } from "./prices/index.mjs";
40
+ import type { ApiPlatformClientOptions } from "./shared-types.mjs";
41
+ import { TokenApiClient } from "./token/index.mjs";
42
+ import { TokensApiClient } from "./tokens/index.mjs";
43
+ /**
44
+ * MetaMask API Platform Client with TanStack Query caching.
45
+ * Provides cached access to all MetaMask backend APIs through a unified interface.
46
+ *
47
+ * Access API methods through the sub-clients:
48
+ * - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)
49
+ * - `client.prices` - Prices API (spot prices, exchange rates, historical prices)
50
+ * - `client.token` - Token API (token metadata, trending, top gainers)
51
+ * - `client.tokens` - Tokens API (bulk asset operations, supported networks)
52
+ */
53
+ export declare class ApiPlatformClient {
54
+ #private;
55
+ /**
56
+ * Accounts API client.
57
+ * Provides methods for balances, transactions, relationships, NFTs, and token discovery.
58
+ */
59
+ readonly accounts: AccountsApiClient;
60
+ /**
61
+ * Prices API client.
62
+ * Provides methods for spot prices, exchange rates, and historical prices.
63
+ */
64
+ readonly prices: PricesApiClient;
65
+ /**
66
+ * Token API client.
67
+ * Provides methods for token metadata, networks, trending tokens, and top assets.
68
+ */
69
+ readonly token: TokenApiClient;
70
+ /**
71
+ * Tokens API client.
72
+ * Provides methods for bulk asset operations and supported networks.
73
+ */
74
+ readonly tokens: TokensApiClient;
75
+ constructor(options: ApiPlatformClientOptions);
76
+ /**
77
+ * Get the underlying QueryClient (for advanced usage).
78
+ *
79
+ * @returns The underlying QueryClient instance.
80
+ */
81
+ get queryClient(): QueryClient;
82
+ /**
83
+ * Get cached data for a query key.
84
+ *
85
+ * @param queryKey - The query key to look up.
86
+ * @returns The cached data or undefined.
87
+ */
88
+ getCachedData<CachedData>(queryKey: QueryKey): CachedData | undefined;
89
+ /**
90
+ * Set cached data for a query key.
91
+ *
92
+ * @param queryKey - The query key to set data for.
93
+ * @param data - The data to cache.
94
+ */
95
+ setCachedData<CachedData>(queryKey: QueryKey, data: CachedData): void;
96
+ /**
97
+ * Check if a query is currently fetching.
98
+ *
99
+ * @param queryKey - The query key to check.
100
+ * @returns True if the query is currently fetching.
101
+ */
102
+ isFetching(queryKey: QueryKey): boolean;
103
+ /**
104
+ * Clear all cached data across all sub-clients.
105
+ */
106
+ clear(): void;
107
+ /**
108
+ * Invalidate all queries across all sub-clients.
109
+ */
110
+ invalidateAll(): Promise<void>;
111
+ /**
112
+ * Invalidate the cached auth token.
113
+ * Call this when the user logs out or the token expires.
114
+ *
115
+ * Uses resetQueries() instead of invalidateQueries() to completely remove
116
+ * the cached value, ensuring the next request fetches a fresh token immediately.
117
+ */
118
+ invalidateAuthToken(): Promise<void>;
119
+ }
120
+ /**
121
+ * Factory function to create an ApiPlatformClient.
122
+ *
123
+ * @param options - Configuration options for the client.
124
+ * @returns A new ApiPlatformClient instance.
125
+ */
126
+ export declare function createApiPlatformClient(options: ApiPlatformClientOptions): ApiPlatformClient;
127
+ //# sourceMappingURL=ApiPlatformClient.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiPlatformClient.d.mts","sourceRoot":"","sources":["../../src/api/ApiPlatformClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,EAAE,WAAW,EAAE,6BAA6B;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAE,6BAA6B;AAGrD,OAAO,EAAE,iBAAiB,EAAE,6BAAmB;AAE/C,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAO3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,2BAAuB;AAC/D,OAAO,EAAE,cAAc,EAAE,0BAAgB;AACzC,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAM3C;;;;;;;;;GASG;AACH,qBAAa,iBAAiB;;IAC5B;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,CAAC;IAErC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;gBAOrB,OAAO,EAAE,wBAAwB;IAiC7C;;;;OAIG;IACH,IAAI,WAAW,IAAI,WAAW,CAE7B;IAED;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,GAAG,UAAU,GAAG,SAAS;IAIrE;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI;IAIrE;;;;;OAKG;IACH,UAAU,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAIvC;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAIpC;;;;;;OAMG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;CAK3C;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,iBAAiB,CAEnB"}
@@ -0,0 +1,172 @@
1
+ /**
2
+ * ApiPlatformClient - MetaMask API Platform Client
3
+ *
4
+ * A comprehensive API client that uses @tanstack/query-core directly for:
5
+ * - Automatic request deduplication
6
+ * - Intelligent caching
7
+ * - Automatic retries with exponential backoff
8
+ *
9
+ * Provides unified access to all MetaMask backend APIs:
10
+ * - Accounts API (accounts.api.cx.metamask.io)
11
+ * - Price API (price.api.cx.metamask.io)
12
+ * - Token API (token.api.cx.metamask.io)
13
+ * - Tokens API (tokens.api.cx.metamask.io)
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const client = new ApiPlatformClient({
18
+ * clientProduct: 'metamask-extension',
19
+ * getBearerToken: async () => token,
20
+ * });
21
+ *
22
+ * // Access API methods through sub-clients
23
+ * const networks = await client.accounts.fetchV2SupportedNetworks();
24
+ * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);
25
+ * const prices = await client.prices.fetchV3SpotPrices(assetIds);
26
+ * const tokenList = await client.token.fetchTokenList(1);
27
+ * const assets = await client.tokens.fetchV3Assets(assetIds);
28
+ *
29
+ * // Cache management
30
+ * await client.invalidateAll(); // Invalidate all caches
31
+ * await client.invalidateAuthToken(); // Invalidate auth token
32
+ * await client.accounts.invalidateBalances(); // Domain-specific via sub-client
33
+ * await client.prices.invalidatePrices(); // Domain-specific via sub-client
34
+ * ```
35
+ */
36
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
37
+ if (kind === "m") throw new TypeError("Private method is not writable");
38
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
39
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
40
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
41
+ };
42
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
43
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
44
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
45
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
46
+ };
47
+ var _ApiPlatformClient_sharedQueryClient;
48
+ import { QueryClient } from "@tanstack/query-core";
49
+ // Import API clients from subfolders
50
+ import { AccountsApiClient } from "./accounts/index.mjs";
51
+ import { authQueryKeys } from "./base-client.mjs";
52
+ import { PricesApiClient } from "./prices/index.mjs";
53
+ import { STALE_TIMES, GC_TIMES, shouldRetry, calculateRetryDelay } from "./shared-types.mjs";
54
+ import { TokenApiClient } from "./token/index.mjs";
55
+ import { TokensApiClient } from "./tokens/index.mjs";
56
+ // ============================================================================
57
+ // UNIFIED API CLIENT
58
+ // ============================================================================
59
+ /**
60
+ * MetaMask API Platform Client with TanStack Query caching.
61
+ * Provides cached access to all MetaMask backend APIs through a unified interface.
62
+ *
63
+ * Access API methods through the sub-clients:
64
+ * - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)
65
+ * - `client.prices` - Prices API (spot prices, exchange rates, historical prices)
66
+ * - `client.token` - Token API (token metadata, trending, top gainers)
67
+ * - `client.tokens` - Tokens API (bulk asset operations, supported networks)
68
+ */
69
+ export class ApiPlatformClient {
70
+ constructor(options) {
71
+ /**
72
+ * Shared QueryClient instance used by all sub-clients.
73
+ */
74
+ _ApiPlatformClient_sharedQueryClient.set(this, void 0);
75
+ // Create or use provided QueryClient - shared by all sub-clients
76
+ __classPrivateFieldSet(this, _ApiPlatformClient_sharedQueryClient, options.queryClient ??
77
+ new QueryClient({
78
+ defaultOptions: {
79
+ queries: {
80
+ staleTime: STALE_TIMES.DEFAULT,
81
+ gcTime: GC_TIMES.DEFAULT,
82
+ retry: shouldRetry,
83
+ retryDelay: calculateRetryDelay,
84
+ refetchOnWindowFocus: false,
85
+ networkMode: 'always',
86
+ },
87
+ },
88
+ }), "f");
89
+ // Pass the shared QueryClient to all sub-clients
90
+ const sharedOptions = {
91
+ ...options,
92
+ queryClient: __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f"),
93
+ };
94
+ this.accounts = new AccountsApiClient(sharedOptions);
95
+ this.prices = new PricesApiClient(sharedOptions);
96
+ this.token = new TokenApiClient(sharedOptions);
97
+ this.tokens = new TokensApiClient(sharedOptions);
98
+ }
99
+ // ==========================================================================
100
+ // CACHE MANAGEMENT (operates on shared QueryClient)
101
+ // ==========================================================================
102
+ /**
103
+ * Get the underlying QueryClient (for advanced usage).
104
+ *
105
+ * @returns The underlying QueryClient instance.
106
+ */
107
+ get queryClient() {
108
+ return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f");
109
+ }
110
+ /**
111
+ * Get cached data for a query key.
112
+ *
113
+ * @param queryKey - The query key to look up.
114
+ * @returns The cached data or undefined.
115
+ */
116
+ getCachedData(queryKey) {
117
+ return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").getQueryData(queryKey);
118
+ }
119
+ /**
120
+ * Set cached data for a query key.
121
+ *
122
+ * @param queryKey - The query key to set data for.
123
+ * @param data - The data to cache.
124
+ */
125
+ setCachedData(queryKey, data) {
126
+ __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").setQueryData(queryKey, data);
127
+ }
128
+ /**
129
+ * Check if a query is currently fetching.
130
+ *
131
+ * @param queryKey - The query key to check.
132
+ * @returns True if the query is currently fetching.
133
+ */
134
+ isFetching(queryKey) {
135
+ return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").isFetching({ queryKey }) > 0;
136
+ }
137
+ /**
138
+ * Clear all cached data across all sub-clients.
139
+ */
140
+ clear() {
141
+ __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").clear();
142
+ }
143
+ /**
144
+ * Invalidate all queries across all sub-clients.
145
+ */
146
+ async invalidateAll() {
147
+ await __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").invalidateQueries();
148
+ }
149
+ /**
150
+ * Invalidate the cached auth token.
151
+ * Call this when the user logs out or the token expires.
152
+ *
153
+ * Uses resetQueries() instead of invalidateQueries() to completely remove
154
+ * the cached value, ensuring the next request fetches a fresh token immediately.
155
+ */
156
+ async invalidateAuthToken() {
157
+ await __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").resetQueries({
158
+ queryKey: authQueryKeys.bearerToken(),
159
+ });
160
+ }
161
+ }
162
+ _ApiPlatformClient_sharedQueryClient = new WeakMap();
163
+ /**
164
+ * Factory function to create an ApiPlatformClient.
165
+ *
166
+ * @param options - Configuration options for the client.
167
+ * @returns A new ApiPlatformClient instance.
168
+ */
169
+ export function createApiPlatformClient(options) {
170
+ return new ApiPlatformClient(options);
171
+ }
172
+ //# sourceMappingURL=ApiPlatformClient.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiPlatformClient.mjs","sourceRoot":"","sources":["../../src/api/ApiPlatformClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;;;;;;;;;;;;;AAEH,OAAO,EAAE,WAAW,EAAE,6BAA6B;AAGnD,qCAAqC;AACrC,OAAO,EAAE,iBAAiB,EAAE,6BAAmB;AAC/C,OAAO,EAAE,aAAa,EAAE,0BAAsB;AAC9C,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAC3C,OAAO,EACL,WAAW,EACX,QAAQ,EACR,WAAW,EACX,mBAAmB,EACpB,2BAAuB;AAExB,OAAO,EAAE,cAAc,EAAE,0BAAgB;AACzC,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAE3C,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E;;;;;;;;;GASG;AACH,MAAM,OAAO,iBAAiB;IA8B5B,YAAY,OAAiC;QAL7C;;WAEG;QACM,uDAAgC;QAGvC,iEAAiE;QACjE,uBAAA,IAAI,wCACF,OAAO,CAAC,WAAW;YACnB,IAAI,WAAW,CAAC;gBACd,cAAc,EAAE;oBACd,OAAO,EAAE;wBACP,SAAS,EAAE,WAAW,CAAC,OAAO;wBAC9B,MAAM,EAAE,QAAQ,CAAC,OAAO;wBACxB,KAAK,EAAE,WAAW;wBAClB,UAAU,EAAE,mBAAmB;wBAC/B,oBAAoB,EAAE,KAAK;wBAC3B,WAAW,EAAE,QAAQ;qBACtB;iBACF;aACF,CAAC,MAAA,CAAC;QAEL,iDAAiD;QACjD,MAAM,aAAa,GAA6B;YAC9C,GAAG,OAAO;YACV,WAAW,EAAE,uBAAA,IAAI,4CAAmB;SACrC,CAAC;QAEF,IAAI,CAAC,QAAQ,GAAG,IAAI,iBAAiB,CAAC,aAAa,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;QACjD,IAAI,CAAC,KAAK,GAAG,IAAI,cAAc,CAAC,aAAa,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,aAAa,CAAC,CAAC;IACnD,CAAC;IAED,6EAA6E;IAC7E,oDAAoD;IACpD,6EAA6E;IAE7E;;;;OAIG;IACH,IAAI,WAAW;QACb,OAAO,uBAAA,IAAI,4CAAmB,CAAC;IACjC,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAa,QAAkB;QAC1C,OAAO,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAa,QAAQ,CAAC,CAAC;IACpE,CAAC;IAED;;;;;OAKG;IACH,aAAa,CAAa,QAAkB,EAAE,IAAgB;QAC5D,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IACvD,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,QAAkB;QAC3B,OAAO,uBAAA,IAAI,4CAAmB,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACH,KAAK;QACH,uBAAA,IAAI,4CAAmB,CAAC,KAAK,EAAE,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,aAAa;QACjB,MAAM,uBAAA,IAAI,4CAAmB,CAAC,iBAAiB,EAAE,CAAC;IACpD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,mBAAmB;QACvB,MAAM,uBAAA,IAAI,4CAAmB,CAAC,YAAY,CAAC;YACzC,QAAQ,EAAE,aAAa,CAAC,WAAW,EAAE;SACtC,CAAC,CAAC;IACL,CAAC;CACF;;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAiC;IAEjC,OAAO,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/**\n * ApiPlatformClient - MetaMask API Platform Client\n *\n * A comprehensive API client that uses @tanstack/query-core directly for:\n * - Automatic request deduplication\n * - Intelligent caching\n * - Automatic retries with exponential backoff\n *\n * Provides unified access to all MetaMask backend APIs:\n * - Accounts API (accounts.api.cx.metamask.io)\n * - Price API (price.api.cx.metamask.io)\n * - Token API (token.api.cx.metamask.io)\n * - Tokens API (tokens.api.cx.metamask.io)\n *\n * @example\n * ```typescript\n * const client = new ApiPlatformClient({\n * clientProduct: 'metamask-extension',\n * getBearerToken: async () => token,\n * });\n *\n * // Access API methods through sub-clients\n * const networks = await client.accounts.fetchV2SupportedNetworks();\n * const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);\n * const prices = await client.prices.fetchV3SpotPrices(assetIds);\n * const tokenList = await client.token.fetchTokenList(1);\n * const assets = await client.tokens.fetchV3Assets(assetIds);\n *\n * // Cache management\n * await client.invalidateAll(); // Invalidate all caches\n * await client.invalidateAuthToken(); // Invalidate auth token\n * await client.accounts.invalidateBalances(); // Domain-specific via sub-client\n * await client.prices.invalidatePrices(); // Domain-specific via sub-client\n * ```\n */\n\nimport { QueryClient } from '@tanstack/query-core';\nimport type { QueryKey } from '@tanstack/query-core';\n\n// Import API clients from subfolders\nimport { AccountsApiClient } from './accounts';\nimport { authQueryKeys } from './base-client';\nimport { PricesApiClient } from './prices';\nimport {\n STALE_TIMES,\n GC_TIMES,\n shouldRetry,\n calculateRetryDelay,\n} from './shared-types';\nimport type { ApiPlatformClientOptions } from './shared-types';\nimport { TokenApiClient } from './token';\nimport { TokensApiClient } from './tokens';\n\n// ============================================================================\n// UNIFIED API CLIENT\n// ============================================================================\n\n/**\n * MetaMask API Platform Client with TanStack Query caching.\n * Provides cached access to all MetaMask backend APIs through a unified interface.\n *\n * Access API methods through the sub-clients:\n * - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)\n * - `client.prices` - Prices API (spot prices, exchange rates, historical prices)\n * - `client.token` - Token API (token metadata, trending, top gainers)\n * - `client.tokens` - Tokens API (bulk asset operations, supported networks)\n */\nexport class ApiPlatformClient {\n /**\n * Accounts API client.\n * Provides methods for balances, transactions, relationships, NFTs, and token discovery.\n */\n readonly accounts: AccountsApiClient;\n\n /**\n * Prices API client.\n * Provides methods for spot prices, exchange rates, and historical prices.\n */\n readonly prices: PricesApiClient;\n\n /**\n * Token API client.\n * Provides methods for token metadata, networks, trending tokens, and top assets.\n */\n readonly token: TokenApiClient;\n\n /**\n * Tokens API client.\n * Provides methods for bulk asset operations and supported networks.\n */\n readonly tokens: TokensApiClient;\n\n /**\n * Shared QueryClient instance used by all sub-clients.\n */\n readonly #sharedQueryClient: QueryClient;\n\n constructor(options: ApiPlatformClientOptions) {\n // Create or use provided QueryClient - shared by all sub-clients\n this.#sharedQueryClient =\n options.queryClient ??\n new QueryClient({\n defaultOptions: {\n queries: {\n staleTime: STALE_TIMES.DEFAULT,\n gcTime: GC_TIMES.DEFAULT,\n retry: shouldRetry,\n retryDelay: calculateRetryDelay,\n refetchOnWindowFocus: false,\n networkMode: 'always',\n },\n },\n });\n\n // Pass the shared QueryClient to all sub-clients\n const sharedOptions: ApiPlatformClientOptions = {\n ...options,\n queryClient: this.#sharedQueryClient,\n };\n\n this.accounts = new AccountsApiClient(sharedOptions);\n this.prices = new PricesApiClient(sharedOptions);\n this.token = new TokenApiClient(sharedOptions);\n this.tokens = new TokensApiClient(sharedOptions);\n }\n\n // ==========================================================================\n // CACHE MANAGEMENT (operates on shared QueryClient)\n // ==========================================================================\n\n /**\n * Get the underlying QueryClient (for advanced usage).\n *\n * @returns The underlying QueryClient instance.\n */\n get queryClient(): QueryClient {\n return this.#sharedQueryClient;\n }\n\n /**\n * Get cached data for a query key.\n *\n * @param queryKey - The query key to look up.\n * @returns The cached data or undefined.\n */\n getCachedData<CachedData>(queryKey: QueryKey): CachedData | undefined {\n return this.#sharedQueryClient.getQueryData<CachedData>(queryKey);\n }\n\n /**\n * Set cached data for a query key.\n *\n * @param queryKey - The query key to set data for.\n * @param data - The data to cache.\n */\n setCachedData<CachedData>(queryKey: QueryKey, data: CachedData): void {\n this.#sharedQueryClient.setQueryData(queryKey, data);\n }\n\n /**\n * Check if a query is currently fetching.\n *\n * @param queryKey - The query key to check.\n * @returns True if the query is currently fetching.\n */\n isFetching(queryKey: QueryKey): boolean {\n return this.#sharedQueryClient.isFetching({ queryKey }) > 0;\n }\n\n /**\n * Clear all cached data across all sub-clients.\n */\n clear(): void {\n this.#sharedQueryClient.clear();\n }\n\n /**\n * Invalidate all queries across all sub-clients.\n */\n async invalidateAll(): Promise<void> {\n await this.#sharedQueryClient.invalidateQueries();\n }\n\n /**\n * Invalidate the cached auth token.\n * Call this when the user logs out or the token expires.\n *\n * Uses resetQueries() instead of invalidateQueries() to completely remove\n * the cached value, ensuring the next request fetches a fresh token immediately.\n */\n async invalidateAuthToken(): Promise<void> {\n await this.#sharedQueryClient.resetQueries({\n queryKey: authQueryKeys.bearerToken(),\n });\n }\n}\n\n/**\n * Factory function to create an ApiPlatformClient.\n *\n * @param options - Configuration options for the client.\n * @returns A new ApiPlatformClient instance.\n */\nexport function createApiPlatformClient(\n options: ApiPlatformClientOptions,\n): ApiPlatformClient {\n return new ApiPlatformClient(options);\n}\n"]}