// apps/miniprogram/e2e/helpers/api-client.ts // 简化版 API Client,用于小程序 E2E 数据准备/清理 const API_BASE = process.env.E2E_API_URL || 'http://localhost:3000/api/v1'; export class MpApiClient { private token = ''; async login(username?: string, password?: string) { const res = await fetch(`${API_BASE}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: username || process.env.E2E_ADMIN_USER || 'admin', password: password || process.env.E2E_ADMIN_PASS || 'Admin@2026', }), }); const json = await res.json(); if (!json.success) throw new Error('Login failed'); this.token = json.data.access_token; return json.data; } getToken() { return this.token; } async createPatient(overrides?: Record) { return this.post('/health/patients', overrides ?? {}); } async deletePatient(id: string, version: number) { await this.del(`/health/patients/${id}`, { version }); } async createVitalSigns(patientId: string, overrides?: Record) { return this.post(`/health/patients/${patientId}/vital-signs`, overrides ?? {}); } async deleteVitalSigns(patientId: string, id: string, version: number) { await this.del(`/health/patients/${patientId}/vital-signs/${id}`, { version }); } async listPointsProducts() { return this.get('/health/points/products'); } private async headers() { return { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` }; } private async get(path: string) { const res = await fetch(`${API_BASE}${path}`, { headers: await this.headers() }); const json = await res.json(); return json.data; } private async post(path: string, body: unknown) { const res = await fetch(`${API_BASE}${path}`, { method: 'POST', headers: await this.headers(), body: JSON.stringify(body), }); const json = await res.json(); if (!json.success) throw new Error(`POST ${path} failed`); return json.data; } private async del(path: string, body?: unknown) { await fetch(`${API_BASE}${path}`, { method: 'DELETE', headers: await this.headers(), body: body ? JSON.stringify(body) : undefined, }); } }