Skip to content

Instantly share code, notes, and snippets.

@whisher
Created October 23, 2018 07:13
Show Gist options
  • Save whisher/c242dfbea2bd4cd3c68b72d9a4ca3be5 to your computer and use it in GitHub Desktop.
Save whisher/c242dfbea2bd4cd3c68b72d9a4ca3be5 to your computer and use it in GitHub Desktop.
import axios from "axios";
import { Storage } from "./utils/storage";
const instance = axios.create({
baseURL: process.env.API_URL,
timeout: 3000
});
const onRequestSuccess = config => {
console.log("request success", config);
const token = Storage.local.get("auth");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
};
const onRequestFail = error => {
console.log("request error", error);
return Promise.reject(error);
};
instance.interceptors.request.use(onRequestSuccess, onRequestFail);
const onResponseSuccess = response => {
console.log("response success", response);
return response;
};
const onResponseFail = error => {
console.log("response error", error);
const status = error.status || error.response.status;
if (status === 403 || status === 401) {
//action logout
}
return Promise.reject(error);
};
instance.interceptors.response.use(onResponseSuccess, onResponseFail);
export default instance;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment