import axios, { type AxiosError } from "axios"; import type { ApiResponse } from "./types"; const baseURL = process.env.NEXT_PUBLIC_ADMIN_API_URL ?? "http://localhost:5081"; export const adminApi = axios.create({ baseURL, headers: { "Content-Type": "application/json" }, }); adminApi.interceptors.request.use((config) => { if (typeof window !== "undefined") { const token = localStorage.getItem("meezi_admin_access_token"); if (token) config.headers.Authorization = `Bearer ${token}`; } return config; }); adminApi.interceptors.response.use( (response) => response, (error: AxiosError>) => { const apiError = error.response?.data?.error; if (apiError?.code) { return Promise.reject(new AdminApiClientError(apiError.code, apiError.message)); } if (error.response?.status === 401 && typeof window !== "undefined") { localStorage.removeItem("meezi_admin_access_token"); localStorage.removeItem("meezi_admin_refresh_token"); localStorage.removeItem("meezi_admin_auth"); const locale = window.location.pathname.split("/")[1] ?? "fa"; window.location.href = `/${locale}/admin/login`; } return Promise.reject(error); } ); export class AdminApiClientError extends Error { constructor( public readonly code: string, message: string ) { super(message); this.name = "AdminApiClientError"; } } export async function adminGet(url: string): Promise { const { data } = await adminApi.get>(url); if (!data.success || data.data === undefined) { throw new Error(data.error?.message ?? "Request failed"); } return data.data; } export async function adminPost(url: string, body?: unknown): Promise { const { data } = await adminApi.post>(url, body); if (!data.success || data.data === undefined) { throw new Error(data.error?.message ?? "Request failed"); } return data.data; } export async function adminPut(url: string, body: unknown): Promise { const { data } = await adminApi.put>(url, body); if (!data.success || data.data === undefined) { throw new Error(data.error?.message ?? "Request failed"); } return data.data; } export async function adminPatch(url: string, body: unknown): Promise { const { data } = await adminApi.patch>(url, body); if (!data.success || data.data === undefined) { throw new Error(data.error?.message ?? "Request failed"); } return data.data; } export async function adminDelete(url: string): Promise { const { data } = await adminApi.delete>(url); if (!data.success) { throw new Error(data.error?.message ?? "Request failed"); } }