
export const jwtDecode = (token) => {
    try {
        let arrToken = token.split('.')
        let base64Token = atob(arrToken[1])
        return JSON.parse(base64Token)
    }
    catch (e) {
        console.log('Лажа, Бро ' + e);
    }
}

export const backURL = 'http://hipstagram.asmer.fs.a-level.com.ua'

const getGQL = url =>
    async (query, variables = {}) => {
        let obj = await fetch(url, {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
                ...(localStorage.authToken ? { Authorization: 'Bearer ' + localStorage.authToken } :
                    sessionStorage.authToken ? { Authorization: 'Bearer ' + sessionStorage.authToken } : {})
            },
            body: JSON.stringify({ query, variables })
        })
        let a = await obj.json()
        if (!a.data && a.errors)
            throw new Error(JSON.stringify(a.errors))
        return a.data[Object.keys(a.data)[0]]
    }

export const gql = getGQL(backURL + '/graphql');