agentic.email/frontend/src/api.js
2026-10-06 01:51:23 +05:30

174 lines
6.7 KiB
JavaScript

const API_BASE = "/api/v1";
function basicAuthHeader({ username, password }) {
return `Basic ${btoa(`${username}:${password}`)}`;
}
async function request(path, { method = "GET", payload, token, basicAuth } = {}) {
const response = await fetch(`${API_BASE}${path}`, {
method,
headers: {
...(payload ? { "Content-Type": "application/json" } : {}),
...(token ? { Authorization: `Bearer ${token}` } : {}),
...(basicAuth
? { Authorization: basicAuthHeader(basicAuth) }
: {}),
},
...(payload ? { body: JSON.stringify(payload) } : {}),
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
const message = Array.isArray(data.detail)
? data.detail[0]?.msg
: data.detail;
throw new Error(message || "Something went wrong. Please try again.");
}
return data;
}
export const login = (payload) =>
request("/auth/login", { method: "POST", payload });
export const previewInvite = (token) =>
request(`/auth/invites/${encodeURIComponent(token)}`);
export const acceptInvite = (token, payload) =>
request(`/auth/invites/${encodeURIComponent(token)}/accept`, {
method: "POST",
payload,
});
export const register = (payload) =>
request("/auth/register", { method: "POST", payload });
export const getCurrentAccount = (token) =>
request("/auth/me", { token });
export const getMyCompanyDomain = (token) =>
request("/domains/me", { token });
export const verifyCompanyDns = (token) =>
request("/domains/me/verify-dns", { method: "POST", token });
export const listMyDomains = (token) =>
request("/domains/me/domains", { token });
export const addMyDomain = (domainName, token) =>
request("/domains/me/domains", {
method: "POST",
payload: { domain_name: domainName },
token,
});
export const getMyDomain = (domainId, token) =>
request(`/domains/me/domains/${domainId}`, { token });
export const verifyMyDomainDns = (domainId, token) =>
request(`/domains/me/domains/${domainId}/verify-dns`, { method: "POST", token });
export const listOrgMembers = (token) =>
request("/people", { token });
export const createOrgMember = (payload, token) =>
request("/people", { method: "POST", payload, token });
export const importOrgMembers = (payload, token) =>
request("/people/import", { method: "POST", payload, token });
export const listOrgSections = (token) =>
request("/sections", { token });
export const createOrgSection = (payload, token) =>
request("/sections", { method: "POST", payload, token });
export const listMailGroups = (token) =>
request("/groups", { token });
export const createMailGroup = (payload, token) =>
request("/groups", { method: "POST", payload, token });
export const checkGroupEmail = (email, token) =>
request(`/groups/check-email?email=${encodeURIComponent(email)}`, { token });
export const listMailAliases = (token) =>
request("/aliases", { token });
export const createMailAlias = (payload, token) =>
request("/aliases", { method: "POST", payload, token });
export const checkAliasEmail = (email, token) =>
request(`/aliases/check-email?email=${encodeURIComponent(email)}`, { token });
export const getOrgSecurity = (token) =>
request("/org/security", { token });
export const updateOrgSecurity = (payload, token) =>
request("/org/security", { method: "PATCH", payload, token });
export const updateOrgMemberRole = (memberId, payload, token) =>
request(`/people/${memberId}/role`, { method: "PATCH", payload, token });
export const changePassword = (payload, token) =>
request("/auth/change-password", { method: "POST", payload, token });
export const sendEmail = (payload, token) =>
request("/emails", { method: "POST", payload, token });
function emailQuery(parameters = {}) {
const query = new URLSearchParams();
Object.entries(parameters).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== "") {
query.set(key, String(value));
}
});
const encoded = query.toString();
return encoded ? `?${encoded}` : "";
}
export const listEmails = (parameters, token) =>
request(`/emails${emailQuery(parameters)}`, { token });
export const listThreads = (parameters, token) =>
request(`/threads${emailQuery(parameters)}`, { token });
export const getThread = (threadId, token) =>
request(`/threads/${encodeURIComponent(threadId)}`, { token });
export const getEmail = (emailId, token) =>
request(`/emails/${encodeURIComponent(emailId)}`, { token });
export const setEmailRead = (emailId, isRead, token) =>
request(`/emails/${encodeURIComponent(emailId)}/read`, {
method: "PATCH",
payload: { is_read: isRead },
token,
});
export const setEmailStar = (emailId, isStarred, token) =>
request(`/emails/${encodeURIComponent(emailId)}/star`, {
method: "PATCH",
payload: { is_starred: isStarred },
token,
});
export const trashEmail = (emailId, token) =>
request(`/emails/${encodeURIComponent(emailId)}`, {
method: "DELETE",
token,
});
export const restoreEmail = (emailId, token) =>
request(`/emails/${encodeURIComponent(emailId)}/restore`, {
method: "POST",
token,
});
export const permanentlyDeleteEmail = (emailId, token) =>
request(`/emails/${encodeURIComponent(emailId)}/permanent`, {
method: "DELETE",
token,
});
export const listHandoffRules = (token) =>
request("/agent/handoff", { token });
export const createHandoffRule = (payload, token) =>
request("/agent/handoff", { method: "POST", payload, token });
export const updateHandoffRule = (ruleId, payload, token) =>
request(`/agent/handoff/${encodeURIComponent(ruleId)}`, { method: "PATCH", payload, token });
export const deleteHandoffRule = (ruleId, token) =>
request(`/agent/handoff/${encodeURIComponent(ruleId)}`, { method: "DELETE", token });
export const testHandoffRule = (payload, token) =>
request("/agent/handoff/test", { method: "POST", payload, token });
export const listHandoffConversations = (token) =>
request("/agent/handoff/conversations", { token });
export const getHandoffConversation = (handoffId, token) =>
request(`/agent/handoff/conversations/${encodeURIComponent(handoffId)}`, { token });
export const listStickyNotes = (token) =>
request("/sticky-notes", { token });
export const createStickyNote = (payload, token) =>
request("/sticky-notes", { method: "POST", payload, token });
export const updateStickyNote = (noteId, payload, token) =>
request(`/sticky-notes/${encodeURIComponent(noteId)}`, {
method: "PATCH",
payload,
token,
});
export const deleteStickyNote = (noteId, token) =>
request(`/sticky-notes/${encodeURIComponent(noteId)}`, {
method: "DELETE",
token,
});
export const previewDnsSetup = (payload, basicAuth) =>
request("/dns-setup/preview", {
method: "POST",
payload,
basicAuth,
});