From 02c8100f0bd4ab9941485a792a0c1a0d6ab1d398 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Tue, 6 May 2025 20:42:12 +0200 Subject: [PATCH] Migrate workspace api to ts --- app/src/api/notes.js | 62 --------------- app/src/api/workspace.ts | 151 +++++++++++++++++++++++++++++++++++++ app/src/types/workspace.ts | 8 ++ 3 files changed, 159 insertions(+), 62 deletions(-) create mode 100644 app/src/api/workspace.ts diff --git a/app/src/api/notes.js b/app/src/api/notes.js index 7e3a057..01fe725 100644 --- a/app/src/api/notes.js +++ b/app/src/api/notes.js @@ -1,31 +1,6 @@ import { API_BASE_URL } from '../utils/constants'; 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) => { const response = await apiCall( `${API_BASE_URL}/workspaces/${workspaceName}/git/pull`, @@ -53,40 +28,3 @@ export const commitAndPush = async (workspaceName, message) => { export const getFileUrl = (workspaceName, 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(); -}; diff --git a/app/src/api/workspace.ts b/app/src/api/workspace.ts new file mode 100644 index 0000000..135fbfc --- /dev/null +++ b/app/src/api/workspace.ts @@ -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} 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 => { + 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} 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 => { + 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} 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 => { + 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} 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 => { + 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} 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 => { + 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} 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 => { + 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; +}; diff --git a/app/src/types/workspace.ts b/app/src/types/workspace.ts index 15ec4f2..b88c041 100644 --- a/app/src/types/workspace.ts +++ b/app/src/types/workspace.ts @@ -1,5 +1,13 @@ import { Theme } from './theme'; +export interface DeleteWorkspaceResponse { + nextWorkspaceName: string; +} + +export interface LastWorkspaceNameResponse { + lastWorkspaceName: string; +} + export interface WorkspaceSettings { theme: Theme; autoSave: boolean;