From 66fe5e485b010b3db65e093d5a458c323881148c Mon Sep 17 00:00:00 2001 From: LordMathis Date: Tue, 6 May 2025 20:16:22 +0200 Subject: [PATCH] Migrate user api to ts --- app/src/api/notes.js | 16 ---------------- app/src/api/user.ts | 41 ++++++++++++++++++++++++++++++++++++++++ app/src/types/userApi.ts | 12 ++++++++++++ 3 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 app/src/api/user.ts create mode 100644 app/src/types/userApi.ts diff --git a/app/src/api/notes.js b/app/src/api/notes.js index cb8d8d4..7e3a057 100644 --- a/app/src/api/notes.js +++ b/app/src/api/notes.js @@ -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(); diff --git a/app/src/api/user.ts b/app/src/api/user.ts new file mode 100644 index 0000000..a8747a2 --- /dev/null +++ b/app/src/api/user.ts @@ -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 => { + 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; +}; diff --git a/app/src/types/userApi.ts b/app/src/types/userApi.ts new file mode 100644 index 0000000..2cec49d --- /dev/null +++ b/app/src/types/userApi.ts @@ -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; +}