Querying Elixir API from a UI

I think if you’re using the browser fetchAPI you need to create the headers from the constructor, I use this:

export function requester({ method, url, data, accept, content_type }, token) {
    let body = typeof data === "string" ? data : JSON.stringify(data);

    token = token ? token : "";
    content_type = content_type ? content_type : "application/json";
    accept = accept ? accept : "application/json";
    
    if (process && process.server) {
        const fetch = require("node-fetch");
        let headers = {
            "Bearer": token,
            "Content-Type": content_type,
            "Accept": accept
        };
        let request = {
            method: method,
            headers: headers,
            body: body,
            mode: "cors"
        };

        return fetch(url, request)
            .then(response => response.json())
            .catch(error => { return {error: error.message} });
        
    } else {
        
        let headers = new Headers({
            "Bearer": token,
            "Content-Type": content_type,
            "Accept": accept
        });
        
        let request = new Request(url, {
            method: method,
            headers: headers,
            body: body,
            mode: "cors"
        });
        
        return fetch(request)
            .then(response => response.json())
            .catch(error => { return {error: error.message} });
    }
}