Merge pull request #14 from rkheftan/feat/axios
feat: add axtios instanse and useApi hook
This commit is contained in:
56
index.html
56
index.html
@@ -1,32 +1,30 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Harmony club</title>
|
||||||
|
<!-- this script add for preventing initial theme flashing -->
|
||||||
|
<script>
|
||||||
|
(function () {
|
||||||
|
try {
|
||||||
|
const THEME_STORAGE_KEY = 'mui-mode';
|
||||||
|
const DARK_THEME_CLASS_NAME = 'dark';
|
||||||
|
const savedMode = localStorage.getItem(THEME_STORAGE_KEY);
|
||||||
|
const prefersDark = window.matchMedia(
|
||||||
|
'(prefers-color-scheme: dark)',
|
||||||
|
).matches;
|
||||||
|
if (savedMode === 'dark' || (!savedMode && prefersDark)) {
|
||||||
|
document.documentElement.classList.add(DARK_THEME_CLASS_NAME);
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
<head>
|
<body>
|
||||||
<meta charset="UTF-8" />
|
<div id="root"></div>
|
||||||
<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
</body>
|
||||||
<title>Harmony club</title>
|
</html>
|
||||||
<!-- this script add for preventing initial theme flashing -->
|
|
||||||
<script>
|
|
||||||
(function () {
|
|
||||||
try {
|
|
||||||
const THEME_STORAGE_KEY = 'mui-mode';
|
|
||||||
const DARK_THEME_CLASS_NAME = 'dark';
|
|
||||||
const savedMode = localStorage.getItem(THEME_STORAGE_KEY);
|
|
||||||
const prefersDark = window.matchMedia(
|
|
||||||
'(prefers-color-scheme: dark)',
|
|
||||||
).matches;
|
|
||||||
if (savedMode === 'dark' || (!savedMode && prefersDark)) {
|
|
||||||
document.documentElement.classList.add(DARK_THEME_CLASS_NAME);
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div id="root"></div>
|
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
|
|||||||
24
src/hooks/useApi.ts
Normal file
24
src/hooks/useApi.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
type ApiFunction<T, P> = (params?: P) => Promise<{ data: T }>;
|
||||||
|
|
||||||
|
export function useApi<T, P>(apiFunction: ApiFunction<T, P>) {
|
||||||
|
const [data, setData] = useState<T | null>(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<unknown>(null);
|
||||||
|
|
||||||
|
const execute = async (params?: P) => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const response = await apiFunction(params);
|
||||||
|
setData(response.data);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return { data, loading, error, execute };
|
||||||
|
}
|
||||||
56
src/lib/apiClient.ts
Normal file
56
src/lib/apiClient.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
// Function to get the token from local storage or state management
|
||||||
|
const getToken = () => localStorage.getItem('authToken');
|
||||||
|
|
||||||
|
const apiClient = axios.create({
|
||||||
|
// Define the base URL for all API requests
|
||||||
|
baseURL: 'https://accounts.business-harmony.com/swagger/index.html',
|
||||||
|
|
||||||
|
// Set a timeout for requests (e.g., 10 seconds)
|
||||||
|
timeout: 10000,
|
||||||
|
|
||||||
|
// Set default headers
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Accept: 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- Request Interceptor ---
|
||||||
|
// This runs BEFORE each request is sent
|
||||||
|
apiClient.interceptors.request.use(
|
||||||
|
(config) => {
|
||||||
|
const token = getToken();
|
||||||
|
if (token) {
|
||||||
|
// Add the authorization token to the headers
|
||||||
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
// Handle request errors
|
||||||
|
return Promise.reject(error);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// --- Response Interceptor ---
|
||||||
|
// This runs AFTER a response is received
|
||||||
|
// TODO: set global post api logic
|
||||||
|
// apiClient.interceptors.response.use(
|
||||||
|
// (response) => {
|
||||||
|
// // Any status code within the 2xx range will trigger this function
|
||||||
|
// return response;
|
||||||
|
// },
|
||||||
|
// (error) => {
|
||||||
|
// // Handle common errors globally
|
||||||
|
// if (error.response?.status === 401) {
|
||||||
|
// // e.g., redirect to login page if unauthorized
|
||||||
|
// console.error("Unauthorized! Redirecting to login...");
|
||||||
|
// // window.location.href = '/login';
|
||||||
|
// }
|
||||||
|
// return Promise.reject(error);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
export default apiClient;
|
||||||
2
vite.config.d.ts
vendored
2
vite.config.d.ts
vendored
@@ -1,2 +1,2 @@
|
|||||||
declare const _default: import("vite").UserConfig;
|
declare const _default: import('vite').UserConfig;
|
||||||
export default _default;
|
export default _default;
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import react from '@vitejs/plugin-react';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [react()],
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'@': path.resolve(__dirname, './src'),
|
'@': path.resolve(__dirname, './src'),
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user