Skip to content

Commit

Permalink
ALL-5273 Extend api key config options (#1076)
Browse files Browse the repository at this point in the history
* ALL-5273 Extend api key config options

* ALL-5273 Adjust tests and api key warning condition

* ALL-5273 Bump up version

* ALL-5273 Update arb-testnet expected chain id

* ALL-5273 Adjust comments and make it clear old way of initializing is deprecated

* ALL-5273 Adjust changelog according to feedback from Filip

---------

Co-authored-by: juraj.bacovcin <[email protected]>
  • Loading branch information
hehe100596 and juraj.bacovcin authored Mar 11, 2024
1 parent 6b75baf commit ff2ed8e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [4.2.14] - 2024.3.11

### Changed

- Added support for simplified way of configuring api key without specifying version.
- Marked old way of configuring api key through object specifying version as `deprecated`.
- Fixed some tests, namely those related to `arb-testnet` `eth_chainId` RPC EVM method.

## [4.2.13] - 2024.2.29

### Removed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "4.2.13",
"version": "4.2.14",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
2 changes: 1 addition & 1 deletion src/dto/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ export const NETWORK_METADATA: Record<Network, NetworkMetadata> = {
currency: Currency.ARB,
testnet: true,
defaultTestnet: true,
chainId: 421613,
chainId: 421614,
},
[Network.ARBITRUM_ONE]: {
currency: Currency.ARB,
Expand Down
2 changes: 1 addition & 1 deletion src/e2e/rpc/other/tatum.rpc.stellar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getStellarRpc = async (testnet?: boolean) =>
network: testnet ? Network.STELLAR_TESTNET : Network.STELLAR,
verbose: e2eUtil.isVerbose,
...(testnet && { apiKey: { v3: process.env.V3_API_KEY_TESTNET } }),
...(!testnet && { apiKey: { v4: process.env.V4_API_KEY_MAINNET } }),
...(!testnet && { apiKey: process.env.V4_API_KEY_MAINNET }),
version: testnet ? ApiVersion.V3 : ApiVersion.V4,
})

Expand Down
21 changes: 13 additions & 8 deletions src/service/tatum/tatum.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ export interface TatumConfig {
* API Key is optional, but your data will by tied to the IP address you are using. If you want to store your data like address notifications, webhooks, etc. you need to use API Key.
* If you are using Tatum API Key, you can use Tatum SDK without any limitations.
*/
apiKey?: {
apiKey?:
| string
/**
* API Key for ApiVersion.V3
* @deprecated Use `string` format instead.
*/
v3?: string
/**
* API Key for ApiVersion.V4
*/
v4?: string
}
| {
/**
* API Key for ApiVersion.V3
*/
v3?: string
/**
* API Key for ApiVersion.V4
*/
v4?: string
}

/**
* Verbose logging is disabled by default.
Expand Down
5 changes: 4 additions & 1 deletion src/service/tatum/tatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ export class TatumSDK {
Container.of(id).set(CONFIG, mergedConfig)
Container.of(id).set(LOGGER, mergedConfig.logger)

if (!mergedConfig.apiKey?.v3 && !mergedConfig.apiKey?.v4) {
if (
!mergedConfig.apiKey ||
(typeof mergedConfig.apiKey !== 'string' && !mergedConfig.apiKey.v3 && !mergedConfig.apiKey.v4)
) {
mergedConfig.logger?.warn(
'API key not provided - only a subset of SDK features will be enabled. Generate an API Key by accessing your Dashboard: https://co.tatum.io/signup',
)
Expand Down
13 changes: 9 additions & 4 deletions src/util/util.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,14 @@ export const Utils = {
'x-ttm-sdk-debug': `${config.verbose}`,
})

const apiKeyHeader = 'x-api-key'
if (config.apiKey) {
if (config.version === ApiVersion.V3 && config.apiKey.v3) {
headers.append('x-api-key', config.apiKey.v3)
if (typeof config.apiKey === 'string') {
headers.append(apiKeyHeader, config.apiKey)
} else if (config.version === ApiVersion.V3 && config.apiKey.v3) {
headers.append(apiKeyHeader, config.apiKey.v3)
} else if (config.version === ApiVersion.V4 && config.apiKey.v4) {
headers.append('x-api-key', config.apiKey.v4)
headers.append(apiKeyHeader, config.apiKey.v4)
}
}
return headers
Expand Down Expand Up @@ -775,7 +778,9 @@ export const Utils = {
if (apiKey) {
const url =
rpc?.nodes?.[0].url ||
`${Constant.TATUM_API_URL.V3}blockchain/node/${network}/${apiKey.v3 ? apiKey.v3 : apiKey.v4}`
`${Constant.TATUM_API_URL.V3}blockchain/node/${network}/${
typeof apiKey === 'string' ? apiKey : apiKey.v3 || apiKey.v4
}`
return url.concat(path || '')
}
return rpc?.nodes?.[0].url || `${Constant.TATUM_API_URL.V3}blockchain/node/${network}`.concat(path || '')
Expand Down

0 comments on commit ff2ed8e

Please sign in to comment.