@sanity/client 6.26.0 → 6.27.0-canary.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/_chunks-cjs/config.cjs +163 -0
- package/dist/_chunks-cjs/config.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveEditInfo.cjs +6 -1
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +14 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/config.js +164 -0
- package/dist/_chunks-es/config.js.map +1 -0
- package/dist/_chunks-es/resolveEditInfo.js +6 -1
- package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +14 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/csm.cjs +40 -18
- package/dist/csm.cjs.map +1 -1
- package/dist/csm.d.cts +6 -3
- package/dist/csm.d.ts +6 -3
- package/dist/csm.js +42 -19
- package/dist/csm.js.map +1 -1
- package/dist/index.cjs +54 -202
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +11 -158
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/csm/applySourceDocuments.ts +10 -40
- package/src/csm/createEditUrl.ts +6 -3
- package/src/csm/createSourceDocumentResolver.ts +54 -0
- package/src/csm/resolvePerspectives.ts +29 -0
- package/umd/sanityClient.js +14 -1
- package/umd/sanityClient.min.js +1 -1
package/dist/csm.cjs
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var resolveEditInfo = require("./_chunks-cjs/resolveEditInfo.cjs");
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
var resolveEditInfo = require("./_chunks-cjs/resolveEditInfo.cjs"), config = require("./_chunks-cjs/config.cjs");
|
|
4
|
+
function resolvePerspectives(perspective) {
|
|
5
|
+
if (config.validateApiPerspective(perspective), Array.isArray(perspective))
|
|
6
|
+
return perspective.includes("published") ? perspective : [...perspective, "published"];
|
|
7
|
+
switch (perspective) {
|
|
8
|
+
case "previewDrafts":
|
|
9
|
+
case "drafts":
|
|
10
|
+
return ["drafts", "published"];
|
|
11
|
+
case "published":
|
|
12
|
+
default:
|
|
13
|
+
return ["published"];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function createSourceDocumentResolver(getCachedDocument, _perspective) {
|
|
17
|
+
const perspectives = resolvePerspectives(_perspective);
|
|
18
|
+
function findDocument(sourceDocument) {
|
|
19
|
+
for (const perspective of perspectives) {
|
|
20
|
+
let match = null;
|
|
21
|
+
if (perspective.startsWith("r") && (match = getCachedDocument({
|
|
22
|
+
...sourceDocument,
|
|
23
|
+
_id: resolveEditInfo.getVersionId(sourceDocument._id, perspective)
|
|
24
|
+
})), perspective === "drafts" && (match = getCachedDocument({
|
|
25
|
+
...sourceDocument,
|
|
26
|
+
_id: resolveEditInfo.getDraftId(sourceDocument._id)
|
|
27
|
+
})), perspective === "published" && (match = getCachedDocument({
|
|
28
|
+
...sourceDocument,
|
|
29
|
+
_id: resolveEditInfo.getPublishedId(sourceDocument._id)
|
|
30
|
+
})), match)
|
|
31
|
+
return { ...match, _id: resolveEditInfo.getPublishedId(match._id), _originalId: match._id };
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return function(sourceDocument) {
|
|
36
|
+
return findDocument(sourceDocument);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function applySourceDocuments(result, resultSourceMap, getCachedDocument, updateFn, perspective) {
|
|
6
40
|
if (!resultSourceMap) return result;
|
|
7
|
-
|
|
8
|
-
throw new Error(`Unknown perspective "${perspective}"`);
|
|
41
|
+
const resolveDocument = createSourceDocumentResolver(getCachedDocument, perspective), cachedDocuments = resultSourceMap.documents.map(resolveDocument);
|
|
9
42
|
return resolveEditInfo.walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {
|
|
10
43
|
const resolveMappingResult = resolveEditInfo.resolveMapping(path, resultSourceMap);
|
|
11
44
|
if (!resolveMappingResult)
|
|
@@ -15,19 +48,8 @@ function applySourceDocuments(result, resultSourceMap, getCachedDocument, update
|
|
|
15
48
|
return value;
|
|
16
49
|
const sourceDocument = resultSourceMap.documents[mapping.source.document], sourcePath = resultSourceMap.paths[mapping.source.path];
|
|
17
50
|
if (sourceDocument) {
|
|
18
|
-
const parsedPath = resolveEditInfo.parseJsonPath(sourcePath + pathSuffix), stringifiedPath = resolveEditInfo.toString(parsedPath);
|
|
19
|
-
if (
|
|
20
|
-
return value;
|
|
21
|
-
let cachedDocument;
|
|
22
|
-
if (perspective === "previewDrafts" ? (cachedDocument = getCachedDocument(
|
|
23
|
-
resolveEditInfo.isDraftId(sourceDocument._id) ? sourceDocument : { ...sourceDocument, _id: resolveEditInfo.getDraftId(sourceDocument._id) }
|
|
24
|
-
), cachedDocument || (cachedDocument = getCachedDocument(
|
|
25
|
-
resolveEditInfo.isDraftId(sourceDocument._id) ? { ...sourceDocument, _id: resolveEditInfo.getPublishedId(sourceDocument._id) } : sourceDocument
|
|
26
|
-
)), cachedDocument && (cachedDocument = {
|
|
27
|
-
...cachedDocument,
|
|
28
|
-
_id: resolveEditInfo.getPublishedId(sourceDocument._id),
|
|
29
|
-
_originalId: sourceDocument._id
|
|
30
|
-
})) : cachedDocument = getCachedDocument(sourceDocument), !cachedDocument)
|
|
51
|
+
const parsedPath = resolveEditInfo.parseJsonPath(sourcePath + pathSuffix), stringifiedPath = resolveEditInfo.toString(parsedPath), cachedDocument = cachedDocuments[mapping.source.document];
|
|
52
|
+
if (!cachedDocument)
|
|
31
53
|
return value;
|
|
32
54
|
const changedValue = cachedDocument ? resolveEditInfo.get(cachedDocument, stringifiedPath, value) : value;
|
|
33
55
|
return value === changedValue ? value : updateFn(changedValue, {
|
package/dist/csm.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"csm.cjs","sources":["../src/csm/applySourceDocuments.ts","../src/csm/resolvedKeyedSourcePath.ts","../src/csm/resolveEditUrl.ts"],"sourcesContent":["import {getDraftId, getPublishedId,
|
|
1
|
+
{"version":3,"file":"csm.cjs","sources":["../src/csm/resolvePerspectives.ts","../src/csm/createSourceDocumentResolver.ts","../src/csm/applySourceDocuments.ts","../src/csm/resolvedKeyedSourcePath.ts","../src/csm/resolveEditUrl.ts"],"sourcesContent":["import {validateApiPerspective} from '../config'\nimport type {ReleaseId} from '../types'\nimport type {ClientPerspective} from './types'\n\n/**\n * This resolves the perspectives to how documents should be resolved when applying optimistic updates,\n * like in `applySourceDocuments`.\n * @internal\n */\nexport function resolvePerspectives(\n perspective: Exclude<ClientPerspective, 'raw'>,\n): ('published' | 'drafts' | ReleaseId)[] {\n validateApiPerspective(perspective)\n\n if (Array.isArray(perspective)) {\n if (!perspective.includes('published')) {\n return [...perspective, 'published']\n }\n return perspective\n }\n switch (perspective) {\n case 'previewDrafts':\n case 'drafts':\n return ['drafts', 'published']\n case 'published':\n default:\n return ['published']\n }\n}\n","import {getDraftId, getPublishedId, getVersionId} from './draftUtils'\nimport {resolvePerspectives} from './resolvePerspectives'\nimport type {ClientPerspective, ContentSourceMapDocuments, SanityDocument} from './types'\n\n/** @internal */\nexport type ResolvedDocument = Partial<SanityDocument> &\n Required<Pick<SanityDocument, '_id' | '_type'>>\n\n/** @internal */\nexport type MatchedDocument = Partial<SanityDocument> &\n Required<Pick<SanityDocument, '_id' | '_type' | '_originalId'>>\n\n/** @internal */\nexport function createSourceDocumentResolver(\n getCachedDocument: (\n sourceDocument: ContentSourceMapDocuments[number],\n ) => ResolvedDocument | null | undefined,\n _perspective: Exclude<ClientPerspective, 'raw'>,\n) {\n const perspectives = resolvePerspectives(_perspective)\n function findDocument(sourceDocument: ContentSourceMapDocuments[number]) {\n for (const perspective of perspectives) {\n let match: ReturnType<typeof getCachedDocument> = null\n if (perspective.startsWith('r')) {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getVersionId(sourceDocument._id, perspective),\n })\n }\n if (perspective === 'drafts') {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getDraftId(sourceDocument._id),\n })\n }\n if (perspective === 'published') {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getPublishedId(sourceDocument._id),\n })\n }\n if (match) {\n return {...match, _id: getPublishedId(match._id), _originalId: match._id}\n }\n }\n return null\n }\n // define resolver that loops over source documents and perspectives\n return function resolveSourceDocument(\n sourceDocument: ContentSourceMapDocuments[number],\n ): MatchedDocument | null {\n return findDocument(sourceDocument)\n }\n}\n","import {createSourceDocumentResolver} from './createSourceDocumentResolver'\nimport {parseJsonPath} from './jsonPath'\nimport {resolveMapping} from './resolveMapping'\nimport * as paths from './studioPath'\nimport type {\n Any,\n ApplySourceDocumentsUpdateFunction,\n ClientPerspective,\n ContentSourceMap,\n ContentSourceMapDocuments,\n Path,\n SanityDocument,\n} from './types'\nimport {walkMap} from './walkMap'\n\n/**\n * Optimistically applies source documents to a result, using the content source map to trace fields.\n * 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.\n * @alpha\n */\nexport function applySourceDocuments<Result = unknown>(\n result: Result,\n resultSourceMap: ContentSourceMap | undefined,\n getCachedDocument: (\n sourceDocument: ContentSourceMapDocuments[number],\n ) =>\n | (Partial<SanityDocument> & Required<Pick<SanityDocument, '_id' | '_type'>>)\n | null\n | undefined,\n updateFn: ApplySourceDocumentsUpdateFunction,\n perspective: Exclude<ClientPerspective, 'raw'>,\n): Result {\n if (!resultSourceMap) return result\n\n const resolveDocument = createSourceDocumentResolver(getCachedDocument, perspective)\n const cachedDocuments = resultSourceMap.documents.map(resolveDocument)\n\n return walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {\n const resolveMappingResult = resolveMapping(path, resultSourceMap)\n if (!resolveMappingResult) {\n return value\n }\n\n const {mapping, pathSuffix} = resolveMappingResult\n if (mapping.type !== 'value') {\n return value\n }\n\n if (mapping.source.type !== 'documentValue') {\n return value\n }\n\n const sourceDocument = resultSourceMap.documents[mapping.source.document]\n const sourcePath = resultSourceMap.paths[mapping.source.path]\n\n if (sourceDocument) {\n const parsedPath = parseJsonPath(sourcePath + pathSuffix)\n const stringifiedPath = paths.toString(parsedPath as Path)\n const cachedDocument = cachedDocuments[mapping.source.document]\n\n if (!cachedDocument) {\n return value\n }\n\n const changedValue = cachedDocument\n ? paths.get<Result[keyof Result]>(cachedDocument, stringifiedPath, value)\n : value\n return value === changedValue\n ? value\n : updateFn<Result[keyof Result]>(changedValue as Any, {\n cachedDocument,\n previousValue: value as Result[keyof Result],\n sourceDocument,\n sourcePath: parsedPath,\n })\n }\n\n return value\n }) as Result\n}\n","import {jsonPath, parseJsonPath} from './jsonPath'\nimport type {ContentSourceMapParsedPath} from './types'\n\n/**\n * @internal\n */\nexport function resolvedKeyedSourcePath(options: {\n keyedResultPath: ContentSourceMapParsedPath\n pathSuffix?: string\n sourceBasePath: string\n}): ContentSourceMapParsedPath {\n const {keyedResultPath, pathSuffix, sourceBasePath} = options\n\n const inferredResultPath = pathSuffix === undefined ? [] : parseJsonPath(pathSuffix)\n\n const inferredPath = keyedResultPath.slice(keyedResultPath.length - inferredResultPath.length)\n\n const inferredPathSuffix = inferredPath.length ? jsonPath(inferredPath).slice(1) : ''\n\n return parseJsonPath(sourceBasePath + inferredPathSuffix)\n}\n","import {createEditUrl} from './createEditUrl'\nimport {studioPathToJsonPath} from './jsonPath'\nimport {resolveEditInfo} from './resolveEditInfo'\nimport type {ResolveEditUrlOptions} from './types'\n\n/** @alpha */\nexport function resolveEditUrl(\n options: ResolveEditUrlOptions,\n): ReturnType<typeof createEditUrl> | undefined {\n const {resultSourceMap, studioUrl} = options\n const resultPath = studioPathToJsonPath(options.resultPath)\n\n const editInfo = resolveEditInfo({\n resultPath,\n resultSourceMap,\n studioUrl,\n })\n if (!editInfo) {\n return undefined\n }\n\n return createEditUrl(editInfo)\n}\n"],"names":["validateApiPerspective","getVersionId","getDraftId","getPublishedId","walkMap","resolveMapping","parseJsonPath","paths.toString","paths.get","jsonPath","studioPathToJsonPath","resolveEditInfo","createEditUrl"],"mappings":";;;AASO,SAAS,oBACd,aACwC;AAGxC,MAFAA,OAAuB,uBAAA,WAAW,GAE9B,MAAM,QAAQ,WAAW;AACtB,WAAA,YAAY,SAAS,WAAW,IAG9B,cAFE,CAAC,GAAG,aAAa,WAAW;AAIvC,UAAQ,aAAa;AAAA,IACnB,KAAK;AAAA,IACL,KAAK;AACI,aAAA,CAAC,UAAU,WAAW;AAAA,IAC/B,KAAK;AAAA,IACL;AACE,aAAO,CAAC,WAAW;AAAA,EAAA;AAEzB;ACfgB,SAAA,6BACd,mBAGA,cACA;AACM,QAAA,eAAe,oBAAoB,YAAY;AACrD,WAAS,aAAa,gBAAmD;AACvE,eAAW,eAAe,cAAc;AACtC,UAAI,QAA8C;AAmBlD,UAlBI,YAAY,WAAW,GAAG,MAC5B,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAKC,gBAAA,aAAa,eAAe,KAAK,WAAW;AAAA,MAClD,CAAA,IAEC,gBAAgB,aAClB,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAKC,gBAAAA,WAAW,eAAe,GAAG;AAAA,MACnC,CAAA,IAEC,gBAAgB,gBAClB,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAKC,gBAAAA,eAAe,eAAe,GAAG;AAAA,MACvC,CAAA,IAEC;AACK,eAAA,EAAC,GAAG,OAAO,KAAKA,gBAAA,eAAe,MAAM,GAAG,GAAG,aAAa,MAAM,IAAG;AAAA,IAAA;AAGrE,WAAA;AAAA,EAAA;AAGT,SAAO,SACL,gBACwB;AACxB,WAAO,aAAa,cAAc;AAAA,EACpC;AACF;ACjCO,SAAS,qBACd,QACA,iBACA,mBAMA,UACA,aACQ;AACJ,MAAA,CAAC,gBAA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UAAa,IAAA,SAC/B,aAAaI,gBAAA,qBAAqB,QAAQ,UAAU,GAEpD,WAAWC,gBAAAA,gBAAgB;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACD;AACI,MAAA;AAIL,WAAOC,gBAAAA,cAAc,QAAQ;AAC/B;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/csm.d.cts
CHANGED
|
@@ -14,9 +14,12 @@ export declare function applySourceDocuments<Result = unknown>(
|
|
|
14
14
|
resultSourceMap: ContentSourceMap | undefined,
|
|
15
15
|
getCachedDocument: (
|
|
16
16
|
sourceDocument: ContentSourceMapDocuments[number],
|
|
17
|
-
) =>
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
) =>
|
|
18
|
+
| (Partial<SanityDocument> & Required<Pick<SanityDocument, '_id' | '_type'>>)
|
|
19
|
+
| null
|
|
20
|
+
| undefined,
|
|
21
|
+
updateFn: ApplySourceDocumentsUpdateFunction,
|
|
22
|
+
perspective: Exclude<ClientPerspective, 'raw'>,
|
|
20
23
|
): Result
|
|
21
24
|
|
|
22
25
|
/**
|
package/dist/csm.d.ts
CHANGED
|
@@ -14,9 +14,12 @@ export declare function applySourceDocuments<Result = unknown>(
|
|
|
14
14
|
resultSourceMap: ContentSourceMap | undefined,
|
|
15
15
|
getCachedDocument: (
|
|
16
16
|
sourceDocument: ContentSourceMapDocuments[number],
|
|
17
|
-
) =>
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
) =>
|
|
18
|
+
| (Partial<SanityDocument> & Required<Pick<SanityDocument, '_id' | '_type'>>)
|
|
19
|
+
| null
|
|
20
|
+
| undefined,
|
|
21
|
+
updateFn: ApplySourceDocumentsUpdateFunction,
|
|
22
|
+
perspective: Exclude<ClientPerspective, 'raw'>,
|
|
20
23
|
): Result
|
|
21
24
|
|
|
22
25
|
/**
|
package/dist/csm.js
CHANGED
|
@@ -1,10 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DRAFTS_FOLDER, VERSION_FOLDER, getVersionFromId,
|
|
3
|
-
|
|
4
|
-
function
|
|
1
|
+
import { getVersionId, getDraftId, getPublishedId, walkMap, resolveMapping, parseJsonPath, toString, get, jsonPath, studioPathToJsonPath, resolveEditInfo, createEditUrl } from "./_chunks-es/resolveEditInfo.js";
|
|
2
|
+
import { DRAFTS_FOLDER, VERSION_FOLDER, getVersionFromId, isDraftId, isPublishedId, isVersionId, jsonPathToStudioPath, studioPath } from "./_chunks-es/resolveEditInfo.js";
|
|
3
|
+
import { validateApiPerspective } from "./_chunks-es/config.js";
|
|
4
|
+
function resolvePerspectives(perspective) {
|
|
5
|
+
if (validateApiPerspective(perspective), Array.isArray(perspective))
|
|
6
|
+
return perspective.includes("published") ? perspective : [...perspective, "published"];
|
|
7
|
+
switch (perspective) {
|
|
8
|
+
case "previewDrafts":
|
|
9
|
+
case "drafts":
|
|
10
|
+
return ["drafts", "published"];
|
|
11
|
+
case "published":
|
|
12
|
+
default:
|
|
13
|
+
return ["published"];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function createSourceDocumentResolver(getCachedDocument, _perspective) {
|
|
17
|
+
const perspectives = resolvePerspectives(_perspective);
|
|
18
|
+
function findDocument(sourceDocument) {
|
|
19
|
+
for (const perspective of perspectives) {
|
|
20
|
+
let match = null;
|
|
21
|
+
if (perspective.startsWith("r") && (match = getCachedDocument({
|
|
22
|
+
...sourceDocument,
|
|
23
|
+
_id: getVersionId(sourceDocument._id, perspective)
|
|
24
|
+
})), perspective === "drafts" && (match = getCachedDocument({
|
|
25
|
+
...sourceDocument,
|
|
26
|
+
_id: getDraftId(sourceDocument._id)
|
|
27
|
+
})), perspective === "published" && (match = getCachedDocument({
|
|
28
|
+
...sourceDocument,
|
|
29
|
+
_id: getPublishedId(sourceDocument._id)
|
|
30
|
+
})), match)
|
|
31
|
+
return { ...match, _id: getPublishedId(match._id), _originalId: match._id };
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return function(sourceDocument) {
|
|
36
|
+
return findDocument(sourceDocument);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function applySourceDocuments(result, resultSourceMap, getCachedDocument, updateFn, perspective) {
|
|
5
40
|
if (!resultSourceMap) return result;
|
|
6
|
-
|
|
7
|
-
throw new Error(`Unknown perspective "${perspective}"`);
|
|
41
|
+
const resolveDocument = createSourceDocumentResolver(getCachedDocument, perspective), cachedDocuments = resultSourceMap.documents.map(resolveDocument);
|
|
8
42
|
return walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {
|
|
9
43
|
const resolveMappingResult = resolveMapping(path, resultSourceMap);
|
|
10
44
|
if (!resolveMappingResult)
|
|
@@ -14,19 +48,8 @@ function applySourceDocuments(result, resultSourceMap, getCachedDocument, update
|
|
|
14
48
|
return value;
|
|
15
49
|
const sourceDocument = resultSourceMap.documents[mapping.source.document], sourcePath = resultSourceMap.paths[mapping.source.path];
|
|
16
50
|
if (sourceDocument) {
|
|
17
|
-
const parsedPath = parseJsonPath(sourcePath + pathSuffix), stringifiedPath = toString(parsedPath);
|
|
18
|
-
if (
|
|
19
|
-
return value;
|
|
20
|
-
let cachedDocument;
|
|
21
|
-
if (perspective === "previewDrafts" ? (cachedDocument = getCachedDocument(
|
|
22
|
-
isDraftId(sourceDocument._id) ? sourceDocument : { ...sourceDocument, _id: getDraftId(sourceDocument._id) }
|
|
23
|
-
), cachedDocument || (cachedDocument = getCachedDocument(
|
|
24
|
-
isDraftId(sourceDocument._id) ? { ...sourceDocument, _id: getPublishedId(sourceDocument._id) } : sourceDocument
|
|
25
|
-
)), cachedDocument && (cachedDocument = {
|
|
26
|
-
...cachedDocument,
|
|
27
|
-
_id: getPublishedId(sourceDocument._id),
|
|
28
|
-
_originalId: sourceDocument._id
|
|
29
|
-
})) : cachedDocument = getCachedDocument(sourceDocument), !cachedDocument)
|
|
51
|
+
const parsedPath = parseJsonPath(sourcePath + pathSuffix), stringifiedPath = toString(parsedPath), cachedDocument = cachedDocuments[mapping.source.document];
|
|
52
|
+
if (!cachedDocument)
|
|
30
53
|
return value;
|
|
31
54
|
const changedValue = cachedDocument ? get(cachedDocument, stringifiedPath, value) : value;
|
|
32
55
|
return value === changedValue ? value : updateFn(changedValue, {
|
package/dist/csm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"csm.js","sources":["../src/csm/applySourceDocuments.ts","../src/csm/resolvedKeyedSourcePath.ts","../src/csm/resolveEditUrl.ts"],"sourcesContent":["import {getDraftId, getPublishedId,
|
|
1
|
+
{"version":3,"file":"csm.js","sources":["../src/csm/resolvePerspectives.ts","../src/csm/createSourceDocumentResolver.ts","../src/csm/applySourceDocuments.ts","../src/csm/resolvedKeyedSourcePath.ts","../src/csm/resolveEditUrl.ts"],"sourcesContent":["import {validateApiPerspective} from '../config'\nimport type {ReleaseId} from '../types'\nimport type {ClientPerspective} from './types'\n\n/**\n * This resolves the perspectives to how documents should be resolved when applying optimistic updates,\n * like in `applySourceDocuments`.\n * @internal\n */\nexport function resolvePerspectives(\n perspective: Exclude<ClientPerspective, 'raw'>,\n): ('published' | 'drafts' | ReleaseId)[] {\n validateApiPerspective(perspective)\n\n if (Array.isArray(perspective)) {\n if (!perspective.includes('published')) {\n return [...perspective, 'published']\n }\n return perspective\n }\n switch (perspective) {\n case 'previewDrafts':\n case 'drafts':\n return ['drafts', 'published']\n case 'published':\n default:\n return ['published']\n }\n}\n","import {getDraftId, getPublishedId, getVersionId} from './draftUtils'\nimport {resolvePerspectives} from './resolvePerspectives'\nimport type {ClientPerspective, ContentSourceMapDocuments, SanityDocument} from './types'\n\n/** @internal */\nexport type ResolvedDocument = Partial<SanityDocument> &\n Required<Pick<SanityDocument, '_id' | '_type'>>\n\n/** @internal */\nexport type MatchedDocument = Partial<SanityDocument> &\n Required<Pick<SanityDocument, '_id' | '_type' | '_originalId'>>\n\n/** @internal */\nexport function createSourceDocumentResolver(\n getCachedDocument: (\n sourceDocument: ContentSourceMapDocuments[number],\n ) => ResolvedDocument | null | undefined,\n _perspective: Exclude<ClientPerspective, 'raw'>,\n) {\n const perspectives = resolvePerspectives(_perspective)\n function findDocument(sourceDocument: ContentSourceMapDocuments[number]) {\n for (const perspective of perspectives) {\n let match: ReturnType<typeof getCachedDocument> = null\n if (perspective.startsWith('r')) {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getVersionId(sourceDocument._id, perspective),\n })\n }\n if (perspective === 'drafts') {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getDraftId(sourceDocument._id),\n })\n }\n if (perspective === 'published') {\n match = getCachedDocument({\n ...sourceDocument,\n _id: getPublishedId(sourceDocument._id),\n })\n }\n if (match) {\n return {...match, _id: getPublishedId(match._id), _originalId: match._id}\n }\n }\n return null\n }\n // define resolver that loops over source documents and perspectives\n return function resolveSourceDocument(\n sourceDocument: ContentSourceMapDocuments[number],\n ): MatchedDocument | null {\n return findDocument(sourceDocument)\n }\n}\n","import {createSourceDocumentResolver} from './createSourceDocumentResolver'\nimport {parseJsonPath} from './jsonPath'\nimport {resolveMapping} from './resolveMapping'\nimport * as paths from './studioPath'\nimport type {\n Any,\n ApplySourceDocumentsUpdateFunction,\n ClientPerspective,\n ContentSourceMap,\n ContentSourceMapDocuments,\n Path,\n SanityDocument,\n} from './types'\nimport {walkMap} from './walkMap'\n\n/**\n * Optimistically applies source documents to a result, using the content source map to trace fields.\n * 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.\n * @alpha\n */\nexport function applySourceDocuments<Result = unknown>(\n result: Result,\n resultSourceMap: ContentSourceMap | undefined,\n getCachedDocument: (\n sourceDocument: ContentSourceMapDocuments[number],\n ) =>\n | (Partial<SanityDocument> & Required<Pick<SanityDocument, '_id' | '_type'>>)\n | null\n | undefined,\n updateFn: ApplySourceDocumentsUpdateFunction,\n perspective: Exclude<ClientPerspective, 'raw'>,\n): Result {\n if (!resultSourceMap) return result\n\n const resolveDocument = createSourceDocumentResolver(getCachedDocument, perspective)\n const cachedDocuments = resultSourceMap.documents.map(resolveDocument)\n\n return walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {\n const resolveMappingResult = resolveMapping(path, resultSourceMap)\n if (!resolveMappingResult) {\n return value\n }\n\n const {mapping, pathSuffix} = resolveMappingResult\n if (mapping.type !== 'value') {\n return value\n }\n\n if (mapping.source.type !== 'documentValue') {\n return value\n }\n\n const sourceDocument = resultSourceMap.documents[mapping.source.document]\n const sourcePath = resultSourceMap.paths[mapping.source.path]\n\n if (sourceDocument) {\n const parsedPath = parseJsonPath(sourcePath + pathSuffix)\n const stringifiedPath = paths.toString(parsedPath as Path)\n const cachedDocument = cachedDocuments[mapping.source.document]\n\n if (!cachedDocument) {\n return value\n }\n\n const changedValue = cachedDocument\n ? paths.get<Result[keyof Result]>(cachedDocument, stringifiedPath, value)\n : value\n return value === changedValue\n ? value\n : updateFn<Result[keyof Result]>(changedValue as Any, {\n cachedDocument,\n previousValue: value as Result[keyof Result],\n sourceDocument,\n sourcePath: parsedPath,\n })\n }\n\n return value\n }) as Result\n}\n","import {jsonPath, parseJsonPath} from './jsonPath'\nimport type {ContentSourceMapParsedPath} from './types'\n\n/**\n * @internal\n */\nexport function resolvedKeyedSourcePath(options: {\n keyedResultPath: ContentSourceMapParsedPath\n pathSuffix?: string\n sourceBasePath: string\n}): ContentSourceMapParsedPath {\n const {keyedResultPath, pathSuffix, sourceBasePath} = options\n\n const inferredResultPath = pathSuffix === undefined ? [] : parseJsonPath(pathSuffix)\n\n const inferredPath = keyedResultPath.slice(keyedResultPath.length - inferredResultPath.length)\n\n const inferredPathSuffix = inferredPath.length ? jsonPath(inferredPath).slice(1) : ''\n\n return parseJsonPath(sourceBasePath + inferredPathSuffix)\n}\n","import {createEditUrl} from './createEditUrl'\nimport {studioPathToJsonPath} from './jsonPath'\nimport {resolveEditInfo} from './resolveEditInfo'\nimport type {ResolveEditUrlOptions} from './types'\n\n/** @alpha */\nexport function resolveEditUrl(\n options: ResolveEditUrlOptions,\n): ReturnType<typeof createEditUrl> | undefined {\n const {resultSourceMap, studioUrl} = options\n const resultPath = studioPathToJsonPath(options.resultPath)\n\n const editInfo = resolveEditInfo({\n resultPath,\n resultSourceMap,\n studioUrl,\n })\n if (!editInfo) {\n return undefined\n }\n\n return createEditUrl(editInfo)\n}\n"],"names":["paths.toString","paths.get"],"mappings":";;;AASO,SAAS,oBACd,aACwC;AAGxC,MAFA,uBAAuB,WAAW,GAE9B,MAAM,QAAQ,WAAW;AACtB,WAAA,YAAY,SAAS,WAAW,IAG9B,cAFE,CAAC,GAAG,aAAa,WAAW;AAIvC,UAAQ,aAAa;AAAA,IACnB,KAAK;AAAA,IACL,KAAK;AACI,aAAA,CAAC,UAAU,WAAW;AAAA,IAC/B,KAAK;AAAA,IACL;AACE,aAAO,CAAC,WAAW;AAAA,EAAA;AAEzB;ACfgB,SAAA,6BACd,mBAGA,cACA;AACM,QAAA,eAAe,oBAAoB,YAAY;AACrD,WAAS,aAAa,gBAAmD;AACvE,eAAW,eAAe,cAAc;AACtC,UAAI,QAA8C;AAmBlD,UAlBI,YAAY,WAAW,GAAG,MAC5B,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAK,aAAa,eAAe,KAAK,WAAW;AAAA,MAClD,CAAA,IAEC,gBAAgB,aAClB,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAK,WAAW,eAAe,GAAG;AAAA,MACnC,CAAA,IAEC,gBAAgB,gBAClB,QAAQ,kBAAkB;AAAA,QACxB,GAAG;AAAA,QACH,KAAK,eAAe,eAAe,GAAG;AAAA,MACvC,CAAA,IAEC;AACK,eAAA,EAAC,GAAG,OAAO,KAAK,eAAe,MAAM,GAAG,GAAG,aAAa,MAAM,IAAG;AAAA,IAAA;AAGrE,WAAA;AAAA,EAAA;AAGT,SAAO,SACL,gBACwB;AACxB,WAAO,aAAa,cAAc;AAAA,EACpC;AACF;ACjCO,SAAS,qBACd,QACA,iBACA,mBAMA,UAC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