fixed some bugs
This commit is contained in:
172
lib/api.ts
172
lib/api.ts
@@ -1,61 +1,24 @@
|
||||
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from "axios"
|
||||
// lib/api.ts
|
||||
|
||||
/**
|
||||
* Token management utilities
|
||||
*/
|
||||
const getTokenFromCookie = (name: string): string | null => {
|
||||
if (typeof document === "undefined") return null
|
||||
const value = `; ${document.cookie}`
|
||||
const parts = value.split(`; ${name}=`)
|
||||
if (parts.length === 2) return parts.pop()?.split(";").shift() || null
|
||||
return null
|
||||
}
|
||||
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from "axios";
|
||||
import TokenStorage from "./tokenStorage";
|
||||
|
||||
const setTokenInCookie = (name: string, token: string): void => {
|
||||
if (typeof document === "undefined") return
|
||||
document.cookie = `${name}=${token}; path=/; secure; SameSite=Strict; max-age=2592000`
|
||||
}
|
||||
|
||||
const removeTokenFromCookie = (name: string): void => {
|
||||
if (typeof document === "undefined") return
|
||||
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;`
|
||||
}
|
||||
|
||||
const getToken = (): string | null => {
|
||||
const authToken = getTokenFromCookie("authToken")
|
||||
if (authToken) return authToken
|
||||
|
||||
const guestToken = getTokenFromCookie("guestToken")
|
||||
if (guestToken) return guestToken
|
||||
|
||||
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
|
||||
}
|
||||
const mapping: Record<string, string> = { tm: "tk", ru: "ru" };
|
||||
return mapping[locale] || locale;
|
||||
};
|
||||
|
||||
/**
|
||||
* Centralized API client with interceptors
|
||||
*/
|
||||
class APIClient {
|
||||
private client: AxiosInstance
|
||||
private baseUrl: string
|
||||
private isRefreshing = false
|
||||
private client: AxiosInstance;
|
||||
private baseUrl: string;
|
||||
private isRefreshing = false;
|
||||
private failedQueue: Array<{
|
||||
resolve: (value?: unknown) => void
|
||||
reject: (reason?: unknown) => void
|
||||
}> = []
|
||||
resolve: (value?: unknown) => void;
|
||||
reject: (reason?: unknown) => void;
|
||||
}> = [];
|
||||
|
||||
constructor() {
|
||||
this.baseUrl = process.env.NEXT_PUBLIC_API_URL || "https://api.example.com"
|
||||
this.baseUrl = process.env.NEXT_PUBLIC_API_URL || "https://api.example.com";
|
||||
|
||||
this.client = axios.create({
|
||||
baseURL: `${this.baseUrl}/api/v1`,
|
||||
@@ -64,64 +27,60 @@ class APIClient {
|
||||
"Content-Type": "application/json",
|
||||
"Api-Token": process.env.NEXT_PUBLIC_API_TOKEN || "123",
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
this.setupInterceptors()
|
||||
this.setupInterceptors();
|
||||
}
|
||||
|
||||
private setupInterceptors(): void {
|
||||
// Request interceptor
|
||||
this.client.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = getToken()
|
||||
const token = TokenStorage.getActiveToken();
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// Add language parameter
|
||||
let lang = "tk" // default fallback
|
||||
let lang = "tk";
|
||||
|
||||
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]
|
||||
lang = localeToApiLang((window as any).i18n.language);
|
||||
} else {
|
||||
const pathLocale = window.location.pathname.split("/")[1];
|
||||
if (pathLocale === "tm" || pathLocale === "ru") {
|
||||
lang = localeToApiLang(pathLocale)
|
||||
lang = localeToApiLang(pathLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const url = config.url || ""
|
||||
const separator = url.includes("?") ? "&" : "?"
|
||||
config.url = `${url}${separator}lang=${lang}`
|
||||
const url = config.url || "";
|
||||
const separator = url.includes("?") ? "&" : "?";
|
||||
config.url = `${url}${separator}lang=${lang}`;
|
||||
|
||||
return config
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
);
|
||||
|
||||
// Response interceptor
|
||||
this.client.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const originalRequest = error.config
|
||||
const originalRequest = error.config;
|
||||
|
||||
// Handle 401 errors
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
if (this.isRefreshing) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.failedQueue.push({ resolve, reject })
|
||||
this.failedQueue.push({ resolve, reject });
|
||||
})
|
||||
.then(() => this.client(originalRequest))
|
||||
.catch((err) => Promise.reject(err))
|
||||
.catch((err) => Promise.reject(err));
|
||||
}
|
||||
|
||||
originalRequest._retry = true
|
||||
this.isRefreshing = true
|
||||
originalRequest._retry = true;
|
||||
this.isRefreshing = true;
|
||||
|
||||
try {
|
||||
const guestTokenResponse = await axios.post(
|
||||
@@ -133,30 +92,29 @@ class APIClient {
|
||||
"Api-Token": process.env.NEXT_PUBLIC_API_TOKEN || "123",
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
const newToken = guestTokenResponse.data?.token || guestTokenResponse.data?.data
|
||||
const newToken = guestTokenResponse.data?.token || guestTokenResponse.data?.data;
|
||||
|
||||
if (newToken) {
|
||||
setTokenInCookie("guestToken", newToken)
|
||||
this.processQueue(null)
|
||||
return this.client(originalRequest)
|
||||
TokenStorage.setGuestToken(newToken);
|
||||
this.processQueue(null);
|
||||
return this.client(originalRequest);
|
||||
}
|
||||
} catch (refreshError) {
|
||||
this.processQueue(refreshError)
|
||||
this.clearAuthToken()
|
||||
this.processQueue(refreshError);
|
||||
TokenStorage.clearTokens();
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.location.href = "/login"
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
return Promise.reject(refreshError)
|
||||
return Promise.reject(refreshError);
|
||||
} finally {
|
||||
this.isRefreshing = false
|
||||
this.isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle HTML error responses
|
||||
if (
|
||||
error.response?.data &&
|
||||
typeof error.response.data === "string" &&
|
||||
@@ -168,64 +126,44 @@ class APIClient {
|
||||
...error.response,
|
||||
data: { message: "Server returned HTML instead of JSON" },
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
return Promise.reject(error);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private processQueue(error: any): void {
|
||||
this.failedQueue.forEach((promise) => {
|
||||
if (error) {
|
||||
promise.reject(error)
|
||||
promise.reject(error);
|
||||
} else {
|
||||
promise.resolve()
|
||||
promise.resolve();
|
||||
}
|
||||
})
|
||||
this.failedQueue = []
|
||||
});
|
||||
this.failedQueue = [];
|
||||
}
|
||||
|
||||
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return this.client.get<T>(url, config)
|
||||
return this.client.get<T>(url, config);
|
||||
}
|
||||
|
||||
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return this.client.post<T>(url, data, config)
|
||||
return this.client.post<T>(url, data, config);
|
||||
}
|
||||
|
||||
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return this.client.put<T>(url, data, config)
|
||||
return this.client.put<T>(url, data, config);
|
||||
}
|
||||
|
||||
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return this.client.patch<T>(url, data, config)
|
||||
return this.client.patch<T>(url, data, config);
|
||||
}
|
||||
|
||||
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
return this.client.delete<T>(url, config)
|
||||
}
|
||||
|
||||
setAuthToken(token: string): void {
|
||||
removeTokenFromCookie("guestToken")
|
||||
setTokenInCookie("authToken", token)
|
||||
this.client.defaults.headers.common["Authorization"] = `Bearer ${token}`
|
||||
}
|
||||
|
||||
setGuestToken(token: string): void {
|
||||
setTokenInCookie("guestToken", token)
|
||||
this.client.defaults.headers.common["Authorization"] = `Bearer ${token}`
|
||||
}
|
||||
|
||||
clearAuthToken(): void {
|
||||
removeTokenFromCookie("authToken")
|
||||
removeTokenFromCookie("guestToken")
|
||||
delete this.client.defaults.headers.common["Authorization"]
|
||||
return this.client.delete<T>(url, config);
|
||||
}
|
||||
}
|
||||
|
||||
export const apiClient = new APIClient()
|
||||
export const setAuthToken = (token: string) => apiClient.setAuthToken(token)
|
||||
export const setGuestToken = (token: string) => apiClient.setGuestToken(token)
|
||||
export const clearAuthToken = () => apiClient.clearAuthToken()
|
||||
export const apiClient = new APIClient();
|
||||
Reference in New Issue
Block a user