From 548c8816f0b12e626e9b95cf87b3bae78d74e524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kapa=C5=82a?= Date: Mon, 13 Oct 2025 10:46:59 +0200 Subject: [PATCH] Refactor watchListCacheClient MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor watchListCacheClient to use Zodios endpoints and improve type inference. Signed-off-by: Szymon Kapała --- .../watchlist/watchListCacheClient.ts | 113 +++++++++++++----- 1 file changed, 81 insertions(+), 32 deletions(-) diff --git a/packages/core-mobile/app/services/watchlist/watchListCacheClient.ts b/packages/core-mobile/app/services/watchlist/watchListCacheClient.ts index f02fd69a8d..194253db4d 100644 --- a/packages/core-mobile/app/services/watchlist/watchListCacheClient.ts +++ b/packages/core-mobile/app/services/watchlist/watchListCacheClient.ts @@ -1,6 +1,7 @@ +import axios from 'axios' import { Zodios } from '@zodios/core' import Config from 'react-native-config' -import { array, string } from 'zod' +import { array, string, z } from 'zod' import Logger from 'utils/Logger' import { SimplePriceResponseSchema, @@ -11,37 +12,85 @@ import { if (!Config.PROXY_URL) Logger.warn('PROXY_URL is missing in env file. Watchlist is disabled.') -const baseUrl = Config.PROXY_URL + '/watchlist' - -export const watchListCacheClient = new Zodios( - baseUrl, - [ - { - method: 'get', - path: '/price', - alias: 'simplePrice', - response: SimplePriceResponseSchema - }, - // tokens endpoint is top 250 + additional markets - { - method: 'get', - path: '/tokens', - parameters: [{ name: 'currency', type: 'Query', schema: string() }], - alias: 'tokens', - response: array(TopTokenSchema) - }, - { - method: 'get', - path: '/trending', - alias: 'trending', - response: array(TrendingTokenSchema) - } - ], +const baseUrl = `${Config.PROXY_URL}/watchlist` + +// Zodios endpoint definitions +const endpoints = [ + { + method: 'get', + path: '/price', + alias: 'simplePrice', + response: SimplePriceResponseSchema + }, + { + method: 'get', + path: '/tokens', + parameters: [{ name: 'currency', type: 'Query', schema: string() }], + alias: 'tokens', + response: array(TopTokenSchema) + }, { - axiosConfig: { - headers: { - 'Content-Type': 'application/json' - } + method: 'get', + path: '/trending', + alias: 'trending', + response: array(TrendingTokenSchema) + } +] as const + +// Infer types from schemas for typings +export type SimplePriceResponse = z.infer +export type TopToken = z.infer +export type TrendingToken = z.infer + +// Dev (validated) and Prod (raw) clients +const devClient = new Zodios(baseUrl, endpoints, { + axiosConfig: { + headers: { 'Content-Type': 'application/json' } + } +}) + +const prodClient = axios.create({ + baseURL: baseUrl, + headers: { 'Content-Type': 'application/json' } +}) + +// Force validation on/off +const useValidation = __DEV__ //in normal use + +export const watchListCacheClient = { + /** + * GET /price + */ + async simplePrice(): Promise { + if (useValidation) { + return devClient.simplePrice() + } + const { data } = await prodClient.get('/price') + return data + }, + + /** + * GET /tokens?currency=... + */ + async tokens(params: { queries: { currency: string } }): Promise { + if (useValidation) { + // Match Zodios’ expected input shape exactly + return devClient.tokens(params) + } + const { data } = await prodClient.get('/tokens', { + params: params.queries + }) + return data + }, + + /** + * GET /trending + */ + async trending(params?: Record): Promise { + if (useValidation) { + return devClient.trending(params) } + const { data } = await prodClient.get('/trending') + return data } -) +}