@granular-software/sdk 0.4.52 → 0.4.53

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.mjs CHANGED
@@ -16425,6 +16425,55 @@ var Granular = class _Granular {
16425
16425
  throw error;
16426
16426
  }
16427
16427
  }
16428
+ /**
16429
+ * Resolve one exact environment only when Granular confirms that it belongs
16430
+ * to the given external user in the requested sandbox (and, optionally,
16431
+ * tag). This is deliberately read-only: callers use it to keep an already
16432
+ * opened delegated workspace stable while a newer active environment is
16433
+ * being prepared in the background.
16434
+ */
16435
+ async getEnvironmentForUser(options) {
16436
+ const sandboxId = options.sandboxId.trim();
16437
+ const userId = options.userId.trim();
16438
+ const environmentId = options.environmentId.trim();
16439
+ const tagName = options.tag?.trim();
16440
+ if (!sandboxId || !userId || !environmentId) {
16441
+ throw new Error(
16442
+ "getEnvironmentForUser() requires sandboxId, userId, and environmentId."
16443
+ );
16444
+ }
16445
+ const subjects = await this.request(
16446
+ `/control/subjects?identityId=${encodeURIComponent(userId)}`
16447
+ );
16448
+ const subject = (subjects.items || []).find(
16449
+ (item) => item.identityId === userId || item.userId === userId
16450
+ );
16451
+ const subjectId = subject?.subjectId || subject?.granularId;
16452
+ if (!subjectId) return null;
16453
+ let environment;
16454
+ try {
16455
+ environment = await this.environments.get(environmentId);
16456
+ } catch (error) {
16457
+ const message = error instanceof Error ? error.message : String(error);
16458
+ if (message.includes("404") || message.includes("not found")) {
16459
+ return null;
16460
+ }
16461
+ throw error;
16462
+ }
16463
+ if (environment.sandboxId !== sandboxId || environment.subjectId !== subjectId) {
16464
+ return null;
16465
+ }
16466
+ if (tagName) {
16467
+ const tags = await this.request(`/control/sandboxes/${encodeURIComponent(sandboxId)}/tags`);
16468
+ const tag = (tags.items || []).find(
16469
+ (candidate) => candidate?.name === tagName
16470
+ );
16471
+ if (!tag || environment.tagId !== tag.tagId) {
16472
+ return null;
16473
+ }
16474
+ }
16475
+ return this.bindEnvironmentHandle(environment);
16476
+ }
16428
16477
  /**
16429
16478
  * Register one reviewed, pre-existing environment as the active workspace
16430
16479
  * for an external user. This is for a controlled migration only: it does