Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use URLSearchParams
Browse files Browse the repository at this point in the history
guanbinrui committed Sep 1, 2020
1 parent b97a0db commit db34445
Showing 2 changed files with 24 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/plugins/Trader/apis/coingecko/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Days } from '../../UI/PriceChartDaysControl'

const BASE_URL = 'https://api.coingecko.com/api/v3'

//#region get currency
@@ -118,7 +116,11 @@ export async function getCoinInfo(coinId: string) {
export type Stat = [number, number]

export async function getPriceStats(coinId: string, currencyId: string, days: number) {
const response = await fetch(`${BASE_URL}/coins/${coinId}/market_chart?vs_currency=${currencyId}&days=${days}`)
const params = new URLSearchParams()
params.append('vs_currency', currencyId)
params.append('days', String(days))

const response = await fetch(`${BASE_URL}/coins/${coinId}/market_chart?${params.toString()}`)
return response.json() as Promise<{
market_caps: Stat[]
prices: Stat[]
28 changes: 19 additions & 9 deletions src/plugins/Trader/apis/coinmarketcap/index.ts
Original file line number Diff line number Diff line change
@@ -77,7 +77,10 @@ export interface CoinInfo {
}

export async function getCoinInfo(id: string, currency: string) {
const response = await fetch(`${WIDGET_BASE_URL}/ticker/${id}/?ref=widget&convert=${currency}`)
const params = new URLSearchParams('ref=widget')
params.append('convert', currency)

const response = await fetch(`${WIDGET_BASE_URL}/ticker/${id}/?${params.toString()}`)
return response.json() as Promise<{
data: CoinInfo
status: Status
@@ -95,12 +98,15 @@ export async function getHistorical(
endDate: Date,
interval: string = '1d',
) {
const toUnixTimestamp = (d: Date) => Math.floor(d.getTime() / 1000)
const response = await fetch(
`${BASE_URL_v1_1}/cryptocurrency/quotes/historical?convert=${currency}&format=chart_crypto_details&id=${id}&interval=${interval}&time_end=${toUnixTimestamp(
endDate,
)}&time_start=${toUnixTimestamp(startDate)}`,
)
const toUnixTimestamp = (d: Date) => String(Math.floor(d.getTime() / 1000))
const params = new URLSearchParams('format=chart_crypto_details')
params.append('convert', currency)
params.append('id', id)
params.append('interval', interval)
params.append('time_end', toUnixTimestamp(endDate))
params.append('time_start', toUnixTimestamp(startDate))

const response = await fetch(`${BASE_URL_v1_1}/cryptocurrency/quotes/historical?${params.toString()}`)
return response.json() as Promise<{
data: Record<string, Record<string, Stat>>
status: Status
@@ -152,9 +158,13 @@ export interface Pair {
}
}
export async function getLatestMarketPairs(id: string, currency: string) {
const response = await fetch(
`${BASE_URL_v1}/cryptocurrency/market-pairs/latest?aux=num_market_pairs,market_url,price_quote,effective_liquidity,market_score,market_reputation&convert=${currency}&id=${id}&limit=40&sort=cmc_rank&start=1`,
const params = new URLSearchParams(
'aux=num_market_pairs,market_url,price_quote,effective_liquidity,market_score,market_reputation&limit=40&sort=cmc_rank&start=1',
)
params.append('convert', currency)
params.append('id', id)

const response = await fetch(`${BASE_URL_v1}/cryptocurrency/market-pairs/latest?${params.toString()}`)
return response.json() as Promise<{
data: {
id: number

0 comments on commit db34445

Please sign in to comment.