@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/index.cjs CHANGED
@@ -5184,7 +5184,10 @@ function buildChunkLookup(tools) {
5184
5184
  // src/operations/support.ts
5185
5185
  var support_exports = {};
5186
5186
  __export(support_exports, {
5187
- createSupportTicket: () => createSupportTicket
5187
+ createSupportTicket: () => createSupportTicket,
5188
+ getSupportTicket: () => getSupportTicket,
5189
+ listSupportTickets: () => listSupportTickets,
5190
+ rateSupportTicket: () => rateSupportTicket
5188
5191
  });
5189
5192
 
5190
5193
  // src/operations/mail-encoding.ts
@@ -5213,6 +5216,29 @@ async function createSupportTicket(arbi, input) {
5213
5216
  "Could not send your support request."
5214
5217
  );
5215
5218
  }
5219
+ async function listSupportTickets(arbi) {
5220
+ return requireData(
5221
+ await arbi.fetch.GET("/v1/support/tickets"),
5222
+ "Could not load your support tickets."
5223
+ );
5224
+ }
5225
+ async function getSupportTicket(arbi, ticketId) {
5226
+ return requireData(
5227
+ await arbi.fetch.GET("/v1/support/tickets/{ticket_id}", {
5228
+ params: { path: { ticket_id: ticketId } }
5229
+ }),
5230
+ "Could not load your support ticket."
5231
+ );
5232
+ }
5233
+ async function rateSupportTicket(arbi, ticketId, rating) {
5234
+ return requireData(
5235
+ await arbi.fetch.POST("/v1/support/tickets/{ticket_id}/rating", {
5236
+ params: { path: { ticket_id: ticketId } },
5237
+ body: rating
5238
+ }),
5239
+ "Could not send your rating."
5240
+ );
5241
+ }
5216
5242
 
5217
5243
  // src/diagnostics.ts
5218
5244
  async function diagnoseSetup(options) {
@@ -10550,8 +10576,10 @@ __export(mail_exports, {
10550
10576
  listFolders: () => listFolders,
10551
10577
  listMailAccounts: () => listMailAccounts,
10552
10578
  listMessages: () => listMessages,
10579
+ openDraftEditor: () => openDraftEditor,
10553
10580
  saveDraft: () => saveDraft,
10554
10581
  sendMessage: () => sendMessage,
10582
+ unlockMailCredentials: () => unlockMailCredentials,
10555
10583
  unlockNativeMail: () => unlockNativeMail,
10556
10584
  updateMessage: () => updateMessage
10557
10585
  });
@@ -10648,6 +10676,47 @@ async function unlockNativeMail(mail, keys) {
10648
10676
  secretKey.fill(0);
10649
10677
  }
10650
10678
  }
10679
+ async function unlockMailCredentials(mail, accountId, keys) {
10680
+ await client.initSodium();
10681
+ const secretKey = client.encryptionKeypairFromSigningKey(keys.signingPrivateKey).secretKey;
10682
+ const { sealed_credentials } = await json(
10683
+ mail(`${accountPath(accountId)}/sealed-credentials`)
10684
+ );
10685
+ const bytes = client.sealedBoxDecrypt(sealed_credentials, secretKey);
10686
+ try {
10687
+ const access = await client.sealKeyForSession(bytes, keys.serverSessionKey);
10688
+ await mail(`${accountPath(accountId)}/session-credentials`, {
10689
+ method: "POST",
10690
+ body: JSON.stringify({ access })
10691
+ });
10692
+ } finally {
10693
+ bytes.fill(0);
10694
+ secretKey.fill(0);
10695
+ }
10696
+ }
10697
+ async function openDraftEditor(mail, body, keys) {
10698
+ const open = (extra = {}) => json(
10699
+ mail("/drafts/editor", { method: "POST", body: JSON.stringify({ ...body, ...extra }) })
10700
+ );
10701
+ try {
10702
+ return await open();
10703
+ } catch (error) {
10704
+ const { status, apiError } = error;
10705
+ const locked = apiError?.detail;
10706
+ if (status !== 423 || !locked?.sealed_key || !locked.room_id) throw error;
10707
+ const { signingPrivateKey, serverSessionKey } = await keys();
10708
+ await client.initSodium();
10709
+ const secretKey = client.encryptionKeypairFromSigningKey(signingPrivateKey).secretKey;
10710
+ const key = client.sealedBoxDecrypt(locked.sealed_key, secretKey);
10711
+ try {
10712
+ const sealed = await client.sealKeyForSession(key, serverSessionKey);
10713
+ return await open({ room_id: locked.room_id, key: sealed });
10714
+ } finally {
10715
+ key.fill(0);
10716
+ secretKey.fill(0);
10717
+ }
10718
+ }
10719
+ }
10651
10720
  function attachmentFromBytes(name, contentType, bytes) {
10652
10721
  return {
10653
10722
  id: "",
@@ -12916,34 +12985,84 @@ var Arbi = class {
12916
12985
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12917
12986
  */
12918
12987
  mail = {
12919
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12920
- fetch: (path5, init = {}) => this.recovering(async () => {
12921
- const { baseUrl, accessToken, fetch: send = globalThis.fetch } = this.getAuthHeaders();
12922
- const headers = new Headers(init.headers);
12923
- headers.set("Authorization", `Bearer ${accessToken}`);
12924
- if (init.body && !headers.has("Content-Type"))
12925
- headers.set("Content-Type", "application/json");
12926
- const response = await send(`${baseUrl}/v1/mail${path5}`, { ...init, headers });
12927
- if (!response.ok) {
12928
- const body = await response.json().catch(() => null);
12929
- throw new ArbiApiError(
12930
- typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12931
- body,
12932
- response.status
12933
- );
12934
- }
12935
- return response;
12988
+ /**
12989
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12990
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12991
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12992
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12993
+ * again once.
12994
+ */
12995
+ fetch: (path5, init = {}, { nativeUnlocked = false } = {}) => this.recovering(async () => {
12996
+ const nativeRead = path5.startsWith("/accounts/native/") && (init.method ?? "GET").toUpperCase() !== "POST";
12997
+ if (nativeRead && !nativeUnlocked) await this.mail.unlockNative();
12998
+ return this.mailRequest(path5, init);
12936
12999
  }),
12937
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12938
- unlockNative: () => {
12939
- const login = this.loginResult;
12940
- if (!login) return Promise.reject(new ArbiError("Sign in to unlock your inbox"));
12941
- return unlockNativeMail(this.mail.fetch, {
12942
- signingPrivateKey: login.signingPrivateKey,
12943
- serverSessionKey: login.serverSessionKey
12944
- });
12945
- }
13000
+ /**
13001
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
13002
+ * Concurrent calls share one unlock.
13003
+ */
13004
+ unlockNative: () => this.nativeUnlock ??= this.nativeMailKeys().then((keys) => unlockNativeMail(this.mail.fetch, keys)).finally(() => {
13005
+ this.nativeUnlock = null;
13006
+ }),
13007
+ /**
13008
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
13009
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
13010
+ */
13011
+ unlockAccount: async (account) => {
13012
+ if (account.provider === "native") return this.mail.unlockNative();
13013
+ if (account.provider === "microsoft" || account.provider === "gmail") {
13014
+ await this.connectors.bridgeTokens({ vendor: account.provider });
13015
+ return;
13016
+ }
13017
+ await unlockMailCredentials(this.mail.fetch, account.id, await this.nativeMailKeys());
13018
+ },
13019
+ /**
13020
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
13021
+ * user's key when the server asks (see `openDraftEditor`).
13022
+ */
13023
+ openDraftEditor: (body) => openDraftEditor(this.mail.fetch, body, () => this.nativeMailKeys())
12946
13024
  };
13025
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
13026
+ nativeUnlock = null;
13027
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
13028
+ async mailRequest(path5, init) {
13029
+ const client = this.requireClient();
13030
+ let token = client.session.getState().accessToken;
13031
+ if (!token) token = await client.auth.relogin();
13032
+ if (!token) throw new ArbiError("Sign in again to restore your session");
13033
+ const send = client.http?.fetch ?? globalThis.fetch;
13034
+ const request = (bearer) => {
13035
+ const headers = new Headers(init.headers);
13036
+ headers.set("Authorization", `Bearer ${bearer}`);
13037
+ if (init.body && !headers.has("Content-Type")) headers.set("Content-Type", "application/json");
13038
+ return send(`${this.options.url}/v1/mail${path5}`, { ...init, headers });
13039
+ };
13040
+ let response = await request(token);
13041
+ if (response.status === 401) {
13042
+ const refreshed = await client.auth.relogin();
13043
+ if (refreshed) response = await request(refreshed);
13044
+ }
13045
+ if (!response.ok) {
13046
+ const body = await response.json().catch(() => null);
13047
+ throw new ArbiApiError(
13048
+ typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
13049
+ body,
13050
+ response.status
13051
+ );
13052
+ }
13053
+ return response;
13054
+ }
13055
+ /**
13056
+ * The keys that unlock the user's mail for this server session: the session's key (current after
13057
+ * a re-login) and the identity key (the login's, or the one kept on this device).
13058
+ */
13059
+ async nativeMailKeys() {
13060
+ const client = this.requireClient();
13061
+ const serverSessionKey = client.session.getState().serverSessionKey ?? this.loginResult?.serverSessionKey ?? null;
13062
+ const signingPrivateKey = this.loginResult?.signingPrivateKey ?? (await client.storage.getSession().catch(() => null))?.signingPrivateKey;
13063
+ if (!serverSessionKey || !signingPrivateKey) throw new ArbiError("Sign in to unlock your inbox");
13064
+ return { signingPrivateKey, serverSessionKey };
13065
+ }
12947
13066
  // ── Secure mail ─────────────────────────────────────────────────────
12948
13067
  /**
12949
13068
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
@@ -13879,6 +13998,9 @@ var Arbi = class {
13879
13998
  };
13880
13999
  // ── Health ────────────────────────────────────────────────────────────
13881
14000
  support = {
14001
+ list: () => this.recovering(() => listSupportTickets(this.requireClient())),
14002
+ get: (ticketId) => this.recovering(() => getSupportTicket(this.requireClient(), ticketId)),
14003
+ rate: (ticketId, rating) => this.recovering(() => rateSupportTicket(this.requireClient(), ticketId, rating)),
13882
14004
  create: (input) => {
13883
14005
  const request = { ...input, idempotencyKey: input.idempotencyKey ?? crypto.randomUUID() };
13884
14006
  return this.recovering(() => createSupportTicket(this.requireClient(), request));