interface APIResponse<T> {
    success?: boolean
    payload?: T[]
    error?: string

}

export const API_HOST = (url = "") => `http://localhost:8000/api/v1/${url}`;

export const apiCall = async function<T>(url: string, method: string = 'GET'): Promise<APIResponse<T>> {
    try {
        const data = await fetch(`${API_HOST}/${url}`, {
            headers: { "Accept": "application/json" },
            method,
        });

        const { success, payload } = await data.json();

        if (success) {
            return { payload };
        }

        return { error: payload };

    } catch (error) {
        console.trace(`API CALL FAILED - ${url}: ${error}`);
        return { error };
    }
}
