import type { ServerMessage, ConnectResponseMessage, 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 * @param message The connect response message * @param connectionId The unique connection ID from WebSocket * @returns Promise resolving when the message is sent */ export async function sendConnect( message: ConnectResponseMessage, connectionId: string, ): Promise { await sendServerMessage(message, connectionId); } /** * Sends a signed transaction response message to the API * This will be received by the client's WebSocket and handled by the socket * @param message The sign transaction response message * @param connectionId The unique connection ID from WebSocket * @returns Promise resolving when the message is sent */ export async function sendSignedTransaction( message: SignTransactionResponseMessage, connectionId: string, ): Promise { await sendServerMessage(message, connectionId); } /** * Sends any server message to the API * This will be received by the client's WebSocket and handled by the socket * @param message The server message to send * @param connectionId The unique connection ID from WebSocket * @returns Promise resolving when the message is sent */ async function sendServerMessage( message: ServerMessage, connectionId: string, ): Promise { try { const response = await fetch(`${API_URL}/send/${connectionId}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(message), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error) { console.error('[VARANINJECT] Error sending server message:', error); throw error; } } /** * 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; } }