From 381c1dfd8b6928eb8b072b3122c0b1ec6d0c3543 Mon Sep 17 00:00:00 2001 From: odiinnn <38395645+odiinnn@users.noreply.github.com> Date: Wed, 5 Nov 2025 21:44:17 +0300 Subject: [PATCH] Add data fetching functionality to API - Introduced a new `getData` function to fetch data from the API for a given connection ID. - Added `DataResponse` interface to define the structure of the response from the `/data/{id}` endpoint. - Updated `index.ts` to export the new `getData` function and `DataResponse` type for external use. --- src/api.ts | 36 +++++++++++++++++++++++++++++++++++- src/index.ts | 6 ++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/api.ts b/src/api.ts index 75c8108..6205ae1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,11 +1,21 @@ import type { ServerMessage, ConnectResponseMessage, - SignTransactionResponseMessage + SignTransactionResponseMessage, + ClientRequest } from './socket'; const API_URL = 'https://connect.varan.app'; +/** + * Response structure for GET /data/{id} endpoint + */ +export interface DataResponse { + id: string; + data: Array; + total: number; +} + /** * Sends a connect response message to the API * This will be received by the client's WebSocket and handled by the socket @@ -63,3 +73,27 @@ async function sendServerMessage( } } +/** + * Fetches data from the API for a given connection ID + * @param connectionId The unique connection ID from WebSocket + * @returns Promise resolving to the data response + */ +export async function getData(connectionId: string): Promise { + try { + const response = await fetch(`${API_URL}/data/${connectionId}`); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data: DataResponse = await response.json(); + return { + ...data, + data: data.data.filter(item => item.method === 'connect' || item.method === 'sT'), + } + } catch (error) { + console.error('[VARANINJECT] Error fetching data:', error); + throw error; + } +} + diff --git a/src/index.ts b/src/index.ts index 5d0b64f..4eb48c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,11 @@ export { injectVaranWallet } from './varan-wallet'; -export { sendConnect, sendSignedTransaction } from './api'; +export { sendConnect, sendSignedTransaction, getData } from './api'; +export type { DataResponse } from './api'; export type { ConnectRequest, SignTransactionRequest, + ClientRequest, ConnectResponseMessage, SignTransactionResponseMessage, - ServerMessage + ServerMessage } from './socket';