refactor(api): 拆分 bgm/projects/tags 为目录结构(API层全面目录化收尾) (#954)
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 项目 API — 目录化入口
|
||||
* 保持与原 projects.ts 相同导出,向后兼容
|
||||
*/
|
||||
|
||||
// 类型
|
||||
export type { ProjectItem, BackendProjectResponse, BackendListProjectsResponse } from "./types"
|
||||
|
||||
// 工具函数
|
||||
export { toProjectItem } from "./utils"
|
||||
|
||||
// API 函数
|
||||
export { getProjects, createProject, getOrCreateDefaultProject } from "./projects"
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 项目相关 API 函数
|
||||
* 素材库需要 project_id,前端自动管理默认项目
|
||||
*/
|
||||
import apiClient from "../client"
|
||||
import type { BackendListProjectsResponse, BackendProjectResponse, ProjectItem } from "./types"
|
||||
import { toProjectItem } from "./utils"
|
||||
|
||||
/** 获取当前用户的项目列表 */
|
||||
export const getProjects = async (): Promise<ProjectItem[]> => {
|
||||
const response = await apiClient.get<BackendListProjectsResponse>("/projects")
|
||||
return (response.data.items || []).map(toProjectItem)
|
||||
}
|
||||
|
||||
/** 创建项目 */
|
||||
export const createProject = async (data: {
|
||||
name: string
|
||||
description?: string
|
||||
}): Promise<ProjectItem> => {
|
||||
const response = await apiClient.post<BackendProjectResponse>("/projects", {
|
||||
name: data.name,
|
||||
description: data.description || "",
|
||||
})
|
||||
return toProjectItem(response.data)
|
||||
}
|
||||
|
||||
/** 获取或创建默认项目(素材库需要 project_id) */
|
||||
export const getOrCreateDefaultProject = async (): Promise<ProjectItem> => {
|
||||
const projects = await getProjects()
|
||||
if (projects.length > 0) {
|
||||
return projects[0]
|
||||
}
|
||||
// 没有项目时自动创建默认项目
|
||||
return createProject({
|
||||
name: "默认项目",
|
||||
description: "系统自动创建的默认项目",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 项目相关类型定义
|
||||
*/
|
||||
|
||||
export interface ProjectItem {
|
||||
id: string
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
|
||||
/** 后端 ProjectResponse 只返回 id, name, description */
|
||||
export interface BackendProjectResponse {
|
||||
id: string
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
|
||||
/** 后端 ListProjectsResponse 返回 { items: [...] } */
|
||||
export interface BackendListProjectsResponse {
|
||||
items: BackendProjectResponse[]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* 项目相关工具函数
|
||||
*/
|
||||
import type { BackendProjectResponse, ProjectItem } from "./types"
|
||||
|
||||
export const toProjectItem = (item: BackendProjectResponse): ProjectItem => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
description: item.description,
|
||||
})
|
||||
Reference in New Issue
Block a user