mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-16 21:50:35 +02:00
add initial files
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
import Axios from 'axios';
|
||||
import { authApi } from 'renderer/api/authApi';
|
||||
|
||||
export const api = Axios.create({
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
withCredentials: false,
|
||||
});
|
||||
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const { serverUrl, accessToken } = JSON.parse(
|
||||
localStorage.getItem('authentication') || '{}'
|
||||
);
|
||||
|
||||
config.baseURL = `${serverUrl}/api`;
|
||||
config.headers = {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
api.interceptors.response.use(
|
||||
(res) => {
|
||||
return res;
|
||||
},
|
||||
async (err) => {
|
||||
if (err.response && err.response.status === 401) {
|
||||
const { config } = err;
|
||||
if (err.response.data.error.message === 'jwt expired' && !config.sent) {
|
||||
config.sent = true;
|
||||
|
||||
const auth = JSON.parse(localStorage.getItem('authentication') || '{}');
|
||||
|
||||
const { accessToken } = (
|
||||
await authApi.refresh(auth.serverUrl, {
|
||||
refreshToken: auth.refreshToken,
|
||||
})
|
||||
).data;
|
||||
|
||||
localStorage.setItem(
|
||||
'authentication',
|
||||
JSON.stringify({ ...auth, accessToken })
|
||||
);
|
||||
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
};
|
||||
|
||||
return Axios(config);
|
||||
}
|
||||
|
||||
localStorage.setItem('authentication', '{}');
|
||||
window.location.reload();
|
||||
}
|
||||
return Promise.reject(err);
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './axios';
|
||||
export * from './queryClient';
|
||||
@@ -0,0 +1,33 @@
|
||||
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]
|
||||
>;
|
||||
Reference in New Issue
Block a user