added some api

This commit is contained in:
Jelaletdin12
2025-11-13 21:56:30 +05:00
parent fdec9e4b0e
commit 21b9e88c5c
22 changed files with 2268 additions and 930 deletions

View File

@@ -22,7 +22,6 @@ const removeTokenFromCookie = (name: string): void => {
}
const getToken = (): string | null => {
// Check cookies first (more secure)
const authToken = getTokenFromCookie("authToken")
if (authToken) return authToken
@@ -32,6 +31,17 @@ const getToken = (): string | null => {
return null
}
/**
* Map internal locale codes to API language codes
*/
const localeToApiLang = (locale: string): string => {
const mapping: Record<string, string> = {
tm: "tk",
ru: "ru",
}
return mapping[locale] || locale
}
/**
* Centralized API client with interceptors
*/
@@ -68,13 +78,27 @@ class APIClient {
config.headers.Authorization = `Bearer ${token}`
}
// Add language parameter if i18n is available
if (typeof window !== "undefined" && (window as any).i18n) {
const lang = (window as any).i18n.language || "tm"
const url = config.url || ""
config.url = `${url}${url.includes("?") ? "&" : "?"}lang=${lang}`
// Add language parameter
let lang = "tk" // default fallback
if (typeof window !== "undefined") {
// Try to get from i18n
if ((window as any).i18n?.language) {
lang = localeToApiLang((window as any).i18n.language)
}
// Try to get from pathname as fallback
else {
const pathLocale = window.location.pathname.split("/")[1]
if (pathLocale === "tm" || pathLocale === "ru") {
lang = localeToApiLang(pathLocale)
}
}
}
const url = config.url || ""
const separator = url.includes("?") ? "&" : "?"
config.url = `${url}${separator}lang=${lang}`
return config
},
(error) => Promise.reject(error)
@@ -89,7 +113,6 @@ class APIClient {
// Handle 401 errors
if (error.response?.status === 401 && !originalRequest._retry) {
if (this.isRefreshing) {
// Queue requests while refreshing
return new Promise((resolve, reject) => {
this.failedQueue.push({ resolve, reject })
})
@@ -101,7 +124,6 @@ class APIClient {
this.isRefreshing = true
try {
// Attempt to get guest token
const guestTokenResponse = await axios.post(
`${this.baseUrl}/api/v1/auth/guest-token`,
{},
@@ -203,10 +225,7 @@ class APIClient {
}
}
// Export singleton instance
export const apiClient = new APIClient()
// Export helper functions
export const setAuthToken = (token: string) => apiClient.setAuthToken(token)
export const setGuestToken = (token: string) => apiClient.setGuestToken(token)
export const clearAuthToken = () => apiClient.clearAuthToken()