@sanity/client
Version:
Client for retrieving, creating and patching data from Sanity.io
1,184 lines (1,182 loc) • 97.3 kB
JavaScript
import { getIt } from "get-it";
import { adapter, environment } from "get-it";
import { retry, jsonRequest, jsonResponse, progress, observable, debug, headers, agent } from "get-it/middleware";
import { Observable, defer, of, isObservable, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, throwError, timer, tap, finalize, share, merge, EMPTY, map as map$1, firstValueFrom } from "rxjs";
import { isRecord } from "./_chunks-es/isRecord.js";
import { stegaClean } from "./_chunks-es/stegaClean.js";
import { combineLatestWith, map, filter, finalize as finalize$1 } from "rxjs/operators";
import { getVersionFromId, isDraftId, getVersionId, getDraftId, isVersionId, getPublishedId } from "@sanity/client/csm";
import { customAlphabet } from "nanoid";
import { validateObject, validateInsert, requireDocumentId, validateDocumentId, requireDocumentType, resourceConfig, hasDataset, requestTag, printPreviewDraftsDeprecationWarning, validateApiPerspective, printCdnPreviewDraftsWarning, validateAssetType, resourceGuard, dataset, validateVersionIdMatch, defaultConfig, initConfig, printNoDefaultExport } from "./_chunks-es/config.js";
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
function codeFrame(query, location2, message) {
const lines = query.split(NEWLINE), loc = {
start: columnToLine(location2.start, lines),
end: location2.end ? columnToLine(location2.end, lines) : void 0
}, { start, end, markerLines } = getMarkerLines(loc, lines), numberMaxWidth = `${end}`.length;
return query.split(NEWLINE, end).slice(start, end).map((line, index) => {
const number = start + 1 + index, gutter = ` ${` ${number}`.slice(-numberMaxWidth)} |`, hasMarker = markerLines[number], lastMarkerLine = !markerLines[number + 1];
if (!hasMarker)
return ` ${gutter}${line.length > 0 ? ` ${line}` : ""}`;
let markerLine = "";
if (Array.isArray(hasMarker)) {
const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " "), numberOfMarkers = hasMarker[1] || 1;
markerLine = [
`
`,
gutter.replace(/\d/g, " "),
" ",
markerSpacing,
"^".repeat(numberOfMarkers)
].join(""), lastMarkerLine && message && (markerLine += " " + message);
}
return [">", gutter, line.length > 0 ? ` ${line}` : "", markerLine].join("");
}).join(`
`);
}
function getMarkerLines(loc, source) {
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;
let start = Math.max(startLine - (linesAbove + 1), 0), end = Math.min(source.length, endLine + linesBelow);
startLine === -1 && (start = 0), endLine === -1 && (end = source.length);
const lineDiff = endLine - startLine, markerLines = {};
if (lineDiff)
for (let i = 0; i <= lineDiff; i++) {
const lineNumber = i + startLine;
if (!startColumn)
markerLines[lineNumber] = !0;
else if (i === 0) {
const sourceLength = source[lineNumber - 1].length;
markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
} else if (i === lineDiff)
markerLines[lineNumber] = [0, endColumn];
else {
const sourceLength = source[lineNumber - i].length;
markerLines[lineNumber] = [0, sourceLength];
}
}
else
startColumn === endColumn ? startColumn ? markerLines[startLine] = [startColumn, 0] : markerLines[startLine] = !0 : markerLines[startLine] = [startColumn, endColumn - startColumn];
return { start, end, markerLines };
}
function columnToLine(column, lines) {
let offset = 0;
for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1;
if (offset + lineLength > column)
return {
line: i + 1,
// 1-based line
column: column - offset
// 0-based column
};
offset += lineLength;
}
return {
line: lines.length,
column: lines[lines.length - 1]?.length ?? 0
};
}
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
class ClientError extends Error {
response;
statusCode = 400;
responseBody;
details;
constructor(res, context) {
const props = extractErrorProps(res, context);
super(props.message), Object.assign(this, props);
}
}
class ServerError extends Error {
response;
statusCode = 500;
responseBody;
details;
constructor(res) {
const props = extractErrorProps(res);
super(props.message), Object.assign(this, props);
}
}
function extractErrorProps(res, context) {
const body = res.body, props = {
response: res,
statusCode: res.statusCode,
responseBody: stringifyBody(body, res),
message: "",
details: void 0
};
if (!isRecord(body))
return props.message = httpErrorMessage(res, body), props;
const error = body.error;
if (typeof error == "string" && typeof body.message == "string")
return props.message = `${error} - ${body.message}`, props;
if (typeof error != "object" || error === null)
return typeof error == "string" ? props.message = error : typeof body.message == "string" ? props.message = body.message : props.message = httpErrorMessage(res, body), props;
if (isMutationError(error) || isActionError(error)) {
const allItems = error.items || [], items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map((item) => item.error?.description).filter(Boolean);
let itemsStr = items.length ? `:
- ${items.join(`
- `)}` : "";
return allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE && (itemsStr += `
...and ${allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE} more`), props.message = `${error.description}${itemsStr}`, props.details = body.error, props;
}
if (isQueryParseError(error)) {
const tag = context?.options?.query?.tag;
return props.message = formatQueryParseError(error, tag), props.details = body.error, props;
}
return "description" in error && typeof error.description == "string" ? (props.message = error.description, props.details = error, props) : (props.message = httpErrorMessage(res, body), props);
}
function isMutationError(error) {
return "type" in error && error.type === "mutationError" && "description" in error && typeof error.description == "string";
}
function isActionError(error) {
return "type" in error && error.type === "actionError" && "description" in error && typeof error.description == "string";
}
function isQueryParseError(error) {
return isRecord(error) && error.type === "queryParseError" && typeof error.query == "string" && typeof error.start == "number" && typeof error.end == "number";
}
function formatQueryParseError(error, tag) {
const { query, start, end, description } = error;
if (!query || typeof start > "u")
return `GROQ query parse error: ${description}`;
const withTag = tag ? `
Tag: ${tag}` : "";
return `GROQ query parse error:
${codeFrame(query, { start, end }, description)}${withTag}`;
}
function httpErrorMessage(res, body) {
const details = typeof body == "string" ? ` (${sliceWithEllipsis(body, 100)})` : "", statusMessage = res.statusMessage ? ` ${res.statusMessage}` : "";
return `${res.method}-request to ${res.url} resulted in HTTP ${res.statusCode}${statusMessage}${details}`;
}
function stringifyBody(body, res) {
return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
}
function sliceWithEllipsis(str, max) {
return str.length > max ? `${str.slice(0, max)}\u2026` : str;
}
class CorsOriginError extends Error {
projectId;
addOriginUrl;
constructor({ projectId }) {
super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId;
const url = new URL(`https://sanity.io/manage/project/${projectId}/api`);
if (typeof location < "u") {
const { origin } = location;
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}`;
} else
this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
}
}
const httpError = {
onResponse: (res, context) => {
if (res.statusCode >= 500)
throw new ServerError(res);
if (res.statusCode >= 400)
throw new ClientError(res, context);
return res;
}
};
function printWarnings() {
const seen = {};
return {
onResponse: (res) => {
const warn = res.headers["x-sanity-warning"], warnings = Array.isArray(warn) ? warn : [warn];
for (const msg of warnings)
!msg || seen[msg] || (seen[msg] = !0, console.warn(msg));
return res;
}
};
}
function defineHttpRequest(envMiddleware) {
return getIt([
retry({ shouldRetry }),
...envMiddleware,
printWarnings(),
jsonRequest(),
jsonResponse(),
progress(),
httpError,
observable({ implementation: Observable })
]);
}
function shouldRetry(err, attempt, options) {
if (options.maxRetries === 0) return !1;
const isSafe = options.method === "GET" || options.method === "HEAD", isQuery2 = (options.uri || options.url).startsWith("/data/query"), isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
return (isSafe || isQuery2) && isRetriableResponse ? !0 : retry.shouldRetry(err, attempt, options);
}
class ConnectionFailedError extends Error {
name = "ConnectionFailedError";
}
class DisconnectError extends Error {
name = "DisconnectError";
reason;
constructor(message, reason, options = {}) {
super(message, options), this.reason = reason;
}
}
class ChannelError extends Error {
name = "ChannelError";
data;
constructor(message, data) {
super(message), this.data = data;
}
}
class MessageError extends Error {
name = "MessageError";
data;
constructor(message, data, options = {}) {
super(message, options), this.data = data;
}
}
class MessageParseError extends Error {
name = "MessageParseError";
}
const REQUIRED_EVENTS = ["channelError", "disconnect"];
function connectEventSource(initEventSource, events) {
return defer(() => {
const es = initEventSource();
return isObservable(es) ? es : of(es);
}).pipe(mergeMap((es) => connectWithESInstance(es, events)));
}
function connectWithESInstance(es, events) {
return new Observable((observer) => {
const emitOpen = events.includes("open"), emitReconnect = events.includes("reconnect");
function onError(evt) {
if ("data" in evt) {
const [parseError, event] = parseEvent(evt);
observer.error(
parseError ? new MessageParseError("Unable to parse EventSource error message", { cause: event }) : new MessageError((event?.data).message, event)
);
return;
}
es.readyState === es.CLOSED ? observer.error(new ConnectionFailedError("EventSource connection failed")) : emitReconnect && observer.next({ type: "reconnect" });
}
function onOpen() {
observer.next({ type: "open" });
}
function onMessage(message) {
const [parseError, event] = parseEvent(message);
if (parseError) {
observer.error(
new MessageParseError("Unable to parse EventSource message", { cause: parseError })
);
return;
}
if (message.type === "channelError") {
const tag = new URL(es.url).searchParams.get("tag");
observer.error(new ChannelError(extractErrorMessage(event?.data, tag), event.data));
return;
}
if (message.type === "disconnect") {
observer.error(
new DisconnectError(
`Server disconnected client: ${event.data?.reason || "unknown error"}`
)
);
return;
}
observer.next({
type: message.type,
id: message.lastEventId,
...event.data ? { data: event.data } : {}
});
}
es.addEventListener("error", onError), emitOpen && es.addEventListener("open", onOpen);
const cleanedEvents = [.../* @__PURE__ */ new Set([...REQUIRED_EVENTS, ...events])].filter((type) => type !== "error" && type !== "open" && type !== "reconnect");
return cleanedEvents.forEach((type) => es.addEventListener(type, onMessage)), () => {
es.removeEventListener("error", onError), emitOpen && es.removeEventListener("open", onOpen), cleanedEvents.forEach((type) => es.removeEventListener(type, onMessage)), es.close();
};
});
}
function parseEvent(message) {
try {
const data = typeof message.data == "string" && JSON.parse(message.data);
return [
null,
{
type: message.type,
id: message.lastEventId,
...isEmptyObject(data) ? {} : { data }
}
];
} catch (err) {
return [err, null];
}
}
function extractErrorMessage(err, tag) {
const error = err.error;
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";
}
function isEmptyObject(data) {
for (const _ in data)
return !1;
return !0;
}
function getSelection(sel) {
if (typeof sel == "string")
return { id: sel };
if (Array.isArray(sel))
return { query: "*[_id in $ids]", params: { ids: sel } };
if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
const selectionOpts = [
"* Document ID (<docId>)",
"* Array of document IDs",
"* Object containing `query`"
].join(`
`);
throw new Error(`Unknown selection - must be one of:
${selectionOpts}`);
}
class BasePatch {
selection;
operations;
constructor(selection, operations = {}) {
this.selection = selection, this.operations = operations;
}
/**
* Sets the given attributes to the document. Does NOT merge objects.
* The operation is added to the current patch, ready to be commited by `commit()`
*
* @param attrs - Attributes to set. To set a deep attribute, use JSONMatch, eg: \{"nested.prop": "value"\}
*/
set(attrs) {
return this._assign("set", attrs);
}
/**
* Sets the given attributes to the document if they are not currently set. Does NOT merge objects.
* The operation is added to the current patch, ready to be commited by `commit()`
*
* @param attrs - Attributes to set. To set a deep attribute, use JSONMatch, eg: \{"nested.prop": "value"\}
*/
setIfMissing(attrs) {
return this._assign("setIfMissing", attrs);
}
/**
* Performs a "diff-match-patch" operation on the string attributes provided.
* The operation is added to the current patch, ready to be commited by `commit()`
*
* @param attrs - Attributes to perform operation on. To set a deep attribute, use JSONMatch, eg: \{"nested.prop": "dmp"\}
*/
diffMatchPatch(attrs) {
return validateObject("diffMatchPatch", attrs), this._assign("diffMatchPatch", attrs);
}
/**
* Unsets the attribute paths provided.
* The operation is added to the current patch, ready to be commited by `commit()`
*
* @param attrs - Attribute paths to unset.
*/
unset(attrs) {
if (!Array.isArray(attrs))
throw new Error("unset(attrs) takes an array of attributes to unset, non-array given");
return this.operations = Object.assign({}, this.operations, { unset: attrs }), this;
}
/**
* Increment a numeric value. Each entry in the argument is either an attribute or a JSON path. The value may be a positive or negative integer or floating-point value. The operation will fail if target value is not a numeric value, or doesn't exist.
*
* @param attrs - Object of attribute paths to increment, values representing the number to increment by.
*/
inc(attrs) {
return this._assign("inc", attrs);
}
/**
* Decrement a numeric value. Each entry in the argument is either an attribute or a JSON path. The value may be a positive or negative integer or floating-point value. The operation will fail if target value is not a numeric value, or doesn't exist.
*
* @param attrs - Object of attribute paths to decrement, values representing the number to decrement by.
*/
dec(attrs) {
return this._assign("dec", attrs);
}
/**
* Provides methods for modifying arrays, by inserting, appending and replacing elements via a JSONPath expression.
*
* @param at - Location to insert at, relative to the given selector, or 'replace' the matched path
* @param selector - JSONPath expression, eg `comments[-1]` or `blocks[_key=="abc123"]`
* @param items - Array of items to insert/replace
*/
insert(at, selector, items) {
return validateInsert(at, selector, items), this._assign("insert", { [at]: selector, items });
}
/**
* Append the given items to the array at the given JSONPath
*
* @param selector - Attribute/path to append to, eg `comments` or `person.hobbies`
* @param items - Array of items to append to the array
*/
append(selector, items) {
return this.insert("after", `${selector}[-1]`, items);
}
/**
* Prepend the given items to the array at the given JSONPath
*
* @param selector - Attribute/path to prepend to, eg `comments` or `person.hobbies`
* @param items - Array of items to prepend to the array
*/
prepend(selector, items) {
return this.insert("before", `${selector}[0]`, items);
}
/**
* Change the contents of an array by removing existing elements and/or adding new elements.
*
* @param selector - Attribute or JSONPath expression for array
* @param start - Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.x
* @param deleteCount - An integer indicating the number of old array elements to remove.
* @param items - The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.
*/
splice(selector, start, deleteCount, items) {
const delAll = typeof deleteCount > "u" || deleteCount === -1, startIndex = start < 0 ? start - 1 : start, delCount = delAll ? -1 : Math.max(0, start + deleteCount), delRange = startIndex < 0 && delCount >= 0 ? "" : delCount, rangeSelector = `${selector}[${startIndex}:${delRange}]`;
return this.insert("replace", rangeSelector, items || []);
}
/**
* Adds a revision clause, preventing the document from being patched if the `_rev` property does not match the given value
*
* @param rev - Revision to lock the patch to
*/
ifRevisionId(rev) {
return this.operations.ifRevisionID = rev, this;
}
/**
* Return a plain JSON representation of the patch
*/
serialize() {
return { ...getSelection(this.selection), ...this.operations };
}
/**
* Return a plain JSON representation of the patch
*/
toJSON() {
return this.serialize();
}
/**
* Clears the patch of all operations
*/
reset() {
return this.operations = {}, this;
}
_assign(op, props, merge2 = !0) {
return validateObject(op, props), this.operations = Object.assign({}, this.operations, {
[op]: Object.assign({}, merge2 && this.operations[op] || {}, props)
}), this;
}
_set(op, props) {
return this._assign(op, props, !1);
}
}
class ObservablePatch extends BasePatch {
#client;
constructor(selection, operations, client) {
super(selection, operations), this.#client = client;
}
/**
* Clones the patch
*/
clone() {
return new ObservablePatch(this.selection, { ...this.operations }, this.#client);
}
commit(options) {
if (!this.#client)
throw new Error(
"No `client` passed to patch, either provide one or pass the patch to a clients `mutate()` method"
);
const returnFirst = typeof this.selection == "string", opts = Object.assign({ returnFirst, returnDocuments: !0 }, options);
return this.#client.mutate({ patch: this.serialize() }, opts);
}
}
class Patch extends BasePatch {
#client;
constructor(selection, operations, client) {
super(selection, operations), this.#client = client;
}
/**
* Clones the patch
*/
clone() {
return new Patch(this.selection, { ...this.operations }, this.#client);
}
commit(options) {
if (!this.#client)
throw new Error(
"No `client` passed to patch, either provide one or pass the patch to a clients `mutate()` method"
);
const returnFirst = typeof this.selection == "string", opts = Object.assign({ returnFirst, returnDocuments: !0 }, options);
return this.#client.mutate({ patch: this.serialize() }, opts);
}
}
const defaultMutateOptions = { returnDocuments: !1 };
class BaseTransaction {
operations;
trxId;
constructor(operations = [], transactionId) {
this.operations = operations, this.trxId = transactionId;
}
/**
* Creates a new Sanity document. If `_id` is provided and already exists, the mutation will fail. If no `_id` is given, one will automatically be generated by the database.
* The operation is added to the current transaction, ready to be commited by `commit()`
*
* @param doc - Document to create. Requires a `_type` property.
*/
create(doc) {
return validateObject("create", doc), this._add({ create: doc });
}
/**
* Creates a new Sanity document. If a document with the same `_id` already exists, the create operation will be ignored.
* The operation is added to the current transaction, ready to be commited by `commit()`
*
* @param doc - Document to create if it does not already exist. Requires `_id` and `_type` properties.
*/
createIfNotExists(doc) {
const op = "createIfNotExists";
return validateObject(op, doc), requireDocumentId(op, doc), this._add({ [op]: doc });
}
/**
* Creates a new Sanity document, or replaces an existing one if the same `_id` is already used.
* The operation is added to the current transaction, ready to be commited by `commit()`
*
* @param doc - Document to create or replace. Requires `_id` and `_type` properties.
*/
createOrReplace(doc) {
const op = "createOrReplace";
return validateObject(op, doc), requireDocumentId(op, doc), this._add({ [op]: doc });
}
/**
* Deletes the document with the given document ID
* The operation is added to the current transaction, ready to be commited by `commit()`
*
* @param documentId - Document ID to delete
*/
delete(documentId) {
return validateDocumentId("delete", documentId), this._add({ delete: { id: documentId } });
}
transactionId(id) {
return id ? (this.trxId = id, this) : this.trxId;
}
/**
* Return a plain JSON representation of the transaction
*/
serialize() {
return [...this.operations];
}
/**
* Return a plain JSON representation of the transaction
*/
toJSON() {
return this.serialize();
}
/**
* Clears the transaction of all operations
*/
reset() {
return this.operations = [], this;
}
_add(mut) {
return this.operations.push(mut), this;
}
}
class Transaction extends BaseTransaction {
#client;
constructor(operations, client, transactionId) {
super(operations, transactionId), this.#client = client;
}
/**
* Clones the transaction
*/
clone() {
return new Transaction([...this.operations], this.#client, this.trxId);
}
commit(options) {
if (!this.#client)
throw new Error(
"No `client` passed to transaction, either provide one or pass the transaction to a clients `mutate()` method"
);
return this.#client.mutate(
this.serialize(),
Object.assign({ transactionId: this.trxId }, defaultMutateOptions, options || {})
);
}
patch(patchOrDocumentId, patchOps) {
const isBuilder = typeof patchOps == "function", isPatch = typeof patchOrDocumentId != "string" && patchOrDocumentId instanceof Patch, isMutationSelection = typeof patchOrDocumentId == "object" && ("query" in patchOrDocumentId || "id" in patchOrDocumentId);
if (isPatch)
return this._add({ patch: patchOrDocumentId.serialize() });
if (isBuilder) {
const patch = patchOps(new Patch(patchOrDocumentId, {}, this.#client));
if (!(patch instanceof Patch))
throw new Error("function passed to `patch()` must return the patch");
return this._add({ patch: patch.serialize() });
}
if (isMutationSelection) {
const patch = new Patch(patchOrDocumentId, patchOps || {}, this.#client);
return this._add({ patch: patch.serialize() });
}
return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
}
}
class ObservableTransaction extends BaseTransaction {
#client;
constructor(operations, client, transactionId) {
super(operations, transactionId), this.#client = client;
}
/**
* Clones the transaction
*/
clone() {
return new ObservableTransaction([...this.operations], this.#client, this.trxId);
}
commit(options) {
if (!this.#client)
throw new Error(
"No `client` passed to transaction, either provide one or pass the transaction to a clients `mutate()` method"
);
return this.#client.mutate(
this.serialize(),
Object.assign({ transactionId: this.trxId }, defaultMutateOptions, options || {})
);
}
patch(patchOrDocumentId, patchOps) {
const isBuilder = typeof patchOps == "function";
if (typeof patchOrDocumentId != "string" && patchOrDocumentId instanceof ObservablePatch)
return this._add({ patch: patchOrDocumentId.serialize() });
if (isBuilder) {
const patch = patchOps(new ObservablePatch(patchOrDocumentId, {}, this.#client));
if (!(patch instanceof ObservablePatch))
throw new Error("function passed to `patch()` must return the patch");
return this._add({ patch: patch.serialize() });
}
return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
}
}
const projectHeader = "X-Sanity-Project-ID";
function requestOptions(config, overrides = {}) {
const headers2 = {};
config.headers && Object.assign(headers2, config.headers);
const token = overrides.token || config.token;
token && (headers2.Authorization = `Bearer ${token}`), !overrides.useGlobalApi && !config.useProjectHostname && config.projectId && (headers2[projectHeader] = config.projectId);
const withCredentials = !!(typeof overrides.withCredentials > "u" ? config.withCredentials : overrides.withCredentials), timeout = typeof overrides.timeout > "u" ? config.timeout : overrides.timeout;
return Object.assign({}, overrides, {
headers: Object.assign({}, headers2, overrides.headers || {}),
timeout: typeof timeout > "u" ? 5 * 60 * 1e3 : timeout,
proxy: overrides.proxy || config.proxy,
json: !0,
withCredentials,
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
});
}
const encodeQueryString = ({
query,
params = {},
options = {}
}) => {
const searchParams = new URLSearchParams(), { tag, includeMutations, returnQuery, ...opts } = options;
tag && searchParams.append("tag", tag), searchParams.append("query", query);
for (const [key, value] of Object.entries(params))
value !== void 0 && searchParams.append(`$${key}`, JSON.stringify(value));
for (const [key, value] of Object.entries(opts))
value && searchParams.append(key, `${value}`);
return returnQuery === !1 && searchParams.append("returnQuery", "false"), includeMutations === !1 && searchParams.append("includeMutations", "false"), `?${searchParams}`;
}, excludeFalsey = (param, defValue) => param === !1 ? void 0 : typeof param > "u" ? defValue : param, getMutationQuery = (options = {}) => ({
dryRun: options.dryRun,
returnIds: !0,
returnDocuments: excludeFalsey(options.returnDocuments, !0),
visibility: options.visibility || "sync",
autoGenerateArrayKeys: options.autoGenerateArrayKeys,
skipCrossDatasetReferenceValidation: options.skipCrossDatasetReferenceValidation
}), isResponse = (event) => event.type === "response", getBody = (event) => event.body, indexBy = (docs, attr) => docs.reduce((indexed, doc) => (indexed[attr(doc)] = doc, indexed), /* @__PURE__ */ Object.create(null)), getQuerySizeLimit = 11264;
function _fetch(client, httpRequest, _stega, query, _params = {}, options = {}) {
const stega = "stega" in options ? {
..._stega || {},
...typeof options.stega == "boolean" ? { enabled: options.stega } : options.stega || {}
} : _stega, params = stega.enabled ? stegaClean(_params) : _params, mapResponse = options.filterResponse === !1 ? (res) => res : (res) => res.result, { cache, next, ...opts } = {
// Opt out of setting a `signal` on an internal `fetch` if one isn't provided.
// This is necessary in React Server Components to avoid opting out of Request Memoization.
useAbortSignal: typeof options.signal < "u",
// Set `resultSourceMap' when stega is enabled, as it's required for encoding.
resultSourceMap: stega.enabled ? "withKeyArraySelector" : options.resultSourceMap,
...options,
// Default to not returning the query, unless `filterResponse` is `false`,
// or `returnQuery` is explicitly set. `true` is the default in Content Lake, so skip if truthy
returnQuery: options.filterResponse === !1 && options.returnQuery !== !1
}, reqOpts = typeof cache < "u" || typeof next < "u" ? { ...opts, fetch: { cache, next } } : opts, $request = _dataRequest(client, httpRequest, "query", { query, params }, reqOpts);
return stega.enabled ? $request.pipe(
combineLatestWith(
from(
import("./_chunks-es/stegaEncodeSourceMap.js").then(function(n) {
return n.stegaEncodeSourceMap$1;
}).then(
({ stegaEncodeSourceMap }) => stegaEncodeSourceMap
)
)
),
map(
([res, stegaEncodeSourceMap]) => {
const result = stegaEncodeSourceMap(res.result, res.resultSourceMap, stega);
return mapResponse({ ...res, result });
}
)
) : $request.pipe(map(mapResponse));
}
function _getDocument(client, httpRequest, id, opts = {}) {
const docId = (() => {
if (!opts.releaseId)
return id;
const versionId = getVersionFromId(id);
if (!versionId) {
if (isDraftId(id))
throw new Error(
`The document ID (\`${id}\`) is a draft, but \`options.releaseId\` is set as \`${opts.releaseId}\``
);
return getVersionId(id, opts.releaseId);
}
if (versionId !== opts.releaseId)
throw new Error(
`The document ID (\`${id}\`) is already a version of \`${versionId}\` release, but this does not match the provided \`options.releaseId\` (\`${opts.releaseId}\`)`
);
return id;
})(), options = {
uri: _getDataUrl(client, "doc", docId),
json: !0,
tag: opts.tag,
signal: opts.signal
};
return _requestObservable(client, httpRequest, options).pipe(
filter(isResponse),
map((event) => event.body.documents && event.body.documents[0])
);
}
function _getDocuments(client, httpRequest, ids, opts = {}) {
const options = {
uri: _getDataUrl(client, "doc", ids.join(",")),
json: !0,
tag: opts.tag,
signal: opts.signal
};
return _requestObservable(client, httpRequest, options).pipe(
filter(isResponse),
map((event) => {
const indexed = indexBy(event.body.documents || [], (doc) => doc._id);
return ids.map((id) => indexed[id] || null);
})
);
}
function _getReleaseDocuments(client, httpRequest, releaseId, opts = {}) {
return _dataRequest(
client,
httpRequest,
"query",
{
query: "*[sanity::partOfRelease($releaseId)]",
params: {
releaseId
}
},
opts
);
}
function _createIfNotExists(client, httpRequest, doc, options) {
return requireDocumentId("createIfNotExists", doc), _create(client, httpRequest, doc, "createIfNotExists", options);
}
function _createOrReplace(client, httpRequest, doc, options) {
return requireDocumentId("createOrReplace", doc), _create(client, httpRequest, doc, "createOrReplace", options);
}
function _createVersion(client, httpRequest, doc, publishedId, options) {
return requireDocumentId("createVersion", doc), requireDocumentType("createVersion", doc), _action(client, httpRequest, {
actionType: "sanity.action.document.version.create",
publishedId,
document: doc
}, options);
}
function _delete(client, httpRequest, selection, options) {
return _dataRequest(
client,
httpRequest,
"mutate",
{ mutations: [{ delete: getSelection(selection) }] },
options
);
}
function _discardVersion(client, httpRequest, versionId, purge = !1, options) {
return _action(client, httpRequest, {
actionType: "sanity.action.document.version.discard",
versionId,
purge
}, options);
}
function _replaceVersion(client, httpRequest, doc, options) {
return requireDocumentId("replaceVersion", doc), requireDocumentType("replaceVersion", doc), _action(client, httpRequest, {
actionType: "sanity.action.document.version.replace",
document: doc
}, options);
}
function _unpublishVersion(client, httpRequest, versionId, publishedId, options) {
return _action(client, httpRequest, {
actionType: "sanity.action.document.version.unpublish",
versionId,
publishedId
}, options);
}
function _mutate(client, httpRequest, mutations, options) {
let mut;
mutations instanceof Patch || mutations instanceof ObservablePatch ? mut = { patch: mutations.serialize() } : mutations instanceof Transaction || mutations instanceof ObservableTransaction ? mut = mutations.serialize() : mut = mutations;
const muts = Array.isArray(mut) ? mut : [mut], transactionId = options && options.transactionId || void 0;
return _dataRequest(client, httpRequest, "mutate", { mutations: muts, transactionId }, options);
}
function _action(client, httpRequest, actions, options) {
const acts = Array.isArray(actions) ? actions : [actions], transactionId = options && options.transactionId || void 0, skipCrossDatasetReferenceValidation = options && options.skipCrossDatasetReferenceValidation || void 0, dryRun = options && options.dryRun || void 0;
return _dataRequest(
client,
httpRequest,
"actions",
{ actions: acts, transactionId, skipCrossDatasetReferenceValidation, dryRun },
options
);
}
function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
const isMutation = endpoint === "mutate", isAction = endpoint === "actions", isQuery2 = endpoint === "query", strQuery = isMutation || isAction ? "" : encodeQueryString(body), useGet = !isMutation && !isAction && strQuery.length < getQuerySizeLimit, stringQuery = useGet ? strQuery : "", returnFirst = options.returnFirst, { timeout, token, tag, headers: headers2, returnQuery, lastLiveEventId, cacheMode } = options, uri = _getDataUrl(client, endpoint, stringQuery), reqOptions = {
method: useGet ? "GET" : "POST",
uri,
json: !0,
body: useGet ? void 0 : body,
query: isMutation && getMutationQuery(options),
timeout,
headers: headers2,
token,
tag,
returnQuery,
perspective: options.perspective,
resultSourceMap: options.resultSourceMap,
lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
cacheMode,
canUseCdn: isQuery2,
signal: options.signal,
fetch: options.fetch,
useAbortSignal: options.useAbortSignal,
useCdn: options.useCdn
};
return _requestObservable(client, httpRequest, reqOptions).pipe(
filter(isResponse),
map(getBody),
map((res) => {
if (!isMutation)
return res;
const results = res.results || [];
if (options.returnDocuments)
return returnFirst ? results[0] && results[0].document : results.map((mut) => mut.document);
const key = returnFirst ? "documentId" : "documentIds", ids = returnFirst ? results[0] && results[0].id : results.map((mut) => mut.id);
return {
transactionId: res.transactionId,
results,
[key]: ids
};
})
);
}
function _create(client, httpRequest, doc, op, options = {}) {
const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: !0, returnDocuments: !0 }, options);
return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
}
const hasDataConfig = (client) => client.config().dataset !== void 0 && client.config().projectId !== void 0 || client.config()["~experimental_resource"] !== void 0, isQuery = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "query")), isMutate = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "mutate")), isDoc = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "doc", "")), isListener = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "listen")), isHistory = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "history", "")), isData = (client, uri) => uri.startsWith("/data/") || isQuery(client, uri) || isMutate(client, uri) || isDoc(client, uri) || isListener(client, uri) || isHistory(client, uri);
function _requestObservable(client, httpRequest, options) {
const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
const perspectiveOption = options.perspective || config.perspective;
typeof perspectiveOption < "u" && (perspectiveOption === "previewDrafts" && printPreviewDraftsDeprecationWarning(), validateApiPerspective(perspectiveOption), options.query = {
perspective: Array.isArray(perspectiveOption) ? perspectiveOption.join(",") : perspectiveOption,
...options.query
}, (Array.isArray(perspectiveOption) && perspectiveOption.length > 0 || // previewDrafts was renamed to drafts, but keep for backwards compat
perspectiveOption === "previewDrafts" || perspectiveOption === "drafts") && useCdn && (useCdn = !1, printCdnPreviewDraftsWarning())), options.lastLiveEventId && (options.query = { ...options.query, lastLiveEventId: options.lastLiveEventId }), options.returnQuery === !1 && (options.query = { returnQuery: "false", ...options.query }), useCdn && options.cacheMode == "noStale" && (options.query = { cacheMode: "noStale", ...options.query });
}
const reqOptions = requestOptions(
config,
Object.assign({}, options, {
url: _getUrl(client, uri, useCdn)
})
), request = new Observable(
(subscriber) => httpRequest(reqOptions, config.requester).subscribe(subscriber)
);
return options.signal ? request.pipe(_withAbortSignal(options.signal)) : request;
}
function _request(client, httpRequest, options) {
return _requestObservable(client, httpRequest, options).pipe(
filter((event) => event.type === "response"),
map((event) => event.body)
);
}
function _getDataUrl(client, operation, path) {
const config = client.config();
if (config["~experimental_resource"]) {
resourceConfig(config);
const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
}
const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
}
function _getUrl(client, uri, canUseCdn = !1) {
const { url, cdnUrl } = client.config();
return `${canUseCdn ? cdnUrl : url}/${uri.replace(/^\//, "")}`;
}
function _withAbortSignal(signal) {
return (input) => new Observable((observer) => {
const abort = () => observer.error(_createAbortError(signal));
if (signal && signal.aborted) {
abort();
return;
}
const subscription = input.subscribe(observer);
return signal.addEventListener("abort", abort), () => {
signal.removeEventListener("abort", abort), subscription.unsubscribe();
};
});
}
const isDomExceptionSupported = !!globalThis.DOMException;
function _createAbortError(signal) {
if (isDomExceptionSupported)
return new DOMException(signal?.reason ?? "The operation was aborted.", "AbortError");
const error = new Error(signal?.reason ?? "The operation was aborted.");
return error.name = "AbortError", error;
}
const resourceDataBase = (config) => {
if (!config["~experimental_resource"])
throw new Error("`resource` must be provided to perform resource queries");
const { type, id } = config["~experimental_resource"];
switch (type) {
case "dataset": {
const segments = id.split(".");
if (segments.length !== 2)
throw new Error('Dataset ID must be in the format "project.dataset"');
return `/projects/${segments[0]}/datasets/${segments[1]}`;
}
case "canvas":
return `/canvases/${id}`;
case "media-library":
return `/media-libraries/${id}`;
case "dashboard":
return `/dashboards/${id}`;
default:
throw new Error(`Unsupported resource type: ${type.toString()}`);
}
};
function _generate(client, httpRequest, request) {
const dataset2 = hasDataset(client.config());
return _request(client, httpRequest, {
method: "POST",
uri: `/agent/action/generate/${dataset2}`,
body: request
});
}
function _patch(client, httpRequest, request) {
const dataset2 = hasDataset(client.config());
return _request(client, httpRequest, {
method: "POST",
uri: `/agent/action/patch/${dataset2}`,
body: request
});
}
function _prompt(client, httpRequest, request) {
const dataset2 = hasDataset(client.config());
return _request(client, httpRequest, {
method: "POST",
uri: `/agent/action/prompt/${dataset2}`,
body: request
});
}
function _transform(client, httpRequest, request) {
const dataset2 = hasDataset(client.config());
return _request(client, httpRequest, {
method: "POST",
uri: `/agent/action/transform/${dataset2}`,
body: request
});
}
function _translate(client, httpRequest, request) {
const dataset2 = hasDataset(client.config());
return _request(client, httpRequest, {
method: "POST",
uri: `/agent/action/translate/${dataset2}`,
body: request
});
}
class ObservableAgentsActionClient {
#client;
#httpRequest;
constructor(client, httpRequest) {
this.#client = client, this.#httpRequest = httpRequest;
}
/**
* Run an instruction to generate content in a target document.
* @param request - instruction request
*/
generate(request) {
return _generate(this.#client, this.#httpRequest, request);
}
/**
* Transform a target document based on a source.
* @param request - translation request
*/
transform(request) {
return _transform(this.#client, this.#httpRequest, request);
}
/**
* Translate a target document based on a source.
* @param request - translation request
*/
translate(request) {
return _translate(this.#client, this.#httpRequest, request);
}
}
class AgentActionsClient {
#client;
#httpRequest;
constructor(client, httpRequest) {
this.#client = client, this.#httpRequest = httpRequest;
}
/**
* Run an instruction to generate content in a target document.
* @param request - instruction request
*/
generate(request) {
return lastValueFrom(_generate(this.#client, this.#httpRequest, request));
}
/**
* Transform a target document based on a source.
* @param request - translation request
*/
transform(request) {
return lastValueFrom(_transform(this.#client, this.#httpRequest, request));
}
/**
* Translate a target document based on a source.
* @param request - translation request
*/
translate(request) {
return lastValueFrom(_translate(this.#client, this.#httpRequest, request));
}
/**
* Run a raw instruction and return the result either as text or json
* @param request - prompt request
*/
prompt(request) {
return lastValueFrom(_prompt(this.#client, this.#httpRequest, request));
}
/**
* Patch a document using a schema aware API.
* Does not use an LLM, but uses the schema to ensure paths and values matches the schema.
* @param request - instruction request
*/
patch(request) {
return lastValueFrom(_patch(this.#client, this.#httpRequest, request));
}
}
class ObservableAssetsClient {
#client;
#httpRequest;
constructor(client, httpRequest) {
this.#client = client, this.#httpRequest = httpRequest;
}
upload(assetType, body, options) {
return _upload(this.#client, this.#httpRequest, assetType, body, options);
}
}
class AssetsClient {
#client;
#httpRequest;
constructor(client, httpRequest) {
this.#client = client, this.#httpRequest = httpRequest;
}
upload(assetType, body, options) {
const observable2 = _upload(this.#client, this.#httpRequest, assetType, body, options);
return lastValueFrom(
observable2.pipe(
filter((event) => event.type === "response"),
map(
(event) => event.body.document
)
)
);
}
}
function _upload(client, httpRequest, assetType, body, opts = {}) {
validateAssetType(assetType);
let meta = opts.extract || void 0;
meta && !meta.length && (meta = ["none"]);
const config = client.config(), options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
label,
title,
description,
filename,
meta,
creditLine
};
return source && (query.sourceId = source.id, query.sourceName = source.name, query.sourceUrl = source.url), _requestObservable(client, httpRequest, {
tag,
method: "POST",
timeout: options.timeout || 0,
uri: buildAssetUploadUrl(config, assetType),
headers: options.contentType ? { "Content-Type": options.contentType } : {},
query,
body
});
}
function buildAssetUploadUrl(config, assetType) {
const assetTypeEndpoint = assetType === "image" ? "images" : "files";
if (config["~experimental_resource"]) {
const { type, id } = config["~experimental_resource"];
switch (type) {
case "dataset":
throw new Error(
"Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
);
case "canvas":
return `/canvases/${id}/assets/${assetTypeEndpoint}`;
case "media-library":
return `/media-libraries/${id}/upload`;
case "dashboard":
return `/dashboards/${id}/assets/${assetTypeEndpoint}`;
default:
throw new Error(`Unsupported resource type: ${type.toString()}`);
}
}
const dataset2 = hasDataset(config);
return `assets/${assetTypeEndpoint}/${dataset2}`;
}
function optionsFromFile(opts, file) {
return typeof File > "u" || !(file instanceof File) ? opts : Object.assign(
{
filename: opts.preserveFilename === !1 ? void 0 : file.name,
contentType: file.type
},
opts
);
}
var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = defer(() => import("@sanity/eventsource")).pipe(
map(({ default: EventSource2 }) => EventSource2),
shareReplay(1)
);
function reconnectOnConnectionFailure() {
return function(source) {
return source.pipe(
catchError((err, caught) => err instanceof ConnectionFailedError ? concat(of({ type: "reconnect" }), timer(1e3).pipe(mergeMap(() => caught))) : throwError(() => err))
);
};
}
const MAX_URL_LENGTH = 14800, possibleOptions = [
"includePreviousRevision",
"includeResult",
"includeMutations",
"includeAllVersions",
"visibility",
"effectFormat",
"tag"
], defaultOptions = {
includeResult: !0
};
function _listen(query, params, opts = {}) {
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)}`;
if (uri.length > MAX_URL_LENGTH)
return throwError(() => new Error("Query too large for listener"));
const listenFor = options.events ? options.events : ["mutation"], esOptions = {};
return withCredentials && (esOptions.withCredentials = !0), (token || configHeaders) && (esOptions.headers = {}, token && (esOptions.header