export async function fetch_(url, init = {}) {
    const token = localStorage.getItem("accessToken");

    const headers = {
        'Content-Type': 'application/json',
        ...(init.headers || {}),
        ...(token ? { Authorization: `Bearer ${token}` } : {}),
    };

    const response = await fetch(url, { ...init, headers });

    if (response.status === 304) {
        return Promise.reject(new Error("Not Modified"));
    }

    if (!response.ok) {
        const errorData = await response.json().catch(() => ({}));
        const error = new Error(response.statusText);
        error.data = errorData;
        throw error;
    }

    if (response.status === 204) {
        return null;
    }

    if (response.status === 203) {
        const data = await response.json();
        return {
            id: null,
            content: data.msg || "Non-authoritative information received",
            title: "Message",
            isMessage: true,
            ...data
        };
    }

    return response.json();
}
