adding base project

This commit is contained in:
Azmat-7860
2026-10-07 09:07:57 +05:30
commit dc69f693f0
43 changed files with 5193 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
const BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:9292';
/**
* Make an HTTP request with automatic token header injection and error handling.
*/
export async function request(endpoint, options = {}) {
const token = localStorage.getItem('authsec_token');
const headers = {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options.headers,
};
const url = endpoint.startsWith('http') ? endpoint : `${BASE_URL}${endpoint}`;
try {
const response = await fetch(url, {
...options,
headers,
});
const contentType = response.headers.get('content-type');
let data;
if (contentType && contentType.includes('application/json')) {
data = await response.json();
} else {
data = await response.text();
}
if (!response.ok) {
const errorMessage =
(typeof data === 'object' && (data.operationMessage || data.message || data.error)) ||
(typeof data === 'string' && data) ||
`HTTP Error: ${response.status}`;
const err = new Error(errorMessage);
err.status = response.status;
err.data = data;
throw err;
}
return data;
} catch (error) {
// If network fetch fails completely (e.g. backend server is not running)
if (error.name === 'TypeError' && error.message.includes('fetch')) {
const netErr = new Error('Cannot reach Spring Boot server at ' + BASE_URL + '. Is backend running?');
netErr.isNetworkError = true;
throw netErr;
}
throw error;
}
}