Last active
July 2, 2018 18:13
-
-
Save enapupe/6c5c34596d3cdbc62d6c1f7419670c28 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 httpAdapter from 'axios/lib/adapters/http' | |
import { API_URL } from '../src/config/environment' | |
axios.defaults.host = API_URL | |
axios.defaults.adapter = httpAdapter |
This file contains hidden or 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 { API_URL } from '../config/environment' | |
import { parseURL, parseConfig } from '../utils/request' | |
const instance = axios.create({ | |
baseURL: API_URL, | |
headers: { 'Content-type': 'application/json; charset=UTF-8' }, | |
}) | |
const returnData = (response) => response.data | |
const handleResponseError = (error) => new Promise((resolve, reject) => reject(error.response.data)) | |
const parseParams = (url, config, data) => (fn) => { | |
if (fn === instance.delete || fn === instance.get) { | |
return fn(parseURL(url), parseConfig(config)) | |
.then(returnData) | |
.catch(handleResponseError) | |
} | |
return fn(parseURL(url), data, parseConfig(config)) | |
.then(returnData) | |
.catch(handleResponseError) | |
} | |
export const post = (...params) => parseParams(...params)(instance.post) | |
export const patch = (...params) => parseParams(...params)(instance.patch) | |
export const put = (...params) => parseParams(...params)(instance.put) | |
export const upload = (...params) => parseParams(...params)(instance.post) | |
export const del = (...params) => parseParams(...params)(instance.delete) | |
export const get = (...params) => parseParams(...params)(instance.get) | |
instance.getURL = (url) => API_URL + parseURL(url) | |
export default instance |
This file contains hidden or 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
const DEFAULT_AUTHORIZATION_KEYWORD = 'Token ' | |
const DEFAULT_AUTHORIZATION_HEADER = 'Authorization' | |
const ensureTrailingAndLeadingSlash = (url) => `/${url}/`.replace(/\/\//g, '/') | |
export const parseURL = (url) => { | |
if (Array.isArray(url)) { | |
return ensureTrailingAndLeadingSlash(url.join('/')) | |
} | |
return ensureTrailingAndLeadingSlash(url) | |
} | |
export const parseConfig = ({ key, ...config } = {}) => { | |
const newConfig = { headers: {}, ...config } | |
if (key) { | |
newConfig.headers[DEFAULT_AUTHORIZATION_HEADER] = `${DEFAULT_AUTHORIZATION_KEYWORD}${key}` | |
} | |
if (credentials) { | |
newConfig.withCredentials = true | |
} | |
return newConfig | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment