Files
XCDesktop/src/lib/api/client.ts

127 lines
3.5 KiB
TypeScript

import type { FileContentDTO, FileItemDTO, PathExistsDTO, SettingsDTO } from '@shared/types'
import { fetchApi } from './http'
export type FileItem = FileItemDTO
export type FileContent = FileContentDTO
export type Settings = SettingsDTO
export type UploadedImage = { name: string; path: string }
export const searchFiles = async (keywords: string[]): Promise<FileItem[]> => {
const data = await fetchApi<{ items: FileItem[] }>('/api/search', {
method: 'POST',
body: { keywords },
})
return data.items
}
export const fetchFiles = async (path: string = ''): Promise<FileItem[]> => {
const data = await fetchApi<{ items: FileItem[] }>(`/api/files?path=${encodeURIComponent(path)}`)
return data.items
}
export const fetchFileContent = async (path: string): Promise<FileContent> => {
return await fetchApi<FileContent>(`/api/files/content?path=${encodeURIComponent(path)}`)
}
export const saveFileContent = async (path: string, content: string): Promise<void> => {
await fetchApi<null>('/api/files/save', {
method: 'POST',
body: { path, content },
})
}
export const uploadClipboardImage = async (image: string): Promise<UploadedImage> => {
return await fetchApi<UploadedImage>('/api/files/upload/image', {
method: 'POST',
body: { image },
})
}
export const deleteFile = async (path: string): Promise<void> => {
await fetchApi<null>('/api/files/delete', {
method: 'DELETE',
body: { path },
})
}
export const createDirectory = async (path: string): Promise<void> => {
await fetchApi<null>('/api/files/create/dir', {
method: 'POST',
body: { path },
})
}
export const createFile = async (path: string): Promise<void> => {
await fetchApi<null>('/api/files/create/file', {
method: 'POST',
body: { path },
})
}
export const checkPathExists = async (path: string): Promise<PathExistsDTO> => {
return await fetchApi<PathExistsDTO>('/api/files/exists', {
method: 'POST',
body: { path },
})
}
export const renameItem = async (oldPath: string, newPath: string): Promise<void> => {
await fetchApi<null>('/api/files/rename', {
method: 'POST',
body: { oldPath, newPath },
})
}
export const runAiTask = async (task: string, path: string): Promise<void> => {
await fetchApi<null>('/api/ai/doubao', {
method: 'POST',
body: { task, path },
})
}
export const getSettings = async (): Promise<Settings> => {
return await fetchApi<Settings>('/api/settings')
}
export const getSettingsConfig = async (): Promise<{ notebookRoot: string }> => {
return await fetchApi<{ notebookRoot: string }>('/api/settings/config')
}
export const saveSettings = async (settings: Settings): Promise<Settings> => {
return await fetchApi<Settings>('/api/settings', {
method: 'POST',
body: settings,
})
}
export const uploadPdfForParsing = async (file: File, targetPath: string): Promise<void> => {
const formData = new FormData()
formData.append('file', file)
formData.append('targetPath', targetPath)
await fetchApi<void>('/api/mineru/parse', {
method: 'POST',
body: formData,
})
}
export interface LocalHtmlInfo {
htmlPath: string
htmlDir: string
assetsDirName?: string
assetsFiles?: string[]
}
export const parseLocalHtml = async (info: LocalHtmlInfo, targetPath: string): Promise<void> => {
await fetchApi<void>('/api/blog/parse-local', {
method: 'POST',
body: {
htmlPath: info.htmlPath,
htmlDir: info.htmlDir,
assetsDirName: info.assetsDirName,
assetsFiles: info.assetsFiles,
targetPath,
},
})
}