Files
2026-03-09 22:30:41 +00:00

118 lines
2.7 KiB
TypeScript

export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000';
export interface Container {
id: string;
name: string;
image: string;
status: string;
uptime: string;
}
export interface AuthResponse {
success: boolean;
token?: string;
username?: string;
message?: string;
}
export interface ContainersResponse {
containers: Container[];
}
class ApiClient {
private token: string | null = null;
setToken(token: string | null) {
this.token = token;
if (token) {
localStorage.setItem('auth_token', token);
} else {
localStorage.removeItem('auth_token');
}
}
getToken(): string | null {
if (!this.token && typeof window !== 'undefined') {
this.token = localStorage.getItem('auth_token');
}
return this.token;
}
async login(username: string, password: string): Promise<AuthResponse> {
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const data = await response.json();
if (data.success && data.token) {
this.setToken(data.token);
}
return data;
}
async logout(): Promise<void> {
const token = this.getToken();
if (token) {
await fetch(`${API_BASE_URL}/api/auth/logout`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
});
}
this.setToken(null);
}
async getContainers(): Promise<Container[]> {
const token = this.getToken();
if (!token) {
throw new Error('Not authenticated');
}
const response = await fetch(`${API_BASE_URL}/api/containers`, {
headers: {
'Authorization': `Bearer ${token}`,
},
});
if (!response.ok) {
if (response.status === 401) {
this.setToken(null);
throw new Error('Session expired');
}
throw new Error('Failed to fetch containers');
}
const data: ContainersResponse = await response.json();
return data.containers;
}
async executeCommand(containerId: string, command: string): Promise<any> {
const token = this.getToken();
if (!token) {
throw new Error('Not authenticated');
}
const response = await fetch(`${API_BASE_URL}/api/containers/${containerId}/exec`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ command }),
});
if (!response.ok) {
throw new Error('Failed to execute command');
}
return response.json();
}
}
export const apiClient = new ApiClient();