diff --git a/apps/web/src/api/titles/index.ts b/apps/web/src/api/titles/index.ts new file mode 100644 index 000000000..9900272d0 --- /dev/null +++ b/apps/web/src/api/titles/index.ts @@ -0,0 +1,19 @@ +/** + * 标题相关 API — 目录化入口 + * 保持与原 titles.ts 相同导出,向后兼容 + */ + +// 类型 +export type { + TitleItem, + BackendTitleResponse, + BackendCreateTitleRequest, + BackendUpdateTitleRequest, + CreateTitleRequest, +} from "./types" + +// 工具函数 +export { toTitleItem } from "./utils" + +// API 函数 +export { getTitles, createTitle, updateTitle, deleteTitle, batchImportTitles } from "./titles" diff --git a/apps/web/src/api/titles.ts b/apps/web/src/api/titles/titles.ts similarity index 59% rename from apps/web/src/api/titles.ts rename to apps/web/src/api/titles/titles.ts index 23ba5ea17..b4174f820 100644 --- a/apps/web/src/api/titles.ts +++ b/apps/web/src/api/titles/titles.ts @@ -1,70 +1,17 @@ /** - * 标题相关 API + * 标题相关 API 函数 * Phase 1 新增:全局标题库 * 注意:后端 schema 使用 name + text 字段,前端 UI 用 content 展示 */ -import apiClient from "./client" - -/** 标题条目(前端展示用) */ -export interface TitleItem { - id: string - content: string - category?: string - source?: string - word_count?: number - is_favorite?: boolean - created_at?: string - updated_at?: string -} - -/** 后端标题响应格式 */ -interface BackendTitleResponse { - id: string - user_id: string - name: string - text: string - category: string - description: string - tags: string[] - usage_count: number - is_active: boolean - created_at: string - updated_at: string -} - -/** 后端创建标题请求格式 */ -interface BackendCreateTitleRequest { - name: string - text: string - category: string - description?: string - tags?: string[] -} - -/** 后端更新标题请求格式 */ -interface BackendUpdateTitleRequest { - name?: string - text?: string - category?: string - description?: string - tags?: string[] -} - -/** 将后端响应映射为前端 TitleItem */ -const toTitleItem = (item: BackendTitleResponse): TitleItem => ({ - id: item.id, - content: item.text, - category: item.category, - word_count: item.text?.length || 0, - created_at: item.created_at, - updated_at: item.updated_at, -}) - -/** 创建标题请求(前端接口,保持向后兼容) */ -export interface CreateTitleRequest { - content: string - category?: string -} +import apiClient from "../client" +import type { + BackendCreateTitleRequest, + BackendTitleResponse, + BackendUpdateTitleRequest, + CreateTitleRequest, + TitleItem, +} from "./types" +import { toTitleItem } from "./utils" /** 获取当前用户的所有标题 */ export const getTitles = async (): Promise => { diff --git a/apps/web/src/api/titles/types.ts b/apps/web/src/api/titles/types.ts new file mode 100644 index 000000000..37dc715d9 --- /dev/null +++ b/apps/web/src/api/titles/types.ts @@ -0,0 +1,54 @@ +/** + * 标题相关类型定义 + */ + +/** 标题条目(前端展示用) */ +export interface TitleItem { + id: string + content: string + category?: string + source?: string + word_count?: number + is_favorite?: boolean + created_at?: string + updated_at?: string +} + +/** 后端标题响应格式 */ +export interface BackendTitleResponse { + id: string + user_id: string + name: string + text: string + category: string + description: string + tags: string[] + usage_count: number + is_active: boolean + created_at: string + updated_at: string +} + +/** 后端创建标题请求格式 */ +export interface BackendCreateTitleRequest { + name: string + text: string + category: string + description?: string + tags?: string[] +} + +/** 后端更新标题请求格式 */ +export interface BackendUpdateTitleRequest { + name?: string + text?: string + category?: string + description?: string + tags?: string[] +} + +/** 创建标题请求(前端接口,保持向后兼容) */ +export interface CreateTitleRequest { + content: string + category?: string +} diff --git a/apps/web/src/api/titles/utils.ts b/apps/web/src/api/titles/utils.ts new file mode 100644 index 000000000..64b88fbfd --- /dev/null +++ b/apps/web/src/api/titles/utils.ts @@ -0,0 +1,14 @@ +/** + * 标题数据转换工具函数 + */ +import type { BackendTitleResponse, TitleItem } from "./types" + +/** 将后端响应映射为前端 TitleItem */ +export const toTitleItem = (item: BackendTitleResponse): TitleItem => ({ + id: item.id, + content: item.text, + category: item.category, + word_count: item.text?.length || 0, + created_at: item.created_at, + updated_at: item.updated_at, +})