@arbidocs/sdk 0.5.76 → 0.5.78

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.
package/dist/browser.cjs CHANGED
@@ -4642,7 +4642,10 @@ function buildChunkLookup(tools) {
4642
4642
  // src/operations/support.ts
4643
4643
  var support_exports = {};
4644
4644
  __export(support_exports, {
4645
- createSupportTicket: () => createSupportTicket
4645
+ createSupportTicket: () => createSupportTicket,
4646
+ getSupportTicket: () => getSupportTicket,
4647
+ listSupportTickets: () => listSupportTickets,
4648
+ rateSupportTicket: () => rateSupportTicket
4646
4649
  });
4647
4650
 
4648
4651
  // src/operations/mail-encoding.ts
@@ -4671,6 +4674,29 @@ async function createSupportTicket(arbi, input) {
4671
4674
  "Could not send your support request."
4672
4675
  );
4673
4676
  }
4677
+ async function listSupportTickets(arbi) {
4678
+ return requireData(
4679
+ await arbi.fetch.GET("/v1/support/tickets"),
4680
+ "Could not load your support tickets."
4681
+ );
4682
+ }
4683
+ async function getSupportTicket(arbi, ticketId) {
4684
+ return requireData(
4685
+ await arbi.fetch.GET("/v1/support/tickets/{ticket_id}", {
4686
+ params: { path: { ticket_id: ticketId } }
4687
+ }),
4688
+ "Could not load your support ticket."
4689
+ );
4690
+ }
4691
+ async function rateSupportTicket(arbi, ticketId, rating) {
4692
+ return requireData(
4693
+ await arbi.fetch.POST("/v1/support/tickets/{ticket_id}/rating", {
4694
+ params: { path: { ticket_id: ticketId } },
4695
+ body: rating
4696
+ }),
4697
+ "Could not send your rating."
4698
+ );
4699
+ }
4674
4700
 
4675
4701
  // src/diagnostics.ts
4676
4702
  async function diagnoseSetup(options) {
@@ -9964,8 +9990,10 @@ __export(mail_exports, {
9964
9990
  listFolders: () => listFolders,
9965
9991
  listMailAccounts: () => listMailAccounts,
9966
9992
  listMessages: () => listMessages,
9993
+ openDraftEditor: () => openDraftEditor,
9967
9994
  saveDraft: () => saveDraft,
9968
9995
  sendMessage: () => sendMessage,
9996
+ unlockMailCredentials: () => unlockMailCredentials,
9969
9997
  unlockNativeMail: () => unlockNativeMail,
9970
9998
  updateMessage: () => updateMessage
9971
9999
  });
@@ -10062,6 +10090,47 @@ async function unlockNativeMail(mail, keys) {
10062
10090
  secretKey.fill(0);
10063
10091
  }
10064
10092
  }
10093
+ async function unlockMailCredentials(mail, accountId, keys) {
10094
+ await client.initSodium();
10095
+ const secretKey = client.encryptionKeypairFromSigningKey(keys.signingPrivateKey).secretKey;
10096
+ const { sealed_credentials } = await json(
10097
+ mail(`${accountPath(accountId)}/sealed-credentials`)
10098
+ );
10099
+ const bytes = client.sealedBoxDecrypt(sealed_credentials, secretKey);
10100
+ try {
10101
+ const access = await client.sealKeyForSession(bytes, keys.serverSessionKey);
10102
+ await mail(`${accountPath(accountId)}/session-credentials`, {
10103
+ method: "POST",
10104
+ body: JSON.stringify({ access })
10105
+ });
10106
+ } finally {
10107
+ bytes.fill(0);
10108
+ secretKey.fill(0);
10109
+ }
10110
+ }
10111
+ async function openDraftEditor(mail, body, keys) {
10112
+ const open = (extra = {}) => json(
10113
+ mail("/drafts/editor", { method: "POST", body: JSON.stringify({ ...body, ...extra }) })
10114
+ );
10115
+ try {
10116
+ return await open();
10117
+ } catch (error) {
10118
+ const { status, apiError } = error;
10119
+ const locked = apiError?.detail;
10120
+ if (status !== 423 || !locked?.sealed_key || !locked.room_id) throw error;
10121
+ const { signingPrivateKey, serverSessionKey } = await keys();
10122
+ await client.initSodium();
10123
+ const secretKey = client.encryptionKeypairFromSigningKey(signingPrivateKey).secretKey;
10124
+ const key = client.sealedBoxDecrypt(locked.sealed_key, secretKey);
10125
+ try {
10126
+ const sealed = await client.sealKeyForSession(key, serverSessionKey);
10127
+ return await open({ room_id: locked.room_id, key: sealed });
10128
+ } finally {
10129
+ key.fill(0);
10130
+ secretKey.fill(0);
10131
+ }
10132
+ }
10133
+ }
10065
10134
  function attachmentFromBytes(name, contentType, bytes) {
10066
10135
  return {
10067
10136
  id: "",
@@ -12330,34 +12399,84 @@ var Arbi = class {
12330
12399
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12331
12400
  */
12332
12401
  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;
12402
+ /**
12403
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12404
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12405
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12406
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12407
+ * again once.
12408
+ */
12409
+ fetch: (path2, init = {}, { nativeUnlocked = false } = {}) => this.recovering(async () => {
12410
+ const nativeRead = path2.startsWith("/accounts/native/") && (init.method ?? "GET").toUpperCase() !== "POST";
12411
+ if (nativeRead && !nativeUnlocked) await this.mail.unlockNative();
12412
+ return this.mailRequest(path2, init);
12350
12413
  }),
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
- }
12414
+ /**
12415
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12416
+ * Concurrent calls share one unlock.
12417
+ */
12418
+ unlockNative: () => this.nativeUnlock ??= this.nativeMailKeys().then((keys) => unlockNativeMail(this.mail.fetch, keys)).finally(() => {
12419
+ this.nativeUnlock = null;
12420
+ }),
12421
+ /**
12422
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12423
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12424
+ */
12425
+ unlockAccount: async (account) => {
12426
+ if (account.provider === "native") return this.mail.unlockNative();
12427
+ if (account.provider === "microsoft" || account.provider === "gmail") {
12428
+ await this.connectors.bridgeTokens({ vendor: account.provider });
12429
+ return;
12430
+ }
12431
+ await unlockMailCredentials(this.mail.fetch, account.id, await this.nativeMailKeys());
12432
+ },
12433
+ /**
12434
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12435
+ * user's key when the server asks (see `openDraftEditor`).
12436
+ */
12437
+ openDraftEditor: (body) => openDraftEditor(this.mail.fetch, body, () => this.nativeMailKeys())
12360
12438
  };
12439
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
12440
+ nativeUnlock = null;
12441
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
12442
+ async mailRequest(path2, init) {
12443
+ const client = this.requireClient();
12444
+ let token = client.session.getState().accessToken;
12445
+ if (!token) token = await client.auth.relogin();
12446
+ if (!token) throw new ArbiError("Sign in again to restore your session");
12447
+ const send = client.http?.fetch ?? globalThis.fetch;
12448
+ const request = (bearer) => {
12449
+ const headers = new Headers(init.headers);
12450
+ headers.set("Authorization", `Bearer ${bearer}`);
12451
+ if (init.body && !headers.has("Content-Type")) headers.set("Content-Type", "application/json");
12452
+ return send(`${this.options.url}/v1/mail${path2}`, { ...init, headers });
12453
+ };
12454
+ let response = await request(token);
12455
+ if (response.status === 401) {
12456
+ const refreshed = await client.auth.relogin();
12457
+ if (refreshed) response = await request(refreshed);
12458
+ }
12459
+ if (!response.ok) {
12460
+ const body = await response.json().catch(() => null);
12461
+ throw new ArbiApiError(
12462
+ typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12463
+ body,
12464
+ response.status
12465
+ );
12466
+ }
12467
+ return response;
12468
+ }
12469
+ /**
12470
+ * The keys that unlock the user's mail for this server session: the session's key (current after
12471
+ * a re-login) and the identity key (the login's, or the one kept on this device).
12472
+ */
12473
+ async nativeMailKeys() {
12474
+ const client = this.requireClient();
12475
+ const serverSessionKey = client.session.getState().serverSessionKey ?? this.loginResult?.serverSessionKey ?? null;
12476
+ const signingPrivateKey = this.loginResult?.signingPrivateKey ?? (await client.storage.getSession().catch(() => null))?.signingPrivateKey;
12477
+ if (!serverSessionKey || !signingPrivateKey) throw new ArbiError("Sign in to unlock your inbox");
12478
+ return { signingPrivateKey, serverSessionKey };
12479
+ }
12361
12480
  // ── Secure mail ─────────────────────────────────────────────────────
12362
12481
  /**
12363
12482
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
@@ -13293,6 +13412,9 @@ var Arbi = class {
13293
13412
  };
13294
13413
  // ── Health ────────────────────────────────────────────────────────────
13295
13414
  support = {
13415
+ list: () => this.recovering(() => listSupportTickets(this.requireClient())),
13416
+ get: (ticketId) => this.recovering(() => getSupportTicket(this.requireClient(), ticketId)),
13417
+ rate: (ticketId, rating) => this.recovering(() => rateSupportTicket(this.requireClient(), ticketId, rating)),
13296
13418
  create: (input) => {
13297
13419
  const request = { ...input, idempotencyKey: input.idempotencyKey ?? crypto.randomUUID() };
13298
13420
  return this.recovering(() => createSupportTicket(this.requireClient(), request));