feat: 前端代码
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import axios from 'axios';
|
||||
import type { GraphData, Node, Edge } from '../types/graph';
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/api';
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
export const graphApi = {
|
||||
async getData(): Promise<GraphData> {
|
||||
try {
|
||||
const response = await apiClient.get<GraphData>('/graph');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching graph data:', error);
|
||||
return { nodes: [], edges: [] };
|
||||
}
|
||||
},
|
||||
|
||||
async searchNodes(query: string): Promise<Node[]> {
|
||||
try {
|
||||
const response = await apiClient.get<Node[]>(`/search?q=${encodeURIComponent(query)}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error searching nodes:', error);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
async getNodeById(id: string): Promise<Node | null> {
|
||||
try {
|
||||
const response = await apiClient.get<Node>(`/nodes/${id}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching node:', error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
async getNeighbors(nodeId: string): Promise<{ nodes: Node[]; edges: Edge[] }> {
|
||||
try {
|
||||
const response = await apiClient.get(`/nodes/${nodeId}/neighbors`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching neighbors:', error);
|
||||
return { nodes: [], edges: [] };
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default apiClient;
|
||||
Reference in New Issue
Block a user