@sanity/client 7.20.0 → 7.22.0

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.
@@ -99,6 +99,7 @@ class ClientError extends Error {
99
99
  response;
100
100
  statusCode = 400;
101
101
  responseBody;
102
+ traceId;
102
103
  details;
103
104
  constructor(res, context) {
104
105
  const props = extractErrorProps(res, context);
@@ -109,6 +110,7 @@ class ServerError extends Error {
109
110
  response;
110
111
  statusCode = 500;
111
112
  responseBody;
113
+ traceId;
112
114
  details;
113
115
  constructor(res) {
114
116
  const props = extractErrorProps(res);
@@ -120,29 +122,30 @@ function extractErrorProps(res, context) {
120
122
  response: res,
121
123
  statusCode: res.statusCode,
122
124
  responseBody: stringifyBody(body, res),
125
+ traceId: extractTraceId(res),
123
126
  message: "",
124
127
  details: void 0
125
128
  };
126
129
  if (!stegaClean.isRecord(body))
127
- return props.message = httpErrorMessage(res, body), props;
130
+ return props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
128
131
  const error = body.error;
129
132
  if (typeof error == "string" && typeof body.message == "string")
130
- return props.message = `${error} - ${body.message}`, props;
133
+ return props.message = `${error} - ${body.message}${formatTraceId(props.traceId)}`, props;
131
134
  if (typeof error != "object" || error === null)
132
- return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
135
+ return typeof error == "string" ? props.message = `${error}${formatTraceId(props.traceId)}` : typeof body.message == "string" ? props.message = `${body.message}${formatTraceId(props.traceId)}` : props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
133
136
  if (isMutationError(error) || isActionError(error)) {
134
137
  const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
135
138
  let itemsStr = items.length ? `:
136
139
  - ${items.join(`
137
140
  - `)}` : "";
138
141
  return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
139
- ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
142
+ ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${formatTraceId(props.traceId)}${itemsStr}`, props.details = body.error, props;
140
143
  }
141
144
  if (isQueryParseError(error)) {
142
145
  const tag = context?.options?.query?.tag;
143
- return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
146
+ return props.message = formatQueryParseError(error, tag, props.traceId), props.details = body.error, props;
144
147
  }
145
- return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
148
+ return "description" in error && typeof error.description == "string" ? (props.message = `${error.description}${formatTraceId(props.traceId)}`, props.details = error, props) : (props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props);
146
149
  }
147
150
  function isMutationError(error) {
148
151
  return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
@@ -153,23 +156,32 @@ function isActionError(error) {
153
156
  function isQueryParseError(error) {
154
157
  return stegaClean.isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
155
158
  }
156
- function formatQueryParseError(error, tag) {
157
- const { query, start, end, description } = error;
159
+ function formatQueryParseError(error, tag, traceId) {
160
+ const { query, start, end, description } = error, withTraceId = traceId ? `
161
+ (traceId: ${traceId})` : "";
158
162
  if (!query || typeof start > "u")
159
- return `GROQ query parse error: ${description}`;
163
+ return `GROQ query parse error: ${description}${withTraceId}`;
160
164
  const withTag = tag ? `
161
165
 
162
166
  Tag: ${tag}` : "";
163
167
  return `GROQ query parse error:
164
- ${codeFrame(query, { start, end }, description)}${withTag}`;
168
+ ${codeFrame(query, { start, end }, description)}${withTag}${withTraceId}`;
165
169
  }
166
170
  function httpErrorMessage(res, body) {
167
171
  const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
168
172
  return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
169
173
  }
174
+ function extractTraceId(res) {
175
+ const traceparent = res?.headers?.traceparent;
176
+ if (traceparent)
177
+ return traceparent.split("-")[1];
178
+ }
170
179
  function stringifyBody(body, res) {
171
180
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
172
181
  }
182
+ function formatTraceId(traceId) {
183
+ return traceId ? ` (traceId: ${traceId})` : "";
184
+ }
173
185
  function sliceWithEllipsis(str, max) {
174
186
  return str.length > max ? `${str.slice(0, max)}\u2026` : str;
175
187
  }
@@ -1467,7 +1479,8 @@ class LiveClient {
1467
1479
  */
1468
1480
  events({
1469
1481
  includeDrafts = !1,
1470
- tag: _tag
1482
+ tag: _tag,
1483
+ waitFor
1471
1484
  } = {}) {
1472
1485
  const {
1473
1486
  projectId: projectId2,
@@ -1486,7 +1499,7 @@ class LiveClient {
1486
1499
  "The live events API requires a token or withCredentials when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role."
1487
1500
  );
1488
1501
  const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
1489
- tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
1502
+ tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true"), waitFor && url.searchParams.set("waitFor", waitFor);
1490
1503
  const esOptions = {};
1491
1504
  includeDrafts && withCredentials && (esOptions.withCredentials = !0), (includeDrafts && token || configHeaders) && (esOptions.headers = {}, includeDrafts && token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders));
1492
1505
  const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
@@ -2384,13 +2397,22 @@ class ObservableSanityClient {
2384
2397
  * Private properties
2385
2398
  */
2386
2399
  #clientConfig;
2400
+ #originalHttpRequest;
2387
2401
  #httpRequest;
2388
2402
  /**
2389
2403
  * Instance properties
2390
2404
  */
2391
2405
  listen = _listen;
2392
2406
  constructor(httpRequest, config = defaultConfig) {
2393
- this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2407
+ this.config(config), this.#originalHttpRequest = httpRequest;
2408
+ const requestHandler = config._requestHandler;
2409
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2410
+ let bareClient;
2411
+ return (options, requester2) => {
2412
+ const opts = options;
2413
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2414
+ };
2415
+ })() : httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2394
2416
  video: new ObservableMediaLibraryVideoClient(this, this.#httpRequest)
2395
2417
  }, this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.agent = {
2396
2418
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
@@ -2400,7 +2422,7 @@ class ObservableSanityClient {
2400
2422
  * Clone the client - returns a new instance
2401
2423
  */
2402
2424
  clone() {
2403
- return new ObservableSanityClient(this.#httpRequest, this.config());
2425
+ return new ObservableSanityClient(this.#originalHttpRequest, this.config());
2404
2426
  }
2405
2427
  config(newConfig) {
2406
2428
  if (newConfig === void 0)
@@ -2418,7 +2440,7 @@ class ObservableSanityClient {
2418
2440
  */
2419
2441
  withConfig(newConfig) {
2420
2442
  const thisConfig = this.config();
2421
- return new ObservableSanityClient(this.#httpRequest, {
2443
+ return new ObservableSanityClient(this.#originalHttpRequest, {
2422
2444
  ...thisConfig,
2423
2445
  ...newConfig,
2424
2446
  stega: {
@@ -2648,13 +2670,22 @@ class SanityClient {
2648
2670
  * Private properties
2649
2671
  */
2650
2672
  #clientConfig;
2673
+ #originalHttpRequest;
2651
2674
  #httpRequest;
2652
2675
  /**
2653
2676
  * Instance properties
2654
2677
  */
2655
2678
  listen = _listen;
2656
2679
  constructor(httpRequest, config = defaultConfig) {
2657
- this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2680
+ this.config(config), this.#originalHttpRequest = httpRequest;
2681
+ const requestHandler = config._requestHandler;
2682
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2683
+ let bareClient;
2684
+ return (options, requester2) => {
2685
+ const opts = options;
2686
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2687
+ };
2688
+ })() : httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2658
2689
  video: new MediaLibraryVideoClient(this, this.#httpRequest)
2659
2690
  }, this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.agent = {
2660
2691
  action: new AgentActionsClient(this, this.#httpRequest)
@@ -2664,7 +2695,7 @@ class SanityClient {
2664
2695
  * Clone the client - returns a new instance
2665
2696
  */
2666
2697
  clone() {
2667
- return new SanityClient(this.#httpRequest, this.config());
2698
+ return new SanityClient(this.#originalHttpRequest, this.config());
2668
2699
  }
2669
2700
  config(newConfig) {
2670
2701
  if (newConfig === void 0)
@@ -2682,7 +2713,7 @@ class SanityClient {
2682
2713
  */
2683
2714
  withConfig(newConfig) {
2684
2715
  const thisConfig = this.config();
2685
- return new SanityClient(this.#httpRequest, {
2716
+ return new SanityClient(this.#originalHttpRequest, {
2686
2717
  ...thisConfig,
2687
2718
  ...newConfig,
2688
2719
  stega: {