Files
xiaoxia-saas/apps/web/src/api/projects/projects.ts
T

39 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 项目相关 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: "系统自动创建的默认项目",
})
}