54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
// electron/api/axiosClient.ts
|
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
import { getToken } from '../utils/tokenHelper.js';
|
|
import chalk from "chalk";
|
|
import { sendTokenExpired } from '../ipc/ipcHandler.js';
|
|
import https from 'https';
|
|
|
|
const TOKEN_KEY = 'ACCESS_TOKEN';
|
|
|
|
const httpsAgent = new https.Agent({
|
|
rejectUnauthorized: false
|
|
});
|
|
|
|
const api: AxiosInstance = axios.create({
|
|
baseURL: 'https://daniela-nonprofessed-boastfully.ngrok-free.app/api/v1/va', // 🔁 your backend
|
|
httpsAgent
|
|
// timeout: 10000,
|
|
});
|
|
|
|
// console.log("Creating Axios Instance", axios.VERSION);
|
|
// console.log("Axios .get reference:", api.get);
|
|
// console.log("Axios .request reference:", api.request);
|
|
|
|
// --- Request Interceptor ---
|
|
api.interceptors.request.use(
|
|
async (config: AxiosRequestConfig): Promise<any> => {
|
|
const token = await getToken(TOKEN_KEY);
|
|
config.headers = config.headers || {}
|
|
|
|
if (token) {
|
|
config.headers['token'] = token;
|
|
config.headers['ngrok-skip-browser-warning'] = 'true';
|
|
}
|
|
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error)
|
|
);
|
|
|
|
// --- Response Interceptor ---
|
|
api.interceptors.response.use(
|
|
(response: AxiosResponse) => response.data,
|
|
async (error) => {
|
|
// console.error('[Axios Error]', error.message);
|
|
if (error.response?.status === 401) {
|
|
console.warn('Token expired — trigger refresh logic if needed');
|
|
sendTokenExpired();
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|