@coinbase/cdp-sdk 1.1.2 → 1.2.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.
- package/CHANGELOG.md +79 -0
- package/LICENSE +21 -0
- package/{dist → _cjs}/accounts/evm/toEvmServerAccount.js +1 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -0
- package/{dist → _cjs}/accounts/evm/toEvmSmartAccount.js +1 -0
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -0
- package/{dist → _cjs}/accounts/types.js +1 -0
- package/_cjs/accounts/types.js.map +1 -0
- package/{dist → _cjs}/actions/evm/sendUserOperation.js +1 -0
- package/_cjs/actions/evm/sendUserOperation.js.map +1 -0
- package/{dist → _cjs}/actions/evm/waitForUserOperation.js +10 -9
- package/_cjs/actions/evm/waitForUserOperation.js.map +1 -0
- package/{dist → _cjs}/analytics.js +7 -6
- package/_cjs/analytics.js.map +1 -0
- package/{dist → _cjs}/auth/errors.js +1 -0
- package/_cjs/auth/errors.js.map +1 -0
- package/{dist → _cjs}/auth/hooks/axios/index.js +2 -1
- package/_cjs/auth/hooks/axios/index.js.map +1 -0
- package/{dist → _cjs}/auth/hooks/axios/withAuth.js +3 -2
- package/_cjs/auth/hooks/axios/withAuth.js.map +1 -0
- package/{dist → _cjs}/auth/index.js +4 -3
- package/_cjs/auth/index.js.map +1 -0
- package/{dist → _cjs}/auth/utils/http.js +6 -5
- package/_cjs/auth/utils/http.js.map +1 -0
- package/{dist → _cjs}/auth/utils/index.js +3 -2
- package/_cjs/auth/utils/index.js.map +1 -0
- package/{dist → _cjs}/auth/utils/jwt.js +4 -3
- package/_cjs/auth/utils/jwt.js.map +1 -0
- package/{dist → _cjs}/client/cdp.js +13 -8
- package/_cjs/client/cdp.js.map +1 -0
- package/{dist → _cjs}/client/evm/evm.js +26 -25
- package/_cjs/client/evm/evm.js.map +1 -0
- package/{dist → _cjs}/client/evm/evm.types.js +1 -0
- package/_cjs/client/evm/evm.types.js.map +1 -0
- package/{dist → _cjs}/client/solana/index.js +3 -2
- package/_cjs/client/solana/index.js.map +1 -0
- package/{dist → _cjs}/client/solana/solana.js +9 -8
- package/_cjs/client/solana/solana.js.map +1 -0
- package/{dist → _cjs}/client/solana/solana.types.js +1 -0
- package/_cjs/client/solana/solana.types.js.map +1 -0
- package/{dist → _cjs}/constants.js +1 -0
- package/_cjs/constants.js.map +1 -0
- package/{dist → _cjs}/errors.js +1 -0
- package/_cjs/errors.js.map +1 -0
- package/{dist → _cjs}/index.js +4 -3
- package/_cjs/index.js.map +1 -0
- package/{dist → _cjs}/openapi-client/cdpApiClient.js +18 -14
- package/_cjs/openapi-client/cdpApiClient.js.map +1 -0
- package/{dist → _cjs}/openapi-client/errors.js +6 -0
- package/_cjs/openapi-client/errors.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +31 -30
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/evm-accounts/evm-accounts.js +46 -41
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/evm-accounts/evm-accounts.msw.js +73 -40
- package/_cjs/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +69 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +188 -0
- package/_cjs/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/faucets/faucets.js +18 -13
- package/_cjs/openapi-client/generated/faucets/faucets.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/faucets/faucets.msw.js +16 -12
- package/_cjs/openapi-client/generated/faucets/faucets.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/index.msw.js +5 -4
- package/_cjs/openapi-client/generated/index.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/solana-accounts/solana-accounts.js +38 -35
- package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -0
- package/{dist → _cjs}/openapi-client/generated/solana-accounts/solana-accounts.msw.js +68 -35
- package/_cjs/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -0
- package/{dist → _cjs}/openapi-client/index.js +12 -11
- package/_cjs/openapi-client/index.js.map +1 -0
- package/_cjs/package.json +1 -0
- package/{dist → _cjs}/types/calls.js +1 -0
- package/_cjs/types/calls.js.map +1 -0
- package/{dist → _cjs}/types/contract.js +1 -0
- package/_cjs/types/contract.js.map +1 -0
- package/{dist → _cjs}/types/misc.js +1 -0
- package/_cjs/types/misc.js.map +1 -0
- package/{dist → _cjs}/types/multicall.js +1 -0
- package/_cjs/types/multicall.js.map +1 -0
- package/{dist → _cjs}/types/utils.js +1 -0
- package/_cjs/types/utils.js.map +1 -0
- package/{dist → _cjs}/utils/wait.js +3 -2
- package/_cjs/utils/wait.js.map +1 -0
- package/_cjs/version.js +5 -0
- package/_cjs/version.js.map +1 -0
- package/_esm/accounts/evm/toEvmServerAccount.js +40 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js +22 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -0
- package/_esm/accounts/types.js +2 -0
- package/_esm/accounts/types.js.map +1 -0
- package/_esm/actions/evm/sendUserOperation.js +85 -0
- package/_esm/actions/evm/sendUserOperation.js.map +1 -0
- package/_esm/actions/evm/waitForUserOperation.js +58 -0
- package/_esm/actions/evm/waitForUserOperation.js.map +1 -0
- package/_esm/analytics.js +84 -0
- package/_esm/analytics.js.map +1 -0
- package/_esm/auth/errors.js +57 -0
- package/_esm/auth/errors.js.map +1 -0
- package/_esm/auth/hooks/axios/index.js +2 -0
- package/_esm/auth/hooks/axios/index.js.map +1 -0
- package/_esm/auth/hooks/axios/withAuth.js +71 -0
- package/_esm/auth/hooks/axios/withAuth.js.map +1 -0
- package/_esm/auth/index.js +5 -0
- package/_esm/auth/index.js.map +1 -0
- package/_esm/auth/utils/http.js +70 -0
- package/_esm/auth/utils/http.js.map +1 -0
- package/_esm/auth/utils/index.js +3 -0
- package/_esm/auth/utils/index.js.map +1 -0
- package/_esm/auth/utils/jwt.js +199 -0
- package/_esm/auth/utils/jwt.js.map +1 -0
- package/_esm/client/cdp.js +95 -0
- package/_esm/client/cdp.js.map +1 -0
- package/_esm/client/evm/evm.js +518 -0
- package/_esm/client/evm/evm.js.map +1 -0
- package/_esm/client/evm/evm.types.js +2 -0
- package/_esm/client/evm/evm.types.js.map +1 -0
- package/_esm/client/solana/index.js +2 -0
- package/_esm/client/solana/index.js.map +1 -0
- package/_esm/client/solana/solana.js +204 -0
- package/_esm/client/solana/solana.js.map +1 -0
- package/_esm/client/solana/solana.types.js +2 -0
- package/_esm/client/solana/solana.types.js.map +1 -0
- package/_esm/constants.js +5 -0
- package/_esm/constants.js.map +1 -0
- package/_esm/errors.js +18 -0
- package/_esm/errors.js.map +1 -0
- package/_esm/index.js +3 -0
- package/_esm/index.js.map +1 -0
- package/_esm/openapi-client/cdpApiClient.js +112 -0
- package/_esm/openapi-client/cdpApiClient.js.map +1 -0
- package/_esm/openapi-client/errors.js +71 -0
- package/_esm/openapi-client/errors.js.map +1 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +52 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js +80 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js +128 -0
- package/_esm/openapi-client/generated/evm-accounts/evm-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +60 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +172 -0
- package/_esm/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/generated/faucets/faucets.js +51 -0
- package/_esm/openapi-client/generated/faucets/faucets.js.map +1 -0
- package/_esm/openapi-client/generated/faucets/faucets.msw.js +39 -0
- package/_esm/openapi-client/generated/faucets/faucets.msw.js.map +1 -0
- package/_esm/openapi-client/generated/index.msw.js +5 -0
- package/_esm/openapi-client/generated/index.msw.js.map +1 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js +73 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.js.map +1 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js +116 -0
- package/_esm/openapi-client/generated/solana-accounts/solana-accounts.msw.js.map +1 -0
- package/_esm/openapi-client/index.js +27 -0
- package/_esm/openapi-client/index.js.map +1 -0
- package/_esm/package.json +1 -0
- package/_esm/types/calls.js +4 -0
- package/_esm/types/calls.js.map +1 -0
- package/_esm/types/contract.js +4 -0
- package/_esm/types/contract.js.map +1 -0
- package/_esm/types/misc.js +3 -0
- package/_esm/types/misc.js.map +1 -0
- package/_esm/types/multicall.js +2 -0
- package/_esm/types/multicall.js.map +1 -0
- package/_esm/types/utils.js +4 -0
- package/_esm/types/utils.js.map +1 -0
- package/_esm/utils/wait.js +32 -0
- package/_esm/utils/wait.js.map +1 -0
- package/_esm/version.js +2 -0
- package/_esm/version.js.map +1 -0
- package/{dist → _types}/accounts/evm/toEvmServerAccount.d.ts +3 -2
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -0
- package/{dist → _types}/accounts/evm/toEvmSmartAccount.d.ts +3 -2
- package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -0
- package/{dist → _types}/accounts/types.d.ts +3 -2
- package/_types/accounts/types.d.ts.map +1 -0
- package/{dist → _types}/actions/evm/sendUserOperation.d.ts +5 -5
- package/_types/actions/evm/sendUserOperation.d.ts.map +1 -0
- package/{dist → _types}/actions/evm/waitForUserOperation.d.ts +4 -3
- package/_types/actions/evm/waitForUserOperation.d.ts.map +1 -0
- package/{dist → _types}/analytics.d.ts +1 -0
- package/_types/analytics.d.ts.map +1 -0
- package/{dist → _types}/auth/errors.d.ts +1 -0
- package/_types/auth/errors.d.ts.map +1 -0
- package/_types/auth/hooks/axios/index.d.ts +2 -0
- package/_types/auth/hooks/axios/index.d.ts.map +1 -0
- package/{dist → _types}/auth/hooks/axios/withAuth.d.ts +1 -0
- package/_types/auth/hooks/axios/withAuth.d.ts.map +1 -0
- package/_types/auth/index.d.ts +5 -0
- package/_types/auth/index.d.ts.map +1 -0
- package/{dist → _types}/auth/utils/http.d.ts +1 -0
- package/_types/auth/utils/http.d.ts.map +1 -0
- package/_types/auth/utils/index.d.ts +3 -0
- package/_types/auth/utils/index.d.ts.map +1 -0
- package/{dist → _types}/auth/utils/jwt.d.ts +1 -0
- package/_types/auth/utils/jwt.d.ts.map +1 -0
- package/{dist → _types}/client/cdp.d.ts +3 -2
- package/_types/client/cdp.d.ts.map +1 -0
- package/{dist → _types}/client/evm/evm.d.ts +4 -3
- package/_types/client/evm/evm.d.ts.map +1 -0
- package/{dist → _types}/client/evm/evm.types.d.ts +7 -6
- package/_types/client/evm/evm.types.d.ts.map +1 -0
- package/_types/client/solana/index.d.ts +2 -0
- package/_types/client/solana/index.d.ts.map +1 -0
- package/{dist → _types}/client/solana/solana.d.ts +2 -1
- package/_types/client/solana/solana.d.ts.map +1 -0
- package/{dist → _types}/client/solana/solana.types.d.ts +2 -1
- package/_types/client/solana/solana.types.d.ts.map +1 -0
- package/{dist → _types}/constants.d.ts +1 -0
- package/_types/constants.d.ts.map +1 -0
- package/{dist → _types}/errors.d.ts +1 -0
- package/_types/errors.d.ts.map +1 -0
- package/_types/index.d.ts +4 -0
- package/_types/index.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/cdpApiClient.d.ts +1 -0
- package/_types/openapi-client/cdpApiClient.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/errors.d.ts +2 -1
- package/_types/openapi-client/errors.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +13 -12
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-accounts/evm-accounts.d.ts +24 -23
- package/_types/openapi-client/generated/evm-accounts/evm-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts +3 -2
- package/_types/openapi-client/generated/evm-accounts/evm-accounts.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts +21 -20
- package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.d.ts +3 -2
- package/_types/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/faucets/faucets.d.ts +9 -8
- package/_types/openapi-client/generated/faucets/faucets.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/faucets/faucets.msw.d.ts +3 -2
- package/_types/openapi-client/generated/faucets/faucets.msw.d.ts.map +1 -0
- package/_types/openapi-client/generated/index.msw.d.ts +5 -0
- package/_types/openapi-client/generated/index.msw.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/solana-accounts/solana-accounts.d.ts +21 -20
- package/_types/openapi-client/generated/solana-accounts/solana-accounts.d.ts.map +1 -0
- package/{dist → _types}/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts +3 -2
- package/_types/openapi-client/generated/solana-accounts/solana-accounts.msw.d.ts.map +1 -0
- package/_types/openapi-client/index.d.ts +56 -0
- package/_types/openapi-client/index.d.ts.map +1 -0
- package/{dist → _types}/types/calls.d.ts +4 -3
- package/_types/types/calls.d.ts.map +1 -0
- package/{dist → _types}/types/contract.d.ts +3 -2
- package/_types/types/contract.d.ts.map +1 -0
- package/{dist → _types}/types/misc.d.ts +1 -0
- package/_types/types/misc.d.ts.map +1 -0
- package/{dist → _types}/types/multicall.d.ts +2 -1
- package/_types/types/multicall.d.ts.map +1 -0
- package/{dist → _types}/types/utils.d.ts +1 -0
- package/_types/types/utils.d.ts.map +1 -0
- package/{dist → _types}/utils/wait.d.ts +1 -0
- package/_types/utils/wait.d.ts.map +1 -0
- package/_types/version.d.ts +2 -0
- package/_types/version.d.ts.map +1 -0
- package/accounts/evm/toEvmServerAccount.ts +60 -0
- package/accounts/evm/toEvmSmartAccount.ts +37 -0
- package/accounts/types.ts +53 -0
- package/actions/evm/sendUserOperation.ts +163 -0
- package/actions/evm/waitForUserOperation.ts +115 -0
- package/analytics.ts +123 -0
- package/auth/README.md +145 -0
- package/auth/errors.ts +59 -0
- package/auth/hooks/axios/index.ts +1 -0
- package/auth/hooks/axios/withAuth.ts +120 -0
- package/auth/index.ts +5 -0
- package/auth/utils/http.ts +146 -0
- package/auth/utils/index.ts +2 -0
- package/auth/utils/jwt.ts +324 -0
- package/client/cdp.ts +113 -0
- package/client/evm/evm.ts +606 -0
- package/client/evm/evm.types.ts +303 -0
- package/client/solana/index.ts +1 -0
- package/client/solana/solana.ts +237 -0
- package/client/solana/solana.types.ts +114 -0
- package/constants.ts +4 -0
- package/errors.ts +17 -0
- package/index.ts +4 -0
- package/openapi-client/cdpApiClient.ts +214 -0
- package/openapi-client/errors.ts +96 -0
- package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +411 -0
- package/openapi-client/generated/evm-accounts/evm-accounts.msw.ts +242 -0
- package/openapi-client/generated/evm-accounts/evm-accounts.ts +150 -0
- package/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.ts +283 -0
- package/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.ts +131 -0
- package/openapi-client/generated/faucets/faucets.msw.ts +76 -0
- package/openapi-client/generated/faucets/faucets.ts +83 -0
- package/openapi-client/generated/index.msw.ts +4 -0
- package/openapi-client/generated/solana-accounts/solana-accounts.msw.ts +219 -0
- package/openapi-client/generated/solana-accounts/solana-accounts.ts +143 -0
- package/openapi-client/index.ts +32 -0
- package/package.json +31 -66
- package/types/calls.ts +40 -0
- package/types/contract.ts +126 -0
- package/types/misc.ts +5 -0
- package/types/multicall.ts +29 -0
- package/types/utils.ts +288 -0
- package/utils/wait.ts +52 -0
- package/version.ts +1 -0
- package/dist/auth/hooks/axios/index.d.ts +0 -1
- package/dist/auth/index.d.ts +0 -4
- package/dist/auth/utils/index.d.ts +0 -2
- package/dist/client/evm/index.d.ts +0 -1
- package/dist/client/evm/index.js +0 -5
- package/dist/client/solana/index.d.ts +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.js +0 -66
- package/dist/openapi-client/generated/evm-smart-accounts/evm-smart-accounts.msw.js +0 -99
- package/dist/openapi-client/generated/index.msw.d.ts +0 -4
- package/dist/openapi-client/index.d.ts +0 -55
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-named-as-default
|
|
2
|
+
import Axios, { AxiosInstance, AxiosRequestConfig, HttpStatusCode } from "axios";
|
|
3
|
+
|
|
4
|
+
import { APIError, isOpenAPIError, HttpErrorType } from "./errors.js";
|
|
5
|
+
import { withAuth } from "../auth/hooks/axios/index.js";
|
|
6
|
+
import { ERROR_DOCS_PAGE_URL } from "../constants.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The options for the CDP API.
|
|
10
|
+
*/
|
|
11
|
+
export type CdpOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* The API key ID or the legacy API key name.
|
|
14
|
+
*
|
|
15
|
+
* Examples:
|
|
16
|
+
* ID format: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
17
|
+
* Legacy name format: 'organizations/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/apiKeys/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
|
18
|
+
*/
|
|
19
|
+
apiKeyId: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The API key secret, using the Ed25519 or legacy EC key format.
|
|
23
|
+
*
|
|
24
|
+
* Examples:
|
|
25
|
+
* Ed25519 key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='
|
|
26
|
+
* EC key: '-----BEGIN EC PRIVATE KEY-----\n...\n...\n...==\n-----END EC PRIVATE KEY-----\n'
|
|
27
|
+
*/
|
|
28
|
+
apiKeySecret: string;
|
|
29
|
+
|
|
30
|
+
/** The Wallet Secret. Only needed if calling certain Wallet APIs. */
|
|
31
|
+
walletSecret?: string;
|
|
32
|
+
|
|
33
|
+
/** If true, logs API requests and responses to the console. */
|
|
34
|
+
debugging?: boolean;
|
|
35
|
+
|
|
36
|
+
/** The base path for the API. */
|
|
37
|
+
basePath?: string;
|
|
38
|
+
|
|
39
|
+
/** The source for the API request, used for analytics. Defaults to `typescript-client`. */
|
|
40
|
+
source?: string;
|
|
41
|
+
|
|
42
|
+
/** The version of the source for the API request, used for analytics. */
|
|
43
|
+
sourceVersion?: string;
|
|
44
|
+
|
|
45
|
+
/** Optional expiration time in seconds (defaults to 120) */
|
|
46
|
+
expiresIn?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
let axiosInstance: AxiosInstance;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Configures the CDP client with the given options.
|
|
53
|
+
*
|
|
54
|
+
* @param {CdpOptions} options - The CDP options.
|
|
55
|
+
*/
|
|
56
|
+
export const configure = (options: CdpOptions) => {
|
|
57
|
+
axiosInstance = Axios.create({
|
|
58
|
+
baseURL: options.basePath || "https://api.cdp.coinbase.com/platform",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
axiosInstance = withAuth(axiosInstance, {
|
|
62
|
+
apiKeyId: options.apiKeyId,
|
|
63
|
+
apiKeySecret: options.apiKeySecret,
|
|
64
|
+
source: options.source || "sdk-openapi-client",
|
|
65
|
+
sourceVersion: options.sourceVersion,
|
|
66
|
+
walletSecret: options.walletSecret,
|
|
67
|
+
expiresIn: options.expiresIn,
|
|
68
|
+
debug: options.debugging,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Adds an idempotency key to request config if provided
|
|
74
|
+
*
|
|
75
|
+
* @param config - The Axios request configuration.
|
|
76
|
+
* @param idempotencyKey - The idempotency key.
|
|
77
|
+
* @returns The Axios request configuration with the idempotency key.
|
|
78
|
+
*/
|
|
79
|
+
const addIdempotencyKey = (
|
|
80
|
+
config: AxiosRequestConfig,
|
|
81
|
+
idempotencyKey?: string,
|
|
82
|
+
): AxiosRequestConfig => {
|
|
83
|
+
if (!idempotencyKey) {
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
...config,
|
|
89
|
+
headers: {
|
|
90
|
+
...(config.headers || {}),
|
|
91
|
+
"X-Idempotency-Key": idempotencyKey,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Mutates the given Axios request configuration to add the CDP API key signature
|
|
98
|
+
* to the request headers.
|
|
99
|
+
*
|
|
100
|
+
* @param {AxiosRequestConfig} config - The Axios request configuration.
|
|
101
|
+
* @param idempotencyKey - The idempotency key.
|
|
102
|
+
* @returns {Promise<T>} A promise that resolves to the response data.
|
|
103
|
+
* @throws {APIError} If the request fails.
|
|
104
|
+
*/
|
|
105
|
+
export const cdpApiClient = async <T>(
|
|
106
|
+
config: AxiosRequestConfig,
|
|
107
|
+
idempotencyKey?: string,
|
|
108
|
+
): Promise<T> => {
|
|
109
|
+
validateCall(config);
|
|
110
|
+
|
|
111
|
+
// Add idempotency key to the request headers if provided
|
|
112
|
+
const configWithIdempotencyKey = addIdempotencyKey(config, idempotencyKey);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const response = await axiosInstance(configWithIdempotencyKey);
|
|
116
|
+
return response.data as T;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
// eslint-disable-next-line import/no-named-as-default-member
|
|
119
|
+
if (Axios.isAxiosError(error) && error.response) {
|
|
120
|
+
if (isOpenAPIError(error.response.data)) {
|
|
121
|
+
throw new APIError(
|
|
122
|
+
error.response.status,
|
|
123
|
+
error.response.data.errorType,
|
|
124
|
+
error.response.data.errorMessage,
|
|
125
|
+
error.response.data.correlationId,
|
|
126
|
+
error.response.data.errorLink,
|
|
127
|
+
);
|
|
128
|
+
} else {
|
|
129
|
+
const statusCode = error.response.status;
|
|
130
|
+
switch (statusCode) {
|
|
131
|
+
case 401:
|
|
132
|
+
throw new APIError(
|
|
133
|
+
statusCode,
|
|
134
|
+
HttpErrorType.unauthorized,
|
|
135
|
+
"Unauthorized.",
|
|
136
|
+
undefined,
|
|
137
|
+
`${ERROR_DOCS_PAGE_URL}#unauthorized`,
|
|
138
|
+
);
|
|
139
|
+
case 404:
|
|
140
|
+
throw new APIError(
|
|
141
|
+
statusCode,
|
|
142
|
+
HttpErrorType.not_found,
|
|
143
|
+
"API not found.",
|
|
144
|
+
undefined,
|
|
145
|
+
`${ERROR_DOCS_PAGE_URL}#not_found`,
|
|
146
|
+
);
|
|
147
|
+
case 502:
|
|
148
|
+
throw new APIError(
|
|
149
|
+
statusCode,
|
|
150
|
+
HttpErrorType.bad_gateway,
|
|
151
|
+
"Bad gateway.",
|
|
152
|
+
undefined,
|
|
153
|
+
`${ERROR_DOCS_PAGE_URL}`,
|
|
154
|
+
);
|
|
155
|
+
case 503:
|
|
156
|
+
throw new APIError(
|
|
157
|
+
statusCode,
|
|
158
|
+
HttpErrorType.service_unavailable,
|
|
159
|
+
"Service unavailable. Please try again later.",
|
|
160
|
+
undefined,
|
|
161
|
+
`${ERROR_DOCS_PAGE_URL}`,
|
|
162
|
+
);
|
|
163
|
+
default:
|
|
164
|
+
throw new APIError(
|
|
165
|
+
statusCode,
|
|
166
|
+
HttpErrorType.unexpected_error,
|
|
167
|
+
"An unexpected error occurred.",
|
|
168
|
+
undefined,
|
|
169
|
+
`${ERROR_DOCS_PAGE_URL}`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// eslint-disable-next-line import/no-named-as-default-member
|
|
174
|
+
} else if (Axios.isAxiosError(error) && error.request) {
|
|
175
|
+
throw new APIError(
|
|
176
|
+
HttpStatusCode.ServiceUnavailable,
|
|
177
|
+
HttpErrorType.service_unavailable,
|
|
178
|
+
"Network error, unable to reach the service.",
|
|
179
|
+
undefined,
|
|
180
|
+
`${ERROR_DOCS_PAGE_URL}`,
|
|
181
|
+
);
|
|
182
|
+
} else {
|
|
183
|
+
throw new APIError(
|
|
184
|
+
HttpStatusCode.InternalServerError,
|
|
185
|
+
HttpErrorType.unexpected_error,
|
|
186
|
+
error instanceof Error
|
|
187
|
+
? error.message
|
|
188
|
+
: `An unexpected error occurred: ${JSON.stringify(error, Object.getOwnPropertyNames(error))}`,
|
|
189
|
+
undefined,
|
|
190
|
+
`${ERROR_DOCS_PAGE_URL}`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Validates the call to the cdpApiClient.
|
|
198
|
+
*
|
|
199
|
+
* @param {AxiosRequestConfig} config - The Axios request configuration.
|
|
200
|
+
* @throws {Error} If the call is not valid.
|
|
201
|
+
*/
|
|
202
|
+
const validateCall = (config: AxiosRequestConfig) => {
|
|
203
|
+
if (!axiosInstance.getUri() || axiosInstance.getUri() === "") {
|
|
204
|
+
throw new Error("CDP client URI not configured. Call configure() first.");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!config.url || config.url === "") {
|
|
208
|
+
throw new Error("AxiosRequestConfig URL is empty. This should never happen.");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (!config.method || config.method === "") {
|
|
212
|
+
throw new Error("AxiosRequestConfig method is empty. This should never happen.");
|
|
213
|
+
}
|
|
214
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Error as OpenAPIError,
|
|
3
|
+
ErrorType as OpenAPIErrorType,
|
|
4
|
+
} from "./generated/coinbaseDeveloperPlatformAPIs.schemas.js";
|
|
5
|
+
|
|
6
|
+
export const HttpErrorType = {
|
|
7
|
+
unexpected_error: "unexpected_error",
|
|
8
|
+
unauthorized: "unauthorized",
|
|
9
|
+
not_found: "not_found",
|
|
10
|
+
bad_gateway: "bad_gateway",
|
|
11
|
+
service_unavailable: "service_unavailable",
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
export type HttpErrorType = (typeof HttpErrorType)[keyof typeof HttpErrorType];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Extended error codes that include both OpenAPI errors and network errors
|
|
18
|
+
*/
|
|
19
|
+
export type APIErrorType = OpenAPIErrorType | HttpErrorType;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Extended API error that encompasses both OpenAPI errors and other API-related errors
|
|
23
|
+
*/
|
|
24
|
+
export class APIError extends Error {
|
|
25
|
+
statusCode: number;
|
|
26
|
+
errorType: APIErrorType;
|
|
27
|
+
errorMessage: string;
|
|
28
|
+
correlationId?: string;
|
|
29
|
+
errorLink?: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Constructor for the APIError class
|
|
33
|
+
*
|
|
34
|
+
* @param statusCode - The HTTP status code
|
|
35
|
+
* @param errorType - The type of error
|
|
36
|
+
* @param errorMessage - The error message
|
|
37
|
+
* @param correlationId - The correlation ID
|
|
38
|
+
* @param errorLink - URL to documentation about this error
|
|
39
|
+
*/
|
|
40
|
+
constructor(
|
|
41
|
+
statusCode: number,
|
|
42
|
+
errorType: APIErrorType,
|
|
43
|
+
errorMessage: string,
|
|
44
|
+
correlationId?: string,
|
|
45
|
+
errorLink?: string,
|
|
46
|
+
) {
|
|
47
|
+
super(errorMessage);
|
|
48
|
+
this.name = "APIError";
|
|
49
|
+
this.statusCode = statusCode;
|
|
50
|
+
this.errorType = errorType;
|
|
51
|
+
this.errorMessage = errorMessage;
|
|
52
|
+
|
|
53
|
+
// Only set correlationId if it's defined
|
|
54
|
+
if (correlationId !== undefined) {
|
|
55
|
+
this.correlationId = correlationId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Only set errorLink if it's defined
|
|
59
|
+
if (errorLink !== undefined) {
|
|
60
|
+
this.errorLink = errorLink;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Convert the error to a JSON object, excluding undefined properties
|
|
66
|
+
*
|
|
67
|
+
* @returns The error as a JSON object
|
|
68
|
+
*/
|
|
69
|
+
toJSON() {
|
|
70
|
+
return {
|
|
71
|
+
name: this.name,
|
|
72
|
+
statusCode: this.statusCode,
|
|
73
|
+
errorType: this.errorType,
|
|
74
|
+
errorMessage: this.errorMessage,
|
|
75
|
+
...(this.correlationId && { correlationId: this.correlationId }),
|
|
76
|
+
...(this.errorLink && { errorLink: this.errorLink }),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Type guard to check if an object is an OpenAPIError
|
|
83
|
+
*
|
|
84
|
+
* @param obj - The object to check
|
|
85
|
+
* @returns True if the object is an OpenAPIError
|
|
86
|
+
*/
|
|
87
|
+
export function isOpenAPIError(obj: unknown): obj is OpenAPIError {
|
|
88
|
+
return (
|
|
89
|
+
obj !== null &&
|
|
90
|
+
typeof obj === "object" &&
|
|
91
|
+
"errorType" in obj &&
|
|
92
|
+
typeof (obj as OpenAPIError).errorType === "string" &&
|
|
93
|
+
"errorMessage" in obj &&
|
|
94
|
+
typeof (obj as OpenAPIError).errorMessage === "string"
|
|
95
|
+
);
|
|
96
|
+
}
|