在Windows 10中,我使用react-router-dom
5.2.0和react-redux
7.2.5和react
17.0.2和axios
0.21.4以及WebStorm 2023.1.3 IDE。
axiosInstance.js:
import jwt_decode from 'jwt-decode';
import dayjs from 'dayjs';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { updateAccessToken } from '../actions/userActions';
const baseURL = 'http://127.0.0.1:8000';
export const axiosInstance = (userInfo, dispatch) => {
const instance = axios.create({
'mode': 'cors',
baseURL: baseURL,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo?.access}`,
}
});
instance.interceptors.request.use(async (req) => {
const user = jwt_decode(userInfo.access);
const isExpired = dayjs.unix(user.exp).diff(dayjs()) < 5000;
if (!(isExpired)) {
return req
}
const response = await axios.post(
'/api/v1/users/token/refresh/', { refresh: userInfo.refresh },
);
dispatch(updateAccessToken(response.data))
req.headers.Authorization = `Bearer ${response.data.access}`;
return req;
});
return instance
}
字符串
userActions.js:
export const getUserDetailsAction = () => async (dispatch, getState) => {
try {
dispatch({
type: USER_DETAILS_REQUEST
});
const { userLogin: { userInfo } } = getState();
const authRequestAxios = axiosInstance(userInfo, dispatch)
const { data } = await authRequestAxios.get(`/api/v1/users/${userInfo._id}/`);
dispatch({
type: USER_DETAILS_SUCCESS,
payload: data,
});
localStorage.setItem('userDetails', JSON.stringify(data));
} catch (error) {
dispatch({ // USER FURTHER INFORMATION FAILED
type: USER_DETAILS_FAILED,
payload: error.response && error.response.data.detail ? error.response.data.detail : error.message,
});
}
型
错误IDE:
参数类型{mode: string, headers: {Authorization: string, "Content-Type": string}, baseURL: string}
不能分配给参数类型 * AxiosRequestConfig| undefined * 对象文字只能指定已知属性,但以下是extra: 'mode'
1条答案
按热度按时间szqfcxe21#