Skip to content

Commit

Permalink
ALL-5273 Extend api key config options
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj.bacovcin committed Mar 7, 2024
1 parent 6b75baf commit 1fc5c52
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
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?:
| {
/**
* API Key for ApiVersion.V3
*/
v3?: string
/**
* API Key for ApiVersion.V4
*/
v4?: string
}
/**
* API Key for ApiVersion.V3
* If only string is provided, version is inferred regardless of ApiVersion
*/
v3?: string
/**
* API Key for ApiVersion.V4
*/
v4?: string
}
| string

/**
* Verbose logging is disabled by default.
Expand Down
2 changes: 1 addition & 1 deletion src/service/tatum/tatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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) {
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 1fc5c52

Please sign in to comment.