Created
September 12, 2025 15:21
-
-
Save Juan-Severiano/380ccd14d2cd10db57621f2fb20bc283 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from "axios" | |
| const logRequest = (config: any) => { | |
| const isDev = true | |
| if (isDev) { | |
| console.group(`🚀 ${config.method?.toUpperCase()} ${config.url}`) | |
| console.log("URL:", `${config.baseURL}${config.url}`) | |
| console.log("Headers:", config.headers) | |
| if (config.data) console.log("Data:", config.data) | |
| if (config.params) console.log("Params:", config.params) | |
| console.log("Time:", new Date().toLocaleTimeString()) | |
| console.groupEnd() | |
| } | |
| } | |
| const logResponse = (response: any) => { | |
| const isDev = true | |
| if (isDev) { | |
| const { config, status, data } = response | |
| console.group(`✅ ${config.method?.toUpperCase()} ${config.url} - ${status}`) | |
| console.log("Status:", `${status} ${response.statusText}`) | |
| console.log("Data:", data) | |
| console.log("Time:", new Date().toLocaleTimeString()) | |
| console.groupEnd() | |
| } | |
| } | |
| const logError = (error: any) => { | |
| const isDev = true | |
| if (isDev) { | |
| const { config, response } = error | |
| console.group(`❌ ${config?.method?.toUpperCase()} ${config?.url} - ERROR`) | |
| console.error("Status:", response?.status || "Network Error") | |
| console.error("Message:", error.message) | |
| if (response?.data) console.error("Error Data:", response.data) | |
| console.log("Time:", new Date().toLocaleTimeString()) | |
| console.groupEnd() | |
| } | |
| } | |
| const apiBaseUrl = import.meta.env.VITE_API_URL || "https://pontofacil-api.vercel.app"; | |
| export const api = axios.create({ | |
| baseURL: apiBaseUrl, | |
| }) | |
| api.interceptors.request.use( | |
| async (config) => { | |
| logRequest(config) | |
| const { useAuthStore } = await import("../store/auth") | |
| const token = useAuthStore.getState().token | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}` | |
| } | |
| return config | |
| }, | |
| (error) => { | |
| logError(error) | |
| return Promise.reject(error) | |
| }, | |
| ) | |
| api.interceptors.response.use( | |
| (response) => { | |
| logResponse(response) | |
| return response | |
| }, | |
| async (error) => { | |
| logError(error) | |
| if (error.response?.status === 401) { | |
| const { useAuthStore } = await import("../store/auth") | |
| useAuthStore.getState().logout() | |
| window.location.href = "/login" | |
| } | |
| return Promise.reject(error) | |
| }, | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment