frontend/src/services/apiClient.js

146 lines
3.1 KiB
JavaScript
Raw Normal View History

2025-09-14 17:44:32 +03:00
import { useAuthStore } from '@/stores/authStore.js'
2025-10-01 11:01:27 +03:00
import { BASE_URL } from '@/main.js'
2025-09-09 23:19:17 +03:00
2025-09-14 17:44:32 +03:00
let isRefreshing = false
let refreshPromise = null
function createConfig(options = {}) {
2025-09-14 17:44:32 +03:00
const authStore = useAuthStore()
const headers = {
2025-09-09 23:19:17 +03:00
'Content-Type': 'application/json',
...options.headers,
2025-09-14 17:44:32 +03:00
}
if (authStore.accessToken) {
2025-09-14 17:44:32 +03:00
headers['Authorization'] = `Bearer ${authStore.accessToken}`
}
return {
headers,
credentials: 'include',
...options,
2025-09-14 17:44:32 +03:00
}
}
async function refreshAccessToken() {
2025-09-14 17:44:32 +03:00
const authStore = useAuthStore()
2025-09-14 17:44:32 +03:00
if (isRefreshing) return refreshPromise
2025-09-14 17:44:32 +03:00
isRefreshing = true
2025-09-14 16:23:34 +03:00
refreshPromise = fetch(`${BASE_URL}/user/auth/refresh`, {
method: 'POST',
credentials: 'include',
})
2025-09-14 16:18:13 +03:00
.then(async (res) => {
2025-09-14 17:44:32 +03:00
if (!res.ok) throw new Error('Failed to refresh access token')
return await res.json()
})
2025-09-14 16:18:13 +03:00
.then((data) => {
2025-09-14 16:18:13 +03:00
const token = data.access_token
if (!token) {
2025-09-14 17:44:32 +03:00
throw new Error('No access_token in refresh response')
2025-09-14 16:18:13 +03:00
}
2025-09-14 17:44:32 +03:00
authStore.setToken(data.access_token)
2025-09-14 16:18:13 +03:00
2025-09-14 17:44:32 +03:00
return data
})
2025-09-14 16:18:13 +03:00
.catch((error) => {
2025-09-14 17:44:32 +03:00
throw error
})
2025-09-14 16:18:13 +03:00
.finally(() => {
2025-09-14 17:44:32 +03:00
isRefreshing = false
refreshPromise = null
})
2025-09-14 17:44:32 +03:00
return refreshPromise
}
async function request(url, options = {}, isRetry = false) {
2025-09-14 17:44:32 +03:00
const config = createConfig(options)
2025-09-14 17:44:32 +03:00
const response = await fetch(`${BASE_URL}${url}`, config)
if (response.status === 401 && !isRetry) {
try {
2025-09-14 17:44:32 +03:00
const data = await refreshAccessToken()
2025-09-14 17:44:32 +03:00
const token = data.access_token
2025-09-14 16:18:13 +03:00
if (!token) {
2025-09-14 17:44:32 +03:00
throw new Error('Refresh response did not contain access_token')
2025-09-14 16:18:13 +03:00
}
const newOptions = {
...options,
headers: {
...options.headers,
2025-09-14 16:18:13 +03:00
'Authorization': `Bearer ${token}`,
},
2025-09-14 17:44:32 +03:00
}
return await request(url, newOptions, true)
} catch (e) {
2025-09-14 17:44:32 +03:00
const authStore = useAuthStore()
authStore.forceLogout()
throw e
}
2025-09-09 23:19:17 +03:00
}
if (!response.ok) {
2025-09-14 17:44:32 +03:00
let errorData
try {
2025-09-14 17:44:32 +03:00
errorData = await response.json()
} catch {
2025-09-14 17:44:32 +03:00
errorData = {}
}
2025-09-14 17:44:32 +03:00
throw new Error(errorData.message || `HTTP Error: ${response.status}`)
}
2025-09-14 17:44:32 +03:00
let data = null
const contentType = response.headers.get('content-type')
if (contentType?.includes('application/json')) {
try {
2025-09-14 17:44:32 +03:00
data = await response.json()
} catch (e) {
2025-09-14 17:44:32 +03:00
console.warn('Failed to parse JSON response', e)
}
}
2025-09-14 17:44:32 +03:00
return {
status: response.status,
ok: response.ok,
data,
}
}
export const apiClient = {
2025-09-24 21:30:31 +03:00
get: (url, params = {}) => {
const queryString = new URLSearchParams(params).toString();
const fullUrl = queryString ? `${url}?${queryString}` : url;
return request(fullUrl, { method: 'GET' });
},
2025-10-18 14:10:21 +03:00
post: (url, data) => {
const isFormData = data instanceof FormData
return request(url, {
method: 'POST',
body: isFormData ? data : JSON.stringify(data),
headers: isFormData
? {}
: { 'Content-Type': 'application/json' }
})
},
put: (url, data) => request(url, {
method: 'PUT',
body: JSON.stringify(data),
}),
delete: (url) => request(url, { method: 'DELETE' }),
2025-09-14 17:44:32 +03:00
}