From db34445831803f01beaf05fcdcea1129bc875f76 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Tue, 1 Sep 2020 19:07:18 +0800 Subject: [PATCH] refactor: use URLSearchParams --- src/plugins/Trader/apis/coingecko/index.ts | 8 ++++-- .../Trader/apis/coinmarketcap/index.ts | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/plugins/Trader/apis/coingecko/index.ts b/src/plugins/Trader/apis/coingecko/index.ts index 8fc1768d189d..7745c81245e9 100644 --- a/src/plugins/Trader/apis/coingecko/index.ts +++ b/src/plugins/Trader/apis/coingecko/index.ts @@ -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[] diff --git a/src/plugins/Trader/apis/coinmarketcap/index.ts b/src/plugins/Trader/apis/coinmarketcap/index.ts index a82aaeac8d4f..38b9e3d4bd2e 100644 --- a/src/plugins/Trader/apis/coinmarketcap/index.ts +++ b/src/plugins/Trader/apis/coinmarketcap/index.ts @@ -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> 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