@sanity/diff-patch 5.0.0 → 6.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/dist/index.d.cts CHANGED
@@ -13,91 +13,36 @@ export declare class DiffError extends Error {
13
13
  }
14
14
 
15
15
  /**
16
- * Diffs two items and returns an array of patches.
17
- * Note that this is different from `diffPatch`, which generates _mutations_.
16
+ * Generates an array of mutations for Sanity, based on the differences between
17
+ * the two passed documents/trees.
18
18
  *
19
- * @param itemA - The first item to compare
20
- * @param itemB - The second item to compare
19
+ * @param source - The first document/tree to compare
20
+ * @param target - The second document/tree to compare
21
21
  * @param opts - Options for the diff generation
22
- * @param path - Path to the current item
23
- * @param patches - Array of patches to append the results to. Note that this is MUTATED.
24
- * @returns Array of patches
25
- * @public
26
- */
27
- export declare function diffItem(
28
- itemA: unknown,
29
- itemB: unknown,
30
- opts?: DiffOptions,
31
- path?: Path,
32
- patches?: Patch[],
33
- ): Patch[]
34
-
35
- /**
36
- * A `diffMatchPatch` operation
37
- * Applies the given `value` (unidiff format) to the given path. Must be a string.
38
- * Note: NOT a serializable mutation, see {@link SanityDiffMatchPatch} for that
39
- *
40
- * @public
41
- */
42
- export declare interface DiffMatchPatch {
43
- op: 'diffMatchPatch'
44
- path: Path
45
- value: string
46
- }
47
-
48
- /**
49
- * Options for the diff-match-patch algorithm.
50
- *
51
- * @public
52
- */
53
- export declare interface DiffMatchPatchOptions {
54
- /**
55
- * Whether or not to use diff-match-patch at all
56
- *
57
- * @defaultValue `true`
58
- */
59
- enabled: boolean
60
- /**
61
- * Threshold at which to start using diff-match-patch instead of a regular `set` patch.
62
- *
63
- * @defaultValue `30`
64
- */
65
- lengthThresholdAbsolute: number
66
- /**
67
- * Only use generated diff-match-patch if the patch length is less than or equal to
68
- * (targetString * relative). Example: A 100 character target with a relative factor
69
- * of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
70
- * it will fall back to a regular `set` patch.
71
- *
72
- * @defaultValue `1.2`
73
- */
74
- lengthThresholdRelative: number
75
- }
76
-
77
- /**
78
- * Options for diff generation, where all DMP properties are required
79
- *
22
+ * @returns Array of mutations
80
23
  * @public
81
24
  */
82
- export declare type DiffOptions = PatchOptions & {
83
- diffMatchPatch: Required<DiffMatchPatchOptions>
84
- }
25
+ export declare function diffPatch(
26
+ source: DocumentStub,
27
+ target: DocumentStub,
28
+ options?: PatchOptions,
29
+ ): SanityPatchMutation[]
85
30
 
86
31
  /**
87
- * Generates an array of mutations for Sanity, based on the differences between
88
- * the two passed documents/trees.
32
+ * Generates an array of patch operation objects for Sanity, based on the
33
+ * differences between the two passed values
89
34
  *
90
- * @param itemA - The first document/tree to compare
91
- * @param itemB - The second document/tree to compare
92
- * @param opts - Options for the diff generation
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
93
38
  * @returns Array of mutations
94
39
  * @public
95
40
  */
96
- export declare function diffPatch(
97
- itemA: DocumentStub,
98
- itemB: DocumentStub,
99
- opts?: PatchOptions,
100
- ): SanityPatchMutation[]
41
+ export declare function diffValue(
42
+ source: unknown,
43
+ target: unknown,
44
+ basePath?: Path,
45
+ ): SanityPatchOperations[]
101
46
 
102
47
  /**
103
48
  * Represents a partial Sanity document (eg a "stub").
@@ -113,26 +58,6 @@ export declare interface DocumentStub {
113
58
  [key: string]: unknown
114
59
  }
115
60
 
116
- /**
117
- * A `insert` operation
118
- * Inserts the given items _after_ the given path
119
- * Note: NOT a serializable mutation, see {@link SanityInsertPatch} for that
120
- *
121
- * @public
122
- */
123
- export declare interface InsertAfterPatch {
124
- op: 'insert'
125
- after: Path
126
- items: any[]
127
- }
128
-
129
- /**
130
- * A patch containing either a Sanity set, unset, insert or diffMatchPatch operation
131
- *
132
- * @public
133
- */
134
- export declare type Patch = SetPatch | UnsetPatch | InsertAfterPatch | DiffMatchPatch
135
-
136
61
  /**
137
62
  * Options for the patch generator
138
63
  *
@@ -159,16 +84,6 @@ export declare interface PatchOptions {
159
84
  * @defaultValue `undefined` (do not apply revision check)
160
85
  */
161
86
  ifRevisionID?: string | true
162
- /**
163
- * Whether or not to hide warnings during the diff process.
164
- *
165
- * @defaultValue `false`
166
- */
167
- hideWarnings?: boolean
168
- /**
169
- * Options for the diff-match-patch algorithm.
170
- */
171
- diffMatchPatch?: Partial<DiffMatchPatchOptions>
172
87
  }
173
88
 
174
89
  /**
@@ -197,11 +112,8 @@ export declare type PathSegment =
197
112
  *
198
113
  * @public
199
114
  */
200
- export declare interface SanityDiffMatchPatch {
201
- id: string
202
- diffMatchPatch: {
203
- [key: string]: string
204
- }
115
+ declare interface SanityDiffMatchPatchOperation {
116
+ diffMatchPatch: Record<string, string>
205
117
  }
206
118
 
207
119
  /**
@@ -210,33 +122,34 @@ export declare interface SanityDiffMatchPatch {
210
122
  *
211
123
  * @public
212
124
  */
213
- export declare interface SanityInsertPatch {
214
- id: string
125
+ declare interface SanityInsertPatchOperation {
215
126
  insert:
216
127
  | {
217
128
  before: string
218
- items: any[]
129
+ items: unknown[]
219
130
  }
220
131
  | {
221
132
  after: string
222
- items: any[]
133
+ items: unknown[]
223
134
  }
224
135
  | {
225
136
  replace: string
226
- items: any[]
137
+ items: unknown[]
227
138
  }
228
139
  }
229
140
 
230
141
  /**
231
- * A patch containing either a set, unset, insert or diffMatchPatch operation
142
+ * Meant to be used as the body of a {@link SanityPatchMutation}'s `patch` key.
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).
232
146
  *
233
147
  * @public
234
148
  */
235
- export declare type SanityPatch =
236
- | SanitySetPatch
237
- | SanityUnsetPatch
238
- | SanityInsertPatch
239
- | SanityDiffMatchPatch
149
+ export declare interface SanityPatch extends SanityPatchOperations {
150
+ id: string
151
+ ifRevisionID?: string
152
+ }
240
153
 
241
154
  /**
242
155
  * A mutation containing a single patch
@@ -248,52 +161,35 @@ export declare interface SanityPatchMutation {
248
161
  }
249
162
 
250
163
  /**
251
- * A Sanity `set` patch mutation operation
252
- * Replaces the current path, does not merge
164
+ * Serializable patch operations that can be applied to a Sanity document.
253
165
  *
254
166
  * @public
255
167
  */
256
- export declare interface SanitySetPatch {
257
- id: string
258
- set: {
259
- [key: string]: any
260
- }
261
- }
168
+ export declare type SanityPatchOperations = Partial<
169
+ SanitySetPatchOperation &
170
+ SanityUnsetPatchOperation &
171
+ SanityInsertPatchOperation &
172
+ SanityDiffMatchPatchOperation
173
+ >
262
174
 
263
175
  /**
264
- * A Sanity `unset` patch mutation operation
265
- * Unsets the entire value of the given path
266
- *
267
- * @public
268
- */
269
- export declare interface SanityUnsetPatch {
270
- id: string
271
- unset: string[]
272
- }
273
-
274
- /**
275
- * A `set` operation
176
+ * A Sanity `set` patch mutation operation
276
177
  * Replaces the current path, does not merge
277
- * Note: NOT a serializable mutation, see {@link SanitySetPatch} for that
278
178
  *
279
179
  * @public
280
180
  */
281
- export declare interface SetPatch {
282
- op: 'set'
283
- path: Path
284
- value: unknown
181
+ declare interface SanitySetPatchOperation {
182
+ set: Record<string, unknown>
285
183
  }
286
184
 
287
185
  /**
288
- * A `unset` operation
186
+ * A Sanity `unset` patch mutation operation
289
187
  * Unsets the entire value of the given path
290
- * Note: NOT a serializable mutation, see {@link SanityUnsetPatch} for that
291
188
  *
292
189
  * @public
293
190
  */
294
- export declare interface UnsetPatch {
295
- op: 'unset'
296
- path: Path
191
+ declare interface SanityUnsetPatchOperation {
192
+ unset: string[]
297
193
  }
298
194
 
299
195
  export {}
package/dist/index.d.ts CHANGED
@@ -13,91 +13,36 @@ export declare class DiffError extends Error {
13
13
  }
14
14
 
15
15
  /**
16
- * Diffs two items and returns an array of patches.
17
- * Note that this is different from `diffPatch`, which generates _mutations_.
16
+ * Generates an array of mutations for Sanity, based on the differences between
17
+ * the two passed documents/trees.
18
18
  *
19
- * @param itemA - The first item to compare
20
- * @param itemB - The second item to compare
19
+ * @param source - The first document/tree to compare
20
+ * @param target - The second document/tree to compare
21
21
  * @param opts - Options for the diff generation
22
- * @param path - Path to the current item
23
- * @param patches - Array of patches to append the results to. Note that this is MUTATED.
24
- * @returns Array of patches
25
- * @public
26
- */
27
- export declare function diffItem(
28
- itemA: unknown,
29
- itemB: unknown,
30
- opts?: DiffOptions,
31
- path?: Path,
32
- patches?: Patch[],
33
- ): Patch[]
34
-
35
- /**
36
- * A `diffMatchPatch` operation
37
- * Applies the given `value` (unidiff format) to the given path. Must be a string.
38
- * Note: NOT a serializable mutation, see {@link SanityDiffMatchPatch} for that
39
- *
40
- * @public
41
- */
42
- export declare interface DiffMatchPatch {
43
- op: 'diffMatchPatch'
44
- path: Path
45
- value: string
46
- }
47
-
48
- /**
49
- * Options for the diff-match-patch algorithm.
50
- *
51
- * @public
52
- */
53
- export declare interface DiffMatchPatchOptions {
54
- /**
55
- * Whether or not to use diff-match-patch at all
56
- *
57
- * @defaultValue `true`
58
- */
59
- enabled: boolean
60
- /**
61
- * Threshold at which to start using diff-match-patch instead of a regular `set` patch.
62
- *
63
- * @defaultValue `30`
64
- */
65
- lengthThresholdAbsolute: number
66
- /**
67
- * Only use generated diff-match-patch if the patch length is less than or equal to
68
- * (targetString * relative). Example: A 100 character target with a relative factor
69
- * of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
70
- * it will fall back to a regular `set` patch.
71
- *
72
- * @defaultValue `1.2`
73
- */
74
- lengthThresholdRelative: number
75
- }
76
-
77
- /**
78
- * Options for diff generation, where all DMP properties are required
79
- *
22
+ * @returns Array of mutations
80
23
  * @public
81
24
  */
82
- export declare type DiffOptions = PatchOptions & {
83
- diffMatchPatch: Required<DiffMatchPatchOptions>
84
- }
25
+ export declare function diffPatch(
26
+ source: DocumentStub,
27
+ target: DocumentStub,
28
+ options?: PatchOptions,
29
+ ): SanityPatchMutation[]
85
30
 
86
31
  /**
87
- * Generates an array of mutations for Sanity, based on the differences between
88
- * the two passed documents/trees.
32
+ * Generates an array of patch operation objects for Sanity, based on the
33
+ * differences between the two passed values
89
34
  *
90
- * @param itemA - The first document/tree to compare
91
- * @param itemB - The second document/tree to compare
92
- * @param opts - Options for the diff generation
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
93
38
  * @returns Array of mutations
94
39
  * @public
95
40
  */
96
- export declare function diffPatch(
97
- itemA: DocumentStub,
98
- itemB: DocumentStub,
99
- opts?: PatchOptions,
100
- ): SanityPatchMutation[]
41
+ export declare function diffValue(
42
+ source: unknown,
43
+ target: unknown,
44
+ basePath?: Path,
45
+ ): SanityPatchOperations[]
101
46
 
102
47
  /**
103
48
  * Represents a partial Sanity document (eg a "stub").
@@ -113,26 +58,6 @@ export declare interface DocumentStub {
113
58
  [key: string]: unknown
114
59
  }
115
60
 
116
- /**
117
- * A `insert` operation
118
- * Inserts the given items _after_ the given path
119
- * Note: NOT a serializable mutation, see {@link SanityInsertPatch} for that
120
- *
121
- * @public
122
- */
123
- export declare interface InsertAfterPatch {
124
- op: 'insert'
125
- after: Path
126
- items: any[]
127
- }
128
-
129
- /**
130
- * A patch containing either a Sanity set, unset, insert or diffMatchPatch operation
131
- *
132
- * @public
133
- */
134
- export declare type Patch = SetPatch | UnsetPatch | InsertAfterPatch | DiffMatchPatch
135
-
136
61
  /**
137
62
  * Options for the patch generator
138
63
  *
@@ -159,16 +84,6 @@ export declare interface PatchOptions {
159
84
  * @defaultValue `undefined` (do not apply revision check)
160
85
  */
161
86
  ifRevisionID?: string | true
162
- /**
163
- * Whether or not to hide warnings during the diff process.
164
- *
165
- * @defaultValue `false`
166
- */
167
- hideWarnings?: boolean
168
- /**
169
- * Options for the diff-match-patch algorithm.
170
- */
171
- diffMatchPatch?: Partial<DiffMatchPatchOptions>
172
87
  }
173
88
 
174
89
  /**
@@ -197,11 +112,8 @@ export declare type PathSegment =
197
112
  *
198
113
  * @public
199
114
  */
200
- export declare interface SanityDiffMatchPatch {
201
- id: string
202
- diffMatchPatch: {
203
- [key: string]: string
204
- }
115
+ declare interface SanityDiffMatchPatchOperation {
116
+ diffMatchPatch: Record<string, string>
205
117
  }
206
118
 
207
119
  /**
@@ -210,33 +122,34 @@ export declare interface SanityDiffMatchPatch {
210
122
  *
211
123
  * @public
212
124
  */
213
- export declare interface SanityInsertPatch {
214
- id: string
125
+ declare interface SanityInsertPatchOperation {
215
126
  insert:
216
127
  | {
217
128
  before: string
218
- items: any[]
129
+ items: unknown[]
219
130
  }
220
131
  | {
221
132
  after: string
222
- items: any[]
133
+ items: unknown[]
223
134
  }
224
135
  | {
225
136
  replace: string
226
- items: any[]
137
+ items: unknown[]
227
138
  }
228
139
  }
229
140
 
230
141
  /**
231
- * A patch containing either a set, unset, insert or diffMatchPatch operation
142
+ * Meant to be used as the body of a {@link SanityPatchMutation}'s `patch` key.
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).
232
146
  *
233
147
  * @public
234
148
  */
235
- export declare type SanityPatch =
236
- | SanitySetPatch
237
- | SanityUnsetPatch
238
- | SanityInsertPatch
239
- | SanityDiffMatchPatch
149
+ export declare interface SanityPatch extends SanityPatchOperations {
150
+ id: string
151
+ ifRevisionID?: string
152
+ }
240
153
 
241
154
  /**
242
155
  * A mutation containing a single patch
@@ -248,52 +161,35 @@ export declare interface SanityPatchMutation {
248
161
  }
249
162
 
250
163
  /**
251
- * A Sanity `set` patch mutation operation
252
- * Replaces the current path, does not merge
164
+ * Serializable patch operations that can be applied to a Sanity document.
253
165
  *
254
166
  * @public
255
167
  */
256
- export declare interface SanitySetPatch {
257
- id: string
258
- set: {
259
- [key: string]: any
260
- }
261
- }
168
+ export declare type SanityPatchOperations = Partial<
169
+ SanitySetPatchOperation &
170
+ SanityUnsetPatchOperation &
171
+ SanityInsertPatchOperation &
172
+ SanityDiffMatchPatchOperation
173
+ >
262
174
 
263
175
  /**
264
- * A Sanity `unset` patch mutation operation
265
- * Unsets the entire value of the given path
266
- *
267
- * @public
268
- */
269
- export declare interface SanityUnsetPatch {
270
- id: string
271
- unset: string[]
272
- }
273
-
274
- /**
275
- * A `set` operation
176
+ * A Sanity `set` patch mutation operation
276
177
  * Replaces the current path, does not merge
277
- * Note: NOT a serializable mutation, see {@link SanitySetPatch} for that
278
178
  *
279
179
  * @public
280
180
  */
281
- export declare interface SetPatch {
282
- op: 'set'
283
- path: Path
284
- value: unknown
181
+ declare interface SanitySetPatchOperation {
182
+ set: Record<string, unknown>
285
183
  }
286
184
 
287
185
  /**
288
- * A `unset` operation
186
+ * A Sanity `unset` patch mutation operation
289
187
  * Unsets the entire value of the given path
290
- * Note: NOT a serializable mutation, see {@link SanityUnsetPatch} for that
291
188
  *
292
189
  * @public
293
190
  */
294
- export declare interface UnsetPatch {
295
- op: 'unset'
296
- path: Path
191
+ declare interface SanityUnsetPatchOperation {
192
+ unset: string[]
297
193
  }
298
194
 
299
195
  export {}