style(api): format code and improve API base URL handling
Some checks failed
CI / Test (1.22) (push) Failing after -21m52s
CI / Test (1.23) (push) Failing after -21m54s
CI / Lint (push) Has been cancelled
CI / Build (push) Has been cancelled

This commit is contained in:
2026-02-20 20:41:13 +02:00
parent cecbd2cef5
commit 1ceaa238b2
4 changed files with 3900 additions and 50 deletions

File diff suppressed because one or more lines are too long

View File

@@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/ui/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>web</title>
<script type="module" crossorigin src="/ui/assets/index-D_NQzvuP.js"></script>
<script type="module" crossorigin src="/ui/assets/index-CExXKuWO.js"></script>
<link rel="stylesheet" crossorigin href="/ui/assets/index-Bfia8Lvm.css">
</head>
<body>

3815
web/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,22 @@
import axios, { type AxiosInstance, AxiosError } from 'axios';
import axios, { type AxiosInstance, AxiosError } from "axios";
import type {
User, Hook, WhatsAppAccount, EventLog, APIKey,
LoginRequest, LoginResponse
} from '../types';
User,
Hook,
WhatsAppAccount,
EventLog,
APIKey,
LoginRequest,
LoginResponse,
} from "../types";
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8080';
function getApiBaseUrl(): string {
if (import.meta.env.VITE_API_URL) return import.meta.env.VITE_API_URL;
const { hostname, protocol, port } = window.location;
if (hostname === "localhost" || hostname === "127.0.0.1") return "http://localhost:8080";
return `${protocol}//${hostname}${port ? `:${port}` : ""}`;
}
const API_BASE_URL = getApiBaseUrl();
class ApiClient {
private client: AxiosInstance;
@@ -13,7 +25,7 @@ class ApiClient {
this.client = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
"Content-Type": "application/json",
},
});
@@ -33,47 +45,50 @@ class ApiClient {
if (error.response?.status === 401) {
// Token expired or invalid, clear auth and redirect
this.clearAuth();
window.location.href = '/ui/login';
window.location.href = "/ui/login";
}
return Promise.reject(error);
}
},
);
}
// Token management
private getToken(): string | null {
return localStorage.getItem('auth_token');
return localStorage.getItem("auth_token");
}
private setToken(token: string): void {
localStorage.setItem('auth_token', token);
localStorage.setItem("auth_token", token);
}
private clearAuth(): void {
localStorage.removeItem('auth_token');
localStorage.removeItem('user');
localStorage.removeItem("auth_token");
localStorage.removeItem("user");
}
// Auth endpoints
async login(credentials: LoginRequest): Promise<LoginResponse> {
const { data } = await this.client.post<LoginResponse>('/api/v1/auth/login', credentials);
const { data } = await this.client.post<LoginResponse>(
"/api/v1/auth/login",
credentials,
);
if (data.token) {
this.setToken(data.token);
localStorage.setItem('user', JSON.stringify(data.user));
localStorage.setItem("user", JSON.stringify(data.user));
}
return data;
}
async logout(): Promise<void> {
try {
await this.client.post('/api/v1/auth/logout');
await this.client.post("/api/v1/auth/logout");
} finally {
this.clearAuth();
}
}
getCurrentUser(): User | null {
const userStr = localStorage.getItem('user');
const userStr = localStorage.getItem("user");
return userStr ? JSON.parse(userStr) : null;
}
@@ -83,7 +98,7 @@ class ApiClient {
// Unified query endpoint
async query(request: {
action: 'list' | 'get' | 'create' | 'update' | 'delete';
action: "list" | "get" | "create" | "update" | "delete";
table: string;
id?: string;
data?: Record<string, any>;
@@ -91,13 +106,13 @@ class ApiClient {
limit?: number;
offset?: number;
}): Promise<any> {
const { data } = await this.client.post('/api/v1/query', request);
const { data } = await this.client.post("/api/v1/query", request);
return data;
}
// Users API
async getUsers(): Promise<User[]> {
const { data } = await this.client.get<User[]>('/api/v1/users');
const { data } = await this.client.get<User[]>("/api/v1/users");
return data;
}
@@ -107,7 +122,7 @@ class ApiClient {
}
async createUser(user: Partial<User>): Promise<User> {
const { data } = await this.client.post<User>('/api/v1/users', user);
const { data } = await this.client.post<User>("/api/v1/users", user);
return data;
}
@@ -122,7 +137,7 @@ class ApiClient {
// Hooks API
async getHooks(): Promise<Hook[]> {
const { data } = await this.client.get<Hook[]>('/api/v1/hooks');
const { data } = await this.client.get<Hook[]>("/api/v1/hooks");
return data;
}
@@ -132,7 +147,7 @@ class ApiClient {
}
async createHook(hook: Partial<Hook>): Promise<Hook> {
const { data } = await this.client.post<Hook>('/api/v1/hooks', hook);
const { data } = await this.client.post<Hook>("/api/v1/hooks", hook);
return data;
}
@@ -147,22 +162,37 @@ class ApiClient {
// WhatsApp Accounts API
async getAccounts(): Promise<WhatsAppAccount[]> {
const { data } = await this.client.get<WhatsAppAccount[]>('/api/v1/whatsapp_accounts');
const { data } = await this.client.get<WhatsAppAccount[]>(
"/api/v1/whatsapp_accounts",
);
return data;
}
async getAccount(id: string): Promise<WhatsAppAccount> {
const { data } = await this.client.get<WhatsAppAccount>(`/api/v1/whatsapp_accounts/${id}`);
const { data } = await this.client.get<WhatsAppAccount>(
`/api/v1/whatsapp_accounts/${id}`,
);
return data;
}
async createAccount(account: Partial<WhatsAppAccount>): Promise<WhatsAppAccount> {
const { data} = await this.client.post<WhatsAppAccount>('/api/v1/whatsapp_accounts', account);
async createAccount(
account: Partial<WhatsAppAccount>,
): Promise<WhatsAppAccount> {
const { data } = await this.client.post<WhatsAppAccount>(
"/api/v1/whatsapp_accounts",
account,
);
return data;
}
async updateAccount(id: string, account: Partial<WhatsAppAccount>): Promise<WhatsAppAccount> {
const { data } = await this.client.put<WhatsAppAccount>(`/api/v1/whatsapp_accounts/${id}`, account);
async updateAccount(
id: string,
account: Partial<WhatsAppAccount>,
): Promise<WhatsAppAccount> {
const { data } = await this.client.put<WhatsAppAccount>(
`/api/v1/whatsapp_accounts/${id}`,
account,
);
return data;
}
@@ -171,19 +201,24 @@ class ApiClient {
}
// Event Logs API
async getEventLogs(params?: { limit?: number; offset?: number }): Promise<EventLog[]> {
const { data } = await this.client.get<EventLog[]>('/api/v1/event_logs', { params });
async getEventLogs(params?: {
limit?: number;
offset?: number;
}): Promise<EventLog[]> {
const { data } = await this.client.get<EventLog[]>("/api/v1/event_logs", {
params,
});
return data;
}
// API Keys API
async getAPIKeys(): Promise<APIKey[]> {
const { data } = await this.client.get<APIKey[]>('/api/v1/api_keys');
const { data } = await this.client.get<APIKey[]>("/api/v1/api_keys");
return data;
}
async createAPIKey(apiKey: Partial<APIKey>): Promise<APIKey> {
const { data } = await this.client.post<APIKey>('/api/v1/api_keys', apiKey);
const { data } = await this.client.post<APIKey>("/api/v1/api_keys", apiKey);
return data;
}
@@ -193,7 +228,7 @@ class ApiClient {
// Health check
async healthCheck(): Promise<{ status: string }> {
const { data } = await this.client.get<{ status: string }>('/health');
const { data } = await this.client.get<{ status: string }>("/health");
return data;
}
}