canina/frontend/application/lib/services/api.ts
parsa aghaei 501addc909
All checks were successful
Deploy Canina / deploy (push) Successful in 5m39s
fix: CI/CD pipeline, dynamic API URLs, Dockerfile fixes
- Backend Dockerfile: add chown for node_modules (Prisma permissions fix)
- Frontend Dockerfile: add ARG/ENV for NEXT_PUBLIC_API_URL, VITE_API_URL
- nginx.conf: template with __PLACEHOLDERS__ for per-env substitution
- deploy.yml: SSH keepalive, sudo for VPN
- api.ts: dynamic API URL via env vars instead of hardcoded
- next.config.ts: rewrite only in development mode
- scripts: deploy.sh, compose.stage.yml, compose.prod.yml
2026-07-28 16:37:05 +03:30

50 lines
1.3 KiB
TypeScript

import axios from 'axios';
const baseURL = process.env.NEXT_PUBLIC_API_URL || (process.env.NODE_ENV === 'development' ? 'http://localhost:4001/api' : '/api');
const api = axios.create({
baseURL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
// Interceptor to add auth token in request headers
api.interceptors.request.use(
(config) => {
let token = null;
if (typeof window !== 'undefined') {
token = localStorage.getItem('accessToken');
}
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
// Interceptor to handle 401/403 token expiration globally
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
if (typeof window !== 'undefined') {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
try {
// Dynamic import to avoid circular dependency
const { useUserStore } = require('../store/userStore');
useUserStore.getState().logout();
} catch {
// Ignore if store not initialized
}
}
}
return Promise.reject(error);
}
);
export default api;