interceptor error types needed

This commit is contained in:
ntr committed 2022-08-14 22:56:46 +10:00
1 parent b0bca74ee2
commit 7121b2f4cd
4 files changed
+29 -24

No files matched your search

+14 -19
View File
@@ -1,53 +1,48 @@
import axios from 'axios';
import type { AxiosInstance } from 'axios';
const api = axios.create({
baseURL: 'http://localhost:41337',
withCredentials: true
});
// Add a response interceptor
api.interceptors.response.use(function onSuccess(response) {
return [null, response.data];
}, function onError(error) {
return [error.response.data.error, null];
});
const actions = {
getBundles: async function getBundles() {
const { data: bundles } = await api.get('/bundles');
return bundles;
return api.get('/bundles');
},
getUserOffers: async function getUserOffers(state, userId) {
const { data: offers } = await api.get(`/user/${userId}/offers`);
return offers;
return api.get(`/user/${userId}/offers`);
},
getOffers: async function getOffers(userId) {
const { data } = await api.get(`/user/${userId}/offers`);
return data;
return api.get(`/user/${userId}/offers`);
},
getLogin: async function getLogin() {
try {
const { data: user } = await api.get('/auth/login')
console.log(user);
return user;
} catch (e) {
return null;
}
return api.get('/auth/login')
},
postLogin: async function postLogin(email, password) {
const { data } = await api.post('/auth/login', {
return api.post('/auth/login', {
email,
password,
// discogs,
});
return data;
},
postRegister: async function postRegister(email, password) {
const { data } = await api.post('/auth/register', {
return api.post('/auth/register', {
email,
password,
});
return data;
},
};
+3 -2
View File
@@ -19,8 +19,8 @@ function App() {
useEffect(() => {
async function getLogin() {
const user = await actions.getLogin();
if (!user) return route('/login');
const [error, user] = await actions.getLogin();
if (error) return route('/login');
setUser(user);
route('/search');
}
@@ -30,6 +30,7 @@ function App() {
function loginSuccess(user) {
setUser(user);
route('/search');
}
return (
+1 -1
View File
@@ -6,7 +6,7 @@ const Header = ({ user }) => {
<nav>
<a href="/search">Offers</a>
<a href="/search">Search</a>
<a href={`/user/${user.id}`}>User</a>
<a href={`/user/${user.id}`}>List</a>
</nav>
);
+11 -2
View File
@@ -9,13 +9,22 @@ const Login = ({ success }) => {
const [confirmPassword, setConfirmPassword] = useState('');
async function postLogin() {
const user = await actions.postLogin(email, password);
const [error, user] = await actions.postLogin(email, password);
console.log(error, user);
if (error) return console.warn(error);
success(user);
}
async function postRegister() {
const [error, user] = await actions.postRegister(email, password);
if (error) return console.warn(error);
success(user);
}
function handleSubmit(e) {
e.preventDefault();
postLogin();
if (mode === 'login') return postLogin()
if (mode === 'register') return postRegister();
}
if (mode === 'register') return (