Created
April 18, 2018 08:52
-
-
Save klummy/1c0743e5c1ef7d99f688d1c0b7ed7222 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import config from '../config' | |
import {getUserIdToken} from './auth' | |
const axiosInstance = axios.create({ | |
baseURL: config.api[process.env.NODE_ENV].base + config.api[process.env.NODE_ENV].graphqlEndpoint, | |
timeout: 5000 | |
}) | |
axiosInstance.defaults.headers.post['Content-Type'] = 'application/json' | |
export default class RequestClient { | |
static combineQueries (args) { | |
const queries = Array.from(args).map(q => q.trim().slice(1, -1)) | |
const queryString = `{ ${queries.join('')} }` | |
return queryString | |
} | |
static async mutate () { | |
axiosInstance.defaults.headers.common['012-User-Token'] = await this.setAuthorization() | |
return axiosInstance.post('', { | |
query: `mutation ${this.combineQueries(arguments)}` | |
}) | |
} | |
static async query () { | |
axiosInstance.defaults.headers.common[config.auth.headerName] = await this.setAuthorization() | |
return axiosInstance.post('', { | |
query: `query ${this.combineQueries(arguments)}` | |
}) | |
} | |
static setAuthorization () { | |
return new Promise((resolve, reject) => { | |
getUserIdToken() | |
.then(token => { | |
resolve(token) | |
}) | |
.catch(error => { | |
console.log('Error retrieving token - ', error) | |
reject(error) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment