style(api): format code and improve API base URL handling
This commit is contained in:
File diff suppressed because one or more lines are too long
2
pkg/serverembed/dist/index.html
vendored
2
pkg/serverembed/dist/index.html
vendored
@@ -5,7 +5,7 @@
|
|||||||
<link rel="icon" type="image/svg+xml" href="/ui/vite.svg" />
|
<link rel="icon" type="image/svg+xml" href="/ui/vite.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>web</title>
|
<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">
|
<link rel="stylesheet" crossorigin href="/ui/assets/index-Bfia8Lvm.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
3815
web/pnpm-lock.yaml
generated
Normal file
3815
web/pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,22 @@
|
|||||||
import axios, { type AxiosInstance, AxiosError } from 'axios';
|
import axios, { type AxiosInstance, AxiosError } from "axios";
|
||||||
import type {
|
import type {
|
||||||
User, Hook, WhatsAppAccount, EventLog, APIKey,
|
User,
|
||||||
LoginRequest, LoginResponse
|
Hook,
|
||||||
} from '../types';
|
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 {
|
class ApiClient {
|
||||||
private client: AxiosInstance;
|
private client: AxiosInstance;
|
||||||
@@ -13,7 +25,7 @@ class ApiClient {
|
|||||||
this.client = axios.create({
|
this.client = axios.create({
|
||||||
baseURL: API_BASE_URL,
|
baseURL: API_BASE_URL,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -33,47 +45,50 @@ class ApiClient {
|
|||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
// Token expired or invalid, clear auth and redirect
|
// Token expired or invalid, clear auth and redirect
|
||||||
this.clearAuth();
|
this.clearAuth();
|
||||||
window.location.href = '/ui/login';
|
window.location.href = "/ui/login";
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token management
|
// Token management
|
||||||
private getToken(): string | null {
|
private getToken(): string | null {
|
||||||
return localStorage.getItem('auth_token');
|
return localStorage.getItem("auth_token");
|
||||||
}
|
}
|
||||||
|
|
||||||
private setToken(token: string): void {
|
private setToken(token: string): void {
|
||||||
localStorage.setItem('auth_token', token);
|
localStorage.setItem("auth_token", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private clearAuth(): void {
|
private clearAuth(): void {
|
||||||
localStorage.removeItem('auth_token');
|
localStorage.removeItem("auth_token");
|
||||||
localStorage.removeItem('user');
|
localStorage.removeItem("user");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth endpoints
|
// Auth endpoints
|
||||||
async login(credentials: LoginRequest): Promise<LoginResponse> {
|
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) {
|
if (data.token) {
|
||||||
this.setToken(data.token);
|
this.setToken(data.token);
|
||||||
localStorage.setItem('user', JSON.stringify(data.user));
|
localStorage.setItem("user", JSON.stringify(data.user));
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async logout(): Promise<void> {
|
async logout(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.client.post('/api/v1/auth/logout');
|
await this.client.post("/api/v1/auth/logout");
|
||||||
} finally {
|
} finally {
|
||||||
this.clearAuth();
|
this.clearAuth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentUser(): User | null {
|
getCurrentUser(): User | null {
|
||||||
const userStr = localStorage.getItem('user');
|
const userStr = localStorage.getItem("user");
|
||||||
return userStr ? JSON.parse(userStr) : null;
|
return userStr ? JSON.parse(userStr) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +98,7 @@ class ApiClient {
|
|||||||
|
|
||||||
// Unified query endpoint
|
// Unified query endpoint
|
||||||
async query(request: {
|
async query(request: {
|
||||||
action: 'list' | 'get' | 'create' | 'update' | 'delete';
|
action: "list" | "get" | "create" | "update" | "delete";
|
||||||
table: string;
|
table: string;
|
||||||
id?: string;
|
id?: string;
|
||||||
data?: Record<string, any>;
|
data?: Record<string, any>;
|
||||||
@@ -91,13 +106,13 @@ class ApiClient {
|
|||||||
limit?: number;
|
limit?: number;
|
||||||
offset?: number;
|
offset?: number;
|
||||||
}): Promise<any> {
|
}): Promise<any> {
|
||||||
const { data } = await this.client.post('/api/v1/query', request);
|
const { data } = await this.client.post("/api/v1/query", request);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Users API
|
// Users API
|
||||||
async getUsers(): Promise<User[]> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +122,7 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createUser(user: Partial<User>): Promise<User> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +137,7 @@ class ApiClient {
|
|||||||
|
|
||||||
// Hooks API
|
// Hooks API
|
||||||
async getHooks(): Promise<Hook[]> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +147,7 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createHook(hook: Partial<Hook>): Promise<Hook> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,22 +162,37 @@ class ApiClient {
|
|||||||
|
|
||||||
// WhatsApp Accounts API
|
// WhatsApp Accounts API
|
||||||
async getAccounts(): Promise<WhatsAppAccount[]> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAccount(id: string): Promise<WhatsAppAccount> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAccount(account: Partial<WhatsAppAccount>): Promise<WhatsAppAccount> {
|
async createAccount(
|
||||||
const { data} = await this.client.post<WhatsAppAccount>('/api/v1/whatsapp_accounts', account);
|
account: Partial<WhatsAppAccount>,
|
||||||
|
): Promise<WhatsAppAccount> {
|
||||||
|
const { data } = await this.client.post<WhatsAppAccount>(
|
||||||
|
"/api/v1/whatsapp_accounts",
|
||||||
|
account,
|
||||||
|
);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateAccount(id: string, account: Partial<WhatsAppAccount>): Promise<WhatsAppAccount> {
|
async updateAccount(
|
||||||
const { data } = await this.client.put<WhatsAppAccount>(`/api/v1/whatsapp_accounts/${id}`, account);
|
id: string,
|
||||||
|
account: Partial<WhatsAppAccount>,
|
||||||
|
): Promise<WhatsAppAccount> {
|
||||||
|
const { data } = await this.client.put<WhatsAppAccount>(
|
||||||
|
`/api/v1/whatsapp_accounts/${id}`,
|
||||||
|
account,
|
||||||
|
);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,19 +201,24 @@ class ApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Event Logs API
|
// Event Logs API
|
||||||
async getEventLogs(params?: { limit?: number; offset?: number }): Promise<EventLog[]> {
|
async getEventLogs(params?: {
|
||||||
const { data } = await this.client.get<EventLog[]>('/api/v1/event_logs', { params });
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
}): Promise<EventLog[]> {
|
||||||
|
const { data } = await this.client.get<EventLog[]>("/api/v1/event_logs", {
|
||||||
|
params,
|
||||||
|
});
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Keys API
|
// API Keys API
|
||||||
async getAPIKeys(): Promise<APIKey[]> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAPIKey(apiKey: Partial<APIKey>): Promise<APIKey> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +228,7 @@ class ApiClient {
|
|||||||
|
|
||||||
// Health check
|
// Health check
|
||||||
async healthCheck(): Promise<{ status: string }> {
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user