mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Migrate user api to ts
This commit is contained in:
@@ -1,22 +1,6 @@
|
||||
import { API_BASE_URL } from '../utils/constants';
|
||||
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 () => {
|
||||
const response = await apiCall(`${API_BASE_URL}/workspaces/last`);
|
||||
return response.json();
|
||||
|
||||
41
app/src/api/user.ts
Normal file
41
app/src/api/user.ts
Normal 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
12
app/src/types/userApi.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user