react
This commit is contained in:
71
frontend/src/services/api.js
Normal file
71
frontend/src/services/api.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import axios from 'axios';
|
||||
|
||||
// Same-origin: backend serves the built frontend, vite dev proxies /api.
|
||||
const api = axios.create({
|
||||
baseURL: '/',
|
||||
withCredentials: true,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
// Centralized error normalization so callers always see err.message + err.statusCode.
|
||||
api.interceptors.response.use(
|
||||
(r) => r,
|
||||
(err) => {
|
||||
const status = err.response?.status;
|
||||
const body = err.response?.data;
|
||||
const message = body?.error || err.message || 'Request failed';
|
||||
const wrapped = new Error(message);
|
||||
wrapped.statusCode = status;
|
||||
wrapped.original = err;
|
||||
return Promise.reject(wrapped);
|
||||
}
|
||||
);
|
||||
|
||||
export const authAPI = {
|
||||
me: async () => (await api.get('/api/auth/me')).data,
|
||||
login: async (email, password) =>
|
||||
(await api.post('/api/auth/login', { email, password })).data,
|
||||
logout: async () => (await api.post('/api/auth/logout')).data,
|
||||
};
|
||||
|
||||
export const domainsAPI = {
|
||||
list: async (resync = false) =>
|
||||
(await api.get(`/api/domains${resync ? '?resync=true' : ''}`)).data,
|
||||
resync: async () => (await api.post('/api/domains/resync')).data,
|
||||
};
|
||||
|
||||
export const mailboxesAPI = {
|
||||
list: async (domain, refreshQuota = false) => {
|
||||
const params = new URLSearchParams();
|
||||
if (domain) params.set('domain', domain);
|
||||
if (refreshQuota) params.set('refreshQuota', 'true');
|
||||
return (await api.get(`/api/mailboxes?${params.toString()}`)).data;
|
||||
},
|
||||
create: async (email, password) =>
|
||||
(await api.post('/api/mailboxes', { email, password })).data,
|
||||
remove: async (email) =>
|
||||
(await api.delete(`/api/mailboxes/${encodeURIComponent(email)}`)).data,
|
||||
setPassword: async (email, password) =>
|
||||
(await api.post(
|
||||
`/api/mailboxes/${encodeURIComponent(email)}/password`,
|
||||
{ password }
|
||||
)).data,
|
||||
getRules: async (email) =>
|
||||
(await api.get(`/api/mailboxes/${encodeURIComponent(email)}/rules`)).data,
|
||||
putRules: async (email, payload) =>
|
||||
(await api.put(
|
||||
`/api/mailboxes/${encodeURIComponent(email)}/rules`,
|
||||
payload
|
||||
)).data,
|
||||
getBlocklist: async (email) =>
|
||||
(await api.get(`/api/mailboxes/${encodeURIComponent(email)}/blocklist`)).data,
|
||||
putBlocklist: async (email, blocked_patterns) =>
|
||||
(await api.put(
|
||||
`/api/mailboxes/${encodeURIComponent(email)}/blocklist`,
|
||||
{ blocked_patterns }
|
||||
)).data,
|
||||
};
|
||||
|
||||
export const auditAPI = {
|
||||
list: async () => (await api.get('/api/audit')).data,
|
||||
};
|
||||
Reference in New Issue
Block a user