Skip to content
File

Blob: src/client/lib/api/emails.ts

typescript100 lines
1import { EmailDetail, EmailPage, OkResponse } from "@/shared/contracts";
2import { getInboxBookmarkScope } from "./bookmarks";
3import { fetchWithSession, parseError, request, ApiError } from "./http";
4import type { AuthDescriptor } from "./shared";
5 
6function buildEmailsPath(address: string, suffix: string, query: URLSearchParams, auth: AuthDescriptor) {
7 if (auth.mode === "admin") {
8 query.set("admin", "1");
9 }
10 const queryString = query.toString();
11 const base = `/api/protected/inboxes/${encodeURIComponent(address)}${suffix}`;
12 return queryString ? `${base}?${queryString}` : base;
13}
14 
15function bearerToken(auth: AuthDescriptor) {
16 return auth.mode === "user" ? auth.token : undefined;
17}
18 
19export async function listEmails(
20 address: string,
21 auth: AuthDescriptor,
22 options: {
23 page?: number;
24 includeTotal?: boolean;
25 } = {},
26) {
27 const params = new URLSearchParams({
28 page: String(options.page ?? 0),
29 });
30 
31 if (options.includeTotal) {
32 params.set("includeTotal", "1");
33 }
34 
35 return request(buildEmailsPath(address, "/emails", params, auth), EmailPage, {
36 token: bearerToken(auth),
37 bookmarkScope: getInboxBookmarkScope(address),
38 });
39}
40 
41export async function getEmail(address: string, emailId: string, auth: AuthDescriptor) {
42 return request(
43 buildEmailsPath(address, `/emails/${encodeURIComponent(emailId)}`, new URLSearchParams(), auth),
44 EmailDetail,
45 {
46 token: bearerToken(auth),
47 bookmarkScope: getInboxBookmarkScope(address),
48 },
49 );
50}
51 
52export async function deleteEmail(address: string, emailId: string, auth: AuthDescriptor) {
53 return request(
54 buildEmailsPath(address, `/emails/${encodeURIComponent(emailId)}`, new URLSearchParams(), auth),
55 OkResponse,
56 {
57 method: "DELETE",
58 token: bearerToken(auth),
59 bookmarkScope: getInboxBookmarkScope(address),
60 },
61 );
62}
63 
64export async function downloadAttachment(address: string, emailId: string, attachmentId: string, auth: AuthDescriptor) {
65 const path = buildEmailsPath(
66 address,
67 `/emails/${encodeURIComponent(emailId)}/attachments/${encodeURIComponent(attachmentId)}`,
68 new URLSearchParams(),
69 auth,
70 );
71 
72 const response = await fetchWithSession(path, {
73 token: bearerToken(auth),
74 bookmarkScope: getInboxBookmarkScope(address),
75 });
76 
77 if (!response.ok) {
78 const { code, message } = await parseError(response);
79 throw new ApiError(message, response.status, code);
80 }
81 
82 return response.blob();
83}
84 
85export async function getRawEmailSource(address: string, emailId: string, auth: AuthDescriptor) {
86 const path = buildEmailsPath(address, `/emails/${encodeURIComponent(emailId)}/raw`, new URLSearchParams(), auth);
87 
88 const response = await fetchWithSession(path, {
89 token: bearerToken(auth),
90 bookmarkScope: getInboxBookmarkScope(address),
91 });
92 
93 if (!response.ok) {
94 const { code, message } = await parseError(response);
95 throw new ApiError(message, response.status, code);
96 }
97 
98 return response.text();
99}