File
Blob: src/client/lib/api/inboxes.ts
| 1 | import { |
| 2 | ExtendInboxRequest, |
| 3 | ExtendInboxResponse, |
| 4 | InboxInfo, |
| 5 | OkResponse, |
| 6 | WebSocketTicketResponse, |
| 7 | type TempMailboxTtlHours, |
| 8 | } from "@/shared/contracts"; |
| 9 | import { getInboxBookmarkScope } from "./bookmarks"; |
| 10 | import { encodeJsonBody, request } from "./http"; |
| 11 | import type { AuthDescriptor } from "./shared"; |
| 12 | |
| 13 | function buildInboxPath(address: string, suffix: string, auth: AuthDescriptor) { |
| 14 | const base = `/api/protected/inboxes/${encodeURIComponent(address)}${suffix}`; |
| 15 | return auth.mode === "admin" ? `${base}${suffix.includes("?") ? "&" : "?"}admin=1` : base; |
| 16 | } |
| 17 | |
| 18 | function bearerToken(auth: AuthDescriptor) { |
| 19 | return auth.mode === "user" ? auth.token : undefined; |
| 20 | } |
| 21 | |
| 22 | export async function getInbox(address: string, auth: AuthDescriptor) { |
| 23 | return request(buildInboxPath(address, "", auth), InboxInfo, { |
| 24 | token: bearerToken(auth), |
| 25 | bookmarkScope: getInboxBookmarkScope(address), |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | export async function deleteInbox(address: string, auth: AuthDescriptor) { |
| 30 | return request(buildInboxPath(address, "", auth), OkResponse, { |
| 31 | method: "DELETE", |
| 32 | token: bearerToken(auth), |
| 33 | bookmarkScope: getInboxBookmarkScope(address), |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | export async function extendInbox(address: string, auth: AuthDescriptor, ttlHours: TempMailboxTtlHours) { |
| 38 | return request(buildInboxPath(address, "/extend", auth), ExtendInboxResponse, { |
| 39 | method: "POST", |
| 40 | token: bearerToken(auth), |
| 41 | bookmarkScope: getInboxBookmarkScope(address), |
| 42 | body: encodeJsonBody(ExtendInboxRequest, { ttlHours }), |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | export async function createWebSocketTicket(address: string, auth: AuthDescriptor) { |
| 47 | return request(buildInboxPath(address, "/ws-ticket", auth), WebSocketTicketResponse, { |
| 48 | method: "POST", |
| 49 | token: bearerToken(auth), |
| 50 | bookmarkScope: getInboxBookmarkScope(address), |
| 51 | }); |
| 52 | } |