Skip to content

Commit 17e9888

Browse files
authored
Merge pull request #1471 from mars-protocol/develop
x-apikey update
2 parents 8b43329 + 7be2f74 commit 17e9888

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/api/assets/getDexAssets.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { LocalStorageKeys } from 'constants/localStorageKeys'
2+
import { FETCH_TIMEOUT } from 'constants/query'
13
import { convertAstroportAssetsResponse } from 'utils/assets'
24
import { setApiError } from 'utils/error'
35
import { fetchWithTimeout } from 'utils/fetch'
4-
import { FETCH_TIMEOUT } from 'constants/query'
5-
import { LocalStorageKeys } from 'constants/localStorageKeys'
6-
6+
import { getUrl } from 'utils/url'
77
type StoredPerpAsset = Omit<AstroportAsset, 'icon'>
88

99
export default async function getDexAssets(chainConfig: ChainConfig) {
10-
const uri = new URL(chainConfig.endpoints.dexAssets)
10+
const uri = getUrl(chainConfig.endpoints.dexAssets, '')
1111
try {
1212
const assets = await fetchWithTimeout(uri.toString(), FETCH_TIMEOUT).then(async (res) => {
1313
const data = (await res.json()) as AstroportAssetsCached
@@ -72,7 +72,7 @@ export default async function getDexAssets(chainConfig: ChainConfig) {
7272
})
7373
return assets
7474
} catch (e) {
75-
setApiError(uri.toString(), e)
75+
setApiError(uri, e)
7676
return []
7777
}
7878
}

src/api/assets/getDexPools.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { setApiError } from 'utils/error'
2-
2+
import { getUrl } from 'utils/url'
33
export default async function getDexPools(chainConfig: ChainConfig) {
44
if (!chainConfig.endpoints.dexPools) return []
5-
const uri = new URL(chainConfig.endpoints.dexPools)
5+
const uri = getUrl(chainConfig.endpoints.dexPools, '')
66
try {
77
const pools = await fetch(uri.toString()).then(async (res) => {
88
const data = (await res.json()) as AstroportPoolsCached
99
return data.pools
1010
})
1111
return pools
1212
} catch (e) {
13-
setApiError(uri.toString(), e)
13+
setApiError(uri, e)
1414
return []
1515
}
1616
}

src/hooks/perps/usePerpsVault.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import useChainConfig from 'hooks/chain/useChainConfig'
77
import useClients from 'hooks/chain/useClients'
88
import { fetchWithTimeout } from 'utils/fetch'
99
import { BN } from 'utils/helpers'
10+
import { getUrl } from 'utils/url'
1011

1112
export default function usePerpsVault() {
1213
const chainConfig = useChainConfig()
@@ -21,7 +22,7 @@ export default function usePerpsVault() {
2122
try {
2223
if (chainConfig.endpoints.aprs.perpsVault) {
2324
const response = await fetchWithTimeout(
24-
chainConfig.endpoints.aprs.perpsVault,
25+
getUrl(chainConfig.endpoints.aprs.perpsVault, ''),
2526
FETCH_TIMEOUT,
2627
)
2728
if (response.ok) {

src/hooks/vaults/useVaultAprs.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FETCH_TIMEOUT } from 'constants/query'
44
import useChainConfig from 'hooks/chain/useChainConfig'
55
import { fetchWithTimeout } from 'utils/fetch'
66
import { convertAprToApy } from 'utils/parsers'
7+
import { getUrl } from 'utils/url'
78

89
export default function useVaultAprs() {
910
const chainConfig = useChainConfig()
@@ -16,7 +17,10 @@ export default function useVaultAprs() {
1617
async function getAprs(chainConfig: ChainConfig) {
1718
if (!chainConfig.farm) return []
1819
try {
19-
const response = await fetchWithTimeout(chainConfig.endpoints.aprs.vaults, FETCH_TIMEOUT)
20+
const response = await fetchWithTimeout(
21+
getUrl(chainConfig.endpoints.aprs.vaults, ''),
22+
FETCH_TIMEOUT,
23+
)
2024

2125
if (response.ok) {
2226
const data: AprResponse = await response.json()

src/utils/url.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ export const getUrl = (baseUrl: string, path: string = ''): string => {
33

44
if (isPlaceholder) return baseUrl + '/' + path
55

6-
const url = new URL(baseUrl.split('?')[0])
6+
let url = new URL(baseUrl)
7+
if (path !== '') url = new URL(path, url)
78

89
if (process.env.NEXT_PUBLIC_API_KEY)
9-
return `${url.href}${path}?x-apikey=${process.env.NEXT_PUBLIC_API_KEY}`
10+
url.searchParams.append('x-apikey', process.env.NEXT_PUBLIC_API_KEY)
1011

11-
return url.href + path
12+
return url.toString()
1213
}

0 commit comments

Comments
 (0)