@granular-software/sdk 0.4.6 → 0.4.7

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
@@ -4481,6 +4481,8 @@ var Session = class {
4481
4481
  environmentId: "",
4482
4482
  sessionId: "",
4483
4483
  user: {
4484
+ granularId: "",
4485
+ userId: "",
4484
4486
  subjectId: ""
4485
4487
  }
4486
4488
  };
@@ -5175,6 +5177,29 @@ function normalizeHeapSnapshot(raw) {
5175
5177
  updatedAt: typeof heap.updatedAt === "number" ? heap.updatedAt : Date.now()
5176
5178
  };
5177
5179
  }
5180
+ function normalizeSubject(subject) {
5181
+ const granularId = subject.granularId || subject.subjectId;
5182
+ const userId = subject.userId || subject.identityId || granularId;
5183
+ return {
5184
+ ...subject,
5185
+ granularId,
5186
+ userId,
5187
+ subjectId: granularId,
5188
+ identityId: subject.identityId || userId
5189
+ };
5190
+ }
5191
+ function normalizeUser(user) {
5192
+ const granularId = user.granularId || user.subjectId;
5193
+ const userId = user.userId || user.identityId || granularId;
5194
+ return {
5195
+ ...user,
5196
+ granularId,
5197
+ userId,
5198
+ subjectId: granularId,
5199
+ identityId: user.identityId || userId,
5200
+ permissions: Array.isArray(user.permissions) ? user.permissions : []
5201
+ };
5202
+ }
5178
5203
  var Environment = class _Environment extends Session {
5179
5204
  envData;
5180
5205
  _apiKey;
@@ -5197,6 +5222,10 @@ var Environment = class _Environment extends Session {
5197
5222
  get subjectId() {
5198
5223
  return this.envData.subjectId;
5199
5224
  }
5225
+ /** Internal Granular user identifier for this environment */
5226
+ get granularId() {
5227
+ return this.envData.subjectId;
5228
+ }
5200
5229
  /** The permission profile ID */
5201
5230
  get permissionProfileId() {
5202
5231
  return this.envData.permissionProfileId;
@@ -5994,7 +6023,7 @@ var Granular = class {
5994
6023
  * Records/upserts a user and prepares them for sandbox connections
5995
6024
  *
5996
6025
  * @param options - User options
5997
- * @returns The user object to pass to connect()
6026
+ * @returns The recorded user with both `userId` and `granularId`
5998
6027
  *
5999
6028
  * @example
6000
6029
  * ```typescript
@@ -6006,21 +6035,61 @@ var Granular = class {
6006
6035
  * ```
6007
6036
  */
6008
6037
  async recordUser(options) {
6009
- const subject = await this.request("/control/subjects", {
6038
+ const subject = normalizeSubject(await this.request("/control/subjects", {
6010
6039
  method: "POST",
6011
6040
  body: JSON.stringify({
6012
6041
  identityId: options.userId,
6013
6042
  name: options.name,
6014
6043
  email: options.email
6015
6044
  })
6016
- });
6017
- return {
6045
+ }));
6046
+ return normalizeUser({
6047
+ granularId: subject.granularId,
6048
+ userId: options.userId,
6018
6049
  subjectId: subject.subjectId,
6019
- identityId: options.userId,
6020
- name: options.name,
6021
- email: options.email,
6050
+ identityId: subject.identityId,
6051
+ name: options.name || subject.name || void 0,
6052
+ email: options.email || subject.email || void 0,
6022
6053
  permissions: options.permissions || []
6023
- };
6054
+ });
6055
+ }
6056
+ async resolveConnectUser(options) {
6057
+ if (options.user) {
6058
+ const user = normalizeUser(options.user);
6059
+ return {
6060
+ granularId: user.granularId,
6061
+ userId: user.userId,
6062
+ name: options.name || user.name,
6063
+ email: options.email || user.email,
6064
+ permissions: options.permissions || user.permissions
6065
+ };
6066
+ }
6067
+ if (options.userId) {
6068
+ const user = await this.recordUser({
6069
+ userId: options.userId,
6070
+ name: options.name,
6071
+ email: options.email,
6072
+ permissions: options.permissions
6073
+ });
6074
+ return {
6075
+ granularId: user.granularId,
6076
+ userId: user.userId,
6077
+ name: user.name,
6078
+ email: user.email,
6079
+ permissions: options.permissions || user.permissions
6080
+ };
6081
+ }
6082
+ if (options.granularId) {
6083
+ const subject = await this.subjects.get(options.granularId);
6084
+ return {
6085
+ granularId: subject.granularId,
6086
+ userId: subject.userId,
6087
+ name: options.name || subject.name || void 0,
6088
+ email: options.email || subject.email || void 0,
6089
+ permissions: options.permissions || []
6090
+ };
6091
+ }
6092
+ throw new Error("connect() requires either userId, granularId, or a user object returned by recordUser().");
6024
6093
  }
6025
6094
  /**
6026
6095
  * Connect to a sandbox and establish a real-time environment session.
@@ -6034,14 +6103,10 @@ var Granular = class {
6034
6103
  *
6035
6104
  * @example
6036
6105
  * ```typescript
6037
- * const user = await granular.recordUser({
6038
- * userId: 'user_123',
6039
- * permissions: ['agent'],
6040
- * });
6041
- *
6042
6106
  * const environment = await granular.connect({
6043
6107
  * sandbox: 'my-sandbox',
6044
- * user,
6108
+ * userId: 'user_123',
6109
+ * permissions: ['agent'],
6045
6110
  * });
6046
6111
  *
6047
6112
  * await granular.registerEffect('my-sandbox', {
@@ -6062,13 +6127,14 @@ var Granular = class {
6062
6127
  */
6063
6128
  async connect(options) {
6064
6129
  const clientId = options.clientId || `client_${Date.now()}`;
6130
+ const user = await this.resolveConnectUser(options);
6065
6131
  const sandbox = await this.findOrCreateSandbox(options.sandbox);
6066
- for (const profileName of options.user.permissions) {
6132
+ for (const profileName of user.permissions) {
6067
6133
  const profileId = await this.ensurePermissionProfile(sandbox.sandboxId, profileName);
6068
- await this.ensureAssignment(options.user.subjectId, sandbox.sandboxId, profileId);
6134
+ await this.ensureAssignment(user.granularId, sandbox.sandboxId, profileId);
6069
6135
  }
6070
6136
  const envData = await this.environments.create(sandbox.sandboxId, {
6071
- subjectId: options.user.subjectId,
6137
+ subjectId: user.granularId,
6072
6138
  permissionProfileId: null
6073
6139
  });
6074
6140
  await this.activateEnvironment(envData.environmentId);
@@ -6501,7 +6567,7 @@ var Granular = class {
6501
6567
  get subjects() {
6502
6568
  return {
6503
6569
  get: async (subjectId) => {
6504
- return this.request(`/control/subjects/${subjectId}`);
6570
+ return normalizeSubject(await this.request(`/control/subjects/${subjectId}`));
6505
6571
  },
6506
6572
  listAssignments: async (subjectId) => {
6507
6573
  return this.request(`/control/subjects/${subjectId}/assignments`);
@@ -6514,17 +6580,17 @@ var Granular = class {
6514
6580
  get users() {
6515
6581
  return {
6516
6582
  create: async (data) => {
6517
- return this.request("/control/subjects", {
6583
+ return normalizeSubject(await this.request("/control/subjects", {
6518
6584
  method: "POST",
6519
6585
  body: JSON.stringify({
6520
6586
  identityId: data.id,
6521
6587
  name: data.name,
6522
6588
  email: data.email
6523
6589
  })
6524
- });
6590
+ }));
6525
6591
  },
6526
6592
  get: async (id) => {
6527
- return this.request(`/control/subjects/${id}`);
6593
+ return normalizeSubject(await this.request(`/control/subjects/${id}`));
6528
6594
  }
6529
6595
  };
6530
6596
  }