@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.d.cts CHANGED
@@ -862,6 +862,7 @@ export declare class ClientError extends Error {
862
862
  response: ErrorProps['response']
863
863
  statusCode: ErrorProps['statusCode']
864
864
  responseBody: ErrorProps['responseBody']
865
+ traceId: ErrorProps['traceId']
865
866
  details: ErrorProps['details']
866
867
  constructor(res: Any, context?: HttpContext)
867
868
  }
@@ -1038,9 +1039,9 @@ export declare interface ContentSourceMapValueMapping {
1038
1039
 
1039
1040
  /** @public */
1040
1041
  export declare class CorsOriginError extends Error {
1041
- projectId: string
1042
+ projectId?: string
1042
1043
  addOriginUrl?: URL
1043
- constructor({projectId}: {projectId: string})
1044
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1044
1045
  }
1045
1046
 
1046
1047
  /**
@@ -1409,6 +1410,7 @@ export declare interface ErrorProps {
1409
1410
  response: Any
1410
1411
  statusCode: number
1411
1412
  responseBody: Any
1413
+ traceId?: string
1412
1414
  details: Any
1413
1415
  }
1414
1416
 
@@ -1559,7 +1561,11 @@ export declare type FitMode = 'preserve' | 'stretch' | 'crop' | 'smartcrop' | 'p
1559
1561
  * @returns A formatted error message string.
1560
1562
  * @public
1561
1563
  */
1562
- export declare function formatQueryParseError(error: QueryParseError, tag?: string | null): string
1564
+ export declare function formatQueryParseError(
1565
+ error: QueryParseError,
1566
+ tag?: string | null,
1567
+ traceId?: string,
1568
+ ): string
1563
1569
 
1564
1570
  /** @beta */
1565
1571
  declare type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
@@ -5871,6 +5877,7 @@ export declare class ServerError extends Error {
5871
5877
  response: ErrorProps['response']
5872
5878
  statusCode: ErrorProps['statusCode']
5873
5879
  responseBody: ErrorProps['responseBody']
5880
+ traceId: ErrorProps['traceId']
5874
5881
  details: ErrorProps['details']
5875
5882
  constructor(res: Any)
5876
5883
  }
package/dist/index.d.ts CHANGED
@@ -862,6 +862,7 @@ export declare class ClientError extends Error {
862
862
  response: ErrorProps['response']
863
863
  statusCode: ErrorProps['statusCode']
864
864
  responseBody: ErrorProps['responseBody']
865
+ traceId: ErrorProps['traceId']
865
866
  details: ErrorProps['details']
866
867
  constructor(res: Any, context?: HttpContext)
867
868
  }
@@ -1038,9 +1039,9 @@ export declare interface ContentSourceMapValueMapping {
1038
1039
 
1039
1040
  /** @public */
1040
1041
  export declare class CorsOriginError extends Error {
1041
- projectId: string
1042
+ projectId?: string
1042
1043
  addOriginUrl?: URL
1043
- constructor({projectId}: {projectId: string})
1044
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1044
1045
  }
1045
1046
 
1046
1047
  /**
@@ -1409,6 +1410,7 @@ export declare interface ErrorProps {
1409
1410
  response: Any
1410
1411
  statusCode: number
1411
1412
  responseBody: Any
1413
+ traceId?: string
1412
1414
  details: Any
1413
1415
  }
1414
1416
 
@@ -1559,7 +1561,11 @@ export declare type FitMode = 'preserve' | 'stretch' | 'crop' | 'smartcrop' | 'p
1559
1561
  * @returns A formatted error message string.
1560
1562
  * @public
1561
1563
  */
1562
- export declare function formatQueryParseError(error: QueryParseError, tag?: string | null): string
1564
+ export declare function formatQueryParseError(
1565
+ error: QueryParseError,
1566
+ tag?: string | null,
1567
+ traceId?: string,
1568
+ ): string
1563
1569
 
1564
1570
  /** @beta */
1565
1571
  declare type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
@@ -5871,6 +5877,7 @@ export declare class ServerError extends Error {
5871
5877
  response: ErrorProps['response']
5872
5878
  statusCode: ErrorProps['statusCode']
5873
5879
  responseBody: ErrorProps['responseBody']
5880
+ traceId: ErrorProps['traceId']
5874
5881
  details: ErrorProps['details']
5875
5882
  constructor(res: Any)
5876
5883
  }
package/dist/index.js CHANGED
@@ -87,6 +87,7 @@ class ClientError extends Error {
87
87
  response;
88
88
  statusCode = 400;
89
89
  responseBody;
90
+ traceId;
90
91
  details;
91
92
  constructor(res, context) {
92
93
  const props = extractErrorProps(res, context);
@@ -97,6 +98,7 @@ class ServerError extends Error {
97
98
  response;
98
99
  statusCode = 500;
99
100
  responseBody;
101
+ traceId;
100
102
  details;
101
103
  constructor(res) {
102
104
  const props = extractErrorProps(res);
@@ -108,29 +110,30 @@ function extractErrorProps(res, context) {
108
110
  response: res,
109
111
  statusCode: res.statusCode,
110
112
  responseBody: stringifyBody(body, res),
113
+ traceId: extractTraceId(res),
111
114
  message: "",
112
115
  details: void 0
113
116
  };
114
117
  if (!isRecord(body))
115
- return props.message = httpErrorMessage(res, body), props;
118
+ return props.message = `${httpErrorMessage(res, body)}${formatTraceId(props.traceId)}`, props;
116
119
  const error = body.error;
117
120
  if (typeof error == "string" && typeof body.message == "string")
118
- return props.message = `${error} - ${body.message}`, props;
121
+ return props.message = `${error} - ${body.message}${formatTraceId(props.traceId)}`, props;
119
122
  if (typeof error != "object" || error === null)
120
- return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
123
+ 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;
121
124
  if (isMutationError(error) || isActionError(error)) {
122
125
  const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
123
126
  let itemsStr = items.length ? `:
124
127
  - ${items.join(`
125
128
  - `)}` : "";
126
129
  return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
127
- ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
130
+ ...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${formatTraceId(props.traceId)}${itemsStr}`, props.details = body.error, props;
128
131
  }
129
132
  if (isQueryParseError(error)) {
130
133
  const tag = context?.options?.query?.tag;
131
- return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
134
+ return props.message = formatQueryParseError(error, tag, props.traceId), props.details = body.error, props;
132
135
  }
133
- return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
136
+ 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);
134
137
  }
135
138
  function isMutationError(error) {
136
139
  return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
@@ -141,37 +144,43 @@ function isActionError(error) {
141
144
  function isQueryParseError(error) {
142
145
  return isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
143
146
  }
144
- function formatQueryParseError(error, tag) {
145
- const { query, start, end, description } = error;
147
+ function formatQueryParseError(error, tag, traceId) {
148
+ const { query, start, end, description } = error, withTraceId = traceId ? `
149
+ (traceId: ${traceId})` : "";
146
150
  if (!query || typeof start > "u")
147
- return `GROQ query parse error: ${description}`;
151
+ return `GROQ query parse error: ${description}${withTraceId}`;
148
152
  const withTag = tag ? `
149
153
 
150
154
  Tag: ${tag}` : "";
151
155
  return `GROQ query parse error:
152
- ${codeFrame(query, { start, end }, description)}${withTag}`;
156
+ ${codeFrame(query, { start, end }, description)}${withTag}${withTraceId}`;
153
157
  }
154
158
  function httpErrorMessage(res, body) {
155
159
  const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
156
160
  return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
157
161
  }
162
+ function extractTraceId(res) {
163
+ const traceparent = res?.headers?.traceparent;
164
+ if (traceparent)
165
+ return traceparent.split("-")[1];
166
+ }
158
167
  function stringifyBody(body, res) {
159
168
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
160
169
  }
170
+ function formatTraceId(traceId) {
171
+ return traceId ? ` (traceId: ${traceId})` : "";
172
+ }
161
173
  function sliceWithEllipsis(str, max) {
162
174
  return str.length > max ? `${str.slice(0, max)}\u2026` : str;
163
175
  }
164
176
  class CorsOriginError extends Error {
165
177
  projectId;
166
178
  addOriginUrl;
167
- constructor({ projectId }) {
168
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId;
169
- const url = new URL(`https://sanity.io/manage/project/${projectId}/api`);
170
- if (typeof location < "u") {
171
- const { origin } = location;
172
- 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}`;
173
- } else
174
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
179
+ constructor({ projectId, credentials } = {}) {
180
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId, projectId && typeof location < "u") {
181
+ const url = new URL(`https://sanity.io/manage/project/${projectId}/api`), { origin } = location;
182
+ 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}`;
183
+ } 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.";
175
184
  }
176
185
  }
177
186
  const httpError = {
@@ -1304,19 +1313,14 @@ class LiveClient {
1304
1313
  "welcome",
1305
1314
  "reconnect",
1306
1315
  "goaway"
1307
- ]), checkCors = fetchObservable(url, {
1308
- method: "OPTIONS",
1309
- mode: "cors",
1310
- credentials: esOptions.withCredentials ? "include" : "omit",
1311
- headers: esOptions.headers
1312
- }).pipe(
1313
- catchError(() => {
1314
- throw new CorsOriginError({ projectId });
1315
- })
1316
+ ]), checkCors = checkCorsObservable(
1317
+ new URL(this.#client.getUrl("/check/cors", !1)),
1318
+ projectId,
1319
+ esOptions.withCredentials === !0
1316
1320
  ), observable2 = events.pipe(
1317
1321
  reconnectOnConnectionFailure(),
1318
1322
  mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
1319
- catchError((err) => checkCors.pipe(
1323
+ catchError((err) => err instanceof CorsOriginError ? throwError(() => err) : checkCors.pipe(
1320
1324
  mergeMap(() => {
1321
1325
  throw err;
1322
1326
  })
@@ -1337,17 +1341,27 @@ class LiveClient {
1337
1341
  return eventsCache.set(key, observable2), observable2;
1338
1342
  }
1339
1343
  }
1340
- function fetchObservable(url, init) {
1344
+ function checkCorsObservable(url, projectId, requireCredentials) {
1341
1345
  return new Observable((observer) => {
1342
- const controller = new AbortController(), signal = controller.signal;
1343
- return fetch(url, { ...init, signal: controller.signal }).then(
1344
- (response) => {
1345
- observer.next(response), observer.complete();
1346
- },
1347
- (err) => {
1348
- signal.aborted || observer.error(err);
1346
+ const controller = new AbortController(), { signal } = controller;
1347
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1348
+ if (!(signal.aborted || !response.ok))
1349
+ return response.json();
1350
+ }).then((body) => {
1351
+ if (!signal.aborted) {
1352
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1353
+ observer.error(new CorsOriginError({ projectId, credentials: !0 }));
1354
+ return;
1355
+ }
1356
+ if (body?.result?.allowed === !1) {
1357
+ observer.error(new CorsOriginError({ projectId, credentials: requireCredentials }));
1358
+ return;
1359
+ }
1360
+ observer.next(), observer.complete();
1349
1361
  }
1350
- ), () => controller.abort();
1362
+ }).catch(() => {
1363
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1364
+ }), () => controller.abort();
1351
1365
  });
1352
1366
  }
1353
1367
  const eventsCache = /* @__PURE__ */ new Map();
@@ -2774,7 +2788,7 @@ function defineDeprecatedCreateClient(createClient2) {
2774
2788
  return printNoDefaultExport(), createClient2(config);
2775
2789
  };
2776
2790
  }
2777
- var name = "@sanity/client", version = "7.21.0";
2791
+ var name = "@sanity/client", version = "7.22.1";
2778
2792
  const middleware = [
2779
2793
  debug({ verbose: !0, namespace: "sanity:client" }),
2780
2794
  headers({ "User-Agent": `${name} ${version}` }),