@sanity/client 7.21.0 → 7.22.1

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.
@@ -2059,6 +2059,7 @@
2059
2059
  response;
2060
2060
  statusCode = 400;
2061
2061
  responseBody;
2062
+ traceId;
2062
2063
  details;
2063
2064
  constructor(res, context) {
2064
2065
  const props = extractErrorProps(res, context);
@@ -2069,6 +2070,7 @@
2069
2070
  response;
2070
2071
  statusCode = 500;
2071
2072
  responseBody;
2073
+ traceId;
2072
2074
  details;
2073
2075
  constructor(res) {
2074
2076
  const props = extractErrorProps(res);
@@ -2080,29 +2082,30 @@
2080
2082
  response: res,
2081
2083
  statusCode: res.statusCode,
2082
2084
  responseBody: stringifyBody(body, res),
2085
+ traceId: extractTraceId(res),
2083
2086
  message: "",
2084
2087
  details: void 0
2085
2088
  };
2086
2089
  if (!isRecord(body))
2087
- return props.message = httpErrorMessage(res, body), props;
2090
+ return props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
2088
2091
  const error = body.error;
2089
2092
  if (typeof error == "string" && typeof body.message == "string")
2090
- return props.message = `${error} - ${body.message}`, props;
2093
+ return props.message = `${error} - ${body.message}${formatTraceId(props.traceId)}`, props;
2091
2094
  if (typeof error != "object" || error === null)
2092
- return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
2095
+ 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;
2093
2096
  if (isMutationError(error) || isActionError(error)) {
2094
2097
  const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
2095
2098
  let itemsStr = items.length ? `:
2096
2099
  - ${items.join(`
2097
2100
  - `)}` : "";
2098
2101
  return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
2099
- ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
2102
+ ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${formatTraceId(props.traceId)}${itemsStr}`, props.details = body.error, props;
2100
2103
  }
2101
2104
  if (isQueryParseError(error)) {
2102
2105
  const tag = context?.options?.query?.tag;
2103
- return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
2106
+ return props.message = formatQueryParseError(error, tag, props.traceId), props.details = body.error, props;
2104
2107
  }
2105
- return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
2108
+ 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);
2106
2109
  }
2107
2110
  function isMutationError(error) {
2108
2111
  return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
@@ -2113,37 +2116,43 @@
2113
2116
  function isQueryParseError(error) {
2114
2117
  return isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
2115
2118
  }
2116
- function formatQueryParseError(error, tag) {
2117
- const { query, start, end, description } = error;
2119
+ function formatQueryParseError(error, tag, traceId) {
2120
+ const { query, start, end, description } = error, withTraceId = traceId ? `
2121
+ (traceId: ${traceId})` : "";
2118
2122
  if (!query || typeof start > "u")
2119
- return `GROQ query parse error: ${description}`;
2123
+ return `GROQ query parse error: ${description}${withTraceId}`;
2120
2124
  const withTag = tag ? `
2121
2125
 
2122
2126
  Tag: ${tag}` : "";
2123
2127
  return `GROQ query parse error:
2124
- ${codeFrame(query, { start, end }, description)}${withTag}`;
2128
+ ${codeFrame(query, { start, end }, description)}${withTag}${withTraceId}`;
2125
2129
  }
2126
2130
  function httpErrorMessage(res, body) {
2127
2131
  const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
2128
2132
  return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
2129
2133
  }
2134
+ function extractTraceId(res) {
2135
+ const traceparent = res?.headers?.traceparent;
2136
+ if (traceparent)
2137
+ return traceparent.split("-")[1];
2138
+ }
2130
2139
  function stringifyBody(body, res) {
2131
2140
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
2132
2141
  }
2142
+ function formatTraceId(traceId) {
2143
+ return traceId ? ` (traceId: ${traceId})` : "";
2144
+ }
2133
2145
  function sliceWithEllipsis(str, max) {
2134
2146
  return str.length > max ? `${str.slice(0, max)}\u2026` : str;
2135
2147
  }
2136
2148
  class CorsOriginError extends Error {
2137
2149
  projectId;
2138
2150
  addOriginUrl;
2139
- constructor({ projectId: projectId2 }) {
2140
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
2141
- const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
2142
- if (typeof location < "u") {
2143
- const { origin } = location;
2144
- url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), this.addOriginUrl = url, this.message = `The current origin is not allowed to connect to the Live Content API. Add it here: ${url}`;
2145
- } else
2146
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
2151
+ constructor({ projectId: projectId2, credentials } = {}) {
2152
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2, projectId2 && typeof location < "u") {
2153
+ const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`), { origin } = location;
2154
+ url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), credentials && url.searchParams.set("credentials", ""), this.addOriginUrl = url, this.message = `The current origin is not allowed to connect to the Live Content API. Add it here: ${url}`;
2155
+ } else projectId2 ? this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: https://sanity.io/manage/project/${projectId2}/api` : this.message = "The current origin is not allowed to connect to the Live Content API.";
2147
2156
  }
2148
2157
  }
2149
2158
  const httpError = {
@@ -3460,19 +3469,14 @@ ${selectionOpts}`);
3460
3469
  "welcome",
3461
3470
  "reconnect",
3462
3471
  "goaway"
3463
- ]), checkCors = fetchObservable(url, {
3464
- method: "OPTIONS",
3465
- mode: "cors",
3466
- credentials: esOptions.withCredentials ? "include" : "omit",
3467
- headers: esOptions.headers
3468
- }).pipe(
3469
- catchError(() => {
3470
- throw new CorsOriginError({ projectId: projectId2 });
3471
- })
3472
+ ]), checkCors = checkCorsObservable(
3473
+ new URL(this.#client.getUrl("/check/cors", false)),
3474
+ projectId2,
3475
+ esOptions.withCredentials === true
3472
3476
  ), observable2 = events.pipe(
3473
3477
  reconnectOnConnectionFailure(),
3474
3478
  mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
3475
- catchError((err) => checkCors.pipe(
3479
+ catchError((err) => err instanceof CorsOriginError ? throwError(() => err) : checkCors.pipe(
3476
3480
  mergeMap(() => {
3477
3481
  throw err;
3478
3482
  })
@@ -3493,17 +3497,27 @@ ${selectionOpts}`);
3493
3497
  return eventsCache.set(key, observable2), observable2;
3494
3498
  }
3495
3499
  }
3496
- function fetchObservable(url, init) {
3500
+ function checkCorsObservable(url, projectId2, requireCredentials) {
3497
3501
  return new Observable((observer) => {
3498
- const controller = new AbortController(), signal = controller.signal;
3499
- return fetch(url, { ...init, signal: controller.signal }).then(
3500
- (response) => {
3501
- observer.next(response), observer.complete();
3502
- },
3503
- (err) => {
3504
- signal.aborted || observer.error(err);
3502
+ const controller = new AbortController(), { signal } = controller;
3503
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
3504
+ if (!(signal.aborted || !response.ok))
3505
+ return response.json();
3506
+ }).then((body) => {
3507
+ if (!signal.aborted) {
3508
+ if (requireCredentials && body?.result?.withCredentials === false) {
3509
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: true }));
3510
+ return;
3511
+ }
3512
+ if (body?.result?.allowed === false) {
3513
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: requireCredentials }));
3514
+ return;
3515
+ }
3516
+ observer.next(), observer.complete();
3505
3517
  }
3506
- ), () => controller.abort();
3518
+ }).catch(() => {
3519
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
3520
+ }), () => controller.abort();
3507
3521
  });
3508
3522
  }
3509
3523
  const eventsCache = /* @__PURE__ */ new Map();