add initial files

This commit is contained in:
jeffvli
2022-07-25 19:40:16 -07:00
commit e8b612c974
283 changed files with 62820 additions and 0 deletions
+65
View File
@@ -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);
}
);
+2
View File
@@ -0,0 +1,2 @@
export * from './axios';
export * from './queryClient';
+33
View File
@@ -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]
>;