mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Refactor file API endpoints for consistency and add moveFile functionality
This commit is contained in:
@@ -47,7 +47,7 @@ export const lookupFileByName = async (
|
|||||||
const response = await apiCall(
|
const response = await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
workspaceName
|
workspaceName
|
||||||
)}/files/_op/lookup?filename=${encodeURIComponent(filename)}`
|
)}/files/lookup?filename=${encodeURIComponent(filename)}`
|
||||||
);
|
);
|
||||||
const data: unknown = await response.json();
|
const data: unknown = await response.json();
|
||||||
if (!isLookupResponse(data)) {
|
if (!isLookupResponse(data)) {
|
||||||
@@ -71,7 +71,7 @@ export const getFileContent = async (
|
|||||||
const response = await apiCall(
|
const response = await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
workspaceName
|
workspaceName
|
||||||
)}/files/${encodeURIComponent(filePath)}`
|
)}/files/content?file_path=${encodeURIComponent(filePath)}`
|
||||||
);
|
);
|
||||||
return response.text();
|
return response.text();
|
||||||
};
|
};
|
||||||
@@ -92,7 +92,7 @@ export const saveFile = async (
|
|||||||
const response = await apiCall(
|
const response = await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
workspaceName
|
workspaceName
|
||||||
)}/files/${encodeURIComponent(filePath)}`,
|
)}/files?file_path=${encodeURIComponent(filePath)}`,
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -118,7 +118,7 @@ export const deleteFile = async (workspaceName: string, filePath: string) => {
|
|||||||
await apiCall(
|
await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
workspaceName
|
workspaceName
|
||||||
)}/files/${encodeURIComponent(filePath)}`,
|
)}/files?file_path=${encodeURIComponent(filePath)}`,
|
||||||
{
|
{
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
}
|
}
|
||||||
@@ -135,9 +135,7 @@ export const getLastOpenedFile = async (
|
|||||||
workspaceName: string
|
workspaceName: string
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
const response = await apiCall(
|
const response = await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}/files/last`
|
||||||
workspaceName
|
|
||||||
)}/files/_op/last`
|
|
||||||
);
|
);
|
||||||
const data: unknown = await response.json();
|
const data: unknown = await response.json();
|
||||||
if (
|
if (
|
||||||
@@ -163,13 +161,71 @@ export const updateLastOpenedFile = async (
|
|||||||
await apiCall(
|
await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
workspaceName
|
workspaceName
|
||||||
)}/files/_op/last`,
|
)}/files/last?file_path=${encodeURIComponent(filePath)}`,
|
||||||
{
|
{
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ filePath }),
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* moveFile moves a file to a new location in a workspace
|
||||||
|
* @param workspaceName - The name of the workspace
|
||||||
|
* @param srcPath - The source path of the file to move
|
||||||
|
* @param destPath - The destination path for the file
|
||||||
|
* @returns {Promise<SaveFileResponse>} A promise that resolves to the move file response
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const moveFile = async (
|
||||||
|
workspaceName: string,
|
||||||
|
srcPath: string,
|
||||||
|
destPath: string
|
||||||
|
): Promise<SaveFileResponse> => {
|
||||||
|
const response = await apiCall(
|
||||||
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
|
workspaceName
|
||||||
|
)}/files/move?src_path=${encodeURIComponent(
|
||||||
|
srcPath
|
||||||
|
)}&dest_path=${encodeURIComponent(destPath)}`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const data: unknown = await response.json();
|
||||||
|
if (!isSaveFileResponse(data)) {
|
||||||
|
throw new Error('Invalid move file response received from API');
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* uploadFile uploads a file to a workspace
|
||||||
|
* @param workspaceName - The name of the workspace
|
||||||
|
* @param directoryPath - The directory path where the file should be uploaded
|
||||||
|
* @param file - The file to upload
|
||||||
|
* @returns {Promise<SaveFileResponse>} A promise that resolves to the upload file response
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const uploadFile = async (
|
||||||
|
workspaceName: string,
|
||||||
|
directoryPath: string,
|
||||||
|
file: File
|
||||||
|
): Promise<SaveFileResponse> => {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
|
const response = await apiCall(
|
||||||
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(
|
||||||
|
workspaceName
|
||||||
|
)}/files/upload?file_path=${encodeURIComponent(directoryPath)}`,
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const data: unknown = await response.json();
|
||||||
|
if (!isSaveFileResponse(data)) {
|
||||||
|
throw new Error('Invalid upload file response received from API');
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user