Files
bw-expo-app/lib/api/client.ts
imeepos af64049c69 feat: 完整应用重构 - 优化界面架构与用户体验
主要变更:
- 重构应用界面:优化首页、登录、认证流程
- 新增用户档案模块:包含完整的档案管理组件
- 优化路由结构:重新组织页面布局和导航
- 改进API集成:新增活动、内容分类等API模块
- 删除冗余组件:清理不必要的文件和依赖

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 12:44:12 +08:00

80 lines
1.8 KiB
TypeScript

import { storage } from '../storage';
const BASE_URL = 'https://api-test.mixvideo.bowong.cc';
export interface ApiRequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
body?: string;
params?: Record<string, any>;
headers?: Record<string, string>;
requiresAuth?: boolean;
}
async function getAuthToken(): Promise<string> {
return (await storage.getItem(`bestaibest.better-auth.session_token`)) || '';
}
export async function apiRequest<T = any>(
endpoint: string,
options: ApiRequestOptions = {}
): Promise<T> {
const {
method = 'GET',
body,
params,
headers = {},
requiresAuth = true,
} = options;
// 构建 URL
const url = new URL(endpoint, BASE_URL);
// 添加查询参数
if (params) {
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
url.searchParams.append(key, String(value));
}
});
}
// 准备请求头
const requestHeaders: Record<string, string> = {
'Content-Type': 'application/json',
...headers,
};
// 如果需要认证,添加 Bearer token
if (requiresAuth) {
const token = await getAuthToken();
if (token) {
requestHeaders['Authorization'] = `Bearer ${token}`;
}
}
// 发起请求
const response = await fetch(url.toString(), {
method,
headers: requestHeaders,
body: method !== 'GET' ? body : undefined,
});
// 检查响应状态
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status}: ${errorText}`);
}
return response.json();
}
export async function apiClient<T = any>(
endpoint: string,
options: ApiRequestOptions = {}
): Promise<T> {
return apiRequest<T>(endpoint, {
...options,
requiresAuth: true,
});
}