Newer
Older
job-tracker / frontend / src / api.js
import axios from 'axios';
const api=axios.create({
    baseURL: "/api",  // proxy forwards to http://localhost:8080/api
});

export async function apiGet(path) {
    const res=await fetch(path);
    if(!res.ok) throw new Error(await res.text());    
    return res.json();
}

export async function apiPost(path, body) {
    const res=await fetch(path, {
        method: "POST",
        headers: {"Content-Type": "application/json"},
        body: JSON.stringify(body)
    });
    if(!res.ok) throw new Error(await res.text());
    return res.json();
}

export async function apiPut(path, body) {
    const res=await fetch(path, {
        method: "PUT",
        headers: {"Content-Type": "application/json"},
        body: JSON.stringify(body)
    });
    if(!res.ok) throw new Error(await res.text());
    return res.json();
}

export async function apiDelete(path) {
    const response = await fetch(path, {
        method: 'DELETE'
    });
    if (!response.ok) {
        throw new Error(`DELETE ${path} failed: ${response.statusText}`);
    }
    return true; // optional: return anything you need
}
export default api;