mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Migrate workspace api to ts
This commit is contained in:
@@ -1,31 +1,6 @@
|
|||||||
import { API_BASE_URL } from '../utils/constants';
|
import { API_BASE_URL } from '../utils/constants';
|
||||||
import { apiCall } from './auth';
|
import { apiCall } from './auth';
|
||||||
|
|
||||||
export const fetchLastWorkspaceName = async () => {
|
|
||||||
const response = await apiCall(`${API_BASE_URL}/workspaces/last`);
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getWorkspace = async (workspaceName) => {
|
|
||||||
const response = await apiCall(`${API_BASE_URL}/workspaces/${workspaceName}`);
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Combined function to update workspace data including settings
|
|
||||||
export const updateWorkspace = async (workspaceName, workspaceData) => {
|
|
||||||
const response = await apiCall(
|
|
||||||
`${API_BASE_URL}/workspaces/${workspaceName}`,
|
|
||||||
{
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(workspaceData),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const pullChanges = async (workspaceName) => {
|
export const pullChanges = async (workspaceName) => {
|
||||||
const response = await apiCall(
|
const response = await apiCall(
|
||||||
`${API_BASE_URL}/workspaces/${workspaceName}/git/pull`,
|
`${API_BASE_URL}/workspaces/${workspaceName}/git/pull`,
|
||||||
@@ -53,40 +28,3 @@ export const commitAndPush = async (workspaceName, message) => {
|
|||||||
export const getFileUrl = (workspaceName, filePath) => {
|
export const getFileUrl = (workspaceName, filePath) => {
|
||||||
return `${API_BASE_URL}/workspaces/${workspaceName}/files/${filePath}`;
|
return `${API_BASE_URL}/workspaces/${workspaceName}/files/${filePath}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const listWorkspaces = async () => {
|
|
||||||
const response = await apiCall(`${API_BASE_URL}/workspaces`);
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createWorkspace = async (name) => {
|
|
||||||
const response = await apiCall(`${API_BASE_URL}/workspaces`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ name }),
|
|
||||||
});
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const deleteWorkspace = async (workspaceName) => {
|
|
||||||
const response = await apiCall(
|
|
||||||
`${API_BASE_URL}/workspaces/${workspaceName}`,
|
|
||||||
{
|
|
||||||
method: 'DELETE',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|
||||||
export const updateLastWorkspaceName = async (workspaceName) => {
|
|
||||||
const response = await apiCall(`${API_BASE_URL}/workspaces/last`, {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ workspaceName }),
|
|
||||||
});
|
|
||||||
return response.json();
|
|
||||||
};
|
|
||||||
|
|||||||
151
app/src/api/workspace.ts
Normal file
151
app/src/api/workspace.ts
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
import { API_BASE_URL } from '@/types/authApi';
|
||||||
|
import { apiCall } from './api';
|
||||||
|
import {
|
||||||
|
DeleteWorkspaceResponse,
|
||||||
|
isWorkspace,
|
||||||
|
LastWorkspaceNameResponse,
|
||||||
|
Workspace,
|
||||||
|
} from '@/types/workspace';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* listWorkspaces fetches the list of workspaces
|
||||||
|
* @returns {Promise<Workspace[]>} A promise that resolves to an array of Workspace objects
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const listWorkspaces = async (): Promise<Workspace[]> => {
|
||||||
|
const response = await apiCall(`${API_BASE_URL}/workspaces`);
|
||||||
|
const data = await response.json();
|
||||||
|
if (!Array.isArray(data)) {
|
||||||
|
throw new Error('Invalid workspaces response received from API');
|
||||||
|
}
|
||||||
|
return data.map((workspace) => {
|
||||||
|
if (!isWorkspace(workspace)) {
|
||||||
|
throw new Error('Invalid workspace object received from API');
|
||||||
|
}
|
||||||
|
return workspace as Workspace;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createWorkspace creates a new workspace with the given name
|
||||||
|
* @param name - The name of the workspace to create
|
||||||
|
* @returns {Promise<Workspace>} A promise that resolves to the created Workspace object
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const createWorkspace = async (name: string): Promise<Workspace> => {
|
||||||
|
const response = await apiCall(`${API_BASE_URL}/workspaces`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ name }),
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (!isWorkspace(data)) {
|
||||||
|
throw new Error('Invalid workspace object received from API');
|
||||||
|
}
|
||||||
|
return data as Workspace;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getWorkspace fetches the workspace with the given name
|
||||||
|
* @param workspaceName - The name of the workspace to fetch
|
||||||
|
* @returns {Promise<Workspace>} A promise that resolves to the Workspace object
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const getWorkspace = async (
|
||||||
|
workspaceName: string
|
||||||
|
): Promise<Workspace> => {
|
||||||
|
const response = await apiCall(
|
||||||
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}`
|
||||||
|
);
|
||||||
|
const data = response.json();
|
||||||
|
if (!isWorkspace(data)) {
|
||||||
|
throw new Error('Invalid workspace object received from API');
|
||||||
|
}
|
||||||
|
return data as Workspace;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* updateWorkspace updates the workspace with the given name
|
||||||
|
* @param workspaceName - The name of the workspace to update
|
||||||
|
* @param workspaceData - The updated Workspace object
|
||||||
|
* @returns {Promise<Workspace>} A promise that resolves to the updated Workspace object
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const updateWorkspace = async (
|
||||||
|
workspaceName: string,
|
||||||
|
workspaceData: Workspace
|
||||||
|
): Promise<Workspace> => {
|
||||||
|
const response = await apiCall(
|
||||||
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}`,
|
||||||
|
{
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(workspaceData),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const data = response.json();
|
||||||
|
if (!isWorkspace(data)) {
|
||||||
|
throw new Error('Invalid workspace object received from API');
|
||||||
|
}
|
||||||
|
return data as Workspace;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deleteWorkspace deletes the workspace with the given name
|
||||||
|
* @param workspaceName - The name of the workspace to delete
|
||||||
|
* @returns {Promise<DeleteWorkspaceResponse>} A promise that resolves to the response object
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const deleteWorkspace = async (
|
||||||
|
workspaceName: string
|
||||||
|
): Promise<DeleteWorkspaceResponse> => {
|
||||||
|
const response = await apiCall(
|
||||||
|
`${API_BASE_URL}/workspaces/${encodeURIComponent(workspaceName)}`,
|
||||||
|
{
|
||||||
|
method: 'DELETE',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
const data = await response.json();
|
||||||
|
if (!('nextWorkspaceName' in data)) {
|
||||||
|
throw new Error('Invalid delete workspace response received from API');
|
||||||
|
}
|
||||||
|
return data as DeleteWorkspaceResponse;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getLastWorkspaceName fetches the last workspace name
|
||||||
|
* @returns {Promise<LastWorkspaceNameResponse>} A promise that resolves to the last workspace name
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const getLastWorkspaceName =
|
||||||
|
async (): Promise<LastWorkspaceNameResponse> => {
|
||||||
|
const response = await apiCall(`${API_BASE_URL}/workspaces/last`);
|
||||||
|
const data = await response.json();
|
||||||
|
if (!('lastWorkspaceName' in data)) {
|
||||||
|
throw new Error('Invalid last workspace name response received from API');
|
||||||
|
}
|
||||||
|
return data as LastWorkspaceNameResponse;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* updateLastWorkspaceName updates the last workspace name
|
||||||
|
* @param workspaceName - The name of the workspace to set as last
|
||||||
|
* @throws {Error} If the API call fails or returns an invalid response
|
||||||
|
*/
|
||||||
|
export const updateLastWorkspaceName = async (workspaceName: string) => {
|
||||||
|
const response = await apiCall(`${API_BASE_URL}/workspaces/last`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ workspaceName }),
|
||||||
|
});
|
||||||
|
if (response.status !== 204) {
|
||||||
|
throw new Error('Failed to update last workspace name');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
@@ -1,5 +1,13 @@
|
|||||||
import { Theme } from './theme';
|
import { Theme } from './theme';
|
||||||
|
|
||||||
|
export interface DeleteWorkspaceResponse {
|
||||||
|
nextWorkspaceName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LastWorkspaceNameResponse {
|
||||||
|
lastWorkspaceName: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface WorkspaceSettings {
|
export interface WorkspaceSettings {
|
||||||
theme: Theme;
|
theme: Theme;
|
||||||
autoSave: boolean;
|
autoSave: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user