feat: midterm shit done

This commit is contained in:
Tony Yang
2025-04-15 03:59:33 +08:00
parent f093df29a1
commit f7ee02586b
34 changed files with 1460 additions and 197 deletions
+175
View File
@@ -0,0 +1,175 @@
import { unauthRedirectToLogin } from '../router';
const API_BASE_URL = '/api';
export async function register(username, password) {
const response = await fetch(API_BASE_URL + "/register", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Registration failed');
}
return response.json();
}
export async function login(username, password) {
const response = await fetch(API_BASE_URL + "/login", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Login failed');
}
return response.json();
}
export async function postMessage(message, jwt) {
const response = await fetch(API_BASE_URL + "/messages", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + jwt,
},
body: JSON.stringify({ message }),
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'Posting message failed');
}
return response.json();
}
export async function deleteMessage(messageId, jwt) {
const response = await fetch(API_BASE_URL + "/messages", {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + jwt,
},
body: JSON.stringify({ messageId }),
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'Deleting message failed');
}
return response.json();
}
export async function getMessages(jwt) {
const response = await fetch(API_BASE_URL + "/messages", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'Getting messages failed');
}
return response.json();
}
export async function getProfile(jwt) {
const response = await fetch(API_BASE_URL + "/me", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + jwt,
},
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'Getting profile failed');
}
return response.json();
}
export async function me(jwt) {
const response = await fetch(API_BASE_URL + "/me", {
method: 'GET',
headers: {
'Authorization': "Bearer " + jwt,
},
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'User operation failed');
}
const userData = await response.json();
const avatarFilename = userData.avatar;
if (avatarFilename) {
return import.meta.env.VITE_R2_BASE_URL + "/" + avatarFilename;
}
return null;
}
export async function uploadAvatar(avatar, jwt) {
const body = new FormData();
body.append('avatar', avatar);
const response = await fetch(API_BASE_URL + "/avatars", {
method: 'PUT',
headers: {
'Authorization': "Bearer " + jwt,
},
body: body,
});
if (!response.ok) {
if (response.status === 401) {
unauthRedirectToLogin();
return;
}
const error = await response.json();
throw new Error(error.error || 'Avatar upload failed');
}
const data = await response.json();
return data;
}