@granular-software/sdk 0.4.75 → 0.4.76

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.
@@ -3993,6 +3993,34 @@ var init_wrapper = __esm({
3993
3993
  }
3994
3994
  });
3995
3995
 
3996
+ // src/api-error.ts
3997
+ var GranularApiError = class extends Error {
3998
+ constructor(message, status, path2, details = {}) {
3999
+ super(message);
4000
+ this.status = status;
4001
+ this.path = path2;
4002
+ this.name = "GranularApiError";
4003
+ this.idempotencyKey = details.idempotencyKey;
4004
+ let body = details.body;
4005
+ if (typeof body === "string") {
4006
+ try {
4007
+ body = JSON.parse(body);
4008
+ } catch {
4009
+ }
4010
+ }
4011
+ this.body = body;
4012
+ const payload = body && typeof body === "object" ? body : void 0;
4013
+ const firstError = Array.isArray(payload?.errors) ? payload.errors[0] : void 0;
4014
+ const extensions = firstError && typeof firstError === "object" ? firstError.extensions : void 0;
4015
+ this.code = [payload?.code, extensions?.code, firstError?.code].find(
4016
+ (value) => typeof value === "string" && value.length > 0
4017
+ ) ?? `HTTP_${status}`;
4018
+ }
4019
+ code;
4020
+ body;
4021
+ idempotencyKey;
4022
+ };
4023
+
3996
4024
  // ../../node_modules/zod/v3/external.js
3997
4025
  var external_exports = {};
3998
4026
  __export(external_exports, {
@@ -16916,8 +16944,12 @@ var Environment = class _Environment {
16916
16944
  }
16917
16945
  });
16918
16946
  if (!response.ok) {
16919
- throw new Error(
16920
- `Control Plane API Error (${response.status}): ${await response.text()}`
16947
+ const errorText = await response.text();
16948
+ throw new GranularApiError(
16949
+ `Control Plane API Error (${response.status}): ${errorText}`,
16950
+ response.status,
16951
+ path2,
16952
+ { body: errorText }
16921
16953
  );
16922
16954
  }
16923
16955
  return response.json();
@@ -16974,12 +17006,12 @@ var Environment = class _Environment {
16974
17006
  * );
16975
17007
  * ```
16976
17008
  */
16977
- async graphql(query, variables) {
17009
+ async graphql(query, variables, options) {
16978
17010
  const authToken = await this.resolveAuthToken();
16979
17011
  if (this._beforeMutationSend && isGraphQLMutationDocument(query)) {
16980
17012
  this._beforeMutationSend?.();
16981
17013
  }
16982
- const idempotencyKey = this._graphqlIdempotencyKey?.(query, variables);
17014
+ const idempotencyKey = options?.idempotencyKey || this._graphqlIdempotencyKey?.(query, variables) || `gr1:${Date.now()}:${crypto.randomUUID()}`;
16983
17015
  const response = await fetch(this._apiEndpoint, {
16984
17016
  method: "POST",
16985
17017
  headers: {
@@ -16990,12 +17022,18 @@ var Environment = class _Environment {
16990
17022
  body: JSON.stringify({
16991
17023
  environmentId: this.environmentId,
16992
17024
  query,
16993
- variables
17025
+ variables,
17026
+ ...options?.readPreference ? { readPreference: options.readPreference } : {}
16994
17027
  })
16995
17028
  });
16996
17029
  if (!response.ok) {
16997
17030
  const errorText = await response.text();
16998
- throw new Error(`GraphQL API Error (${response.status}): ${errorText}`);
17031
+ throw new GranularApiError(
17032
+ `GraphQL API Error (${response.status}): ${errorText}`,
17033
+ response.status,
17034
+ this._apiEndpoint,
17035
+ { body: errorText, idempotencyKey }
17036
+ );
16999
17037
  }
17000
17038
  return response.json();
17001
17039
  }
@@ -19147,7 +19185,7 @@ var Granular = class _Granular {
19147
19185
  const outdatedMatches = this.sortEnvironmentsByRecency(
19148
19186
  userEnvironments.filter((environment2) => environment2.tagId === tag.tagId)
19149
19187
  );
19150
- if (outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19188
+ if (!resetKey && outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19151
19189
  requireMatchingEnvironmentProfile(outdatedMatches[0]);
19152
19190
  const environment2 = requireMatchingEnvironmentProfile(
19153
19191
  await activate(outdatedMatches[0])
@@ -20311,6 +20349,9 @@ var Granular = class _Granular {
20311
20349
  )
20312
20350
  );
20313
20351
  },
20352
+ /** Retire a settled environment and release its named slot. Ordinary
20353
+ * access is revoked; durable graph data and history are retained.
20354
+ * Mounted or unsettled environments reject with HTTP409. */
20314
20355
  delete: async (environmentId) => {
20315
20356
  return this.request(
20316
20357
  `/control/environments/${environmentId}`,
@@ -20525,7 +20566,12 @@ var Granular = class _Granular {
20525
20566
  await sleep(LOCAL_CONTROL_REQUEST_RETRY_DELAY_MS * attempt);
20526
20567
  continue;
20527
20568
  }
20528
- throw new Error(`Granular API Error (${response.status}): ${errorText}`);
20569
+ throw new GranularApiError(
20570
+ `Granular API Error (${response.status}): ${errorText}`,
20571
+ response.status,
20572
+ path2,
20573
+ { body: errorText }
20574
+ );
20529
20575
  }
20530
20576
  throw new Error(`Granular API Error: exhausted retries for ${url}`);
20531
20577
  }