Files
varan-connect/src/api.ts
T
odiinnn 381c1dfd8b 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.
2025-11-05 21:44:17 +03:00

100 lines
2.9 KiB
TypeScript

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<ClientRequest>;
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<void> {
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<void> {
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<void> {
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<DataResponse> {
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;
}
}