@deeplake/hivemind 0.7.75 → 0.7.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.
@@ -112,6 +112,19 @@ function decodeJwtPayload(token) {
112
112
  return null;
113
113
  }
114
114
  }
115
+ async function apiGet(path, token, apiUrl, orgId) {
116
+ const headers = {
117
+ Authorization: `Bearer ${token}`,
118
+ "Content-Type": "application/json",
119
+ ...deeplakeClientHeader()
120
+ };
121
+ if (orgId)
122
+ headers["X-Activeloop-Org-Id"] = orgId;
123
+ const resp = await fetch(`${apiUrl}${path}`, { headers });
124
+ if (!resp.ok)
125
+ throw new Error(`API ${resp.status}: ${await resp.text().catch(() => "")}`);
126
+ return resp.json();
127
+ }
115
128
  async function apiPost(path, body, token, apiUrl, orgId) {
116
129
  const headers = {
117
130
  Authorization: `Bearer ${token}`,
@@ -125,6 +138,10 @@ async function apiPost(path, body, token, apiUrl, orgId) {
125
138
  throw new Error(`API ${resp.status}: ${await resp.text().catch(() => "")}`);
126
139
  return resp.json();
127
140
  }
141
+ async function listOrgs(token, apiUrl = DEFAULT_API_URL) {
142
+ const data = await apiGet("/organizations", token, apiUrl);
143
+ return Array.isArray(data) ? data : [];
144
+ }
128
145
  async function healDriftedOrgToken(creds, log6 = () => {
129
146
  }) {
130
147
  if (!creds.token || !creds.orgId)
@@ -143,6 +160,33 @@ async function healDriftedOrgToken(creds, log6 = () => {
143
160
  organization_id: creds.orgId
144
161
  }, creds.token, apiUrl);
145
162
  const healed = { ...creds, token: tokenData.token.token };
163
+ try {
164
+ const orgs = await listOrgs(healed.token, apiUrl);
165
+ const matchedOrg = orgs.find((o) => o.id === creds.orgId);
166
+ if (matchedOrg && matchedOrg.name !== creds.orgName) {
167
+ log6(`orgName realigned: ${creds.orgName ?? "(unset)"} -> ${matchedOrg.name}`);
168
+ healed.orgName = matchedOrg.name;
169
+ }
170
+ } catch (e) {
171
+ log6(`orgName realign skipped: ${e.message}`);
172
+ }
173
+ const currentWs = creds.workspaceId ?? "default";
174
+ if (currentWs !== "default") {
175
+ try {
176
+ const wsList = await listWorkspaces(healed.token, apiUrl, creds.orgId);
177
+ const lcWs = currentWs.toLowerCase();
178
+ const wsMatch = wsList.find((w) => w.id === currentWs || w.name && w.name.toLowerCase() === lcWs);
179
+ if (!wsMatch) {
180
+ log6(`workspace '${currentWs}' not in org ${creds.orgId} \u2014 reset to default`);
181
+ healed.workspaceId = "default";
182
+ } else if (wsMatch.id !== currentWs) {
183
+ log6(`workspace '${currentWs}' resolved to id '${wsMatch.id}'`);
184
+ healed.workspaceId = wsMatch.id;
185
+ }
186
+ } catch (e) {
187
+ log6(`workspace realign skipped: ${e.message}`);
188
+ }
189
+ }
146
190
  saveCredentials(healed);
147
191
  log6(`token re-minted for org=${creds.orgId}`);
148
192
  return healed;
@@ -151,6 +195,11 @@ async function healDriftedOrgToken(creds, log6 = () => {
151
195
  return creds;
152
196
  }
153
197
  }
198
+ async function listWorkspaces(token, apiUrl = DEFAULT_API_URL, orgId) {
199
+ const raw = await apiGet("/workspaces", token, apiUrl, orgId);
200
+ const data = raw.data ?? raw;
201
+ return Array.isArray(data) ? data : [];
202
+ }
154
203
 
155
204
  // dist/src/utils/stdin.js
156
205
  function readStdin() {