import Config from "@/config/config"; const apiServer = Config.apiServer; import { RunType } from "@/types/run"; async function fetchRun(runId: string) { const url = `${apiServer}/runs/${runId}`; try { const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", }, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error: any) { console.error("Error fetching data:", error.message); } } async function fetchRuns(projectId: string) { const url = `${apiServer}/runs?projectId=${projectId}`; try { const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", }, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error: any) { console.error("Error fetching data:", error.message); } } async function createRun(projectId: string) { const newTestRun = { name: "untitled run", configurations: 0, description: "", state: 0, projectId: projectId, }; const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(newTestRun), }; const url = `${apiServer}/runs`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error: any) { console.error("Error creating new test run:", error); throw error; } } async function updateRun(updateTestRun: RunType) { const fetchOptions = { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(updateTestRun), }; const url = `${apiServer}/runs/${updateTestRun.id}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error: any) { console.error("Error updating run:", error); throw error; } } async function deleteRun(runId: number) { const fetchOptions = { method: "DELETE", headers: { "Content-Type": "application/json", }, }; const url = `${apiServer}/runs/${runId}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } } catch (error: any) { console.error("Error deleting run:", error); throw error; } } async function createRunCase(runId: string, caseId: number) { const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json", }, }; const url = `${apiServer}/runcases?runId=${runId}&caseId=${caseId}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error: any) { console.error("Error creating new runcase:", error); throw error; } } async function deleteRunCase(runId: string, caseId: number) { const fetchOptions = { method: "DELETE", headers: { "Content-Type": "application/json", }, }; const url = `${apiServer}/runcases?runId=${runId}&caseId=${caseId}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } } catch (error: any) { console.error("Error deleting runcase:", error); throw error; } } export { fetchRun, fetchRuns, createRun, updateRun, deleteRun, createRunCase, deleteRunCase };