Migrate user api to ts

This commit is contained in:
2025-05-06 20:16:22 +02:00
parent 905df9f6dd
commit 66fe5e485b
3 changed files with 53 additions and 16 deletions

View File

@@ -1,22 +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 updateProfile = async (updates) => {
const response = await apiCall(`${API_BASE_URL}/profile`, {
method: 'PUT',
body: JSON.stringify(updates),
});
return response.json();
};
export const deleteProfile = async (password) => {
const response = await apiCall(`${API_BASE_URL}/profile`, {
method: 'DELETE',
body: JSON.stringify({ password }),
});
return response.json();
};
export const fetchLastWorkspaceName = async () => { export const fetchLastWorkspaceName = async () => {
const response = await apiCall(`${API_BASE_URL}/workspaces/last`); const response = await apiCall(`${API_BASE_URL}/workspaces/last`);
return response.json(); return response.json();

41
app/src/api/user.ts Normal file
View File

@@ -0,0 +1,41 @@
import { API_BASE_URL, isUser, User } from '@/types/authApi';
import { apiCall } from './api';
import { UpdateProfileRequest } from '@/types/userApi';
/**
* updateProfile updates the user's profile information.
* @param updateRequest - The request object containing the updated profile information.
* @returns A promise that resolves to the updated user object.
* @throws An error if the response is not valid user data.
*/
export const updateProfile = async (
updateRequest: UpdateProfileRequest
): Promise<User> => {
const response = await apiCall(`${API_BASE_URL}/profile`, {
method: 'PUT',
body: JSON.stringify(updateRequest),
});
const data = response.json();
if (!isUser(data)) {
throw new Error('Invalid user data');
}
return data as User;
};
/**
* deleteProfile deletes the user's profile.
* @param password - The password of the user.
* @throws An error if the response status is not 204 (No Content).
*/
export const deleteUser = async (password: string) => {
const response = await apiCall(`${API_BASE_URL}/profile`, {
method: 'DELETE',
body: JSON.stringify({ password }),
});
if (response.status !== 204) {
throw new Error('Failed to delete profile');
}
return;
};

12
app/src/types/userApi.ts Normal file
View File

@@ -0,0 +1,12 @@
// UpdateProfileRequest represents a user profile update request
export interface UpdateProfileRequest {
displayName?: string;
email?: string;
currentPassword?: string;
newPassword?: string;
}
// DeleteAccountRequest represents a user account deletion request
export interface DeleteAccountRequest {
password: string;
}