@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.
@@ -861,6 +861,7 @@ export declare class ClientError extends Error {
861
861
  response: ErrorProps['response']
862
862
  statusCode: ErrorProps['statusCode']
863
863
  responseBody: ErrorProps['responseBody']
864
+ traceId: ErrorProps['traceId']
864
865
  details: ErrorProps['details']
865
866
  constructor(res: Any, context?: HttpContext)
866
867
  }
@@ -1045,9 +1046,9 @@ export declare interface ContentSourceMapValueMapping {
1045
1046
 
1046
1047
  /** @public */
1047
1048
  export declare class CorsOriginError extends Error {
1048
- projectId: string
1049
+ projectId?: string
1049
1050
  addOriginUrl?: URL
1050
- constructor({projectId}: {projectId: string})
1051
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1051
1052
  }
1052
1053
 
1053
1054
  /**
@@ -1417,6 +1418,7 @@ export declare interface ErrorProps {
1417
1418
  response: Any
1418
1419
  statusCode: number
1419
1420
  responseBody: Any
1421
+ traceId?: string
1420
1422
  details: Any
1421
1423
  }
1422
1424
 
@@ -1567,7 +1569,11 @@ export declare type FitMode = 'preserve' | 'stretch' | 'crop' | 'smartcrop' | 'p
1567
1569
  * @returns A formatted error message string.
1568
1570
  * @public
1569
1571
  */
1570
- export declare function formatQueryParseError(error: QueryParseError, tag?: string | null): string
1572
+ export declare function formatQueryParseError(
1573
+ error: QueryParseError,
1574
+ tag?: string | null,
1575
+ traceId?: string,
1576
+ ): string
1571
1577
 
1572
1578
  /** @beta */
1573
1579
  declare type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
@@ -5879,6 +5885,7 @@ export declare class ServerError extends Error {
5879
5885
  response: ErrorProps['response']
5880
5886
  statusCode: ErrorProps['statusCode']
5881
5887
  responseBody: ErrorProps['responseBody']
5888
+ traceId: ErrorProps['traceId']
5882
5889
  details: ErrorProps['details']
5883
5890
  constructor(res: Any)
5884
5891
  }
@@ -861,6 +861,7 @@ export declare class ClientError extends Error {
861
861
  response: ErrorProps['response']
862
862
  statusCode: ErrorProps['statusCode']
863
863
  responseBody: ErrorProps['responseBody']
864
+ traceId: ErrorProps['traceId']
864
865
  details: ErrorProps['details']
865
866
  constructor(res: Any, context?: HttpContext)
866
867
  }
@@ -1045,9 +1046,9 @@ export declare interface ContentSourceMapValueMapping {
1045
1046
 
1046
1047
  /** @public */
1047
1048
  export declare class CorsOriginError extends Error {
1048
- projectId: string
1049
+ projectId?: string
1049
1050
  addOriginUrl?: URL
1050
- constructor({projectId}: {projectId: string})
1051
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1051
1052
  }
1052
1053
 
1053
1054
  /**
@@ -1417,6 +1418,7 @@ export declare interface ErrorProps {
1417
1418
  response: Any
1418
1419
  statusCode: number
1419
1420
  responseBody: Any
1421
+ traceId?: string
1420
1422
  details: Any
1421
1423
  }
1422
1424
 
@@ -1567,7 +1569,11 @@ export declare type FitMode = 'preserve' | 'stretch' | 'crop' | 'smartcrop' | 'p
1567
1569
  * @returns A formatted error message string.
1568
1570
  * @public
1569
1571
  */
1570
- export declare function formatQueryParseError(error: QueryParseError, tag?: string | null): string
1572
+ export declare function formatQueryParseError(
1573
+ error: QueryParseError,
1574
+ tag?: string | null,
1575
+ traceId?: string,
1576
+ ): string
1571
1577
 
1572
1578
  /** @beta */
1573
1579
  declare type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
@@ -5879,6 +5885,7 @@ export declare class ServerError extends Error {
5879
5885
  response: ErrorProps['response']
5880
5886
  statusCode: ErrorProps['statusCode']
5881
5887
  responseBody: ErrorProps['responseBody']
5888
+ traceId: ErrorProps['traceId']
5882
5889
  details: ErrorProps['details']
5883
5890
  constructor(res: Any)
5884
5891
  }
@@ -85,6 +85,7 @@ class ClientError extends Error {
85
85
  response;
86
86
  statusCode = 400;
87
87
  responseBody;
88
+ traceId;
88
89
  details;
89
90
  constructor(res, context) {
90
91
  const props = extractErrorProps(res, context);
@@ -95,6 +96,7 @@ class ServerError extends Error {
95
96
  response;
96
97
  statusCode = 500;
97
98
  responseBody;
99
+ traceId;
98
100
  details;
99
101
  constructor(res) {
100
102
  const props = extractErrorProps(res);
@@ -106,29 +108,30 @@ function extractErrorProps(res, context) {
106
108
  response: res,
107
109
  statusCode: res.statusCode,
108
110
  responseBody: stringifyBody(body, res),
111
+ traceId: extractTraceId(res),
109
112
  message: "",
110
113
  details: void 0
111
114
  };
112
115
  if (!isRecord(body))
113
- return props.message = httpErrorMessage(res, body), props;
116
+ return props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
114
117
  const error = body.error;
115
118
  if (typeof error == "string" && typeof body.message == "string")
116
- return props.message = `${error} - ${body.message}`, props;
119
+ return props.message = `${error} - ${body.message}${formatTraceId(props.traceId)}`, props;
117
120
  if (typeof error != "object" || error === null)
118
- return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
121
+ 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;
119
122
  if (isMutationError(error) || isActionError(error)) {
120
123
  const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
121
124
  let itemsStr = items.length ? `:
122
125
  - ${items.join(`
123
126
  - `)}` : "";
124
127
  return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
125
- ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
128
+ ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${formatTraceId(props.traceId)}${itemsStr}`, props.details = body.error, props;
126
129
  }
127
130
  if (isQueryParseError(error)) {
128
131
  const tag = context?.options?.query?.tag;
129
- return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
132
+ return props.message = formatQueryParseError(error, tag, props.traceId), props.details = body.error, props;
130
133
  }
131
- return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
134
+ 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);
132
135
  }
133
136
  function isMutationError(error) {
134
137
  return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
@@ -139,37 +142,43 @@ function isActionError(error) {
139
142
  function isQueryParseError(error) {
140
143
  return isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
141
144
  }
142
- function formatQueryParseError(error, tag) {
143
- const { query, start, end, description } = error;
145
+ function formatQueryParseError(error, tag, traceId) {
146
+ const { query, start, end, description } = error, withTraceId = traceId ? `
147
+ (traceId: ${traceId})` : "";
144
148
  if (!query || typeof start > "u")
145
- return `GROQ query parse error: ${description}`;
149
+ return `GROQ query parse error: ${description}${withTraceId}`;
146
150
  const withTag = tag ? `
147
151
 
148
152
  Tag: ${tag}` : "";
149
153
  return `GROQ query parse error:
150
- ${codeFrame(query, { start, end }, description)}${withTag}`;
154
+ ${codeFrame(query, { start, end }, description)}${withTag}${withTraceId}`;
151
155
  }
152
156
  function httpErrorMessage(res, body) {
153
157
  const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
154
158
  return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
155
159
  }
160
+ function extractTraceId(res) {
161
+ const traceparent = res?.headers?.traceparent;
162
+ if (traceparent)
163
+ return traceparent.split("-")[1];
164
+ }
156
165
  function stringifyBody(body, res) {
157
166
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
158
167
  }
168
+ function formatTraceId(traceId) {
169
+ return traceId ? ` (traceId: ${traceId})` : "";
170
+ }
159
171
  function sliceWithEllipsis(str, max) {
160
172
  return str.length > max ? `${str.slice(0, max)}\u2026` : str;
161
173
  }
162
174
  class CorsOriginError extends Error {
163
175
  projectId;
164
176
  addOriginUrl;
165
- constructor({ projectId: projectId2 }) {
166
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
167
- const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
168
- if (typeof location < "u") {
169
- const { origin } = location;
170
- 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}`;
171
- } else
172
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
177
+ constructor({ projectId: projectId2, credentials } = {}) {
178
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2, projectId2 && typeof location < "u") {
179
+ const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`), { origin } = location;
180
+ 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}`;
181
+ } 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.";
173
182
  }
174
183
  }
175
184
  const httpError = {
@@ -1486,19 +1495,14 @@ class LiveClient {
1486
1495
  "welcome",
1487
1496
  "reconnect",
1488
1497
  "goaway"
1489
- ]), checkCors = fetchObservable(url, {
1490
- method: "OPTIONS",
1491
- mode: "cors",
1492
- credentials: esOptions.withCredentials ? "include" : "omit",
1493
- headers: esOptions.headers
1494
- }).pipe(
1495
- catchError(() => {
1496
- throw new CorsOriginError({ projectId: projectId2 });
1497
- })
1498
+ ]), checkCors = checkCorsObservable(
1499
+ new URL(this.#client.getUrl("/check/cors", !1)),
1500
+ projectId2,
1501
+ esOptions.withCredentials === !0
1498
1502
  ), observable2 = events.pipe(
1499
1503
  reconnectOnConnectionFailure(),
1500
1504
  mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
1501
- catchError((err) => checkCors.pipe(
1505
+ catchError((err) => err instanceof CorsOriginError ? throwError(() => err) : checkCors.pipe(
1502
1506
  mergeMap(() => {
1503
1507
  throw err;
1504
1508
  })
@@ -1519,17 +1523,27 @@ class LiveClient {
1519
1523
  return eventsCache.set(key, observable2), observable2;
1520
1524
  }
1521
1525
  }
1522
- function fetchObservable(url, init) {
1526
+ function checkCorsObservable(url, projectId2, requireCredentials) {
1523
1527
  return new Observable((observer) => {
1524
- const controller = new AbortController(), signal = controller.signal;
1525
- return fetch(url, { ...init, signal: controller.signal }).then(
1526
- (response) => {
1527
- observer.next(response), observer.complete();
1528
- },
1529
- (err) => {
1530
- signal.aborted || observer.error(err);
1528
+ const controller = new AbortController(), { signal } = controller;
1529
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1530
+ if (!(signal.aborted || !response.ok))
1531
+ return response.json();
1532
+ }).then((body) => {
1533
+ if (!signal.aborted) {
1534
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1535
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: !0 }));
1536
+ return;
1537
+ }
1538
+ if (body?.result?.allowed === !1) {
1539
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: requireCredentials }));
1540
+ return;
1541
+ }
1542
+ observer.next(), observer.complete();
1531
1543
  }
1532
- ), () => controller.abort();
1544
+ }).catch(() => {
1545
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1546
+ }), () => controller.abort();
1533
1547
  });
1534
1548
  }
1535
1549
  const eventsCache = /* @__PURE__ */ new Map();