From 8d9257d904b30b93801342da52fb29cbea402444 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 18 Jan 2026 05:33:46 +0300 Subject: [PATCH] Add files via upload --- src/api/currency.ts | 53 +++++++++++++++++++++++---------------------- src/api/miniapp.ts | 39 +++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/api/currency.ts b/src/api/currency.ts index 3288dcf..e5aa9f3 100644 --- a/src/api/currency.ts +++ b/src/api/currency.ts @@ -1,6 +1,8 @@ // Currency exchange rate API // Uses free exchangerate.host API +import axios from 'axios' + interface ExchangeRates { USD: number CNY: number @@ -33,39 +35,38 @@ export const currencyApi = { try { // Try primary API (exchangerate.host) - get RUB based rates - const response = await fetch( - 'https://api.exchangerate.host/latest?base=RUB&symbols=USD,CNY,IRR' - ) + const response = await axios.get<{ + success?: boolean + rates?: { USD?: number; CNY?: number; IRR?: number } + }>('https://api.exchangerate.host/latest', { + params: { base: 'RUB', symbols: 'USD,CNY,IRR' }, + }) - if (response.ok) { - const data = await response.json() - if (data.success && data.rates) { - // API returns how much of each currency equals 1 RUB - // We need the inverse: how many RUB equals 1 of each currency - const rates: ExchangeRates = { - USD: data.rates.USD ? 1 / data.rates.USD : FALLBACK_RATES.USD, - CNY: data.rates.CNY ? 1 / data.rates.CNY : FALLBACK_RATES.CNY, - IRR: data.rates.IRR ? 1 / data.rates.IRR : FALLBACK_RATES.IRR, - } - cachedRates = { rates, timestamp: Date.now() } - return rates + if (response.data.success && response.data.rates) { + // API returns how much of each currency equals 1 RUB + // We need the inverse: how many RUB equals 1 of each currency + const rates: ExchangeRates = { + USD: response.data.rates.USD ? 1 / response.data.rates.USD : FALLBACK_RATES.USD, + CNY: response.data.rates.CNY ? 1 / response.data.rates.CNY : FALLBACK_RATES.CNY, + IRR: response.data.rates.IRR ? 1 / response.data.rates.IRR : FALLBACK_RATES.IRR, } + cachedRates = { rates, timestamp: Date.now() } + return rates } // Try backup API (open.er-api.com) - const backupResponse = await fetch('https://open.er-api.com/v6/latest/RUB') + const backupResponse = await axios.get<{ + rates?: { USD?: number; CNY?: number; IRR?: number } + }>('https://open.er-api.com/v6/latest/RUB') - if (backupResponse.ok) { - const backupData = await backupResponse.json() - if (backupData.rates) { - const rates: ExchangeRates = { - USD: backupData.rates.USD ? 1 / backupData.rates.USD : FALLBACK_RATES.USD, - CNY: backupData.rates.CNY ? 1 / backupData.rates.CNY : FALLBACK_RATES.CNY, - IRR: backupData.rates.IRR ? 1 / backupData.rates.IRR : FALLBACK_RATES.IRR, - } - cachedRates = { rates, timestamp: Date.now() } - return rates + if (backupResponse.data.rates) { + const rates: ExchangeRates = { + USD: backupResponse.data.rates.USD ? 1 / backupResponse.data.rates.USD : FALLBACK_RATES.USD, + CNY: backupResponse.data.rates.CNY ? 1 / backupResponse.data.rates.CNY : FALLBACK_RATES.CNY, + IRR: backupResponse.data.rates.IRR ? 1 / backupResponse.data.rates.IRR : FALLBACK_RATES.IRR, } + cachedRates = { rates, timestamp: Date.now() } + return rates } // Return fallback rates if both APIs fail diff --git a/src/api/miniapp.ts b/src/api/miniapp.ts index 3fc2157..0801c30 100644 --- a/src/api/miniapp.ts +++ b/src/api/miniapp.ts @@ -1,4 +1,6 @@ -export interface MiniappCreatePaymentPayload { +import axios, { AxiosError } from 'axios' + +export interface MiniappCreatePaymentPayload { initData: string method: string amountKopeks?: number | null @@ -16,21 +18,26 @@ export const miniappApi = { createPayment: async ( payload: MiniappCreatePaymentPayload ): Promise => { - const res = await fetch('/miniapp/payments/create', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - initData: payload.initData || '', - method: payload.method, - amountKopeks: payload.amountKopeks ?? null, - option: payload.option ?? null, - }), - }) - const data = await res.json().catch(() => ({})) - if (!res.ok) { - const message = (data && (data.detail || data.message)) || 'Failed to create payment' - throw new Error(String(message)) + try { + const response = await axios.post( + '/miniapp/payments/create', + { + initData: payload.initData || '', + method: payload.method, + amountKopeks: payload.amountKopeks ?? null, + option: payload.option ?? null, + }, + { + headers: { 'Content-Type': 'application/json' }, + } + ) + return response.data + } catch (error) { + const axiosError = error as AxiosError<{ detail?: string; message?: string }> + const message = axiosError.response?.data?.detail + || axiosError.response?.data?.message + || 'Failed to create payment' + throw new Error(message) } - return data as MiniappCreatePaymentResponse }, }