Initial Commit
This commit is contained in:
98
frontend/src/utils/api.js
Normal file
98
frontend/src/utils/api.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const BASE = '/api';
|
||||
|
||||
function getToken() {
|
||||
return localStorage.getItem('tc_token') || sessionStorage.getItem('tc_token');
|
||||
}
|
||||
|
||||
async function req(method, path, body, opts = {}) {
|
||||
const token = getToken();
|
||||
const headers = {};
|
||||
if (token) headers['Authorization'] = `Bearer ${token}`;
|
||||
|
||||
let fetchOpts = { method, headers };
|
||||
|
||||
if (body instanceof FormData) {
|
||||
fetchOpts.body = body;
|
||||
} else if (body) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
fetchOpts.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
const res = await fetch(BASE + path, fetchOpts);
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Request failed');
|
||||
return data;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
// Auth
|
||||
login: (body) => req('POST', '/auth/login', body),
|
||||
submitSupport: (body) => req('POST', '/auth/support', body),
|
||||
logout: () => req('POST', '/auth/logout'),
|
||||
me: () => req('GET', '/auth/me'),
|
||||
changePassword: (body) => req('POST', '/auth/change-password', body),
|
||||
|
||||
// Users
|
||||
getUsers: () => req('GET', '/users'),
|
||||
searchUsers: (q) => req('GET', `/users/search?q=${encodeURIComponent(q)}`),
|
||||
createUser: (body) => req('POST', '/users', body),
|
||||
bulkUsers: (users) => req('POST', '/users/bulk', { users }),
|
||||
updateRole: (id, role) => req('PATCH', `/users/${id}/role`, { role }),
|
||||
resetPassword: (id, password) => req('PATCH', `/users/${id}/reset-password`, { password }),
|
||||
suspendUser: (id) => req('PATCH', `/users/${id}/suspend`),
|
||||
activateUser: (id) => req('PATCH', `/users/${id}/activate`),
|
||||
deleteUser: (id) => req('DELETE', `/users/${id}`),
|
||||
updateProfile: (body) => req('PATCH', '/users/me/profile', body), // body: { displayName, aboutMe, hideAdminTag }
|
||||
uploadAvatar: (file) => {
|
||||
const form = new FormData(); form.append('avatar', file);
|
||||
return req('POST', '/users/me/avatar', form);
|
||||
},
|
||||
|
||||
// Groups
|
||||
getGroups: () => req('GET', '/groups'),
|
||||
createGroup: (body) => req('POST', '/groups', body),
|
||||
renameGroup: (id, name) => req('PATCH', `/groups/${id}/rename`, { name }),
|
||||
getMembers: (id) => req('GET', `/groups/${id}/members`),
|
||||
addMember: (groupId, userId) => req('POST', `/groups/${groupId}/members`, { userId }),
|
||||
leaveGroup: (id) => req('DELETE', `/groups/${id}/leave`),
|
||||
takeOwnership: (id) => req('POST', `/groups/${id}/take-ownership`),
|
||||
deleteGroup: (id) => req('DELETE', `/groups/${id}`),
|
||||
|
||||
// Messages
|
||||
getMessages: (groupId, before) => req('GET', `/messages/group/${groupId}${before ? `?before=${before}` : ''}`),
|
||||
sendMessage: (groupId, body) => req('POST', `/messages/group/${groupId}`, body),
|
||||
uploadImage: (groupId, file, extra = {}) => {
|
||||
const form = new FormData();
|
||||
form.append('image', file);
|
||||
if (extra.replyToId) form.append('replyToId', extra.replyToId);
|
||||
if (extra.content) form.append('content', extra.content);
|
||||
return req('POST', `/messages/group/${groupId}/image`, form);
|
||||
},
|
||||
deleteMessage: (id) => req('DELETE', `/messages/${id}`),
|
||||
toggleReaction: (id, emoji) => req('POST', `/messages/${id}/reactions`, { emoji }),
|
||||
|
||||
// Settings
|
||||
getSettings: () => req('GET', '/settings'),
|
||||
updateAppName: (name) => req('PATCH', '/settings/app-name', { name }),
|
||||
uploadLogo: (file) => {
|
||||
const form = new FormData(); form.append('logo', file);
|
||||
return req('POST', '/settings/logo', form);
|
||||
},
|
||||
uploadIconNewChat: (file) => {
|
||||
const form = new FormData(); form.append('icon', file);
|
||||
return req('POST', '/settings/icon-newchat', form);
|
||||
},
|
||||
uploadIconGroupInfo: (file) => {
|
||||
const form = new FormData(); form.append('icon', file);
|
||||
return req('POST', '/settings/icon-groupinfo', form);
|
||||
},
|
||||
resetSettings: () => req('POST', '/settings/reset'),
|
||||
|
||||
// Push notifications
|
||||
getPushKey: () => req('GET', '/push/vapid-public'),
|
||||
subscribePush: (sub) => req('POST', '/push/subscribe', sub),
|
||||
unsubscribePush: (endpoint) => req('POST', '/push/unsubscribe', { endpoint }),
|
||||
|
||||
// Link preview
|
||||
getLinkPreview: (url) => req('GET', `/link-preview?url=${encodeURIComponent(url)}`),
|
||||
};
|
||||
Reference in New Issue
Block a user