Skip to content

Commit

Permalink
feat: move auth and file api, cleanup api.ts buerokratt#627
Browse files Browse the repository at this point in the history
  • Loading branch information
joonasroosalung committed Aug 23, 2024
1 parent 3fc2a53 commit e56a75e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 90 deletions.
5 changes: 2 additions & 3 deletions GUI/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import {QueryClient, QueryClientProvider, QueryFunction, QueryKey} from '@tanstack/react-query';

import { api, genericApi, rasaApi, AxiosInterceptor} from 'services/api';
import { api, authApi, genericApi, rasaApi, AxiosInterceptor} from 'services/api';
import App from './App';
import { ToastProvider } from 'context/ToastContext';
import 'styles/main.scss';
import '../i18n';
import auth from "./services/auth";
import {mockedEndpoints} from "./services/mocked-endpoints";

const defaultQueryFn: QueryFunction | undefined = async ({ queryKey }) => {
Expand Down Expand Up @@ -37,7 +36,7 @@ const defaultQueryFn: QueryFunction | undefined = async ({ queryKey }) => {
}

if ((queryKey[0] as string).includes('auth')) {
const { data } = await auth.get(queryKey[0] as string);
const { data } = await authApi.get(queryKey[0] as string);
return data;
}
if(queryKey.includes('regex') && queryKey.includes('examples')) {
Expand Down
43 changes: 37 additions & 6 deletions GUI/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ const api = axios.create({
withCredentials: true,
});

const authApi = axios.create({
baseURL: import.meta.env.REACT_APP_AUTH_BASE_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
withCredentials: true,
});

const fileApi = axios.create({
baseURL: import.meta.env.REACT_APP_RUUTER_URL + 'rasa/',
headers: {
'Content-Type': 'multipart/form-data',
},
withCredentials: true,
});

const genericApi = axios.create({
baseURL: import.meta.env.REACT_APP_RUUTER_URL + 'generic/',
headers: {
Expand All @@ -32,37 +49,41 @@ const AxiosInterceptor = ({ children }) => {
const { t } = useTranslation();

useEffect(() => {

const resInterceptor = (response: any) => {
import.meta.env.DEBUG_ENABLED && console.log(response);
import.meta.env.DEBUG_ENABLED && console.debug(response);

return response;
}

const errInterceptor = (error: any) => {
import.meta.env.DEBUG_ENABLED && console.log(error);
import.meta.env.DEBUG_ENABLED && console.debug(error);

let message = t('global.notificationErrorMsg');

return Promise.reject(new Error(message));
}

const apiInterceptor = api.interceptors.response.use(resInterceptor, errInterceptor);
const authApiInterceptor = authApi.interceptors.response.use(resInterceptor, errInterceptor);
const fileApiInterceptor = fileApi.interceptors.response.use(resInterceptor, errInterceptor);
const genericInterceptor = genericApi.interceptors.response.use(resInterceptor, errInterceptor);
const rasaApiInterceptor = rasaApi.interceptors.response.use(resInterceptor, errInterceptor);

return () => {
api.interceptors.response.eject(apiInterceptor);
authApi.interceptors.response.eject(authApiInterceptor);
fileApi.interceptors.response.eject(fileApiInterceptor);
genericApi.interceptors.response.eject(genericInterceptor);
rasaApi.interceptors.response.eject(rasaApiInterceptor);
};

}, [t])
}, [t]);

return children;
}

const handleRequestError = (error: AxiosError) => {
console.log(error);
import.meta.env.DEBUG_ENABLED && console.debug(error);
if (error.response?.status === 401) {
// To be added: handle unauthorized requests
}
Expand All @@ -77,6 +98,16 @@ api.interceptors.request.use(
handleRequestError
);

authApi.interceptors.request.use(
(axiosRequest) => axiosRequest,
handleRequestError
);

fileApi.interceptors.request.use(
(axiosRequest) => axiosRequest,
handleRequestError
);

genericApi.interceptors.request.use(
(axiosRequest) => axiosRequest,
handleRequestError
Expand All @@ -87,4 +118,4 @@ rasaApi.interceptors.request.use(
handleRequestError
);

export { api, genericApi, rasaApi, AxiosInterceptor };
export { api, authApi, fileApi, genericApi, rasaApi, AxiosInterceptor };
40 changes: 0 additions & 40 deletions GUI/src/services/auth.ts

This file was deleted.

39 changes: 0 additions & 39 deletions GUI/src/services/file.api.ts

This file was deleted.

3 changes: 1 addition & 2 deletions GUI/src/services/intents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { rasaApi } from './api';
import fileApi from './file.api';
import { fileApi, rasaApi } from './api';
import { Intent } from 'types/intent';

export async function addIntent(newIntentData: { name: string }) {
Expand Down

0 comments on commit e56a75e

Please sign in to comment.