import { useStorage } from "@vueuse/core"; export default defineNuxtPlugin((nuxtApp) => { // ====================== // Runtime Config (Public) // ====================== const config = useRuntimeConfig(); const BASE_URL = (config.public.NUXT_PUBLIC_BASE_URL as string) || ""; const API_NAME = (config.public.NUXT_PUBLIC_API_NAME as string) || ""; // ====================== // Safe URL Joiner // ====================== const joinURL = (base: string, path: string) => { if (!base) return path; if (!path) return base; return `${base.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`; }; const baseURL = joinURL(BASE_URL, API_NAME); // ====================== // Reactive Token // ====================== const token = useStorage("id_token", ""); // ====================== // Fetch Instance // ====================== const api = $fetch.create({ baseURL, // ====================== // Request Interceptor // ====================== onRequest({ options }) { const auth = token.value; if (!auth) return; const headers = (options.headers ||= {}); if (headers instanceof Headers) { headers.set("Authorization", `Bearer ${auth}`); } else if (Array.isArray(headers)) { headers.push(["Authorization", `Bearer ${auth}`]); } else { (headers as Record).Authorization = `Bearer ${auth}`; } }, // ====================== // Response Handler // ====================== onResponse({ response }) { return response._data; }, // ====================== // Error Handler // ====================== async onResponseError({ response }) { const status = response?.status; if (status === 401) { token.value = ""; if (import.meta.client) { await nuxtApp.runWithContext(() => navigateTo("/login")); } } throw { status, message: response?._data?.message || response?.statusText || "خطای نامشخص در ارتباط با سرور", data: response?._data || null, }; }, }); // ====================== // HTTP Service (Public API) // ====================== const http = { getRequest: (url: string, options: any = {}) => api(url, { method: "GET", ...options }), postRequest: (url: string, body?: any, options: any = {}) => api(url, { method: "POST", body, ...options }), putRequest: (url: string, body?: any, options: any = {}) => api(url, { method: "PUT", body, ...options }), patchRequest: (url: string, body?: any, options: any = {}) => api(url, { method: "PATCH", body, ...options }), deleteRequest: (url: string, options: any = {}) => api(url, { method: "DELETE", ...options }), // متدهای مدیریت توکن که در authStore استفاده می‌شوند setToken: (newToken: string) => { token.value = newToken; }, clearToken: () => { token.value = ""; }, getToken: () => token.value, // دسترسی مستقیم به کلاینت خام Fetch raw: api, }; // ====================== // Provide to Nuxt // ====================== return { provide: { http, }, }; });