@metamask-previews/core-backend 5.0.0-preview-fa81dffb → 5.0.0-preview-ab102d86
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.
- package/CHANGELOG.md +9 -0
- package/README.md +252 -1
- package/dist/api/ApiPlatformClient.cjs +173 -0
- package/dist/api/ApiPlatformClient.cjs.map +1 -0
- package/dist/api/ApiPlatformClient.d.cts +124 -0
- package/dist/api/ApiPlatformClient.d.cts.map +1 -0
- package/dist/api/ApiPlatformClient.d.mts +124 -0
- package/dist/api/ApiPlatformClient.d.mts.map +1 -0
- package/dist/api/ApiPlatformClient.mjs +168 -0
- package/dist/api/ApiPlatformClient.mjs.map +1 -0
- package/dist/api/accounts/client.cjs +492 -0
- package/dist/api/accounts/client.cjs.map +1 -0
- package/dist/api/accounts/client.d.cts +213 -0
- package/dist/api/accounts/client.d.cts.map +1 -0
- package/dist/api/accounts/client.d.mts +213 -0
- package/dist/api/accounts/client.d.mts.map +1 -0
- package/dist/api/accounts/client.mjs +488 -0
- package/dist/api/accounts/client.mjs.map +1 -0
- package/dist/api/accounts/index.cjs +9 -0
- package/dist/api/accounts/index.cjs.map +1 -0
- package/dist/api/accounts/index.d.cts +6 -0
- package/dist/api/accounts/index.d.cts.map +1 -0
- package/dist/api/accounts/index.d.mts +6 -0
- package/dist/api/accounts/index.d.mts.map +1 -0
- package/dist/api/accounts/index.mjs +5 -0
- package/dist/api/accounts/index.mjs.map +1 -0
- package/dist/api/accounts/types.cjs +7 -0
- package/dist/api/accounts/types.cjs.map +1 -0
- package/dist/api/accounts/types.d.cts +195 -0
- package/dist/api/accounts/types.d.cts.map +1 -0
- package/dist/api/accounts/types.d.mts +195 -0
- package/dist/api/accounts/types.d.mts.map +1 -0
- package/dist/api/accounts/types.mjs +6 -0
- package/dist/api/accounts/types.mjs.map +1 -0
- package/dist/api/base-client.cjs +121 -0
- package/dist/api/base-client.cjs.map +1 -0
- package/dist/api/base-client.d.cts +43 -0
- package/dist/api/base-client.d.cts.map +1 -0
- package/dist/api/base-client.d.mts +43 -0
- package/dist/api/base-client.d.mts.map +1 -0
- package/dist/api/base-client.mjs +115 -0
- package/dist/api/base-client.mjs.map +1 -0
- package/dist/api/index.cjs +35 -0
- package/dist/api/index.cjs.map +1 -0
- package/dist/api/index.d.cts +18 -0
- package/dist/api/index.d.cts.map +1 -0
- package/dist/api/index.d.mts +18 -0
- package/dist/api/index.d.mts.map +1 -0
- package/dist/api/index.mjs +18 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/api/prices/client.cjs +521 -0
- package/dist/api/prices/client.cjs.map +1 -0
- package/dist/api/prices/client.d.cts +235 -0
- package/dist/api/prices/client.d.cts.map +1 -0
- package/dist/api/prices/client.d.mts +235 -0
- package/dist/api/prices/client.d.mts.map +1 -0
- package/dist/api/prices/client.mjs +517 -0
- package/dist/api/prices/client.mjs.map +1 -0
- package/dist/api/prices/index.cjs +9 -0
- package/dist/api/prices/index.cjs.map +1 -0
- package/dist/api/prices/index.d.cts +6 -0
- package/dist/api/prices/index.d.cts.map +1 -0
- package/dist/api/prices/index.d.mts +6 -0
- package/dist/api/prices/index.d.mts.map +1 -0
- package/dist/api/prices/index.mjs +5 -0
- package/dist/api/prices/index.mjs.map +1 -0
- package/dist/api/prices/types.cjs +7 -0
- package/dist/api/prices/types.cjs.map +1 -0
- package/dist/api/prices/types.d.cts +61 -0
- package/dist/api/prices/types.d.cts.map +1 -0
- package/dist/api/prices/types.d.mts +61 -0
- package/dist/api/prices/types.d.mts.map +1 -0
- package/dist/api/prices/types.mjs +6 -0
- package/dist/api/prices/types.mjs.map +1 -0
- package/dist/api/shared-types.cjs +99 -0
- package/dist/api/shared-types.cjs.map +1 -0
- package/dist/api/shared-types.d.cts +134 -0
- package/dist/api/shared-types.d.cts.map +1 -0
- package/dist/api/shared-types.d.mts +134 -0
- package/dist/api/shared-types.d.mts.map +1 -0
- package/dist/api/shared-types.mjs +93 -0
- package/dist/api/shared-types.mjs.map +1 -0
- package/dist/api/test-utils.cjs +70 -0
- package/dist/api/test-utils.cjs.map +1 -0
- package/dist/api/test-utils.d.cts +30 -0
- package/dist/api/test-utils.d.cts.map +1 -0
- package/dist/api/test-utils.d.mts +30 -0
- package/dist/api/test-utils.d.mts.map +1 -0
- package/dist/api/test-utils.mjs +64 -0
- package/dist/api/test-utils.mjs.map +1 -0
- package/dist/api/token/client.cjs +342 -0
- package/dist/api/token/client.cjs.map +1 -0
- package/dist/api/token/client.d.cts +193 -0
- package/dist/api/token/client.d.cts.map +1 -0
- package/dist/api/token/client.d.mts +193 -0
- package/dist/api/token/client.d.mts.map +1 -0
- package/dist/api/token/client.mjs +338 -0
- package/dist/api/token/client.mjs.map +1 -0
- package/dist/api/token/index.cjs +9 -0
- package/dist/api/token/index.cjs.map +1 -0
- package/dist/api/token/index.d.cts +6 -0
- package/dist/api/token/index.d.cts.map +1 -0
- package/dist/api/token/index.d.mts +6 -0
- package/dist/api/token/index.d.mts.map +1 -0
- package/dist/api/token/index.mjs +5 -0
- package/dist/api/token/index.mjs.map +1 -0
- package/dist/api/token/types.cjs +7 -0
- package/dist/api/token/types.cjs.map +1 -0
- package/dist/api/token/types.d.cts +75 -0
- package/dist/api/token/types.d.cts.map +1 -0
- package/dist/api/token/types.d.mts +75 -0
- package/dist/api/token/types.d.mts.map +1 -0
- package/dist/api/token/types.mjs +6 -0
- package/dist/api/token/types.mjs.map +1 -0
- package/dist/api/tokens/client.cjs +83 -0
- package/dist/api/tokens/client.cjs.map +1 -0
- package/dist/api/tokens/client.d.cts +44 -0
- package/dist/api/tokens/client.d.cts.map +1 -0
- package/dist/api/tokens/client.d.mts +44 -0
- package/dist/api/tokens/client.d.mts.map +1 -0
- package/dist/api/tokens/client.mjs +79 -0
- package/dist/api/tokens/client.mjs.map +1 -0
- package/dist/api/tokens/index.cjs +9 -0
- package/dist/api/tokens/index.cjs.map +1 -0
- package/dist/api/tokens/index.d.cts +6 -0
- package/dist/api/tokens/index.d.cts.map +1 -0
- package/dist/api/tokens/index.d.mts +6 -0
- package/dist/api/tokens/index.d.mts.map +1 -0
- package/dist/api/tokens/index.mjs +5 -0
- package/dist/api/tokens/index.mjs.map +1 -0
- package/dist/api/tokens/types.cjs +7 -0
- package/dist/api/tokens/types.cjs.map +1 -0
- package/dist/api/tokens/types.d.cts +28 -0
- package/dist/api/tokens/types.d.cts.map +1 -0
- package/dist/api/tokens/types.d.mts +28 -0
- package/dist/api/tokens/types.d.mts.map +1 -0
- package/dist/api/tokens/types.mjs +6 -0
- package/dist/api/tokens/types.mjs.map +1 -0
- package/dist/index.cjs +33 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +21 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add `ApiPlatformClient` for unified access to MetaMask backend APIs with TanStack Query caching ([#7658](https://github.com/MetaMask/core/pull/7658))
|
|
13
|
+
- Automatic request deduplication and intelligent caching
|
|
14
|
+
- Automatic retries with exponential backoff for transient failures
|
|
15
|
+
- Support for Accounts API, Price API, Token API, and Tokens API endpoints
|
|
16
|
+
- Export helper functions `shouldRetry` and `calculateRetryDelay` for custom retry logic
|
|
17
|
+
- Export API types for external consumers
|
|
18
|
+
|
|
10
19
|
### Changed
|
|
11
20
|
|
|
12
21
|
- Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
|
package/README.md
CHANGED
|
@@ -19,6 +19,17 @@ Core backend services for MetaMask, serving as the data layer between Backend se
|
|
|
19
19
|
- [WebSocket Connection Management](#websocket-connection-management)
|
|
20
20
|
- [Connection Requirements](#connection-requirements)
|
|
21
21
|
- [Connection Behavior](#connection-behavior)
|
|
22
|
+
- [HTTP API](#http-api)
|
|
23
|
+
- [Overview](#overview)
|
|
24
|
+
- [Features](#features)
|
|
25
|
+
- [Quick Start](#quick-start-1)
|
|
26
|
+
- [API Clients](#api-clients)
|
|
27
|
+
- [AccountsApiClient](#accountsapiclient)
|
|
28
|
+
- [PricesApiClient](#pricesapiclient)
|
|
29
|
+
- [TokenApiClient](#tokenapiclient)
|
|
30
|
+
- [TokensApiClient](#tokensapiclient)
|
|
31
|
+
- [Configuration](#configuration)
|
|
32
|
+
- [Cache Management](#cache-management)
|
|
22
33
|
- [API Reference](#api-reference)
|
|
23
34
|
- [BackendWebSocketService](#backendwebsocketservice)
|
|
24
35
|
- [Constructor Options](#constructor-options)
|
|
@@ -44,6 +55,8 @@ npm install @metamask/core-backend
|
|
|
44
55
|
|
|
45
56
|
### Basic Usage
|
|
46
57
|
|
|
58
|
+
**WebSocket for Real-time Updates:**
|
|
59
|
+
|
|
47
60
|
```typescript
|
|
48
61
|
import {
|
|
49
62
|
BackendWebSocketService,
|
|
@@ -82,6 +95,28 @@ messenger.subscribe(
|
|
|
82
95
|
);
|
|
83
96
|
```
|
|
84
97
|
|
|
98
|
+
**HTTP API for REST Requests:**
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { ApiPlatformClient } from '@metamask/core-backend';
|
|
102
|
+
|
|
103
|
+
// Create API client
|
|
104
|
+
const apiClient = new ApiPlatformClient({
|
|
105
|
+
clientProduct: 'metamask-extension',
|
|
106
|
+
getBearerToken: async () => authController.getBearerToken(),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Fetch data with automatic caching and deduplication
|
|
110
|
+
const balances = await apiClient.accounts.fetchV5MultiAccountBalances([
|
|
111
|
+
'eip155:1:0x742d35cc6634c0532925a3b8d40c4e0e2c6e4e6',
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
const prices = await apiClient.prices.fetchV3SpotPrices([
|
|
115
|
+
'eip155:1/slip44:60', // ETH
|
|
116
|
+
'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC
|
|
117
|
+
]);
|
|
118
|
+
```
|
|
119
|
+
|
|
85
120
|
### Integration with Controllers
|
|
86
121
|
|
|
87
122
|
```typescript
|
|
@@ -147,7 +182,7 @@ graph TD
|
|
|
147
182
|
|
|
148
183
|
subgraph "Transport Layer"
|
|
149
184
|
WSS[WebSocketService<br/>• Connection management<br/>• Automatic reconnection<br/>• Message routing<br/>• Subscription management]
|
|
150
|
-
HTTP[HTTP
|
|
185
|
+
HTTP[HTTP API Clients<br/>• REST API calls<br/>• Automatic caching<br/>• Request deduplication<br/>• Retry with backoff]
|
|
151
186
|
end
|
|
152
187
|
end
|
|
153
188
|
end
|
|
@@ -344,6 +379,222 @@ The WebSocket connects when **ALL 3 conditions are true**:
|
|
|
344
379
|
- ✅ **Unexpected disconnects** (network issues, server restart) → Auto-reconnect
|
|
345
380
|
- ❌ **Manual disconnects** (app backgrounds, wallet locks, user signs out) → Stay disconnected
|
|
346
381
|
|
|
382
|
+
## HTTP API
|
|
383
|
+
|
|
384
|
+
### Overview
|
|
385
|
+
|
|
386
|
+
The HTTP API provides type-safe clients for accessing MetaMask backend REST APIs. It uses `@tanstack/query-core` for intelligent caching, request deduplication, and automatic retries.
|
|
387
|
+
|
|
388
|
+
**Available APIs:**
|
|
389
|
+
|
|
390
|
+
| API | Base URL | Purpose |
|
|
391
|
+
| ------------ | ----------------------------- | ---------------------------------------------- |
|
|
392
|
+
| **Accounts** | `accounts.api.cx.metamask.io` | Balances, transactions, NFTs, token discovery |
|
|
393
|
+
| **Prices** | `price.api.cx.metamask.io` | Spot prices, exchange rates, historical prices |
|
|
394
|
+
| **Token** | `token.api.cx.metamask.io` | Token metadata, trending, top gainers |
|
|
395
|
+
| **Tokens** | `tokens.api.cx.metamask.io` | Bulk asset operations, supported networks |
|
|
396
|
+
|
|
397
|
+
### Features
|
|
398
|
+
|
|
399
|
+
- ✅ **Automatic request deduplication** - Identical concurrent requests share a single network call
|
|
400
|
+
- ✅ **Intelligent caching** - Configurable stale times per data type (prices: 30s, balances: 1min, networks: 30min)
|
|
401
|
+
- ✅ **Automatic retries** - Exponential backoff with jitter, skips 4xx errors (except 429, 408)
|
|
402
|
+
- ✅ **Type safety** - Full TypeScript support with response types
|
|
403
|
+
- ✅ **Bearer token caching** - Auth tokens cached for 5 minutes
|
|
404
|
+
- ✅ **Unified client** - Single entry point or individual API clients
|
|
405
|
+
|
|
406
|
+
### Quick Start
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
import {
|
|
410
|
+
ApiPlatformClient,
|
|
411
|
+
createApiPlatformClient,
|
|
412
|
+
} from '@metamask/core-backend';
|
|
413
|
+
|
|
414
|
+
// Create unified client
|
|
415
|
+
const client = new ApiPlatformClient({
|
|
416
|
+
clientProduct: 'metamask-extension',
|
|
417
|
+
clientVersion: '12.0.0',
|
|
418
|
+
getBearerToken: async () => authController.getBearerToken(),
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// Access API methods through sub-clients
|
|
422
|
+
const networks = await client.accounts.fetchV2SupportedNetworks();
|
|
423
|
+
const balances = await client.accounts.fetchV5MultiAccountBalances([
|
|
424
|
+
'eip155:1:0x742d35cc6634c0532925a3b8d40c4e0e2c6e4e6',
|
|
425
|
+
]);
|
|
426
|
+
const prices = await client.prices.fetchV3SpotPrices([
|
|
427
|
+
'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
|
|
428
|
+
]);
|
|
429
|
+
const tokenList = await client.token.fetchTokenList(1);
|
|
430
|
+
const assets = await client.tokens.fetchV3Assets([
|
|
431
|
+
'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
|
|
432
|
+
]);
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
Or use individual clients:
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
import { AccountsApiClient, PricesApiClient } from '@metamask/core-backend';
|
|
439
|
+
|
|
440
|
+
const accountsClient = new AccountsApiClient({
|
|
441
|
+
clientProduct: 'metamask-extension',
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
const pricesClient = new PricesApiClient({
|
|
445
|
+
clientProduct: 'metamask-extension',
|
|
446
|
+
getBearerToken: async () => token,
|
|
447
|
+
});
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### API Clients
|
|
451
|
+
|
|
452
|
+
#### AccountsApiClient
|
|
453
|
+
|
|
454
|
+
Handles account-related operations including balances, transactions, NFTs, and token discovery.
|
|
455
|
+
|
|
456
|
+
| Method | Description |
|
|
457
|
+
| ------------------------------------------------------- | ------------------------------------------ |
|
|
458
|
+
| `fetchV1SupportedNetworks()` | Get supported networks (v1) |
|
|
459
|
+
| `fetchV2SupportedNetworks()` | Get supported networks (v2) |
|
|
460
|
+
| `fetchV2ActiveNetworks(accountIds, options?)` | Get active networks by CAIP-10 account IDs |
|
|
461
|
+
| `fetchV2Balances(address, options?)` | Get balances for single address |
|
|
462
|
+
| `fetchV2BalancesWithOptions(address, options?)` | Get balances with filters |
|
|
463
|
+
| `fetchV4MultiAccountBalances(addresses, options?)` | Get balances for multiple addresses |
|
|
464
|
+
| `fetchV5MultiAccountBalances(accountIds, options?)` | Get balances using CAIP-10 IDs |
|
|
465
|
+
| `fetchV1TransactionByHash(chainId, txHash, options?)` | Get transaction by hash |
|
|
466
|
+
| `fetchV1AccountTransactions(address, options?)` | Get account transactions |
|
|
467
|
+
| `fetchV4MultiAccountTransactions(accountIds, options?)` | Get multi-account transactions |
|
|
468
|
+
| `fetchV1AccountRelationship(chainId, from, to)` | Get address relationship |
|
|
469
|
+
| `fetchV2AccountNfts(address, options?)` | Get account NFTs |
|
|
470
|
+
| `fetchV2AccountTokens(address, options?)` | Get detected ERC20 tokens |
|
|
471
|
+
| `invalidateBalances()` | Invalidate all balance cache |
|
|
472
|
+
| `invalidateAccounts()` | Invalidate all account cache |
|
|
473
|
+
|
|
474
|
+
#### PricesApiClient
|
|
475
|
+
|
|
476
|
+
Handles price-related operations including spot prices, exchange rates, and historical data.
|
|
477
|
+
|
|
478
|
+
| Method | Description |
|
|
479
|
+
| ----------------------------------------------------------------------- | ------------------------------------------------ |
|
|
480
|
+
| `fetchPriceV1SupportedNetworks()` | Get price-supported networks (v1) |
|
|
481
|
+
| `fetchPriceV2SupportedNetworks()` | Get price-supported networks in CAIP format (v2) |
|
|
482
|
+
| `fetchV1ExchangeRates(baseCurrency)` | Get exchange rates for base currency |
|
|
483
|
+
| `fetchV1FiatExchangeRates()` | Get fiat exchange rates |
|
|
484
|
+
| `fetchV1CryptoExchangeRates()` | Get crypto exchange rates |
|
|
485
|
+
| `fetchV1SpotPricesByCoinIds(coinIds)` | Get spot prices by CoinGecko IDs |
|
|
486
|
+
| `fetchV1SpotPriceByCoinId(coinId, currency?)` | Get single coin spot price |
|
|
487
|
+
| `fetchV1TokenPrices(chainId, addresses, options?)` | Get token prices on chain |
|
|
488
|
+
| `fetchV1TokenPrice(chainId, address, currency?)` | Get single token price |
|
|
489
|
+
| `fetchV2SpotPrices(chainId, addresses, options?)` | Get spot prices with market data |
|
|
490
|
+
| `fetchV3SpotPrices(assetIds, options?)` | Get spot prices by CAIP-19 asset IDs |
|
|
491
|
+
| `fetchV1HistoricalPricesByCoinId(coinId, options?)` | Get historical prices by CoinGecko ID |
|
|
492
|
+
| `fetchV1HistoricalPricesByTokenAddresses(chainId, addresses, options?)` | Get historical prices for tokens |
|
|
493
|
+
| `fetchV1HistoricalPrices(chainId, address, options?)` | Get historical prices for single token |
|
|
494
|
+
| `fetchV3HistoricalPrices(chainId, assetType, options?)` | Get historical prices by CAIP-19 |
|
|
495
|
+
| `fetchV1HistoricalPriceGraphByCoinId(coinId, options?)` | Get price graph by CoinGecko ID |
|
|
496
|
+
| `fetchV1HistoricalPriceGraphByTokenAddress(chainId, address, options?)` | Get price graph by token address |
|
|
497
|
+
| `invalidatePrices()` | Invalidate all price cache |
|
|
498
|
+
|
|
499
|
+
#### TokenApiClient
|
|
500
|
+
|
|
501
|
+
Handles token metadata, lists, and trending/popular token discovery.
|
|
502
|
+
|
|
503
|
+
| Method | Description |
|
|
504
|
+
| -------------------------------------------------- | ------------------------------- |
|
|
505
|
+
| `fetchNetworks()` | Get all networks |
|
|
506
|
+
| `fetchNetworkByChainId(chainId)` | Get network by chain ID |
|
|
507
|
+
| `fetchTokenList(chainId, options?)` | Get token list for chain |
|
|
508
|
+
| `fetchV1TokenMetadata(chainId, address, options?)` | Get token metadata |
|
|
509
|
+
| `fetchTokenDescription(chainId, address)` | Get token description |
|
|
510
|
+
| `fetchV3TrendingTokens(chainIds, options?)` | Get trending tokens |
|
|
511
|
+
| `fetchV3TopGainers(chainIds, options?)` | Get top gainers/losers |
|
|
512
|
+
| `fetchV3PopularTokens(chainIds, options?)` | Get popular tokens |
|
|
513
|
+
| `fetchTopAssets(chainId)` | Get top assets for chain |
|
|
514
|
+
| `fetchV1SuggestedOccurrenceFloors()` | Get suggested occurrence floors |
|
|
515
|
+
|
|
516
|
+
#### TokensApiClient
|
|
517
|
+
|
|
518
|
+
Handles bulk token operations and supported network queries.
|
|
519
|
+
|
|
520
|
+
| Method | Description |
|
|
521
|
+
| --------------------------------- | ----------------------------------------------------------- |
|
|
522
|
+
| `fetchTokenV1SupportedNetworks()` | Get token-supported networks (v1) |
|
|
523
|
+
| `fetchTokenV2SupportedNetworks()` | Get token-supported networks with full/partial support (v2) |
|
|
524
|
+
| `fetchV3Assets(assetIds)` | Fetch assets by CAIP-19 IDs |
|
|
525
|
+
| `invalidateTokens()` | Invalidate all token cache |
|
|
526
|
+
|
|
527
|
+
### Configuration
|
|
528
|
+
|
|
529
|
+
```typescript
|
|
530
|
+
type ApiPlatformClientOptions = {
|
|
531
|
+
/** Client product identifier (e.g., 'metamask-extension', 'metamask-mobile') */
|
|
532
|
+
clientProduct: string;
|
|
533
|
+
/** Optional client version (default: '1.0.0') */
|
|
534
|
+
clientVersion?: string;
|
|
535
|
+
/** Function to get bearer token for authenticated requests */
|
|
536
|
+
getBearerToken?: () => Promise<string | undefined>;
|
|
537
|
+
/** Optional custom QueryClient instance for shared caching */
|
|
538
|
+
queryClient?: QueryClient;
|
|
539
|
+
};
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
**Default Stale Times:**
|
|
543
|
+
|
|
544
|
+
| Data Type | Stale Time |
|
|
545
|
+
| ------------------ | ---------- |
|
|
546
|
+
| Prices | 30 seconds |
|
|
547
|
+
| Balances | 1 minute |
|
|
548
|
+
| Transactions | 30 seconds |
|
|
549
|
+
| Networks | 10 minutes |
|
|
550
|
+
| Supported Networks | 30 minutes |
|
|
551
|
+
| Token Metadata | 5 minutes |
|
|
552
|
+
| Token List | 10 minutes |
|
|
553
|
+
| Exchange Rates | 5 minutes |
|
|
554
|
+
| Trending | 2 minutes |
|
|
555
|
+
| Auth Token | 5 minutes |
|
|
556
|
+
|
|
557
|
+
**Override Stale Time:**
|
|
558
|
+
|
|
559
|
+
```typescript
|
|
560
|
+
// Use custom stale time for specific request
|
|
561
|
+
const balances = await client.accounts.fetchV5MultiAccountBalances(
|
|
562
|
+
accountIds,
|
|
563
|
+
{ networks: ['eip155:1'] },
|
|
564
|
+
{ staleTime: 10000 }, // 10 seconds
|
|
565
|
+
);
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
### Cache Management
|
|
569
|
+
|
|
570
|
+
```typescript
|
|
571
|
+
// Invalidate all caches
|
|
572
|
+
await client.invalidateAll();
|
|
573
|
+
|
|
574
|
+
// Invalidate auth token (on logout)
|
|
575
|
+
await client.invalidateAuthToken();
|
|
576
|
+
|
|
577
|
+
// Domain-specific invalidation
|
|
578
|
+
await client.accounts.invalidateBalances();
|
|
579
|
+
await client.prices.invalidatePrices();
|
|
580
|
+
await client.tokens.invalidateTokens();
|
|
581
|
+
|
|
582
|
+
// Clear all cached data
|
|
583
|
+
client.clear();
|
|
584
|
+
|
|
585
|
+
// Check if query is fetching
|
|
586
|
+
const isFetching = client.isFetching(['accounts', 'balances']);
|
|
587
|
+
|
|
588
|
+
// Access cached data directly
|
|
589
|
+
const cached = client.getCachedData(['accounts', 'balances', 'v5', { ... }]);
|
|
590
|
+
|
|
591
|
+
// Set cached data
|
|
592
|
+
client.setCachedData(queryKey, data);
|
|
593
|
+
|
|
594
|
+
// Access underlying QueryClient for advanced usage
|
|
595
|
+
const queryClient = client.queryClient;
|
|
596
|
+
```
|
|
597
|
+
|
|
347
598
|
## API Reference
|
|
348
599
|
|
|
349
600
|
### BackendWebSocketService
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ApiPlatformClient - MetaMask API Platform Client
|
|
4
|
+
*
|
|
5
|
+
* A comprehensive API client that uses @tanstack/query-core directly for:
|
|
6
|
+
* - Automatic request deduplication
|
|
7
|
+
* - Intelligent caching
|
|
8
|
+
* - Automatic retries with exponential backoff
|
|
9
|
+
*
|
|
10
|
+
* Provides unified access to all MetaMask backend APIs:
|
|
11
|
+
* - Accounts API (accounts.api.cx.metamask.io)
|
|
12
|
+
* - Price API (price.api.cx.metamask.io)
|
|
13
|
+
* - Token API (token.api.cx.metamask.io)
|
|
14
|
+
* - Tokens API (tokens.api.cx.metamask.io)
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const client = new ApiPlatformClient({
|
|
19
|
+
* clientProduct: 'metamask-extension',
|
|
20
|
+
* getBearerToken: async () => token,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Access API methods through sub-clients
|
|
24
|
+
* const networks = await client.accounts.fetchV2SupportedNetworks();
|
|
25
|
+
* const balances = await client.accounts.fetchV5MultiAccountBalances(accountIds);
|
|
26
|
+
* const prices = await client.prices.fetchV3SpotPrices(assetIds);
|
|
27
|
+
* const tokenList = await client.token.fetchTokenList(1);
|
|
28
|
+
* const assets = await client.tokens.fetchV3Assets(assetIds);
|
|
29
|
+
*
|
|
30
|
+
* // Cache management
|
|
31
|
+
* await client.invalidateAll(); // Invalidate all caches
|
|
32
|
+
* await client.invalidateAuthToken(); // Invalidate auth token
|
|
33
|
+
* await client.accounts.invalidateBalances(); // Domain-specific via sub-client
|
|
34
|
+
* await client.prices.invalidatePrices(); // Domain-specific via sub-client
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
38
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
39
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
40
|
+
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");
|
|
41
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
42
|
+
};
|
|
43
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
44
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
45
|
+
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");
|
|
46
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
47
|
+
};
|
|
48
|
+
var _ApiPlatformClient_sharedQueryClient;
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.createApiPlatformClient = exports.ApiPlatformClient = void 0;
|
|
51
|
+
const query_core_1 = require("@tanstack/query-core");
|
|
52
|
+
// Import API clients from subfolders
|
|
53
|
+
const accounts_1 = require("./accounts/index.cjs");
|
|
54
|
+
const prices_1 = require("./prices/index.cjs");
|
|
55
|
+
const shared_types_1 = require("./shared-types.cjs");
|
|
56
|
+
const token_1 = require("./token/index.cjs");
|
|
57
|
+
const tokens_1 = require("./tokens/index.cjs");
|
|
58
|
+
// ============================================================================
|
|
59
|
+
// UNIFIED API CLIENT
|
|
60
|
+
// ============================================================================
|
|
61
|
+
/**
|
|
62
|
+
* MetaMask API Platform Client with TanStack Query caching.
|
|
63
|
+
* Provides cached access to all MetaMask backend APIs through a unified interface.
|
|
64
|
+
*
|
|
65
|
+
* Access API methods through the sub-clients:
|
|
66
|
+
* - `client.accounts` - Accounts API (balances, transactions, NFTs, etc.)
|
|
67
|
+
* - `client.prices` - Prices API (spot prices, exchange rates, historical prices)
|
|
68
|
+
* - `client.token` - Token API (token metadata, trending, top gainers)
|
|
69
|
+
* - `client.tokens` - Tokens API (bulk asset operations, supported networks)
|
|
70
|
+
*/
|
|
71
|
+
class ApiPlatformClient {
|
|
72
|
+
constructor(options) {
|
|
73
|
+
/**
|
|
74
|
+
* Shared QueryClient instance used by all sub-clients.
|
|
75
|
+
*/
|
|
76
|
+
_ApiPlatformClient_sharedQueryClient.set(this, void 0);
|
|
77
|
+
// Create or use provided QueryClient - shared by all sub-clients
|
|
78
|
+
__classPrivateFieldSet(this, _ApiPlatformClient_sharedQueryClient, options.queryClient ??
|
|
79
|
+
new query_core_1.QueryClient({
|
|
80
|
+
defaultOptions: {
|
|
81
|
+
queries: {
|
|
82
|
+
staleTime: shared_types_1.STALE_TIMES.DEFAULT,
|
|
83
|
+
gcTime: shared_types_1.GC_TIMES.DEFAULT,
|
|
84
|
+
retry: shared_types_1.shouldRetry,
|
|
85
|
+
retryDelay: shared_types_1.calculateRetryDelay,
|
|
86
|
+
refetchOnWindowFocus: false,
|
|
87
|
+
networkMode: 'always',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
}), "f");
|
|
91
|
+
// Pass the shared QueryClient to all sub-clients
|
|
92
|
+
const sharedOptions = {
|
|
93
|
+
...options,
|
|
94
|
+
queryClient: __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f"),
|
|
95
|
+
};
|
|
96
|
+
this.accounts = new accounts_1.AccountsApiClient(sharedOptions);
|
|
97
|
+
this.prices = new prices_1.PricesApiClient(sharedOptions);
|
|
98
|
+
this.token = new token_1.TokenApiClient(sharedOptions);
|
|
99
|
+
this.tokens = new tokens_1.TokensApiClient(sharedOptions);
|
|
100
|
+
}
|
|
101
|
+
// ==========================================================================
|
|
102
|
+
// CACHE MANAGEMENT (operates on shared QueryClient)
|
|
103
|
+
// ==========================================================================
|
|
104
|
+
/**
|
|
105
|
+
* Get the underlying QueryClient (for advanced usage).
|
|
106
|
+
*
|
|
107
|
+
* @returns The underlying QueryClient instance.
|
|
108
|
+
*/
|
|
109
|
+
get queryClient() {
|
|
110
|
+
return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f");
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get cached data for a query key.
|
|
114
|
+
*
|
|
115
|
+
* @param queryKey - The query key to look up.
|
|
116
|
+
* @returns The cached data or undefined.
|
|
117
|
+
*/
|
|
118
|
+
getCachedData(queryKey) {
|
|
119
|
+
return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").getQueryData(queryKey);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Set cached data for a query key.
|
|
123
|
+
*
|
|
124
|
+
* @param queryKey - The query key to set data for.
|
|
125
|
+
* @param data - The data to cache.
|
|
126
|
+
*/
|
|
127
|
+
setCachedData(queryKey, data) {
|
|
128
|
+
__classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").setQueryData(queryKey, data);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Check if a query is currently fetching.
|
|
132
|
+
*
|
|
133
|
+
* @param queryKey - The query key to check.
|
|
134
|
+
* @returns True if the query is currently fetching.
|
|
135
|
+
*/
|
|
136
|
+
isFetching(queryKey) {
|
|
137
|
+
return __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").isFetching({ queryKey }) > 0;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Clear all cached data across all sub-clients.
|
|
141
|
+
*/
|
|
142
|
+
clear() {
|
|
143
|
+
__classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").clear();
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Invalidate all queries across all sub-clients.
|
|
147
|
+
*/
|
|
148
|
+
async invalidateAll() {
|
|
149
|
+
await __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").invalidateQueries();
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Invalidate the cached auth token.
|
|
153
|
+
* Call this when the user logs out or the token expires.
|
|
154
|
+
*/
|
|
155
|
+
async invalidateAuthToken() {
|
|
156
|
+
await __classPrivateFieldGet(this, _ApiPlatformClient_sharedQueryClient, "f").invalidateQueries({
|
|
157
|
+
queryKey: shared_types_1.authQueryKeys.bearerToken(),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.ApiPlatformClient = ApiPlatformClient;
|
|
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
|
+
function createApiPlatformClient(options) {
|
|
170
|
+
return new ApiPlatformClient(options);
|
|
171
|
+
}
|
|
172
|
+
exports.createApiPlatformClient = createApiPlatformClient;
|
|
173
|
+
//# sourceMappingURL=ApiPlatformClient.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiPlatformClient.cjs","sourceRoot":"","sources":["../../src/api/ApiPlatformClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;;;;;;;;;;;;;;;AAEH,qDAAmD;AAGnD,qCAAqC;AACrC,mDAA+C;AAC/C,+CAA2C;AAC3C,qDAMwB;AAExB,6CAAyC;AACzC,+CAA2C;AAE3C,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E;;;;;;;;;GASG;AACH,MAAa,iBAAiB;IA8B5B,YAAY,OAAiC;QAL7C;;WAEG;QACM,uDAAgC;QAGvC,iEAAiE;QACjE,uBAAA,IAAI,wCACF,OAAO,CAAC,WAAW;YACnB,IAAI,wBAAW,CAAC;gBACd,cAAc,EAAE;oBACd,OAAO,EAAE;wBACP,SAAS,EAAE,0BAAW,CAAC,OAAO;wBAC9B,MAAM,EAAE,uBAAQ,CAAC,OAAO;wBACxB,KAAK,EAAE,0BAAW;wBAClB,UAAU,EAAE,kCAAmB;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,4BAAiB,CAAC,aAAa,CAAC,CAAC;QACrD,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAe,CAAC,aAAa,CAAC,CAAC;QACjD,IAAI,CAAC,KAAK,GAAG,IAAI,sBAAc,CAAC,aAAa,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAe,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;;;OAGG;IACH,KAAK,CAAC,mBAAmB;QACvB,MAAM,uBAAA,IAAI,4CAAmB,CAAC,iBAAiB,CAAC;YAC9C,QAAQ,EAAE,4BAAa,CAAC,WAAW,EAAE;SACtC,CAAC,CAAC;IACL,CAAC;CACF;AA7HD,8CA6HC;;AAED;;;;;GAKG;AACH,SAAgB,uBAAuB,CACrC,OAAiC;IAEjC,OAAO,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC;AAJD,0DAIC","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 { PricesApiClient } from './prices';\nimport {\n STALE_TIMES,\n GC_TIMES,\n authQueryKeys,\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 async invalidateAuthToken(): Promise<void> {\n await this.#sharedQueryClient.invalidateQueries({\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"]}
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
invalidateAuthToken(): Promise<void>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Factory function to create an ApiPlatformClient.
|
|
119
|
+
*
|
|
120
|
+
* @param options - Configuration options for the client.
|
|
121
|
+
* @returns A new ApiPlatformClient instance.
|
|
122
|
+
*/
|
|
123
|
+
export declare function createApiPlatformClient(options: ApiPlatformClientOptions): ApiPlatformClient;
|
|
124
|
+
//# 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;AAC/C,OAAO,EAAE,eAAe,EAAE,2BAAiB;AAQ3C,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;;;OAGG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC;CAK3C;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,iBAAiB,CAEnB"}
|