@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.
package/dist/index.cjs CHANGED
@@ -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 (!isRecord.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 isRecord.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
  }
@@ -1283,7 +1295,8 @@ class LiveClient {
1283
1295
  */
1284
1296
  events({
1285
1297
  includeDrafts = !1,
1286
- tag: _tag
1298
+ tag: _tag,
1299
+ waitFor
1287
1300
  } = {}) {
1288
1301
  const {
1289
1302
  projectId,
@@ -1302,7 +1315,7 @@ class LiveClient {
1302
1315
  "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."
1303
1316
  );
1304
1317
  const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
1305
- tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
1318
+ tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true"), waitFor && url.searchParams.set("waitFor", waitFor);
1306
1319
  const esOptions = {};
1307
1320
  includeDrafts && withCredentials && (esOptions.withCredentials = !0), (includeDrafts && token || configHeaders) && (esOptions.headers = {}, includeDrafts && token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders));
1308
1321
  const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
@@ -2200,13 +2213,22 @@ class ObservableSanityClient {
2200
2213
  * Private properties
2201
2214
  */
2202
2215
  #clientConfig;
2216
+ #originalHttpRequest;
2203
2217
  #httpRequest;
2204
2218
  /**
2205
2219
  * Instance properties
2206
2220
  */
2207
2221
  listen = _listen;
2208
2222
  constructor(httpRequest, config$1 = config.defaultConfig) {
2209
- this.config(config$1), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2223
+ this.config(config$1), this.#originalHttpRequest = httpRequest;
2224
+ const requestHandler = config$1._requestHandler;
2225
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2226
+ let bareClient;
2227
+ return (options, requester2) => {
2228
+ const opts = options;
2229
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config$1, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2230
+ };
2231
+ })() : httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2210
2232
  video: new ObservableMediaLibraryVideoClient(this, this.#httpRequest)
2211
2233
  }, this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.agent = {
2212
2234
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
@@ -2216,7 +2238,7 @@ class ObservableSanityClient {
2216
2238
  * Clone the client - returns a new instance
2217
2239
  */
2218
2240
  clone() {
2219
- return new ObservableSanityClient(this.#httpRequest, this.config());
2241
+ return new ObservableSanityClient(this.#originalHttpRequest, this.config());
2220
2242
  }
2221
2243
  config(newConfig) {
2222
2244
  if (newConfig === void 0)
@@ -2234,7 +2256,7 @@ class ObservableSanityClient {
2234
2256
  */
2235
2257
  withConfig(newConfig) {
2236
2258
  const thisConfig = this.config();
2237
- return new ObservableSanityClient(this.#httpRequest, {
2259
+ return new ObservableSanityClient(this.#originalHttpRequest, {
2238
2260
  ...thisConfig,
2239
2261
  ...newConfig,
2240
2262
  stega: {
@@ -2464,13 +2486,22 @@ class SanityClient {
2464
2486
  * Private properties
2465
2487
  */
2466
2488
  #clientConfig;
2489
+ #originalHttpRequest;
2467
2490
  #httpRequest;
2468
2491
  /**
2469
2492
  * Instance properties
2470
2493
  */
2471
2494
  listen = _listen;
2472
2495
  constructor(httpRequest, config$1 = config.defaultConfig) {
2473
- this.config(config$1), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2496
+ this.config(config$1), this.#originalHttpRequest = httpRequest;
2497
+ const requestHandler = config$1._requestHandler;
2498
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2499
+ let bareClient;
2500
+ return (options, requester2) => {
2501
+ const opts = options;
2502
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config$1, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2503
+ };
2504
+ })() : httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2474
2505
  video: new MediaLibraryVideoClient(this, this.#httpRequest)
2475
2506
  }, this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.agent = {
2476
2507
  action: new AgentActionsClient(this, this.#httpRequest)
@@ -2480,7 +2511,7 @@ class SanityClient {
2480
2511
  * Clone the client - returns a new instance
2481
2512
  */
2482
2513
  clone() {
2483
- return new SanityClient(this.#httpRequest, this.config());
2514
+ return new SanityClient(this.#originalHttpRequest, this.config());
2484
2515
  }
2485
2516
  config(newConfig) {
2486
2517
  if (newConfig === void 0)
@@ -2498,7 +2529,7 @@ class SanityClient {
2498
2529
  */
2499
2530
  withConfig(newConfig) {
2500
2531
  const thisConfig = this.config();
2501
- return new SanityClient(this.#httpRequest, {
2532
+ return new SanityClient(this.#originalHttpRequest, {
2502
2533
  ...thisConfig,
2503
2534
  ...newConfig,
2504
2535
  stega: {
@@ -2769,7 +2800,7 @@ function defineDeprecatedCreateClient(createClient2) {
2769
2800
  return config.printNoDefaultExport(), createClient2(config$1);
2770
2801
  };
2771
2802
  }
2772
- var name = "@sanity/client", version = "7.20.0";
2803
+ var name = "@sanity/client", version = "7.22.0";
2773
2804
  const middleware = [
2774
2805
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
2775
2806
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),