Skip to content

Commit 00cd286

Browse files
committed
Add base API configuration and interceptors
1 parent 66db326 commit 00cd286

File tree

9 files changed

+64
-8
lines changed

9 files changed

+64
-8
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import axios from 'axios';
2+
3+
// Base API configuration
4+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000';
5+
6+
// Create axios instance with default config
7+
const api = axios.create({
8+
baseURL: API_BASE_URL,
9+
timeout: 10000, // 10 seconds timeout
10+
headers: {
11+
'Content-Type': 'application/json',
12+
},
13+
});
14+
15+
// Request interceptor to add auth token
16+
api.interceptors.request.use(
17+
(config) => {
18+
const token = localStorage.getItem('access_token');
19+
if (token) {
20+
config.headers.Authorization = `Bearer ${token}`;
21+
}
22+
return config;
23+
},
24+
(error) => {
25+
return Promise.reject(error);
26+
}
27+
);
28+
29+
// Response interceptor to handle common errors
30+
api.interceptors.response.use(
31+
(response) => {
32+
return response;
33+
},
34+
(error) => {
35+
// Handle 401 Unauthorized
36+
if (error.response?.status === 401) {
37+
localStorage.removeItem('access_token');
38+
localStorage.removeItem('user');
39+
window.location.href = '/auth';
40+
}
41+
42+
// Handle 403 Forbidden
43+
if (error.response?.status === 403) {
44+
console.error('Access forbidden');
45+
}
46+
47+
// Handle 500 Internal Server Error
48+
if (error.response?.status === 500) {
49+
console.error('Server error occurred');
50+
}
51+
52+
return Promise.reject(error);
53+
}
54+
);
55+
56+
export default api;

apps/container-engine-frontend/src/components/DeploymentDetail/LogsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { useState, useEffect, useRef } from 'react';
33
import { ClipboardDocumentListIcon } from "@heroicons/react/24/outline";
44
import { useParams } from 'react-router-dom';
5-
import api from '../../lib/api';
5+
import api from '../../api/api';
66

77
export default function LogsPage() {
88
const { deploymentId } = useParams();

apps/container-engine-frontend/src/pages/AccountSettingsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/pages/AccountSettingsPage.tsx
22
import React, { useState, useEffect } from 'react';
3-
import api from '../lib/api';
3+
import api from '../api/api';
44
import DashboardLayout from '../components/Layout/DashboardLayout';
55

66
const AccountSettingsPage: React.FC = () => {

apps/container-engine-frontend/src/pages/ApiKeysPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useState, useCallback } from 'react';
2-
import api from '../lib/api';
2+
import api from '../api/api';
33
import DashboardLayout from '../components/Layout/DashboardLayout';
44
import {
55
PlusIcon,

apps/container-engine-frontend/src/pages/AuthPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/pages/AuthPage.tsx
22
import React, { useState } from 'react';
3-
import api from '../lib/api';
3+
import api from '../api/api';
44
import { useAuth } from '../context/AuthContext';
55
import { useNavigate } from 'react-router-dom';
66

apps/container-engine-frontend/src/pages/DashboardPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/pages/DashboardPage.tsx
22
import React, { useEffect, useState } from 'react';
3-
import api from '../lib/api';
3+
import api from '../api/api';
44
import DashboardLayout from '../components/Layout/DashboardLayout'; // Layout với Sidebar, Header
55

66
interface DashboardStats {

apps/container-engine-frontend/src/pages/DeploymentDetailPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// src/pages/DeploymentDetailPage.tsx
22
import React, { useEffect, useState } from 'react';
33
import { useParams, useNavigate } from 'react-router-dom';
4-
import api from '../lib/api';
4+
import api from '../api/api';
55
import DashboardLayout from '../components/Layout/DashboardLayout';
66
import LogsPage from '../components/DeploymentDetail/LogsPage';
77
import {

apps/container-engine-frontend/src/pages/DeploymentsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/pages/DeploymentsPage.tsx
22
import React, { useEffect, useState } from 'react';
3-
import api from '../lib/api';
3+
import api from '../api/api';
44
import DashboardLayout from '../components/Layout/DashboardLayout';
55
import { Link } from 'react-router-dom';
66
import { formatDistanceToNow, parseISO } from 'date-fns';

apps/container-engine-frontend/src/pages/NewDeploymentPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/pages/NewDeploymentPage.tsx
22
import React, { useState } from 'react';
3-
import api from '../lib/api';
3+
import api from '../api/api';
44
import DashboardLayout from '../components/Layout/DashboardLayout';
55
import { useNavigate } from 'react-router-dom';
66
import {

0 commit comments

Comments
 (0)