Skip to content

Commit

Permalink
feat(csm): support perspective modes when optimistically applying doc…
Browse files Browse the repository at this point in the history
…ument mutations
  • Loading branch information
stipsan committed Jan 9, 2024
1 parent 9749ae8 commit 68a1823
Show file tree
Hide file tree
Showing 6 changed files with 1,353 additions and 5 deletions.
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@sanity/pkg-utils": "^3.3.7",
"@types/json-diff": "^1.0.3",
"@types/node": "^20.8.8",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
Expand All @@ -149,6 +150,7 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"faucet": "^0.0.4",
"happy-dom": "^12.10.3",
"json-diff": "^1.0.6",
"ls-engines": "^0.9.1",
"nock": "^13.4.0",
"prettier": "^3.1.1",
Expand Down
44 changes: 41 additions & 3 deletions src/csm/applySourceDocuments.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {DRAFTS_PREFIX, getPublishedId} from './getPublishedId'
import {parseJsonPath} from './jsonPath'
import {resolveMapping} from './resolveMapping'
import * as paths from './studioPath'
import type {
Any,
ApplySourceDocumentsUpdateFunction,
ClientPerspective,
ContentSourceMap,
ContentSourceMapDocuments,
Path,
Expand All @@ -25,9 +27,14 @@ export function applySourceDocuments<Result = unknown>(
sourceDocument: ContentSourceMapDocuments[number],
) => SanityDocument | undefined,
updateFn: ApplySourceDocumentsUpdateFunction = defaultUpdateFunction,
perspective: ClientPerspective = 'raw',
): Result {
if (!resultSourceMap) return result

if (perspective !== 'published' && perspective !== 'raw' && perspective !== 'previewDrafts') {
throw new Error(`Unknown perspective "${perspective}"`)
}

return walkMap(JSON.parse(JSON.stringify(result)), (value, path) => {
const resolveMappingResult = resolveMapping(path, resultSourceMap)
if (!resolveMappingResult) {
Expand All @@ -48,14 +55,45 @@ export function applySourceDocuments<Result = unknown>(
const sourcePath = resultSourceMap.paths[mapping.source.path]

if (sourceDocument) {
const cachedDocument = getCachedDocument(sourceDocument)
const parsedPath = parseJsonPath(sourcePath + pathSuffix)
const stringifiedPath = paths.toString(parsedPath as Path)

// The _id is sometimes used used as `key` in lists, and should not be changed optimistically
if (stringifiedPath === '_id') {
return value
}

let cachedDocument: SanityDocument | undefined
if (perspective === 'previewDrafts') {
cachedDocument = getCachedDocument(
sourceDocument._id.startsWith(DRAFTS_PREFIX)
? sourceDocument
: {...sourceDocument, _id: `${DRAFTS_PREFIX}.${sourceDocument._id}}`},
)
if (!cachedDocument) {
cachedDocument = getCachedDocument(
sourceDocument._id.startsWith(DRAFTS_PREFIX)
? {...sourceDocument, _id: getPublishedId(sourceDocument._id)}
: sourceDocument,
)
}
if (cachedDocument) {
cachedDocument = {
...cachedDocument,
_id: getPublishedId(sourceDocument._id),
_originalId: sourceDocument._id,
}
}
} else {
cachedDocument = getCachedDocument(sourceDocument)
}

if (!cachedDocument) {
return value
}

const parsedPath = parseJsonPath(sourcePath + pathSuffix)
const changedValue = cachedDocument
? paths.get<Result[keyof Result]>(cachedDocument, paths.toString(parsedPath as Path), value)
? paths.get<Result[keyof Result]>(cachedDocument, stringifiedPath, value)
: value
return value === changedValue
? value
Expand Down
2 changes: 1 addition & 1 deletion src/csm/getPublishedId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DRAFTS_PREFIX = 'drafts.'
export const DRAFTS_PREFIX = 'drafts.'

/** @internal */
export function getPublishedId(id: string): string {
Expand Down
1 change: 1 addition & 0 deletions src/csm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Path} from './studioPath'

export type {
Any,
ClientPerspective,
ContentSourceMap,
ContentSourceMapDocument,
ContentSourceMapDocumentBase,
Expand Down
Loading

0 comments on commit 68a1823

Please sign in to comment.