diff --git a/src/components/dev/debug-toast.tsx b/src/components/dev/debug-toast.tsx index baadcec..5b5a788 100644 --- a/src/components/dev/debug-toast.tsx +++ b/src/components/dev/debug-toast.tsx @@ -6,60 +6,94 @@ interface DebugLog { id: string; timestamp: string; url: string; + fullUrl?: string; method: string; status: number; - headers: Record; - response?: any; + statusText?: string; + requestHeaders: Record; + requestBody?: any; + responseData?: any; + error?: string; + duration?: number; } export default function DebugToast() { const [logs, setLogs] = useState([]); - const [isOpen, setIsOpen] = useState(false); + const [isOpen, setIsOpen] = useState(true); useEffect(() => { - const handleDebugLog = (event: CustomEvent) => { - setLogs((prev) => [event.detail, ...prev].slice(0, 10)); - setIsOpen(true); + const handleDebugLog = (event: any) => { + setLogs((prev) => [event.detail, ...prev].slice(0, 20)); }; - window.addEventListener("debug-log" as any, handleDebugLog); - return () => window.removeEventListener("debug-log" as any, handleDebugLog); + window.addEventListener("debug-log", handleDebugLog); + return () => window.removeEventListener("debug-log", handleDebugLog); }, []); - if (!isOpen || logs.length === 0) return null; + const getStatusColor = (status: number) => { + if (status >= 200 && status < 300) return "bg-green-900 border-green-500"; + if (status >= 400) return "bg-red-900 border-red-500"; + return "bg-yellow-900 border-yellow-500"; + }; return ( -
-
-

🐛 Debug Log

- +
+
+

🔍 API Debug ({logs.length})

+
+ + +
- {logs.map((log) => ( -
-
- {log.method} {log.status} + {isOpen && logs.map((log) => ( +
+
+ {log.method} {log.status} {log.statusText} + {log.duration && {log.duration}ms} +
+ +
+ {log.fullUrl || log.url}
-
{log.url}
-
{log.timestamp}
-
- Headers -
-              {JSON.stringify(log.headers, null, 2)}
+          
{log.timestamp}
+ + {log.error && ( +
❌ {log.error}
+ )} + +
+ 📤 Request Headers +
+              {JSON.stringify(log.requestHeaders, null, 2)}
             
- {log.response && ( -
- Response -
-                {JSON.stringify(log.response, null, 2)}
+          {log.requestBody && (
+            
+ 📦 Request Body +
+                {JSON.stringify(log.requestBody, null, 2)}
+              
+
+ )} + + {log.responseData && ( +
+ 📥 Response +
+                {JSON.stringify(log.responseData, null, 2)}
               
)} diff --git a/src/lib/http.ts b/src/lib/http.ts index 1b091c0..4dfa1c6 100644 --- a/src/lib/http.ts +++ b/src/lib/http.ts @@ -64,29 +64,54 @@ http.interceptors.request.use((config) => { config.url = ""; } + (config as any).requestStartTime = Date.now(); return config; }); http.interceptors.response.use( - (response) => response, + (response) => { + const config = response.config as any; + const debugLog = { + id: Date.now().toString(), + timestamp: new Date().toLocaleString("fa-IR"), + url: config.url || "unknown", + fullUrl: config.baseURL ? `${config.baseURL}${config.url}` : config.url, + method: config.method?.toUpperCase() || "GET", + status: response.status, + statusText: response.statusText, + requestHeaders: config.headers || {}, + requestBody: config.data, + responseData: response.data, + duration: Date.now() - (config.requestStartTime || Date.now()), + }; + + if (typeof window !== "undefined") { + window.dispatchEvent(new CustomEvent("debug-log", { detail: debugLog })); + } + + return response; + }, (error) => { - if (error.response?.status === 401) { - const debugLog = { - id: Date.now().toString(), - timestamp: new Date().toLocaleString("fa-IR"), - url: error.config?.url || "unknown", - method: error.config?.method?.toUpperCase() || "GET", - status: 401, - headers: error.config?.headers || {}, - response: error.response?.data, - }; - - if (typeof window !== "undefined") { - window.dispatchEvent( - new CustomEvent("debug-log", { detail: debugLog }) - ); - } + const config = error.config as any; + const debugLog = { + id: Date.now().toString(), + timestamp: new Date().toLocaleString("fa-IR"), + url: config?.url || "unknown", + fullUrl: config?.baseURL ? `${config.baseURL}${config.url}` : config?.url, + method: config?.method?.toUpperCase() || "GET", + status: error.response?.status || 0, + statusText: error.response?.statusText || "Network Error", + requestHeaders: config?.headers || {}, + requestBody: config?.data, + responseData: error.response?.data, + error: error.message, + duration: Date.now() - (config?.requestStartTime || Date.now()), + }; + + if (typeof window !== "undefined") { + window.dispatchEvent(new CustomEvent("debug-log", { detail: debugLog })); } + return Promise.reject(error); } );