@sanity/client 7.3.0 → 7.4.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/README.md +122 -1
- package/dist/_chunks-cjs/isRecord.cjs +6 -0
- package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveEditInfo.cjs +3 -5
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaClean.cjs +4 -0
- package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +2 -5
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/isRecord.js +7 -0
- package/dist/_chunks-es/isRecord.js.map +1 -0
- package/dist/_chunks-es/resolveEditInfo.js +1 -3
- package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
- package/dist/_chunks-es/stegaClean.js +4 -0
- package/dist/_chunks-es/stegaClean.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -4
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/index.browser.cjs +155 -32
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +473 -68
- package/dist/index.browser.d.ts +473 -68
- package/dist/index.browser.js +156 -33
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +157 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +473 -68
- package/dist/index.d.ts +473 -68
- package/dist/index.js +157 -33
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +473 -68
- package/dist/stega.browser.d.ts +473 -68
- package/dist/stega.d.cts +473 -68
- package/dist/stega.d.ts +473 -68
- package/package.json +1 -1
- package/src/agent/actions/AgentActionsClient.ts +29 -2
- package/src/agent/actions/commonTypes.ts +57 -17
- package/src/agent/actions/generate.ts +36 -2
- package/src/agent/actions/patch.ts +136 -0
- package/src/agent/actions/prompt.ts +145 -0
- package/src/agent/actions/transform.ts +27 -4
- package/src/agent/actions/translate.ts +5 -2
- package/src/csm/walkMap.ts +1 -1
- package/src/data/eventsource.ts +16 -7
- package/src/data/listen.ts +10 -4
- package/src/data/live.ts +13 -5
- package/src/defineCreateClient.ts +7 -1
- package/src/http/errors.ts +92 -27
- package/src/http/request.ts +3 -3
- package/src/types.ts +25 -10
- package/src/util/codeFrame.ts +174 -0
- package/src/{csm → util}/isRecord.ts +1 -1
- package/umd/sanityClient.js +158 -35
- package/umd/sanityClient.min.js +2 -2
package/dist/index.browser.cjs
CHANGED
|
@@ -20,13 +20,82 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
20
20
|
));
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
22
22
|
var getIt = require("get-it"), middleware = require("get-it/middleware"), rxjs = require("rxjs"), stegaClean = require("./_chunks-cjs/stegaClean.cjs"), operators = require("rxjs/operators"), csm = require("@sanity/client/csm"), nanoid = require("nanoid");
|
|
23
|
+
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
|
24
|
+
function codeFrame(query, location2, message) {
|
|
25
|
+
const lines = query.split(NEWLINE), loc = {
|
|
26
|
+
start: columnToLine(location2.start, lines),
|
|
27
|
+
end: location2.end ? columnToLine(location2.end, lines) : void 0
|
|
28
|
+
}, { start, end, markerLines } = getMarkerLines(loc, lines), numberMaxWidth = `${end}`.length;
|
|
29
|
+
return query.split(NEWLINE, end).slice(start, end).map((line, index) => {
|
|
30
|
+
const number = start + 1 + index, gutter = ` ${` ${number}`.slice(-numberMaxWidth)} |`, hasMarker = markerLines[number], lastMarkerLine = !markerLines[number + 1];
|
|
31
|
+
if (!hasMarker)
|
|
32
|
+
return ` ${gutter}${line.length > 0 ? ` ${line}` : ""}`;
|
|
33
|
+
let markerLine = "";
|
|
34
|
+
if (Array.isArray(hasMarker)) {
|
|
35
|
+
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " "), numberOfMarkers = hasMarker[1] || 1;
|
|
36
|
+
markerLine = [
|
|
37
|
+
`
|
|
38
|
+
`,
|
|
39
|
+
gutter.replace(/\d/g, " "),
|
|
40
|
+
" ",
|
|
41
|
+
markerSpacing,
|
|
42
|
+
"^".repeat(numberOfMarkers)
|
|
43
|
+
].join(""), lastMarkerLine && message && (markerLine += " " + message);
|
|
44
|
+
}
|
|
45
|
+
return [">", gutter, line.length > 0 ? ` ${line}` : "", markerLine].join("");
|
|
46
|
+
}).join(`
|
|
47
|
+
`);
|
|
48
|
+
}
|
|
49
|
+
function getMarkerLines(loc, source) {
|
|
50
|
+
const startLoc = { ...loc.start }, endLoc = { ...startLoc, ...loc.end }, linesAbove = 2, linesBelow = 3, startLine = startLoc.line ?? -1, startColumn = startLoc.column ?? 0, endLine = endLoc.line, endColumn = endLoc.column;
|
|
51
|
+
let start = Math.max(startLine - (linesAbove + 1), 0), end = Math.min(source.length, endLine + linesBelow);
|
|
52
|
+
startLine === -1 && (start = 0), endLine === -1 && (end = source.length);
|
|
53
|
+
const lineDiff = endLine - startLine, markerLines = {};
|
|
54
|
+
if (lineDiff)
|
|
55
|
+
for (let i = 0; i <= lineDiff; i++) {
|
|
56
|
+
const lineNumber = i + startLine;
|
|
57
|
+
if (!startColumn)
|
|
58
|
+
markerLines[lineNumber] = !0;
|
|
59
|
+
else if (i === 0) {
|
|
60
|
+
const sourceLength = source[lineNumber - 1].length;
|
|
61
|
+
markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
|
|
62
|
+
} else if (i === lineDiff)
|
|
63
|
+
markerLines[lineNumber] = [0, endColumn];
|
|
64
|
+
else {
|
|
65
|
+
const sourceLength = source[lineNumber - i].length;
|
|
66
|
+
markerLines[lineNumber] = [0, sourceLength];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
startColumn === endColumn ? startColumn ? markerLines[startLine] = [startColumn, 0] : markerLines[startLine] = !0 : markerLines[startLine] = [startColumn, endColumn - startColumn];
|
|
71
|
+
return { start, end, markerLines };
|
|
72
|
+
}
|
|
73
|
+
function columnToLine(column, lines) {
|
|
74
|
+
let offset = 0;
|
|
75
|
+
for (let i = 0; i < lines.length; i++) {
|
|
76
|
+
const lineLength = lines[i].length + 1;
|
|
77
|
+
if (offset + lineLength > column)
|
|
78
|
+
return {
|
|
79
|
+
line: i + 1,
|
|
80
|
+
// 1-based line
|
|
81
|
+
column: column - offset
|
|
82
|
+
// 0-based column
|
|
83
|
+
};
|
|
84
|
+
offset += lineLength;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
line: lines.length,
|
|
88
|
+
column: lines[lines.length - 1]?.length ?? 0
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
|
|
23
92
|
class ClientError extends Error {
|
|
24
93
|
response;
|
|
25
94
|
statusCode = 400;
|
|
26
95
|
responseBody;
|
|
27
96
|
details;
|
|
28
|
-
constructor(res) {
|
|
29
|
-
const props = extractErrorProps(res);
|
|
97
|
+
constructor(res, context) {
|
|
98
|
+
const props = extractErrorProps(res, context);
|
|
30
99
|
super(props.message), Object.assign(this, props);
|
|
31
100
|
}
|
|
32
101
|
}
|
|
@@ -40,7 +109,7 @@ class ServerError extends Error {
|
|
|
40
109
|
super(props.message), Object.assign(this, props);
|
|
41
110
|
}
|
|
42
111
|
}
|
|
43
|
-
function extractErrorProps(res) {
|
|
112
|
+
function extractErrorProps(res, context) {
|
|
44
113
|
const body = res.body, props = {
|
|
45
114
|
response: res,
|
|
46
115
|
statusCode: res.statusCode,
|
|
@@ -48,34 +117,56 @@ function extractErrorProps(res) {
|
|
|
48
117
|
message: "",
|
|
49
118
|
details: void 0
|
|
50
119
|
};
|
|
51
|
-
if (
|
|
52
|
-
return props.message =
|
|
53
|
-
|
|
54
|
-
|
|
120
|
+
if (!stegaClean.isRecord(body))
|
|
121
|
+
return props.message = httpErrorMessage(res, body), props;
|
|
122
|
+
const error = body.error;
|
|
123
|
+
if (typeof error == "string" && typeof body.message == "string")
|
|
124
|
+
return props.message = `${error} - ${body.message}`, props;
|
|
125
|
+
if (typeof error != "object" || error === null)
|
|
126
|
+
return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
|
|
127
|
+
if (isMutationError(error) || isActionError(error)) {
|
|
128
|
+
const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
|
|
55
129
|
let itemsStr = items.length ? `:
|
|
56
130
|
- ${items.join(`
|
|
57
131
|
- `)}` : "";
|
|
58
|
-
return allItems.length >
|
|
59
|
-
...and ${allItems.length -
|
|
132
|
+
return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
|
|
133
|
+
...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
|
|
134
|
+
}
|
|
135
|
+
if (isQueryParseError(error)) {
|
|
136
|
+
const tag = context?.options?.query?.tag;
|
|
137
|
+
return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
|
|
60
138
|
}
|
|
61
|
-
return
|
|
139
|
+
return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
|
|
62
140
|
}
|
|
63
|
-
function isMutationError(
|
|
64
|
-
return
|
|
141
|
+
function isMutationError(error) {
|
|
142
|
+
return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
|
|
65
143
|
}
|
|
66
|
-
function isActionError(
|
|
67
|
-
return
|
|
144
|
+
function isActionError(error) {
|
|
145
|
+
return "type" in error && error.type === "actionError" && "description" in error && typeof error.description == "string";
|
|
68
146
|
}
|
|
69
|
-
function
|
|
70
|
-
return typeof
|
|
147
|
+
function isQueryParseError(error) {
|
|
148
|
+
return stegaClean.isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
|
|
71
149
|
}
|
|
72
|
-
function
|
|
73
|
-
const
|
|
74
|
-
|
|
150
|
+
function formatQueryParseError(error, tag) {
|
|
151
|
+
const { query, start, end, description } = error;
|
|
152
|
+
if (!query || typeof start > "u")
|
|
153
|
+
return `GROQ query parse error: ${description}`;
|
|
154
|
+
const withTag = tag ? `
|
|
155
|
+
|
|
156
|
+
Tag: ${tag}` : "";
|
|
157
|
+
return `GROQ query parse error:
|
|
158
|
+
${codeFrame(query, { start, end }, description)}${withTag}`;
|
|
159
|
+
}
|
|
160
|
+
function httpErrorMessage(res, body) {
|
|
161
|
+
const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
|
|
162
|
+
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
|
|
75
163
|
}
|
|
76
164
|
function stringifyBody(body, res) {
|
|
77
165
|
return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
|
|
78
166
|
}
|
|
167
|
+
function sliceWithEllipsis(str, max) {
|
|
168
|
+
return str.length > max ? `${str.slice(0, max)}\u2026` : str;
|
|
169
|
+
}
|
|
79
170
|
class CorsOriginError extends Error {
|
|
80
171
|
projectId;
|
|
81
172
|
addOriginUrl;
|
|
@@ -90,11 +181,11 @@ class CorsOriginError extends Error {
|
|
|
90
181
|
}
|
|
91
182
|
}
|
|
92
183
|
const httpError = {
|
|
93
|
-
onResponse: (res) => {
|
|
184
|
+
onResponse: (res, context) => {
|
|
94
185
|
if (res.statusCode >= 500)
|
|
95
186
|
throw new ServerError(res);
|
|
96
187
|
if (res.statusCode >= 400)
|
|
97
|
-
throw new ClientError(res);
|
|
188
|
+
throw new ClientError(res, context);
|
|
98
189
|
return res;
|
|
99
190
|
}
|
|
100
191
|
};
|
|
@@ -355,7 +446,8 @@ function connectWithESInstance(es, events) {
|
|
|
355
446
|
return;
|
|
356
447
|
}
|
|
357
448
|
if (message.type === "channelError") {
|
|
358
|
-
|
|
449
|
+
const tag = new URL(es.url).searchParams.get("tag");
|
|
450
|
+
observer.error(new ChannelError(extractErrorMessage(event?.data, tag), event.data));
|
|
359
451
|
return;
|
|
360
452
|
}
|
|
361
453
|
if (message.type === "disconnect") {
|
|
@@ -394,8 +486,9 @@ function parseEvent(message) {
|
|
|
394
486
|
return [err, null];
|
|
395
487
|
}
|
|
396
488
|
}
|
|
397
|
-
function extractErrorMessage(err) {
|
|
398
|
-
|
|
489
|
+
function extractErrorMessage(err, tag) {
|
|
490
|
+
const error = err.error;
|
|
491
|
+
return error ? isQueryParseError(error) ? formatQueryParseError(error, tag) : error.description ? error.description : typeof error == "string" ? error : JSON.stringify(error, null, 2) : err.message || "Unknown listener error";
|
|
399
492
|
}
|
|
400
493
|
function isEmptyObject(data) {
|
|
401
494
|
for (const _ in data)
|
|
@@ -1060,6 +1153,22 @@ function _generate(client, httpRequest, request) {
|
|
|
1060
1153
|
body: request
|
|
1061
1154
|
});
|
|
1062
1155
|
}
|
|
1156
|
+
function _patch(client, httpRequest, request) {
|
|
1157
|
+
const dataset2 = hasDataset(client.config());
|
|
1158
|
+
return _request(client, httpRequest, {
|
|
1159
|
+
method: "POST",
|
|
1160
|
+
uri: `/agent/action/patch/${dataset2}`,
|
|
1161
|
+
body: request
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
function _prompt(client, httpRequest, request) {
|
|
1165
|
+
const dataset2 = hasDataset(client.config());
|
|
1166
|
+
return _request(client, httpRequest, {
|
|
1167
|
+
method: "POST",
|
|
1168
|
+
uri: `/agent/action/prompt/${dataset2}`,
|
|
1169
|
+
body: request
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1063
1172
|
function _transform(client, httpRequest, request) {
|
|
1064
1173
|
const dataset2 = hasDataset(client.config());
|
|
1065
1174
|
return _request(client, httpRequest, {
|
|
@@ -1131,6 +1240,21 @@ class AgentActionsClient {
|
|
|
1131
1240
|
translate(request) {
|
|
1132
1241
|
return rxjs.lastValueFrom(_translate(this.#client, this.#httpRequest, request));
|
|
1133
1242
|
}
|
|
1243
|
+
/**
|
|
1244
|
+
* Run a raw instruction and return the result either as text or json
|
|
1245
|
+
* @param request - prompt request
|
|
1246
|
+
*/
|
|
1247
|
+
prompt(request) {
|
|
1248
|
+
return rxjs.lastValueFrom(_prompt(this.#client, this.#httpRequest, request));
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Patch a document using a schema aware API.
|
|
1252
|
+
* Does not use an LLM, but uses the schema to ensure paths and values matches the schema.
|
|
1253
|
+
* @param request - instruction request
|
|
1254
|
+
*/
|
|
1255
|
+
patch(request) {
|
|
1256
|
+
return rxjs.lastValueFrom(_patch(this.#client, this.#httpRequest, request));
|
|
1257
|
+
}
|
|
1134
1258
|
}
|
|
1135
1259
|
class ObservableAssetsClient {
|
|
1136
1260
|
#client;
|
|
@@ -1237,13 +1361,11 @@ const MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
|
1237
1361
|
includeResult: !0
|
|
1238
1362
|
};
|
|
1239
1363
|
function _listen(query, params, opts = {}) {
|
|
1240
|
-
const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } }), uri = `${url}${_getDataUrl(this, "listen", qs)}`;
|
|
1364
|
+
const { url, token, withCredentials, requestTagPrefix, headers: configHeaders } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } }), uri = `${url}${_getDataUrl(this, "listen", qs)}`;
|
|
1241
1365
|
if (uri.length > MAX_URL_LENGTH)
|
|
1242
1366
|
return rxjs.throwError(() => new Error("Query too large for listener"));
|
|
1243
1367
|
const listenFor = options.events ? options.events : ["mutation"], esOptions = {};
|
|
1244
|
-
return withCredentials && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
1245
|
-
Authorization: `Bearer ${token}`
|
|
1246
|
-
}), connectEventSource(() => (
|
|
1368
|
+
return withCredentials && (esOptions.withCredentials = !0), (token || configHeaders) && (esOptions.headers = {}, token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders)), connectEventSource(() => (
|
|
1247
1369
|
// use polyfill if there is no global EventSource or if we need to set headers
|
|
1248
1370
|
(typeof EventSource > "u" || esOptions.headers ? eventSourcePolyfill : rxjs.of(EventSource)).pipe(operators.map((EventSource2) => new EventSource2(uri, esOptions)))
|
|
1249
1371
|
), listenFor).pipe(
|
|
@@ -1301,7 +1423,8 @@ class LiveClient {
|
|
|
1301
1423
|
apiVersion: _apiVersion,
|
|
1302
1424
|
token,
|
|
1303
1425
|
withCredentials,
|
|
1304
|
-
requestTagPrefix
|
|
1426
|
+
requestTagPrefix,
|
|
1427
|
+
headers: configHeaders
|
|
1305
1428
|
} = this.#client.config(), apiVersion = _apiVersion.replace(/^v/, "");
|
|
1306
1429
|
if (apiVersion !== "X" && apiVersion < requiredApiVersion)
|
|
1307
1430
|
throw new Error(
|
|
@@ -1314,9 +1437,7 @@ class LiveClient {
|
|
|
1314
1437
|
const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
|
|
1315
1438
|
tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
|
|
1316
1439
|
const esOptions = {};
|
|
1317
|
-
includeDrafts && token && (esOptions.headers = {
|
|
1318
|
-
Authorization: `Bearer ${token}`
|
|
1319
|
-
}), includeDrafts && withCredentials && (esOptions.withCredentials = !0);
|
|
1440
|
+
includeDrafts && withCredentials && (esOptions.withCredentials = !0), (includeDrafts && token || configHeaders) && (esOptions.headers = {}, includeDrafts && token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders));
|
|
1320
1441
|
const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
|
|
1321
1442
|
if (existing)
|
|
1322
1443
|
return existing;
|
|
@@ -2606,6 +2727,8 @@ exports.Transaction = Transaction;
|
|
|
2606
2727
|
exports.connectEventSource = connectEventSource;
|
|
2607
2728
|
exports.createClient = createClient;
|
|
2608
2729
|
exports.default = deprecatedCreateClient;
|
|
2730
|
+
exports.formatQueryParseError = formatQueryParseError;
|
|
2731
|
+
exports.isQueryParseError = isQueryParseError;
|
|
2609
2732
|
exports.requester = requester;
|
|
2610
2733
|
exports.validateApiPerspective = validateApiPerspective;
|
|
2611
2734
|
//# sourceMappingURL=index.browser.cjs.map
|