@sanity/client
Version:
Client for retrieving, creating and patching data from Sanity.io
89 lines (88 loc) • 4.52 kB
JavaScript
import { r as validateApiPerspective } from "./config-CgJ16jET.js";
import { S as toString, _ as isPublishedId, a as resolveMapping, c as parseJsonPath, d as VERSION_FOLDER, f as getDraftId, g as isDraftId, h as getVersionId, i as walkMap, l as studioPathToJsonPath, m as getVersionFromId, o as jsonPath, p as getPublishedId, r as createEditUrl, s as jsonPathToStudioPath, t as resolveEditInfo, u as DRAFTS_FOLDER, v as isVersionId, x as studioPath_exports, y as get } from "./resolveEditInfo-Cz-smq3a.js";
/**
* This resolves the perspectives to how documents should be resolved when applying optimistic updates,
* like in `applySourceDocuments`.
* @internal
*/
function resolvePerspectives(perspective) {
if (validateApiPerspective(perspective), Array.isArray(perspective)) return perspective.includes("published") ? perspective : [...perspective, "published"];
switch (perspective) {
case "previewDrafts":
case "drafts": return ["drafts", "published"];
default: return ["published"];
}
}
/** @internal */
function createSourceDocumentResolver(getCachedDocument, _perspective) {
let perspectives = resolvePerspectives(_perspective);
function findDocument(sourceDocument) {
for (let perspective of perspectives) {
let match = null;
if (perspective.startsWith("r") && (match = getCachedDocument({
...sourceDocument,
_id: getVersionId(sourceDocument._id, perspective)
})), perspective === "drafts" && (match = getCachedDocument({
...sourceDocument,
_id: getDraftId(sourceDocument._id)
})), perspective === "published" && (match = getCachedDocument({
...sourceDocument,
_id: getPublishedId(sourceDocument._id)
})), match) return {
...match,
_id: getPublishedId(match._id),
_originalId: match._id
};
}
return null;
}
return function resolveSourceDocument(sourceDocument) {
return findDocument(sourceDocument);
};
}
/**
* Optimistically applies source documents to a result, using the content source map to trace fields.
* Can be used to apply mutations to documents being edited in a Studio, or any mutation on Content Lake, to a result with extremely low latency.
* @alpha
*/
function applySourceDocuments(result, resultSourceMap, getCachedDocument, updateFn, perspective) {
if (!resultSourceMap) return result;
let resolveDocument = createSourceDocumentResolver(getCachedDocument, perspective), cachedDocuments = resultSourceMap.documents?.map?.(resolveDocument) || [];
return walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {
let resolveMappingResult = resolveMapping(path, resultSourceMap);
if (!resolveMappingResult) return value;
let { mapping, pathSuffix } = resolveMappingResult;
if (mapping.type !== "value" || mapping.source.type !== "documentValue") return value;
let sourceDocument = resultSourceMap.documents[mapping.source.document], sourcePath = resultSourceMap.paths[mapping.source.path];
if (sourceDocument) {
let parsedPath = parseJsonPath(sourcePath + pathSuffix), stringifiedPath = toString(parsedPath), cachedDocument = cachedDocuments[mapping.source.document];
if (!cachedDocument) return value;
let changedValue = cachedDocument ? get(cachedDocument, stringifiedPath, value) : value;
return value === changedValue ? value : updateFn(changedValue, {
cachedDocument,
previousValue: value,
sourceDocument,
sourcePath: parsedPath
});
}
return value;
});
}
/**
* @internal
*/
function resolvedKeyedSourcePath(options) {
let { keyedResultPath, pathSuffix, sourceBasePath } = options, inferredResultPath = pathSuffix === void 0 ? [] : parseJsonPath(pathSuffix), inferredPath = keyedResultPath.slice(keyedResultPath.length - inferredResultPath.length), inferredPathSuffix = inferredPath.length ? jsonPath(inferredPath).slice(1) : "";
return parseJsonPath(sourceBasePath + inferredPathSuffix);
}
/** @alpha */
function resolveEditUrl(options) {
let { resultSourceMap, studioUrl } = options, resultPath = studioPathToJsonPath(options.resultPath), editInfo = resolveEditInfo({
resultPath,
resultSourceMap,
studioUrl
});
if (editInfo) return createEditUrl(editInfo);
}
export { DRAFTS_FOLDER, VERSION_FOLDER, applySourceDocuments, createEditUrl, getDraftId, getPublishedId, getVersionFromId, getVersionId, isDraftId, isPublishedId, isVersionId, jsonPath, jsonPathToStudioPath, parseJsonPath, resolveEditInfo, resolveEditUrl, resolveMapping, resolvedKeyedSourcePath, studioPath_exports as studioPath, studioPathToJsonPath, walkMap };
//# sourceMappingURL=csm.js.map