Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core-backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add optional `apiUrls` option to `ApiPlatformClientOptions`, allowing API base URLs (accounts, prices, token, tokens) to be overridden per client instance, e.g. from client env vars; unspecified services fall back to the production `API_URLS` ([#10196](https://github.com/MetaMask/core/pull/10196))

### Changed

- **BREAKING:** Accounts API `V2SupportedNetworksResponse` now uses CAIP-2 string arrays for both `fullSupport` and `partialSupport`, replacing decimal `fullSupport` and object-shaped `partialSupport.balances` ([#10144](https://github.com/MetaMask/core/pull/10144))
Expand Down
35 changes: 15 additions & 20 deletions packages/core-backend/src/api/accounts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import type {
QueryFunctionContext,
} from '@tanstack/query-core';

import {
BaseApiClient,
API_URLS,
STALE_TIMES,
GC_TIMES,
} from '../base-client.js';
import { BaseApiClient, STALE_TIMES, GC_TIMES } from '../base-client.js';
import { getQueryOptionsOverrides } from '../shared-types.js';
import type { FetchOptions } from '../shared-types.js';
import type {
Expand Down Expand Up @@ -97,7 +92,7 @@ export class AccountsApiClient extends BaseApiClient {
queryKey: ['accounts', 'v1SupportedNetworks'],
queryFn: ({ signal }: QueryFunctionContext) =>
this.fetch<V1SupportedNetworksResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v1/supportedNetworks',
{ signal },
),
Expand Down Expand Up @@ -134,7 +129,7 @@ export class AccountsApiClient extends BaseApiClient {
queryKey: ['accounts', 'v2SupportedNetworks'],
queryFn: ({ signal }: QueryFunctionContext) =>
this.fetch<V2SupportedNetworksResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v2/supportedNetworks',
{ signal },
),
Expand Down Expand Up @@ -198,7 +193,7 @@ export class AccountsApiClient extends BaseApiClient {
return { activeNetworks: [] };
}
return this.fetch<V2ActiveNetworksResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v2/activeNetworks',
{
signal,
Expand Down Expand Up @@ -289,7 +284,7 @@ export class AccountsApiClient extends BaseApiClient {
return { count: 0, balances: [], unprocessedNetworks: [] };
}
return this.fetch<V2BalancesResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v2/accounts/${address}/balances`,
{
signal,
Expand Down Expand Up @@ -373,7 +368,7 @@ export class AccountsApiClient extends BaseApiClient {
return { count: 0, balances: [], unprocessedNetworks: [] };
}
return this.fetch<V4BalancesResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v4/multiaccount/balances',
{
signal,
Expand Down Expand Up @@ -457,7 +452,7 @@ export class AccountsApiClient extends BaseApiClient {
return { count: 0, unprocessedNetworks: [], balances: [] };
}
return this.fetch<V5BalancesResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v5/multiaccount/balances',
{
signal,
Expand Down Expand Up @@ -584,7 +579,7 @@ export class AccountsApiClient extends BaseApiClient {
};
}
return this.fetch<V6BalancesResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v6/multiaccount/balances',
{
signal,
Expand Down Expand Up @@ -709,7 +704,7 @@ export class AccountsApiClient extends BaseApiClient {
],
queryFn: ({ signal }: QueryFunctionContext) =>
this.fetch<V1TransactionByHashResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v1/networks/${chainId}/transactions/${txHash}`,
{
signal,
Expand Down Expand Up @@ -806,7 +801,7 @@ export class AccountsApiClient extends BaseApiClient {
return { data: [], pageInfo: { count: 0, hasNextPage: false } };
}
return this.fetch<V1AccountTransactionsResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v1/accounts/${address}/transactions`,
{
signal,
Expand Down Expand Up @@ -914,7 +909,7 @@ export class AccountsApiClient extends BaseApiClient {
],
queryFn: ({ signal }: QueryFunctionContext) =>
this.fetch<V4MultiAccountTransactionsResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v4/multiaccount/transactions',
{
signal,
Expand Down Expand Up @@ -1006,7 +1001,7 @@ export class AccountsApiClient extends BaseApiClient {
signal?: AbortSignal;
}) =>
this.fetch<V4MultiAccountTransactionsResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
'/v4/multiaccount/transactions',
{
signal,
Expand Down Expand Up @@ -1106,7 +1101,7 @@ export class AccountsApiClient extends BaseApiClient {
signal,
}: QueryFunctionContext): Promise<V1AccountRelationshipResult> =>
this.fetch<V1AccountRelationshipResult>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v1/networks/${chainId}/accounts/${from}/relationships/${to}`,
{ signal },
),
Expand Down Expand Up @@ -1175,7 +1170,7 @@ export class AccountsApiClient extends BaseApiClient {
return { data: [], pageInfo: { count: 0, hasNextPage: false } };
}
return this.fetch<V2NftsResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v2/accounts/${address}/nfts`,
{
signal,
Expand Down Expand Up @@ -1253,7 +1248,7 @@ export class AccountsApiClient extends BaseApiClient {
return { data: [] };
}
return this.fetch<V2TokensResponse>(
API_URLS.ACCOUNTS,
this.apiUrls.ACCOUNTS,
`/v2/accounts/${address}/tokens`,
{
signal,
Expand Down
34 changes: 34 additions & 0 deletions packages/core-backend/src/api/base-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ describe('BaseApiClient', () => {
});
});

describe('API URL overrides', () => {
beforeEach(() => {
mockFetch.mockReset();
mockFetch.mockResolvedValue(
createMockResponse({ supportedNetworks: [] }),
);
});

it('uses the production URL by default', async () => {
const client = new AccountsApiClient({
clientProduct: 'test-product',
});

await client.fetchV1SupportedNetworks();

expect(String(mockFetch.mock.calls[0]?.[0])).toMatch(
'https://accounts.api.cx.metamask.io/',
);
});

it('uses the overridden URL when apiUrls is provided', async () => {
const client = new AccountsApiClient({
clientProduct: 'test-product',
apiUrls: { ACCOUNTS: 'https://accounts.dev-api.cx.metamask.io' },
});

await client.fetchV1SupportedNetworks();

expect(String(mockFetch.mock.calls[0]?.[0])).toMatch(
'https://accounts.dev-api.cx.metamask.io/',
);
});
});

describe('bearer token timeout', () => {
/**
* Creates a client whose token provider only resolves when told to.
Expand Down
10 changes: 7 additions & 3 deletions packages/core-backend/src/api/base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
shouldRetry,
HttpError,
} from './shared-types.js';
import type { ApiPlatformClientOptions } from './shared-types.js';
import type { ApiPlatformClientOptions, ApiUrls } from './shared-types.js';

// Auth query keys - shared for token management across clients
export const authQueryKeys = {
Expand Down Expand Up @@ -47,6 +47,9 @@ export class BaseApiClient {

protected readonly authTokenTimeout: number;

/** Resolved API base URLs (production defaults merged with any overrides). */
readonly apiUrls: ApiUrls;

readonly #queryClientInstance: QueryClient;

/**
Expand Down Expand Up @@ -78,6 +81,7 @@ export class BaseApiClient {
this.getBearerToken = options.getBearerToken;
this.authTokenTimeout =
options.authTokenTimeout ?? DEFAULT_AUTH_TOKEN_TIMEOUT;
this.apiUrls = { ...API_URLS, ...options.apiUrls };

this.#queryClientInstance =
options.queryClient ??
Expand Down Expand Up @@ -213,5 +217,5 @@ export class BaseApiClient {
}
}

// Re-export constants for use by API clients
export { API_URLS, STALE_TIMES, GC_TIMES, HttpError };
// Re-export cache timings for use by API clients
export { STALE_TIMES, GC_TIMES };
1 change: 1 addition & 0 deletions packages/core-backend/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {
SupportedCurrency,
MarketDataDetails,
ApiPlatformClientOptions,
ApiUrls,
FetchOptions,
} from './shared-types.js';
export {
Expand Down
Loading