Add frontend
This commit is contained in:
124
frontend/src/lib/api.ts
Normal file
124
frontend/src/lib/api.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
export const API_URL = 'http://127.0.0.1:8000';
|
||||
|
||||
export async function checkHealth(): Promise<'healthy' | 'unhealthy'> {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/health`);
|
||||
if (response.ok) {
|
||||
return 'healthy';
|
||||
} else {
|
||||
return 'unhealthy';
|
||||
}
|
||||
} catch {
|
||||
return 'unhealthy';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Type definitions for Script and Log
|
||||
*/
|
||||
export interface Log {
|
||||
id: number;
|
||||
script_id: number;
|
||||
message: string;
|
||||
created_at?: string;
|
||||
}
|
||||
export interface Script {
|
||||
id: number;
|
||||
name: string;
|
||||
script_content?: string;
|
||||
created_at?: string;
|
||||
}
|
||||
|
||||
// Fetch all scripts
|
||||
export async function fetchScripts(): Promise<Script[]> {
|
||||
const response = await fetch(`${API_URL}/script`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch scripts ' + response.statusText);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Add a new script
|
||||
export async function addScript(script: Omit<Script, 'id' | 'created_at'>): Promise<Script> {
|
||||
const response = await fetch(`${API_URL}/script`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(script)
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to add script');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Fetch a single script by ID
|
||||
export async function fetchScriptById(id: number): Promise<Script> {
|
||||
const response = await fetch(`${API_URL}/script/${id}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch script');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Update an existing script
|
||||
export async function updateScript(id: number, updatedScript: Partial<Script>): Promise<Script> {
|
||||
const response = await fetch(`${API_URL}/script/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(updatedScript)
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to update script');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Fetch logs for a specific script
|
||||
export async function fetchLogs(scriptId: number): Promise<Log[]> {
|
||||
const response = await fetch(`${API_URL}/script/${scriptId}/log`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch logs');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Add a new log to a specific script
|
||||
export async function addLog(scriptId: number, message: string): Promise<Log> {
|
||||
const response = await fetch(`${API_URL}/script/${scriptId}/log`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ message })
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to add log');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Delete a log from a specific script
|
||||
export async function deleteLog(scriptId: number, logId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/script/${scriptId}/log/${logId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete log');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Delete a script
|
||||
export async function deleteScript(id: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/script/${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete script');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
Reference in New Issue
Block a user