@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.
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,37 +156,43 @@ 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
  }
176
188
  class CorsOriginError extends Error {
177
189
  projectId;
178
190
  addOriginUrl;
179
- constructor({ projectId }) {
180
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId;
181
- const url = new URL(`https://sanity.io/manage/project/${projectId}/api`);
182
- if (typeof location < "u") {
183
- const { origin } = location;
184
- 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}`;
185
- } else
186
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
191
+ constructor({ projectId, credentials } = {}) {
192
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId, projectId && typeof location < "u") {
193
+ const url = new URL(`https://sanity.io/manage/project/${projectId}/api`), { origin } = location;
194
+ 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}`;
195
+ } else projectId ? this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: https://sanity.io/manage/project/${projectId}/api` : this.message = "The current origin is not allowed to connect to the Live Content API.";
187
196
  }
188
197
  }
189
198
  const httpError = {
@@ -1318,19 +1327,14 @@ class LiveClient {
1318
1327
  "welcome",
1319
1328
  "reconnect",
1320
1329
  "goaway"
1321
- ]), checkCors = fetchObservable(url, {
1322
- method: "OPTIONS",
1323
- mode: "cors",
1324
- credentials: esOptions.withCredentials ? "include" : "omit",
1325
- headers: esOptions.headers
1326
- }).pipe(
1327
- rxjs.catchError(() => {
1328
- throw new CorsOriginError({ projectId });
1329
- })
1330
+ ]), checkCors = checkCorsObservable(
1331
+ new URL(this.#client.getUrl("/check/cors", !1)),
1332
+ projectId,
1333
+ esOptions.withCredentials === !0
1330
1334
  ), observable = events.pipe(
1331
1335
  reconnectOnConnectionFailure(),
1332
1336
  rxjs.mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(rxjs.mergeMap(() => rxjs.of(event))) : rxjs.of(event)),
1333
- rxjs.catchError((err) => checkCors.pipe(
1337
+ rxjs.catchError((err) => err instanceof CorsOriginError ? rxjs.throwError(() => err) : checkCors.pipe(
1334
1338
  rxjs.mergeMap(() => {
1335
1339
  throw err;
1336
1340
  })
@@ -1351,17 +1355,27 @@ class LiveClient {
1351
1355
  return eventsCache.set(key, observable), observable;
1352
1356
  }
1353
1357
  }
1354
- function fetchObservable(url, init) {
1358
+ function checkCorsObservable(url, projectId, requireCredentials) {
1355
1359
  return new rxjs.Observable((observer) => {
1356
- const controller = new AbortController(), signal = controller.signal;
1357
- return fetch(url, { ...init, signal: controller.signal }).then(
1358
- (response) => {
1359
- observer.next(response), observer.complete();
1360
- },
1361
- (err) => {
1362
- signal.aborted || observer.error(err);
1360
+ const controller = new AbortController(), { signal } = controller;
1361
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1362
+ if (!(signal.aborted || !response.ok))
1363
+ return response.json();
1364
+ }).then((body) => {
1365
+ if (!signal.aborted) {
1366
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1367
+ observer.error(new CorsOriginError({ projectId, credentials: !0 }));
1368
+ return;
1369
+ }
1370
+ if (body?.result?.allowed === !1) {
1371
+ observer.error(new CorsOriginError({ projectId, credentials: requireCredentials }));
1372
+ return;
1373
+ }
1374
+ observer.next(), observer.complete();
1363
1375
  }
1364
- ), () => controller.abort();
1376
+ }).catch(() => {
1377
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1378
+ }), () => controller.abort();
1365
1379
  });
1366
1380
  }
1367
1381
  const eventsCache = /* @__PURE__ */ new Map();
@@ -2788,7 +2802,7 @@ function defineDeprecatedCreateClient(createClient2) {
2788
2802
  return config.printNoDefaultExport(), createClient2(config$1);
2789
2803
  };
2790
2804
  }
2791
- var name = "@sanity/client", version = "7.21.0";
2805
+ var name = "@sanity/client", version = "7.22.1";
2792
2806
  const middleware = [
2793
2807
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
2794
2808
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),