@sanity/diff-patch 6.0.0 → 7.0.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/README.md +8 -1
- package/dist/index.d.ts +99 -132
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +365 -224
- package/dist/index.js.map +1 -1
- package/package.json +56 -77
- package/src/diffPatch.ts +7 -15
- package/src/patches.ts +2 -2
- package/dist/index.cjs +0 -271
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -195
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# @sanity/diff-patch
|
|
2
2
|
|
|
3
|
-
[](https://npmx.dev/package/@sanity/diff-patch)
|
|
4
|
+
[](https://npmx.dev/package/@sanity/diff-patch)
|
|
5
|
+
[](https://npmx.dev/package/@sanity/diff-patch)
|
|
6
|
+
[](https://npmx.dev/package/@sanity/diff-patch)
|
|
4
7
|
|
|
5
8
|
Generate Sanity patch mutations by comparing two documents or values. This library creates conflict-resistant patches designed for collaborative editing environments where multiple users may be editing the same document simultaneously.
|
|
6
9
|
|
|
@@ -15,6 +18,10 @@ Used internally by the Sanity App SDK for its collaborative editing system.
|
|
|
15
18
|
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
21
|
+
Requires Node.js 22.12.0 or later. This package ships ES modules only.
|
|
22
|
+
|
|
23
|
+
To work on this repository, use Node.js 24 (`nvm use`) and the pnpm version specified in `package.json`.
|
|
24
|
+
|
|
18
25
|
```bash
|
|
19
26
|
npm install @sanity/diff-patch
|
|
20
27
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,195 +1,162 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Contains `path`, `value` and `serializedPath` properties,
|
|
4
|
-
* which is helpful for debugging and showing friendly messages.
|
|
2
|
+
* A segment of a path
|
|
5
3
|
*
|
|
6
4
|
* @public
|
|
7
5
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
serializedPath: string
|
|
12
|
-
constructor(message: string, path: Path, value?: unknown)
|
|
13
|
-
}
|
|
14
|
-
|
|
6
|
+
type PathSegment = string | number | {
|
|
7
|
+
_key: string;
|
|
8
|
+
} | [number | '', number | ''];
|
|
15
9
|
/**
|
|
16
|
-
*
|
|
17
|
-
* the two passed documents/trees.
|
|
10
|
+
* An array of path segments representing a path in a document
|
|
18
11
|
*
|
|
19
|
-
* @param source - The first document/tree to compare
|
|
20
|
-
* @param target - The second document/tree to compare
|
|
21
|
-
* @param opts - Options for the diff generation
|
|
22
|
-
* @returns Array of mutations
|
|
23
12
|
* @public
|
|
24
13
|
*/
|
|
25
|
-
|
|
26
|
-
source: DocumentStub,
|
|
27
|
-
target: DocumentStub,
|
|
28
|
-
options?: PatchOptions,
|
|
29
|
-
): SanityPatchMutation[]
|
|
30
|
-
|
|
14
|
+
type Path = PathSegment[];
|
|
31
15
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
16
|
+
* A Sanity `set` patch mutation operation
|
|
17
|
+
* Replaces the current path, does not merge
|
|
34
18
|
*
|
|
35
|
-
* @param source - The source value to start off with
|
|
36
|
-
* @param target - The target value that the patch operations will aim to create
|
|
37
|
-
* @param basePath - An optional path that will be prefixed to all subsequent patch operations
|
|
38
|
-
* @returns Array of mutations
|
|
39
19
|
* @public
|
|
40
20
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
basePath?: Path,
|
|
45
|
-
): SanityPatchOperations[]
|
|
46
|
-
|
|
21
|
+
interface SanitySetPatchOperation {
|
|
22
|
+
set: Record<string, unknown>;
|
|
23
|
+
}
|
|
47
24
|
/**
|
|
48
|
-
*
|
|
25
|
+
* A Sanity `unset` patch mutation operation
|
|
26
|
+
* Unsets the entire value of the given path
|
|
49
27
|
*
|
|
50
28
|
* @public
|
|
51
29
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
_type?: string
|
|
55
|
-
_rev?: string
|
|
56
|
-
_createdAt?: string
|
|
57
|
-
_updatedAt?: string
|
|
58
|
-
[key: string]: unknown
|
|
30
|
+
interface SanityUnsetPatchOperation {
|
|
31
|
+
unset: string[];
|
|
59
32
|
}
|
|
60
|
-
|
|
61
33
|
/**
|
|
62
|
-
*
|
|
34
|
+
* A Sanity `insert` patch mutation operation
|
|
35
|
+
* Inserts the given items at the given path (before/after)
|
|
63
36
|
*
|
|
64
37
|
* @public
|
|
65
38
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*/
|
|
78
|
-
basePath?: Path
|
|
79
|
-
/**
|
|
80
|
-
* Only apply the patch if the document revision matches this value.
|
|
81
|
-
* If the property is the boolean value `true`, it will attempt to extract
|
|
82
|
-
* the revision from the document `_rev` property.
|
|
83
|
-
*
|
|
84
|
-
* @defaultValue `undefined` (do not apply revision check)
|
|
85
|
-
*/
|
|
86
|
-
ifRevisionID?: string | true
|
|
39
|
+
interface SanityInsertPatchOperation {
|
|
40
|
+
insert: {
|
|
41
|
+
before: string;
|
|
42
|
+
items: unknown[];
|
|
43
|
+
} | {
|
|
44
|
+
after: string;
|
|
45
|
+
items: unknown[];
|
|
46
|
+
} | {
|
|
47
|
+
replace: string;
|
|
48
|
+
items: unknown[];
|
|
49
|
+
};
|
|
87
50
|
}
|
|
88
|
-
|
|
89
51
|
/**
|
|
90
|
-
*
|
|
52
|
+
* A Sanity `diffMatchPatch` patch mutation operation
|
|
53
|
+
* Patches the given path with the given unidiff string.
|
|
91
54
|
*
|
|
92
55
|
* @public
|
|
93
56
|
*/
|
|
94
|
-
|
|
95
|
-
|
|
57
|
+
interface SanityDiffMatchPatchOperation {
|
|
58
|
+
diffMatchPatch: Record<string, string>;
|
|
59
|
+
}
|
|
96
60
|
/**
|
|
97
|
-
*
|
|
61
|
+
* Serializable patch operations that can be applied to a Sanity document.
|
|
98
62
|
*
|
|
99
63
|
* @public
|
|
100
64
|
*/
|
|
101
|
-
|
|
102
|
-
| string
|
|
103
|
-
| number
|
|
104
|
-
| {
|
|
105
|
-
_key: string
|
|
106
|
-
}
|
|
107
|
-
| [number | '', number | '']
|
|
108
|
-
|
|
65
|
+
type SanityPatchOperations = Partial<SanitySetPatchOperation & SanityUnsetPatchOperation & SanityInsertPatchOperation & SanityDiffMatchPatchOperation>;
|
|
109
66
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
67
|
+
* Meant to be used as the body of a {@link SanityPatchMutation}'s `patch` key.
|
|
68
|
+
*
|
|
69
|
+
* Contains additional properties to target a particular ID and optionally add
|
|
70
|
+
* an optimistic lock via [`ifRevisionID`](https://www.sanity.io/docs/content-lake/transactions#k29b2c75639d5).
|
|
112
71
|
*
|
|
113
72
|
* @public
|
|
114
73
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
74
|
+
interface SanityPatch extends SanityPatchOperations {
|
|
75
|
+
id: string;
|
|
76
|
+
ifRevisionID?: string;
|
|
117
77
|
}
|
|
118
|
-
|
|
119
78
|
/**
|
|
120
|
-
* A
|
|
121
|
-
* Inserts the given items at the given path (before/after)
|
|
79
|
+
* A mutation containing a single patch
|
|
122
80
|
*
|
|
123
81
|
* @public
|
|
124
82
|
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
| {
|
|
128
|
-
before: string
|
|
129
|
-
items: unknown[]
|
|
130
|
-
}
|
|
131
|
-
| {
|
|
132
|
-
after: string
|
|
133
|
-
items: unknown[]
|
|
134
|
-
}
|
|
135
|
-
| {
|
|
136
|
-
replace: string
|
|
137
|
-
items: unknown[]
|
|
138
|
-
}
|
|
83
|
+
interface SanityPatchMutation {
|
|
84
|
+
patch: SanityPatch;
|
|
139
85
|
}
|
|
140
|
-
|
|
141
86
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* Contains additional properties to target a particular ID and optionally add
|
|
145
|
-
* an optimistic lock via [`ifRevisionID`](https://www.sanity.io/docs/content-lake/transactions#k29b2c75639d5).
|
|
87
|
+
* Represents a partial Sanity document (eg a "stub").
|
|
146
88
|
*
|
|
147
89
|
* @public
|
|
148
90
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
91
|
+
interface DocumentStub {
|
|
92
|
+
_id?: string;
|
|
93
|
+
_type?: string;
|
|
94
|
+
_rev?: string;
|
|
95
|
+
_createdAt?: string;
|
|
96
|
+
_updatedAt?: string;
|
|
97
|
+
[key: string]: unknown;
|
|
152
98
|
}
|
|
153
|
-
|
|
154
99
|
/**
|
|
155
|
-
*
|
|
100
|
+
* Options for the patch generator
|
|
156
101
|
*
|
|
157
102
|
* @public
|
|
158
103
|
*/
|
|
159
|
-
|
|
160
|
-
|
|
104
|
+
interface PatchOptions {
|
|
105
|
+
/**
|
|
106
|
+
* Document ID to apply the patch to.
|
|
107
|
+
*
|
|
108
|
+
* @defaultValue `undefined` - tries to extract `_id` from passed document
|
|
109
|
+
*/
|
|
110
|
+
id?: string;
|
|
111
|
+
/**
|
|
112
|
+
* Base path to apply the patch to - useful if diffing sub-branches of a document.
|
|
113
|
+
*
|
|
114
|
+
* @defaultValue `[]` - eg root of the document
|
|
115
|
+
*/
|
|
116
|
+
basePath?: Path;
|
|
117
|
+
/**
|
|
118
|
+
* Only apply the patch if the document revision matches this value.
|
|
119
|
+
* If the property is the boolean value `true`, it will attempt to extract
|
|
120
|
+
* the revision from the document `_rev` property.
|
|
121
|
+
*
|
|
122
|
+
* @defaultValue `undefined` (do not apply revision check)
|
|
123
|
+
*/
|
|
124
|
+
ifRevisionID?: string | true;
|
|
161
125
|
}
|
|
162
|
-
|
|
163
126
|
/**
|
|
164
|
-
*
|
|
127
|
+
* Generates an array of mutations for Sanity, based on the differences between
|
|
128
|
+
* the two passed documents/trees.
|
|
165
129
|
*
|
|
130
|
+
* @param source - The first document/tree to compare
|
|
131
|
+
* @param target - The second document/tree to compare
|
|
132
|
+
* @param opts - Options for the diff generation
|
|
133
|
+
* @returns Array of mutations
|
|
166
134
|
* @public
|
|
167
135
|
*/
|
|
168
|
-
export declare
|
|
169
|
-
SanitySetPatchOperation &
|
|
170
|
-
SanityUnsetPatchOperation &
|
|
171
|
-
SanityInsertPatchOperation &
|
|
172
|
-
SanityDiffMatchPatchOperation
|
|
173
|
-
>
|
|
174
|
-
|
|
136
|
+
export declare function diffPatch(source: DocumentStub, target: DocumentStub, options?: PatchOptions): SanityPatchMutation[];
|
|
175
137
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
138
|
+
* Generates an array of patch operation objects for Sanity, based on the
|
|
139
|
+
* differences between the two passed values
|
|
178
140
|
*
|
|
141
|
+
* @param source - The source value to start off with
|
|
142
|
+
* @param target - The target value that the patch operations will aim to create
|
|
143
|
+
* @param basePath - An optional path that will be prefixed to all subsequent patch operations
|
|
144
|
+
* @returns Array of mutations
|
|
179
145
|
* @public
|
|
180
146
|
*/
|
|
181
|
-
declare
|
|
182
|
-
set: Record<string, unknown>
|
|
183
|
-
}
|
|
184
|
-
|
|
147
|
+
export declare function diffValue(source: unknown, target: unknown, basePath?: Path): SanityPatchOperations[];
|
|
185
148
|
/**
|
|
186
|
-
*
|
|
187
|
-
*
|
|
149
|
+
* Represents an error that occurred during a diff process.
|
|
150
|
+
* Contains `path`, `value` and `serializedPath` properties,
|
|
151
|
+
* which is helpful for debugging and showing friendly messages.
|
|
188
152
|
*
|
|
189
153
|
* @public
|
|
190
154
|
*/
|
|
191
|
-
declare
|
|
192
|
-
|
|
155
|
+
export declare class DiffError extends Error {
|
|
156
|
+
path: Path;
|
|
157
|
+
value: unknown;
|
|
158
|
+
serializedPath: string;
|
|
159
|
+
constructor(message: string, path: Path, value?: unknown);
|
|
193
160
|
}
|
|
194
|
-
|
|
195
|
-
|
|
161
|
+
export type { DocumentStub, PatchOptions, Path, PathSegment, SanityPatch, SanityPatchMutation, SanityPatchOperations };
|
|
162
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/paths.ts","../src/patches.ts","../src/diffPatch.ts","../src/diffError.ts"],"mappings":"AAOA;;;;;KAAY;EAGP;;;;;;;KAQO,OAAO;;;;;;;UC2DF;EACf,KAAK;;;;;;;;UASU;EACf;;;;;;;;UASe;EACf;IACK;IAAgB;;IAChB;IAAe;;IACf;IAAiB;;;;;;;;;UASP;EACf,gBAAgB;;;;;;;KAQN,wBAAwB,QAClC,0BACE,4BACA,6BACA;;;;;;;;;UAWa,oBAAoB;EACnC;EACA;;;;;;;UAQe;EACf,OAAO;;;;;;;UClGQ;EACf;EACA;EACA;EACA;EACA;GACC;;;;;;;UAQc;;;;;;EAMf;;;;;;EAOA,WAAW;;;;;;;;EASX;;;;;;;;;;;;wBAac,UACd,QAAQ,cACR,QAAQ,cACR,UAAS,eACR;;;;;;;;;;;wBA0Ca,UACd,iBACA,iBACA,WAAU,OACT;;;;;;;;qBCzIU,kBAAkB;EACtB,MAAM;EACN;EACA;EAEP,YAAY,iBAAiB,MAAM,MAAM"}
|