0fcb77b991
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Frontend Lint (push) Successful in 1m6s
CI Build & Deploy Pipeline / Build Staging Web Image (push) Successful in 1m49s
CI Build & Deploy Pipeline / Build Production API Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Web Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Production Worker Image (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (push) Has been skipped
CI Build & Deploy Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Validate Code Quality And Tests (push) Successful in 3m1s
CI/CD Pipeline / Unit Tests (push) Successful in 3m20s
CI/CD Pipeline / Integration Tests (push) Successful in 1m29s
CI Build & Deploy Pipeline / Build Staging API Image (push) Successful in 7m4s
CI Build & Deploy Pipeline / Build Staging Worker Image (push) Successful in 7m54s
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 45s
CI Build & Deploy Pipeline / Staging E2E Tests (push) Failing after 2m50s
CI Build & Deploy Pipeline / Staging API Integration Tests (push) Successful in 3m28s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
119 lines
3.2 KiB
TypeScript
119 lines
3.2 KiB
TypeScript
/**
|
|
* 标题相关 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
|
|
}
|
|
|
|
/** 获取当前用户的所有标题 */
|
|
export const getTitles = async (): Promise<TitleItem[]> => {
|
|
const response = await apiClient.get<{ items: BackendTitleResponse[] } | BackendTitleResponse[]>(
|
|
"/titles",
|
|
)
|
|
// 兼容两种后端返回格式:{ items: [...] } 或直接 [...]
|
|
const items = Array.isArray(response.data) ? response.data : response.data.items || []
|
|
return items.map(toTitleItem)
|
|
}
|
|
|
|
/** 创建标题 */
|
|
export const createTitle = async (data: CreateTitleRequest): Promise<TitleItem> => {
|
|
// 后端要求 name(≤255)和 text(≤500),name 从 content 截取
|
|
const payload: BackendCreateTitleRequest = {
|
|
name: data.content.slice(0, 255),
|
|
text: data.content.slice(0, 500),
|
|
category: data.category || "default",
|
|
}
|
|
const response = await apiClient.post<BackendTitleResponse>("/titles", payload)
|
|
return toTitleItem(response.data)
|
|
}
|
|
|
|
/** 更新标题 */
|
|
export const updateTitle = async (
|
|
titleId: string,
|
|
data: Partial<CreateTitleRequest>,
|
|
): Promise<TitleItem> => {
|
|
const payload: BackendUpdateTitleRequest = {}
|
|
if (data.content !== undefined) {
|
|
payload.name = data.content.slice(0, 255)
|
|
payload.text = data.content.slice(0, 500)
|
|
}
|
|
if (data.category !== undefined) {
|
|
payload.category = data.category
|
|
}
|
|
// 后端用 PUT,非 PATCH
|
|
const response = await apiClient.put<BackendTitleResponse>(`/titles/${titleId}`, payload)
|
|
return toTitleItem(response.data)
|
|
}
|
|
|
|
/** 删除标题 */
|
|
export const deleteTitle = async (titleId: string): Promise<void> => {
|
|
await apiClient.delete(`/titles/${titleId}`)
|
|
}
|
|
|
|
/** 批量导入标题 */
|
|
export const batchImportTitles = async (titles: string[]): Promise<{ imported_count: number }> => {
|
|
const response = await apiClient.post("/titles/batch-import", { titles })
|
|
return response.data
|
|
}
|