Files
accounts-manager-web/lib/api.ts
Your Name 0b95ca36f1 Initial project setup with Next.js accounts manager
- Set up Next.js project structure
- Added UI components and styling
- Configured package dependencies
- Added feature documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-23 01:40:14 +08:00

73 lines
2.0 KiB
TypeScript

import {
ApiResponse,
ListAccountsBody,
ListAccountsResponse,
BatchDeleteBody,
BatchUpdateBody,
StatsOverview,
ScriptUploadItem
} from './types';
const API_BASE_URL = 'http://localhost:3006';
class ApiClient {
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<ApiResponse<T>> {
const url = `${API_BASE_URL}${endpoint}`;
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers,
},
...options,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
// 获取账户列表
async getAccountsList(body: ListAccountsBody): Promise<ApiResponse<ListAccountsResponse>> {
return this.request<ListAccountsResponse>('/web/v1/accounts/list', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 批量删除账户
async batchDeleteAccounts(body: BatchDeleteBody): Promise<ApiResponse<{ deletedCount: number }>> {
return this.request<{ deletedCount: number }>('/web/v1/accounts/delete-batch', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 批量更新账户
async batchUpdateAccounts(body: BatchUpdateBody): Promise<ApiResponse<{ updatedCount: number }>> {
return this.request<{ updatedCount: number }>('/web/v1/accounts/update-batch', {
method: 'POST',
body: JSON.stringify(body),
});
}
// 获取统计概览
async getStatsOverview(): Promise<ApiResponse<StatsOverview>> {
return this.request<StatsOverview>('/web/v1/stats/overview');
}
// 上传账户
async uploadAccounts(accounts: ScriptUploadItem[], ownerId: string): Promise<ApiResponse<{ processedCount: number; createdCount: number; updatedCount: number }>> {
return this.request<{ processedCount: number; createdCount: number; updatedCount: number }>(`/s/v1/${ownerId}/upload`, {
method: 'POST',
body: JSON.stringify(accounts),
});
}
}
export const apiClient = new ApiClient();