-
Notifications
You must be signed in to change notification settings - Fork 1
feat!: replace diffItem with diffValue
#39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Jun 3, 2025
Contributor
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
This was referenced Jun 3, 2025
1911ba8 to
fc0b806
Compare
7c56fc3 to
61a3ffc
Compare
This was referenced Jun 4, 2025
rexxars
requested changes
Jun 6, 2025
fc0b806 to
e538df3
Compare
61a3ffc to
5067260
Compare
rexxars
approved these changes
Jun 9, 2025
Contributor
Author
Merge activity
|
- Add new diffValue function that returns SanityPatchOperations[] - Remove diffItem from public API (now internal) - Simplify exported types to reduce API surface area - Internalize special patch format implementation details BREAKING CHANGE: diffItem is no longer exported from the public API. Use diffValue instead, which returns an array of SanityPatchOperations rather than the internal Patch format.
Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
c4070aa to
11e9d4f
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

This PR introduces a BREAKING CHANGE by replacing the previously exported
diffItemfunction with a new, more clearly named public API:diffValue. It also significantly refactors the internal patch serialization logic and introduces more granular type definitions for patch operations.The primary goals are to:
diffValue) for generating patch operations on arbitrary values (not necessarily whole documents).diffItemfunction truly internal.SanityPatchtypes to more specificSanityPatchOperationtypes.ifRevisionIDis only added to the very first patch in a set of mutations.Key Changes:
diffItemfunction is no longer exported. Its functionality is now primarily internal.diffValue(source: unknown, target: unknown, basePath?: Path): SanityPatchOperations[]is introduced and exported. This function generates an array ofSanityPatchOperations(which are plain objects like{set: {...}},{unset: [...]}) based on the differences betweensourceandtargetvalues. It does not wrap these operations in theSanityPatchMutationstructure.diffPatchfunction (which diffs documents and returnsSanityPatchMutation[]) now internally callsdiffItemand then uses the refactoredserializePatchesto construct the final mutations. The logic for addingidandifRevisionIDto the patch mutations now resides withindiffPatch.SetPatch,InsertAfterPatch,SanitySetPatch,SanityUnsetPatch,SanityInsertPatch, andSanityDiffMatchPatchfrom the public API (some were previously exported frompatches.ts).SanitySetPatchOperation({ set: Record<string, unknown> })SanityUnsetPatchOperation({ unset: string[] })SanityInsertPatchOperation({ insert: { before/after/replace: string, items: unknown[] } })SanityDiffMatchPatchOperation({ diffMatchPatch: Record<string, string> })SanityPatchOperationstype is now aPartialunion of these new operation types, reflecting that a single patch object fromdiffValuewill contain one or more of these operations.SanityPatchtype (used withinSanityPatchMutation) nowextends SanityPatchOperationsand includesidand optionalifRevisionID.Patchtype (used bydiffItem) remains but is now an internal detail.serializePatchesFunction:serializePatchesfunction now takes an array of internalPatchobjects and returns an array ofSanityPatchOperation[](the raw operation objects like{set: {...}}).idorifRevisionID; this responsibility is moved to thediffPatchfunction.set,unset,insert, anddiffMatchPatchoperations into distinct objects in the output array has been improved for clarity.diffPatchFunction:diffItemto get the raw patch list.serializePatchesto getSanityPatchOperations[].SanityPatchMutation[], adding theidto each andifRevisionIDonly to the first patch mutation in the array.diffValueto clearly explain its purpose, parameters, and return type.diffPatchand internal types to reflect the changes.Rationale:
diffValueprovides a more intuitive name for diffing arbitrary JavaScript values and returning the raw operations, distinct fromdiffPatchwhich is document-centric.Sanity...Operationtypes are more precise and make it easier to work with the different kinds of patch operations programmatically.ifRevisionIDHandling: EnsuringifRevisionIDis only on the first patch of a transaction is crucial for correct optimistic locking in Sanity.diffItemfocuses on generating a flat list of diffs,serializePatches(as used bydiffValue) groups them into operations, anddiffPatchhandles the document-specific concerns like_idandifRevisionID.This refactor provides a cleaner and more robust API for generating patches, both for full documents and for arbitrary values.