@sanity/client 7.21.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.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
  }
@@ -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
  }
@@ -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,23 +144,32 @@ 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
  }
@@ -2774,7 +2786,7 @@ function defineDeprecatedCreateClient(createClient2) {
2774
2786
  return printNoDefaultExport(), createClient2(config);
2775
2787
  };
2776
2788
  }
2777
- var name = "@sanity/client", version = "7.21.0";
2789
+ var name = "@sanity/client", version = "7.22.0";
2778
2790
  const middleware = [
2779
2791
  debug({ verbose: !0, namespace: "sanity:client" }),
2780
2792
  headers({ "User-Agent": `${name} ${version}` }),