118 lines
4.3 KiB
JavaScript
118 lines
4.3 KiB
JavaScript
import axios from 'axios';
|
|
|
|
const api = axios.create({
|
|
baseURL: '/',
|
|
withCredentials: true,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
|
|
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,
|
|
changePassword: async (current_password, new_password) =>
|
|
(await api.post('/api/auth/change-password', { current_password, new_password })).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,
|
|
setQuota: async (email, quota_gb) =>
|
|
(await api.post(`/api/mailboxes/${encodeURIComponent(email)}/quota`, { quota_gb })).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,
|
|
};
|
|
|
|
export const adminsAPI = {
|
|
list: async () => (await api.get('/api/admins')).data,
|
|
create: async ({ email, password, role, allowed_domains }) =>
|
|
(await api.post('/api/admins', { email, password, role, allowed_domains })).data,
|
|
update: async (email, payload) =>
|
|
(await api.put(`/api/admins/${encodeURIComponent(email)}`, payload)).data,
|
|
remove: async (email) =>
|
|
(await api.delete(`/api/admins/${encodeURIComponent(email)}`)).data,
|
|
};
|
|
|
|
export const billingAPI = {
|
|
summary: async (domain) => {
|
|
const params = new URLSearchParams();
|
|
if (domain) params.set('domain', domain);
|
|
const qs = params.toString();
|
|
return (await api.get(`/api/billing/summary${qs ? '?' + qs : ''}`)).data;
|
|
},
|
|
events: async ({ domain, from, to, limit } = {}) => {
|
|
const params = new URLSearchParams();
|
|
if (domain) params.set('domain', domain);
|
|
if (from) params.set('from', from);
|
|
if (to) params.set('to', to);
|
|
if (limit) params.set('limit', String(limit));
|
|
const qs = params.toString();
|
|
return (await api.get(`/api/billing/events${qs ? '?' + qs : ''}`)).data;
|
|
},
|
|
volume: async ({ domain, ym }) => {
|
|
const params = new URLSearchParams();
|
|
params.set('domain', domain);
|
|
if (ym) params.set('ym', ym);
|
|
return (await api.get(`/api/billing/volume?${params.toString()}`)).data;
|
|
},
|
|
volumeOverview: async ({ ym } = {}) => {
|
|
const params = new URLSearchParams();
|
|
if (ym) params.set('ym', ym);
|
|
const qs = params.toString();
|
|
return (await api.get(`/api/billing/volume-overview${qs ? '?' + qs : ''}`)).data;
|
|
},
|
|
};
|
|
|
|
export const healthAPI = {
|
|
getStatus: async (domain) => {
|
|
try {
|
|
return (await api.get(`/api/health/domains/${encodeURIComponent(domain)}`)).data;
|
|
} catch (err) {
|
|
if (err.statusCode === 404) return null;
|
|
throw err;
|
|
}
|
|
},
|
|
runCheck: async (domain) =>
|
|
(await api.post(`/api/health/domains/${encodeURIComponent(domain)}/check`)).data,
|
|
};
|