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.
This commit is contained in:
1 parent
fe69920c76
commit
381c1dfd8b
2 files changed
+38
-2
No files matched your search
+35
-1
@@ -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<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
|
||||
@@ -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<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;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -1,8 +1,10 @@
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user