From 8b11383bc4848e6b5e9e0fe8471cc1df8b700c52 Mon Sep 17 00:00:00 2001 From: sion123 <450702724@qq.com> Date: Thu, 7 May 2026 02:27:01 +0800 Subject: [PATCH] feat(web): add typed API client --- web/client/src/lib/api.ts | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 web/client/src/lib/api.ts diff --git a/web/client/src/lib/api.ts b/web/client/src/lib/api.ts new file mode 100644 index 0000000..89c06ee --- /dev/null +++ b/web/client/src/lib/api.ts @@ -0,0 +1,48 @@ +import type { Account, Asset, Conversation, Message, ConfigItem } from '@/types'; + +const BASE = '/api'; + +async function request(path: string, options?: RequestInit): Promise { + const res = await fetch(`${BASE}${path}`, { + headers: { 'Content-Type': 'application/json' }, + ...options, + }); + if (!res.ok) throw new Error(await res.text()); + return res.json(); +} + +export const api = { + // Accounts + listAccounts: () => request('/accounts'), + getAccount: (id: string) => request(`/accounts/${id}`), + createAccount: (data: Partial) => + request('/accounts', { method: 'POST', body: JSON.stringify(data) }), + updateAccount: (id: string, data: Partial) => + request(`/accounts/${id}`, { method: 'PUT', body: JSON.stringify(data) }), + deleteAccount: (id: string) => + request(`/accounts/${id}`, { method: 'DELETE' }), + + // Prompts + getPrompt: (accountId: string, type: string) => + request<{ path: string; content: string }>(`/prompts/${accountId}/${type}`), + savePrompt: (accountId: string, type: string, content: string) => + request(`/prompts/${accountId}/${type}`, { method: 'PUT', body: JSON.stringify({ content }) }), + + // Conversations + listConversations: () => request('/pipeline/conversations'), + getMessages: (convId: string) => request(`/pipeline/conversations/${convId}/messages`), + + // Assets + listAssets: (params?: { accountId?: string; type?: string }) => { + const qs = new URLSearchParams(); + if (params?.accountId) qs.set('accountId', params.accountId); + if (params?.type) qs.set('type', params.type); + return request(`/assets?${qs}`); + }, + deleteAsset: (id: string) => request(`/assets/${id}`, { method: 'DELETE' }), + + // Configs + getConfigs: () => request('/configs'), + saveConfig: (key: string, value: Record) => + request(`/configs/${key}`, { method: 'PUT', body: JSON.stringify({ value }) }), +};