feat: introduce react-query and react-query-devtools
This commit is contained in:
34
packages/web/src/helpers/api.js
Normal file
34
packages/web/src/helpers/api.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import axios from 'axios';
|
||||
|
||||
import appConfig from 'config/app.js';
|
||||
import * as URLS from 'config/urls.js';
|
||||
import { getItem, removeItem } from 'helpers/storage.js';
|
||||
|
||||
const api = axios.create({
|
||||
...axios.defaults,
|
||||
baseURL: appConfig.restApiUrl,
|
||||
headers: {
|
||||
Authorization: getItem('token'),
|
||||
},
|
||||
});
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
const status = error.response?.status;
|
||||
|
||||
if (status === 401) {
|
||||
removeItem('token');
|
||||
|
||||
// hard reload to clear all state
|
||||
if (window.location.pathname !== URLS.LOGIN) {
|
||||
window.location.href = URLS.LOGIN;
|
||||
}
|
||||
}
|
||||
|
||||
// re-throw what's already intercepted here.
|
||||
throw error;
|
||||
},
|
||||
);
|
||||
|
||||
export default api;
|
@@ -1,8 +1,14 @@
|
||||
const NAMESPACE = 'automatisch';
|
||||
const makeKey = (key) => `${NAMESPACE}.${key}`;
|
||||
|
||||
export const setItem = (key, value) => {
|
||||
return localStorage.setItem(makeKey(key), value);
|
||||
};
|
||||
|
||||
export const getItem = (key) => {
|
||||
return localStorage.getItem(makeKey(key));
|
||||
};
|
||||
|
||||
export const removeItem = (key) => {
|
||||
return localStorage.removeItem(makeKey(key));
|
||||
};
|
||||
|
Reference in New Issue
Block a user