fmt: use biome

This commit is contained in:
2025-12-06 22:26:28 +08:00
parent 8be7af6815
commit 9a82ecedac
22 changed files with 400 additions and 429 deletions

View File

@@ -1,18 +1,22 @@
import type { InterceptionRule, Command, CreateRuleRequest, UpdateRuleRequest, UpdateCommandRequest, RequestLog } from '../types';
import { authStore } from './auth';
import type {
InterceptionRule,
Command,
CreateRuleRequest,
UpdateRuleRequest,
UpdateCommandRequest,
RequestLog,
} from "../types";
import { authStore } from "./auth";
const API_BASE = '/admin/api';
const API_BASE = "/admin/api";
async function request<T>(
endpoint: string,
options?: RequestInit
): Promise<T> {
async function request<T>(endpoint: string, options?: RequestInit): Promise<T> {
const token = authStore.getToken();
const response = await fetch(`${API_BASE}${endpoint}`, {
headers: {
'Content-Type': 'application/json',
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options?.headers,
},
...options,
@@ -22,7 +26,7 @@ async function request<T>(
// Unauthorized - clear token and trigger re-authentication
authStore.clearToken();
window.location.reload();
throw new Error('Unauthorized');
throw new Error("Unauthorized");
}
if (!response.ok) {
@@ -32,14 +36,14 @@ async function request<T>(
if (response.status === 204 || response.status === 200) {
// Check if response has content
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
return undefined as T;
}
// Check if there's actually content to parse
const text = await response.text();
if (!text || text.trim() === '') {
if (!text || text.trim() === "") {
return undefined as T;
}
@@ -51,43 +55,40 @@ async function request<T>(
// Rules API
export const rulesApi = {
list: () =>
request<InterceptionRule[]>('/rules'),
list: () => request<InterceptionRule[]>("/rules"),
create: (rule: CreateRuleRequest) =>
request<InterceptionRule>('/rules', {
method: 'POST',
request<InterceptionRule>("/rules", {
method: "POST",
body: JSON.stringify(rule),
}),
update: (id: number, rule: UpdateRuleRequest) =>
request<InterceptionRule>(`/rules/${id}`, {
method: 'PUT',
method: "PUT",
body: JSON.stringify(rule),
}),
delete: (id: number) =>
request<void>(`/rules/${id}`, { method: 'DELETE' }),
delete: (id: number) => request<void>(`/rules/${id}`, { method: "DELETE" }),
};
// Commands API
export const commandsApi = {
list: (status?: string) =>
request<Command[]>(`/commands${status ? `?status=${status}` : ''}`),
list: (status?: string) => request<Command[]>(`/commands${status ? `?status=${status}` : ""}`),
updateStatus: (id: number, req: UpdateCommandRequest) =>
request<Command>(`/commands/${id}`, {
method: 'POST',
method: "POST",
body: JSON.stringify(req),
}),
};
// Config API
export const configApi = {
get: () => request<Record<string, string>>('/config'),
get: () => request<Record<string, string>>("/config"),
update: (config: Record<string, string>) =>
request<void>('/config', {
method: 'PUT',
request<void>("/config", {
method: "PUT",
body: JSON.stringify(config),
}),
};
@@ -95,8 +96,8 @@ export const configApi = {
// Auth API
export const authApi = {
changePassword: (oldPassword: string, newPassword: string) =>
request<void>('/password', {
method: 'PUT',
request<void>("/password", {
method: "PUT",
body: JSON.stringify({
old_password: oldPassword,
new_password: newPassword,
@@ -109,12 +110,12 @@ export const logsApi = {
list: (params?: { method?: string; search?: string }) => {
const query = new URLSearchParams();
if (params?.method) {
query.set('method', params.method);
query.set("method", params.method);
}
if (params?.search) {
query.set('search', params.search);
query.set("search", params.search);
}
const queryString = query.toString();
return request<RequestLog[]>(`/logs${queryString ? `?${queryString}` : ''}`);
return request<RequestLog[]>(`/logs${queryString ? `?${queryString}` : ""}`);
},
};