File
Blob: src/client/lib/api/public.ts
| 1 | import { |
| 2 | CreateInboxRequest, |
| 3 | CreateInboxResponse, |
| 4 | DomainsResponse, |
| 5 | OkResponse, |
| 6 | PublicConfigResponse, |
| 7 | type TempMailboxTtlHours, |
| 8 | } from "@/shared/contracts"; |
| 9 | import { getInboxBookmarkScope, setStoredBookmark } from "./bookmarks"; |
| 10 | import { encodeJsonBody, request } from "./http"; |
| 11 | |
| 12 | let publicConfigPromise: Promise<{ turnstileSiteKey: string }> | null = null; |
| 13 | |
| 14 | export async function listDomains() { |
| 15 | const response = await request("/api/public/domains", DomainsResponse); |
| 16 | return response.domains; |
| 17 | } |
| 18 | |
| 19 | export async function getPublicConfig() { |
| 20 | if (!publicConfigPromise) { |
| 21 | publicConfigPromise = request("/api/public/config", PublicConfigResponse).catch((error) => { |
| 22 | publicConfigPromise = null; |
| 23 | throw error; |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | return publicConfigPromise; |
| 28 | } |
| 29 | |
| 30 | export async function createInbox(domain: string, ttlHours: TempMailboxTtlHours, turnstileToken: string) { |
| 31 | let bookmark: string | null = null; |
| 32 | const response = await request("/api/public/inboxes", CreateInboxResponse, { |
| 33 | method: "POST", |
| 34 | body: encodeJsonBody(CreateInboxRequest, { domain, ttlHours, turnstileToken }), |
| 35 | onBookmark: (value) => { |
| 36 | bookmark = value; |
| 37 | }, |
| 38 | }); |
| 39 | |
| 40 | if (bookmark) { |
| 41 | setStoredBookmark(getInboxBookmarkScope(response.address), bookmark); |
| 42 | } |
| 43 | |
| 44 | return response; |
| 45 | } |
| 46 | |
| 47 | export function adminSignInUrl(): string { |
| 48 | return "/api/public/admin/start"; |
| 49 | } |
| 50 | |
| 51 | export async function adminLogout() { |
| 52 | return request("/api/public/admin/logout", OkResponse, { |
| 53 | method: "POST", |
| 54 | }); |
| 55 | } |