mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-10 04:30:25 +02:00
Update libs
This commit is contained in:
+30
-13
@@ -1,23 +1,23 @@
|
|||||||
/* eslint-disable no-underscore-dangle */
|
/* eslint-disable no-underscore-dangle */
|
||||||
import Axios from 'axios';
|
import Axios from 'axios';
|
||||||
import { authApi } from '../api/authApi';
|
import { authApi } from '../api/auth.api';
|
||||||
|
|
||||||
export const api = Axios.create({
|
export const ax = Axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
withCredentials: false,
|
withCredentials: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
api.interceptors.request.use(
|
ax.interceptors.request.use(
|
||||||
(config) => {
|
(config) => {
|
||||||
const { serverUrl, accessToken } = JSON.parse(
|
const { state } = JSON.parse(
|
||||||
localStorage.getItem('authentication') || '{}'
|
localStorage.getItem('authentication') || '{}'
|
||||||
);
|
);
|
||||||
|
|
||||||
config.baseURL = `${serverUrl}/api`;
|
config.baseURL = `${state.serverUrl}/api`;
|
||||||
config.headers = {
|
config.headers = {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${state.accessToken}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
return config;
|
return config;
|
||||||
@@ -26,27 +26,28 @@ api.interceptors.request.use(
|
|||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
api.interceptors.response.use(
|
|
||||||
|
ax.interceptors.response.use(
|
||||||
(res) => {
|
(res) => {
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
async (err) => {
|
async (err) => {
|
||||||
if (err.response && err.response.status === 401) {
|
if (err.response && err.response.status === 401) {
|
||||||
const { config } = err;
|
const { config } = err;
|
||||||
|
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
||||||
|
|
||||||
if (err.response.data.error.message === 'jwt expired' && !config.sent) {
|
if (err.response.data.error.message === 'jwt expired' && !config.sent) {
|
||||||
config.sent = true;
|
config.sent = true;
|
||||||
|
|
||||||
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
|
||||||
|
|
||||||
const { accessToken } = (
|
const { accessToken } = (
|
||||||
await authApi.refresh(auth.serverUrl, {
|
await authApi.refresh(auth.state.serverUrl, {
|
||||||
refreshToken: auth.refreshToken,
|
refreshToken: auth.refreshToken,
|
||||||
})
|
})
|
||||||
).data;
|
).data;
|
||||||
|
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
'authentication',
|
'authentication',
|
||||||
JSON.stringify({ ...auth, accessToken })
|
JSON.stringify({ ...auth, state: { ...auth.state, accessToken } })
|
||||||
);
|
);
|
||||||
|
|
||||||
config.headers = {
|
config.headers = {
|
||||||
@@ -57,8 +58,24 @@ api.interceptors.response.use(
|
|||||||
return Axios(config);
|
return Axios(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem('authentication', '{}');
|
localStorage.setItem(
|
||||||
window.location.reload();
|
'authentication',
|
||||||
|
JSON.stringify({
|
||||||
|
...auth,
|
||||||
|
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (err.response.data.error.message === 'No auth token') {
|
||||||
|
localStorage.setItem(
|
||||||
|
'authentication',
|
||||||
|
JSON.stringify({
|
||||||
|
...auth,
|
||||||
|
state: { ...auth.state, accessToken: '', refreshToken: '' },
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// window.location.reload();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
export * from './axios';
|
|
||||||
export * from './queryClient';
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { AxiosError } from 'axios';
|
|
||||||
import {
|
|
||||||
QueryClient,
|
|
||||||
UseQueryOptions,
|
|
||||||
UseMutationOptions,
|
|
||||||
DefaultOptions,
|
|
||||||
} from 'react-query';
|
|
||||||
import { PromiseValue } from 'type-fest';
|
|
||||||
|
|
||||||
const queryConfig: DefaultOptions = {
|
|
||||||
queries: {
|
|
||||||
refetchOnWindowFocus: false,
|
|
||||||
retry: false,
|
|
||||||
useErrorBoundary: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const queryClient = new QueryClient({ defaultOptions: queryConfig });
|
|
||||||
|
|
||||||
export type ExtractFnReturnType<FnType extends (...args: any) => any> =
|
|
||||||
PromiseValue<ReturnType<FnType>>;
|
|
||||||
|
|
||||||
export type QueryConfig<QueryFnType extends (...args: any) => any> = Omit<
|
|
||||||
UseQueryOptions<ExtractFnReturnType<QueryFnType>>,
|
|
||||||
'queryKey' | 'queryFn'
|
|
||||||
>;
|
|
||||||
|
|
||||||
export type MutationConfig<MutationFnType extends (...args: any) => any> =
|
|
||||||
UseMutationOptions<
|
|
||||||
ExtractFnReturnType<MutationFnType>,
|
|
||||||
AxiosError,
|
|
||||||
Parameters<MutationFnType>[0]
|
|
||||||
>;
|
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import {
|
||||||
|
QueryClient,
|
||||||
|
UseQueryOptions,
|
||||||
|
UseMutationOptions,
|
||||||
|
DefaultOptions,
|
||||||
|
QueryCache,
|
||||||
|
} from '@tanstack/react-query';
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
|
import { PromiseValue } from 'type-fest';
|
||||||
|
import { toast } from '@/renderer/components';
|
||||||
|
|
||||||
|
const queryCache = new QueryCache({
|
||||||
|
onError: (error: any, query) => {
|
||||||
|
if (query.state.data !== undefined) {
|
||||||
|
toast.show({ message: `${error.message}`, type: 'error' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const queryConfig: DefaultOptions = {
|
||||||
|
mutations: {
|
||||||
|
retry: process.env.NODE_ENV === 'production',
|
||||||
|
},
|
||||||
|
queries: {
|
||||||
|
cacheTime: 1000 * 60 * 15,
|
||||||
|
onError: (err) => {
|
||||||
|
console.error(err);
|
||||||
|
},
|
||||||
|
refetchOnWindowFocus: process.env.NODE_ENV === 'production',
|
||||||
|
retry: process.env.NODE_ENV === 'production',
|
||||||
|
staleTime: 1000 * 5,
|
||||||
|
useErrorBoundary: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const queryClient = new QueryClient({
|
||||||
|
defaultOptions: queryConfig,
|
||||||
|
queryCache,
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ExtractFnReturnType<FnType extends (...args: any) => any> =
|
||||||
|
PromiseValue<ReturnType<FnType>>;
|
||||||
|
|
||||||
|
export type QueryConfig<QueryFnType extends (...args: any) => any> = Omit<
|
||||||
|
UseQueryOptions<ExtractFnReturnType<QueryFnType>>,
|
||||||
|
'queryKey' | 'queryFn'
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type MutationConfig<MutationFnType extends (...args: any) => any> =
|
||||||
|
UseMutationOptions<
|
||||||
|
ExtractFnReturnType<MutationFnType>,
|
||||||
|
AxiosError,
|
||||||
|
Parameters<MutationFnType>[0]
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type QueryOptions<TResponse> = {
|
||||||
|
cacheTime?: UseQueryOptions['cacheTime'];
|
||||||
|
enabled?: UseQueryOptions['enabled'];
|
||||||
|
onError?: (err: any) => void;
|
||||||
|
onSuccess?: (data: TResponse) => void;
|
||||||
|
refetchInterval?: number;
|
||||||
|
refetchIntervalInBackground?: UseQueryOptions['refetchIntervalInBackground'];
|
||||||
|
refetchOnWindowFocus?: boolean;
|
||||||
|
retry?: UseQueryOptions['retry'];
|
||||||
|
retryDelay?: UseQueryOptions['retryDelay'];
|
||||||
|
staleTime?: UseQueryOptions['staleTime'];
|
||||||
|
useErrorBoundary?: any;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user