@arbidocs/sdk 0.5.76 → 0.5.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2245,6 +2245,27 @@ interface NativeMailKeys {
2245
2245
  * Call it again to unlock mail that arrived since.
2246
2246
  */
2247
2247
  declare function unlockNativeMail(mail: MailFetch, keys: NativeMailKeys): Promise<void>;
2248
+ /**
2249
+ * Hand this session a connected IMAP account's credentials: they are kept sealed to the user, so
2250
+ * unwrap them with the user's private key and re-seal them to the server session.
2251
+ */
2252
+ declare function unlockMailCredentials(mail: MailFetch, accountId: string, keys: NativeMailKeys): Promise<void>;
2253
+ /** What opens a draft's body in the Word editor (`key` is filled in by {@link openDraftEditor}). */
2254
+ type MailDraftEditorOpen = Omit<Schemas$2['MailRoomOpen'], 'key'>;
2255
+ /** A collaboration session on a draft's body; `identity` is who edits in the room. */
2256
+ type MailDraftEditorSession = Omit<Schemas$2['MailRoomSession'], 'identity'> & {
2257
+ identity: {
2258
+ actorId: string;
2259
+ name: string;
2260
+ role: 'human' | 'agent';
2261
+ };
2262
+ };
2263
+ /**
2264
+ * Open a native draft's body in the Word editor. A room this session has not unlocked answers 423
2265
+ * with its key sealed to the user: unwrap it with their private key and seal it to the server
2266
+ * session, as unlocking the inbox does, then open again with it.
2267
+ */
2268
+ declare function openDraftEditor(mail: MailFetch, body: MailDraftEditorOpen, keys: () => Promise<NativeMailKeys>): Promise<MailDraftEditorSession>;
2248
2269
  /** Build an attachment from bytes. */
2249
2270
  declare function attachmentFromBytes(name: string, contentType: string, bytes: Uint8Array): MailAttachment;
2250
2271
 
@@ -2254,6 +2275,8 @@ type mail_MailAddress = MailAddress;
2254
2275
  type mail_MailAddressInput = MailAddressInput;
2255
2276
  type mail_MailAttachment = MailAttachment;
2256
2277
  type mail_MailCompose = MailCompose;
2278
+ type mail_MailDraftEditorOpen = MailDraftEditorOpen;
2279
+ type mail_MailDraftEditorSession = MailDraftEditorSession;
2257
2280
  type mail_MailDraftSaved = MailDraftSaved;
2258
2281
  type mail_MailFetch = MailFetch;
2259
2282
  type mail_MailFolder = MailFolder;
@@ -2273,12 +2296,14 @@ declare const mail_getMailPolicy: typeof getMailPolicy;
2273
2296
  declare const mail_listFolders: typeof listFolders;
2274
2297
  declare const mail_listMailAccounts: typeof listMailAccounts;
2275
2298
  declare const mail_listMessages: typeof listMessages;
2299
+ declare const mail_openDraftEditor: typeof openDraftEditor;
2276
2300
  declare const mail_saveDraft: typeof saveDraft;
2277
2301
  declare const mail_sendMessage: typeof sendMessage;
2302
+ declare const mail_unlockMailCredentials: typeof unlockMailCredentials;
2278
2303
  declare const mail_unlockNativeMail: typeof unlockNativeMail;
2279
2304
  declare const mail_updateMessage: typeof updateMessage;
2280
2305
  declare namespace mail {
2281
- export { type mail_ListMessagesOptions as ListMessagesOptions, type mail_MailAccount as MailAccount, type mail_MailAddress as MailAddress, type mail_MailAddressInput as MailAddressInput, type mail_MailAttachment as MailAttachment, type mail_MailCompose as MailCompose, type mail_MailDraftSaved as MailDraftSaved, type mail_MailFetch as MailFetch, type mail_MailFolder as MailFolder, type mail_MailMessage as MailMessage, type mail_MailMessagePage as MailMessagePage, type mail_MailMessageUpdate as MailMessageUpdate, type mail_MailPolicy as MailPolicy, type mail_MailSend as MailSend, type mail_MailSendResult as MailSendResult, mail_NATIVE_ACCOUNT as NATIVE_ACCOUNT, type mail_NativeMailKeys as NativeMailKeys, mail_attachmentFromBytes as attachmentFromBytes, mail_claimNativeAlias as claimNativeAlias, mail_createMailFetch as createMailFetch, mail_downloadAttachment as downloadAttachment, mail_getMailPolicy as getMailPolicy, getMessage$1 as getMessage, mail_listFolders as listFolders, mail_listMailAccounts as listMailAccounts, mail_listMessages as listMessages, mail_saveDraft as saveDraft, mail_sendMessage as sendMessage, mail_unlockNativeMail as unlockNativeMail, mail_updateMessage as updateMessage };
2306
+ export { type mail_ListMessagesOptions as ListMessagesOptions, type mail_MailAccount as MailAccount, type mail_MailAddress as MailAddress, type mail_MailAddressInput as MailAddressInput, type mail_MailAttachment as MailAttachment, type mail_MailCompose as MailCompose, type mail_MailDraftEditorOpen as MailDraftEditorOpen, type mail_MailDraftEditorSession as MailDraftEditorSession, type mail_MailDraftSaved as MailDraftSaved, type mail_MailFetch as MailFetch, type mail_MailFolder as MailFolder, type mail_MailMessage as MailMessage, type mail_MailMessagePage as MailMessagePage, type mail_MailMessageUpdate as MailMessageUpdate, type mail_MailPolicy as MailPolicy, type mail_MailSend as MailSend, type mail_MailSendResult as MailSendResult, mail_NATIVE_ACCOUNT as NATIVE_ACCOUNT, type mail_NativeMailKeys as NativeMailKeys, mail_attachmentFromBytes as attachmentFromBytes, mail_claimNativeAlias as claimNativeAlias, mail_createMailFetch as createMailFetch, mail_downloadAttachment as downloadAttachment, mail_getMailPolicy as getMailPolicy, getMessage$1 as getMessage, mail_listFolders as listFolders, mail_listMailAccounts as listMailAccounts, mail_listMessages as listMessages, mail_openDraftEditor as openDraftEditor, mail_saveDraft as saveDraft, mail_sendMessage as sendMessage, mail_unlockMailCredentials as unlockMailCredentials, mail_unlockNativeMail as unlockNativeMail, mail_updateMessage as updateMessage };
2282
2307
  }
2283
2308
 
2284
2309
  /**
@@ -12467,11 +12492,44 @@ declare class Arbi {
12467
12492
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12468
12493
  */
12469
12494
  readonly mail: {
12470
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12471
- fetch: (path: string, init?: RequestInit) => Promise<Response>;
12472
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12495
+ /**
12496
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12497
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12498
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12499
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12500
+ * again once.
12501
+ */
12502
+ fetch: (path: string, init?: RequestInit, { nativeUnlocked }?: {
12503
+ nativeUnlocked?: boolean;
12504
+ }) => Promise<Response>;
12505
+ /**
12506
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12507
+ * Concurrent calls share one unlock.
12508
+ */
12473
12509
  unlockNative: () => Promise<void>;
12510
+ /**
12511
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12512
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12513
+ */
12514
+ unlockAccount: (account: {
12515
+ id: string;
12516
+ provider: string;
12517
+ }) => Promise<void>;
12518
+ /**
12519
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12520
+ * user's key when the server asks (see `openDraftEditor`).
12521
+ */
12522
+ openDraftEditor: (body: MailDraftEditorOpen) => Promise<MailDraftEditorSession>;
12474
12523
  };
12524
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
12525
+ private nativeUnlock;
12526
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
12527
+ private mailRequest;
12528
+ /**
12529
+ * The keys that unlock the user's mail for this server session: the session's key (current after
12530
+ * a re-login) and the identity key (the login's, or the one kept on this device).
12531
+ */
12532
+ private nativeMailKeys;
12475
12533
  /**
12476
12534
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
12477
12535
  * (see `operations/secure-mail`). Both sides use the signed-in user's encryption key: the
@@ -2245,6 +2245,27 @@ interface NativeMailKeys {
2245
2245
  * Call it again to unlock mail that arrived since.
2246
2246
  */
2247
2247
  declare function unlockNativeMail(mail: MailFetch, keys: NativeMailKeys): Promise<void>;
2248
+ /**
2249
+ * Hand this session a connected IMAP account's credentials: they are kept sealed to the user, so
2250
+ * unwrap them with the user's private key and re-seal them to the server session.
2251
+ */
2252
+ declare function unlockMailCredentials(mail: MailFetch, accountId: string, keys: NativeMailKeys): Promise<void>;
2253
+ /** What opens a draft's body in the Word editor (`key` is filled in by {@link openDraftEditor}). */
2254
+ type MailDraftEditorOpen = Omit<Schemas$2['MailRoomOpen'], 'key'>;
2255
+ /** A collaboration session on a draft's body; `identity` is who edits in the room. */
2256
+ type MailDraftEditorSession = Omit<Schemas$2['MailRoomSession'], 'identity'> & {
2257
+ identity: {
2258
+ actorId: string;
2259
+ name: string;
2260
+ role: 'human' | 'agent';
2261
+ };
2262
+ };
2263
+ /**
2264
+ * Open a native draft's body in the Word editor. A room this session has not unlocked answers 423
2265
+ * with its key sealed to the user: unwrap it with their private key and seal it to the server
2266
+ * session, as unlocking the inbox does, then open again with it.
2267
+ */
2268
+ declare function openDraftEditor(mail: MailFetch, body: MailDraftEditorOpen, keys: () => Promise<NativeMailKeys>): Promise<MailDraftEditorSession>;
2248
2269
  /** Build an attachment from bytes. */
2249
2270
  declare function attachmentFromBytes(name: string, contentType: string, bytes: Uint8Array): MailAttachment;
2250
2271
 
@@ -2254,6 +2275,8 @@ type mail_MailAddress = MailAddress;
2254
2275
  type mail_MailAddressInput = MailAddressInput;
2255
2276
  type mail_MailAttachment = MailAttachment;
2256
2277
  type mail_MailCompose = MailCompose;
2278
+ type mail_MailDraftEditorOpen = MailDraftEditorOpen;
2279
+ type mail_MailDraftEditorSession = MailDraftEditorSession;
2257
2280
  type mail_MailDraftSaved = MailDraftSaved;
2258
2281
  type mail_MailFetch = MailFetch;
2259
2282
  type mail_MailFolder = MailFolder;
@@ -2273,12 +2296,14 @@ declare const mail_getMailPolicy: typeof getMailPolicy;
2273
2296
  declare const mail_listFolders: typeof listFolders;
2274
2297
  declare const mail_listMailAccounts: typeof listMailAccounts;
2275
2298
  declare const mail_listMessages: typeof listMessages;
2299
+ declare const mail_openDraftEditor: typeof openDraftEditor;
2276
2300
  declare const mail_saveDraft: typeof saveDraft;
2277
2301
  declare const mail_sendMessage: typeof sendMessage;
2302
+ declare const mail_unlockMailCredentials: typeof unlockMailCredentials;
2278
2303
  declare const mail_unlockNativeMail: typeof unlockNativeMail;
2279
2304
  declare const mail_updateMessage: typeof updateMessage;
2280
2305
  declare namespace mail {
2281
- export { type mail_ListMessagesOptions as ListMessagesOptions, type mail_MailAccount as MailAccount, type mail_MailAddress as MailAddress, type mail_MailAddressInput as MailAddressInput, type mail_MailAttachment as MailAttachment, type mail_MailCompose as MailCompose, type mail_MailDraftSaved as MailDraftSaved, type mail_MailFetch as MailFetch, type mail_MailFolder as MailFolder, type mail_MailMessage as MailMessage, type mail_MailMessagePage as MailMessagePage, type mail_MailMessageUpdate as MailMessageUpdate, type mail_MailPolicy as MailPolicy, type mail_MailSend as MailSend, type mail_MailSendResult as MailSendResult, mail_NATIVE_ACCOUNT as NATIVE_ACCOUNT, type mail_NativeMailKeys as NativeMailKeys, mail_attachmentFromBytes as attachmentFromBytes, mail_claimNativeAlias as claimNativeAlias, mail_createMailFetch as createMailFetch, mail_downloadAttachment as downloadAttachment, mail_getMailPolicy as getMailPolicy, getMessage$1 as getMessage, mail_listFolders as listFolders, mail_listMailAccounts as listMailAccounts, mail_listMessages as listMessages, mail_saveDraft as saveDraft, mail_sendMessage as sendMessage, mail_unlockNativeMail as unlockNativeMail, mail_updateMessage as updateMessage };
2306
+ export { type mail_ListMessagesOptions as ListMessagesOptions, type mail_MailAccount as MailAccount, type mail_MailAddress as MailAddress, type mail_MailAddressInput as MailAddressInput, type mail_MailAttachment as MailAttachment, type mail_MailCompose as MailCompose, type mail_MailDraftEditorOpen as MailDraftEditorOpen, type mail_MailDraftEditorSession as MailDraftEditorSession, type mail_MailDraftSaved as MailDraftSaved, type mail_MailFetch as MailFetch, type mail_MailFolder as MailFolder, type mail_MailMessage as MailMessage, type mail_MailMessagePage as MailMessagePage, type mail_MailMessageUpdate as MailMessageUpdate, type mail_MailPolicy as MailPolicy, type mail_MailSend as MailSend, type mail_MailSendResult as MailSendResult, mail_NATIVE_ACCOUNT as NATIVE_ACCOUNT, type mail_NativeMailKeys as NativeMailKeys, mail_attachmentFromBytes as attachmentFromBytes, mail_claimNativeAlias as claimNativeAlias, mail_createMailFetch as createMailFetch, mail_downloadAttachment as downloadAttachment, mail_getMailPolicy as getMailPolicy, getMessage$1 as getMessage, mail_listFolders as listFolders, mail_listMailAccounts as listMailAccounts, mail_listMessages as listMessages, mail_openDraftEditor as openDraftEditor, mail_saveDraft as saveDraft, mail_sendMessage as sendMessage, mail_unlockMailCredentials as unlockMailCredentials, mail_unlockNativeMail as unlockNativeMail, mail_updateMessage as updateMessage };
2282
2307
  }
2283
2308
 
2284
2309
  /**
@@ -12467,11 +12492,44 @@ declare class Arbi {
12467
12492
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12468
12493
  */
12469
12494
  readonly mail: {
12470
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12471
- fetch: (path: string, init?: RequestInit) => Promise<Response>;
12472
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12495
+ /**
12496
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12497
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12498
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12499
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12500
+ * again once.
12501
+ */
12502
+ fetch: (path: string, init?: RequestInit, { nativeUnlocked }?: {
12503
+ nativeUnlocked?: boolean;
12504
+ }) => Promise<Response>;
12505
+ /**
12506
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12507
+ * Concurrent calls share one unlock.
12508
+ */
12473
12509
  unlockNative: () => Promise<void>;
12510
+ /**
12511
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12512
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12513
+ */
12514
+ unlockAccount: (account: {
12515
+ id: string;
12516
+ provider: string;
12517
+ }) => Promise<void>;
12518
+ /**
12519
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12520
+ * user's key when the server asks (see `openDraftEditor`).
12521
+ */
12522
+ openDraftEditor: (body: MailDraftEditorOpen) => Promise<MailDraftEditorSession>;
12474
12523
  };
12524
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
12525
+ private nativeUnlock;
12526
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
12527
+ private mailRequest;
12528
+ /**
12529
+ * The keys that unlock the user's mail for this server session: the session's key (current after
12530
+ * a re-login) and the identity key (the login's, or the one kept on this device).
12531
+ */
12532
+ private nativeMailKeys;
12475
12533
  /**
12476
12534
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
12477
12535
  * (see `operations/secure-mail`). Both sides use the signed-in user's encryption key: the
package/dist/browser.cjs CHANGED
@@ -9964,8 +9964,10 @@ __export(mail_exports, {
9964
9964
  listFolders: () => listFolders,
9965
9965
  listMailAccounts: () => listMailAccounts,
9966
9966
  listMessages: () => listMessages,
9967
+ openDraftEditor: () => openDraftEditor,
9967
9968
  saveDraft: () => saveDraft,
9968
9969
  sendMessage: () => sendMessage,
9970
+ unlockMailCredentials: () => unlockMailCredentials,
9969
9971
  unlockNativeMail: () => unlockNativeMail,
9970
9972
  updateMessage: () => updateMessage
9971
9973
  });
@@ -10062,6 +10064,47 @@ async function unlockNativeMail(mail, keys) {
10062
10064
  secretKey.fill(0);
10063
10065
  }
10064
10066
  }
10067
+ async function unlockMailCredentials(mail, accountId, keys) {
10068
+ await client.initSodium();
10069
+ const secretKey = client.encryptionKeypairFromSigningKey(keys.signingPrivateKey).secretKey;
10070
+ const { sealed_credentials } = await json(
10071
+ mail(`${accountPath(accountId)}/sealed-credentials`)
10072
+ );
10073
+ const bytes = client.sealedBoxDecrypt(sealed_credentials, secretKey);
10074
+ try {
10075
+ const access = await client.sealKeyForSession(bytes, keys.serverSessionKey);
10076
+ await mail(`${accountPath(accountId)}/session-credentials`, {
10077
+ method: "POST",
10078
+ body: JSON.stringify({ access })
10079
+ });
10080
+ } finally {
10081
+ bytes.fill(0);
10082
+ secretKey.fill(0);
10083
+ }
10084
+ }
10085
+ async function openDraftEditor(mail, body, keys) {
10086
+ const open = (extra = {}) => json(
10087
+ mail("/drafts/editor", { method: "POST", body: JSON.stringify({ ...body, ...extra }) })
10088
+ );
10089
+ try {
10090
+ return await open();
10091
+ } catch (error) {
10092
+ const { status, apiError } = error;
10093
+ const locked = apiError?.detail;
10094
+ if (status !== 423 || !locked?.sealed_key || !locked.room_id) throw error;
10095
+ const { signingPrivateKey, serverSessionKey } = await keys();
10096
+ await client.initSodium();
10097
+ const secretKey = client.encryptionKeypairFromSigningKey(signingPrivateKey).secretKey;
10098
+ const key = client.sealedBoxDecrypt(locked.sealed_key, secretKey);
10099
+ try {
10100
+ const sealed = await client.sealKeyForSession(key, serverSessionKey);
10101
+ return await open({ room_id: locked.room_id, key: sealed });
10102
+ } finally {
10103
+ key.fill(0);
10104
+ secretKey.fill(0);
10105
+ }
10106
+ }
10107
+ }
10065
10108
  function attachmentFromBytes(name, contentType, bytes) {
10066
10109
  return {
10067
10110
  id: "",
@@ -12330,34 +12373,84 @@ var Arbi = class {
12330
12373
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12331
12374
  */
12332
12375
  mail = {
12333
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12334
- fetch: (path2, init = {}) => this.recovering(async () => {
12335
- const { baseUrl, accessToken, fetch: send = globalThis.fetch } = this.getAuthHeaders();
12336
- const headers = new Headers(init.headers);
12337
- headers.set("Authorization", `Bearer ${accessToken}`);
12338
- if (init.body && !headers.has("Content-Type"))
12339
- headers.set("Content-Type", "application/json");
12340
- const response = await send(`${baseUrl}/v1/mail${path2}`, { ...init, headers });
12341
- if (!response.ok) {
12342
- const body = await response.json().catch(() => null);
12343
- throw new ArbiApiError(
12344
- typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12345
- body,
12346
- response.status
12347
- );
12348
- }
12349
- return response;
12376
+ /**
12377
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12378
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12379
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12380
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12381
+ * again once.
12382
+ */
12383
+ fetch: (path2, init = {}, { nativeUnlocked = false } = {}) => this.recovering(async () => {
12384
+ const nativeRead = path2.startsWith("/accounts/native/") && (init.method ?? "GET").toUpperCase() !== "POST";
12385
+ if (nativeRead && !nativeUnlocked) await this.mail.unlockNative();
12386
+ return this.mailRequest(path2, init);
12350
12387
  }),
12351
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12352
- unlockNative: () => {
12353
- const login = this.loginResult;
12354
- if (!login) return Promise.reject(new ArbiError("Sign in to unlock your inbox"));
12355
- return unlockNativeMail(this.mail.fetch, {
12356
- signingPrivateKey: login.signingPrivateKey,
12357
- serverSessionKey: login.serverSessionKey
12358
- });
12359
- }
12388
+ /**
12389
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12390
+ * Concurrent calls share one unlock.
12391
+ */
12392
+ unlockNative: () => this.nativeUnlock ??= this.nativeMailKeys().then((keys) => unlockNativeMail(this.mail.fetch, keys)).finally(() => {
12393
+ this.nativeUnlock = null;
12394
+ }),
12395
+ /**
12396
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12397
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12398
+ */
12399
+ unlockAccount: async (account) => {
12400
+ if (account.provider === "native") return this.mail.unlockNative();
12401
+ if (account.provider === "microsoft" || account.provider === "gmail") {
12402
+ await this.connectors.bridgeTokens({ vendor: account.provider });
12403
+ return;
12404
+ }
12405
+ await unlockMailCredentials(this.mail.fetch, account.id, await this.nativeMailKeys());
12406
+ },
12407
+ /**
12408
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12409
+ * user's key when the server asks (see `openDraftEditor`).
12410
+ */
12411
+ openDraftEditor: (body) => openDraftEditor(this.mail.fetch, body, () => this.nativeMailKeys())
12360
12412
  };
12413
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
12414
+ nativeUnlock = null;
12415
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
12416
+ async mailRequest(path2, init) {
12417
+ const client = this.requireClient();
12418
+ let token = client.session.getState().accessToken;
12419
+ if (!token) token = await client.auth.relogin();
12420
+ if (!token) throw new ArbiError("Sign in again to restore your session");
12421
+ const send = client.http?.fetch ?? globalThis.fetch;
12422
+ const request = (bearer) => {
12423
+ const headers = new Headers(init.headers);
12424
+ headers.set("Authorization", `Bearer ${bearer}`);
12425
+ if (init.body && !headers.has("Content-Type")) headers.set("Content-Type", "application/json");
12426
+ return send(`${this.options.url}/v1/mail${path2}`, { ...init, headers });
12427
+ };
12428
+ let response = await request(token);
12429
+ if (response.status === 401) {
12430
+ const refreshed = await client.auth.relogin();
12431
+ if (refreshed) response = await request(refreshed);
12432
+ }
12433
+ if (!response.ok) {
12434
+ const body = await response.json().catch(() => null);
12435
+ throw new ArbiApiError(
12436
+ typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12437
+ body,
12438
+ response.status
12439
+ );
12440
+ }
12441
+ return response;
12442
+ }
12443
+ /**
12444
+ * The keys that unlock the user's mail for this server session: the session's key (current after
12445
+ * a re-login) and the identity key (the login's, or the one kept on this device).
12446
+ */
12447
+ async nativeMailKeys() {
12448
+ const client = this.requireClient();
12449
+ const serverSessionKey = client.session.getState().serverSessionKey ?? this.loginResult?.serverSessionKey ?? null;
12450
+ const signingPrivateKey = this.loginResult?.signingPrivateKey ?? (await client.storage.getSession().catch(() => null))?.signingPrivateKey;
12451
+ if (!serverSessionKey || !signingPrivateKey) throw new ArbiError("Sign in to unlock your inbox");
12452
+ return { signingPrivateKey, serverSessionKey };
12453
+ }
12361
12454
  // ── Secure mail ─────────────────────────────────────────────────────
12362
12455
  /**
12363
12456
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment