@sanity/diff-patch 5.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/dist/index.d.ts CHANGED
@@ -1,156 +1,119 @@
1
1
  /**
2
- * Represents an error that occurred during a diff process.
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
- export declare class DiffError extends Error {
9
- path: Path
10
- value: unknown
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
- * Diffs two items and returns an array of patches.
17
- * Note that this is different from `diffPatch`, which generates _mutations_.
10
+ * An array of path segments representing a path in a document
18
11
  *
19
- * @param itemA - The first item to compare
20
- * @param itemB - The second item to compare
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
12
  * @public
26
13
  */
27
- export declare function diffItem(
28
- itemA: unknown,
29
- itemB: unknown,
30
- opts?: DiffOptions,
31
- path?: Path,
32
- patches?: Patch[],
33
- ): Patch[]
34
-
14
+ type Path = PathSegment[];
35
15
  /**
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
16
+ * A Sanity `set` patch mutation operation
17
+ * Replaces the current path, does not merge
39
18
  *
40
19
  * @public
41
20
  */
42
- export declare interface DiffMatchPatch {
43
- op: 'diffMatchPatch'
44
- path: Path
45
- value: string
21
+ interface SanitySetPatchOperation {
22
+ set: Record<string, unknown>;
46
23
  }
47
-
48
24
  /**
49
- * Options for the diff-match-patch algorithm.
25
+ * A Sanity `unset` patch mutation operation
26
+ * Unsets the entire value of the given path
50
27
  *
51
28
  * @public
52
29
  */
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
30
+ interface SanityUnsetPatchOperation {
31
+ unset: string[];
75
32
  }
76
-
77
33
  /**
78
- * Options for diff generation, where all DMP properties are required
34
+ * A Sanity `insert` patch mutation operation
35
+ * Inserts the given items at the given path (before/after)
79
36
  *
80
37
  * @public
81
38
  */
82
- export declare type DiffOptions = PatchOptions & {
83
- diffMatchPatch: Required<DiffMatchPatchOptions>
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
+ };
84
50
  }
85
-
86
51
  /**
87
- * Generates an array of mutations for Sanity, based on the differences between
88
- * the two passed documents/trees.
52
+ * A Sanity `diffMatchPatch` patch mutation operation
53
+ * Patches the given path with the given unidiff string.
89
54
  *
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
93
- * @returns Array of mutations
94
55
  * @public
95
56
  */
96
- export declare function diffPatch(
97
- itemA: DocumentStub,
98
- itemB: DocumentStub,
99
- opts?: PatchOptions,
100
- ): SanityPatchMutation[]
101
-
57
+ interface SanityDiffMatchPatchOperation {
58
+ diffMatchPatch: Record<string, string>;
59
+ }
102
60
  /**
103
- * Represents a partial Sanity document (eg a "stub").
61
+ * Serializable patch operations that can be applied to a Sanity document.
104
62
  *
105
63
  * @public
106
64
  */
107
- export declare interface DocumentStub {
108
- _id?: string
109
- _type?: string
110
- _rev?: string
111
- _createdAt?: string
112
- _updatedAt?: string
113
- [key: string]: unknown
65
+ type SanityPatchOperations = Partial<SanitySetPatchOperation & SanityUnsetPatchOperation & SanityInsertPatchOperation & SanityDiffMatchPatchOperation>;
66
+ /**
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).
71
+ *
72
+ * @public
73
+ */
74
+ interface SanityPatch extends SanityPatchOperations {
75
+ id: string;
76
+ ifRevisionID?: string;
114
77
  }
115
-
116
78
  /**
117
- * A `insert` operation
118
- * Inserts the given items _after_ the given path
119
- * Note: NOT a serializable mutation, see {@link SanityInsertPatch} for that
79
+ * A mutation containing a single patch
120
80
  *
121
81
  * @public
122
82
  */
123
- export declare interface InsertAfterPatch {
124
- op: 'insert'
125
- after: Path
126
- items: any[]
83
+ interface SanityPatchMutation {
84
+ patch: SanityPatch;
127
85
  }
128
-
129
86
  /**
130
- * A patch containing either a Sanity set, unset, insert or diffMatchPatch operation
87
+ * Represents a partial Sanity document (eg a "stub").
131
88
  *
132
89
  * @public
133
90
  */
134
- export declare type Patch = SetPatch | UnsetPatch | InsertAfterPatch | DiffMatchPatch
135
-
91
+ interface DocumentStub {
92
+ _id?: string;
93
+ _type?: string;
94
+ _rev?: string;
95
+ _createdAt?: string;
96
+ _updatedAt?: string;
97
+ [key: string]: unknown;
98
+ }
136
99
  /**
137
100
  * Options for the patch generator
138
101
  *
139
102
  * @public
140
103
  */
141
- export declare interface PatchOptions {
104
+ interface PatchOptions {
142
105
  /**
143
106
  * Document ID to apply the patch to.
144
107
  *
145
108
  * @defaultValue `undefined` - tries to extract `_id` from passed document
146
109
  */
147
- id?: string
110
+ id?: string;
148
111
  /**
149
112
  * Base path to apply the patch to - useful if diffing sub-branches of a document.
150
113
  *
151
114
  * @defaultValue `[]` - eg root of the document
152
115
  */
153
- basePath?: Path
116
+ basePath?: Path;
154
117
  /**
155
118
  * Only apply the patch if the document revision matches this value.
156
119
  * If the property is the boolean value `true`, it will attempt to extract
@@ -158,142 +121,42 @@ export declare interface PatchOptions {
158
121
  *
159
122
  * @defaultValue `undefined` (do not apply revision check)
160
123
  */
161
- 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
- }
173
-
174
- /**
175
- * An array of path segments representing a path in a document
176
- *
177
- * @public
178
- */
179
- export declare type Path = PathSegment[]
180
-
181
- /**
182
- * A segment of a path
183
- *
184
- * @public
185
- */
186
- export declare type PathSegment =
187
- | string
188
- | number
189
- | {
190
- _key: string
191
- }
192
- | [number | '', number | '']
193
-
194
- /**
195
- * A Sanity `diffMatchPatch` patch mutation operation
196
- * Patches the given path with the given unidiff string.
197
- *
198
- * @public
199
- */
200
- export declare interface SanityDiffMatchPatch {
201
- id: string
202
- diffMatchPatch: {
203
- [key: string]: string
204
- }
205
- }
206
-
207
- /**
208
- * A Sanity `insert` patch mutation operation
209
- * Inserts the given items at the given path (before/after)
210
- *
211
- * @public
212
- */
213
- export declare interface SanityInsertPatch {
214
- id: string
215
- insert:
216
- | {
217
- before: string
218
- items: any[]
219
- }
220
- | {
221
- after: string
222
- items: any[]
223
- }
224
- | {
225
- replace: string
226
- items: any[]
227
- }
228
- }
229
-
230
- /**
231
- * A patch containing either a set, unset, insert or diffMatchPatch operation
232
- *
233
- * @public
234
- */
235
- export declare type SanityPatch =
236
- | SanitySetPatch
237
- | SanityUnsetPatch
238
- | SanityInsertPatch
239
- | SanityDiffMatchPatch
240
-
241
- /**
242
- * A mutation containing a single patch
243
- *
244
- * @public
245
- */
246
- export declare interface SanityPatchMutation {
247
- patch: SanityPatch
124
+ ifRevisionID?: string | true;
248
125
  }
249
-
250
126
  /**
251
- * A Sanity `set` patch mutation operation
252
- * Replaces the current path, does not merge
253
- *
254
- * @public
255
- */
256
- export declare interface SanitySetPatch {
257
- id: string
258
- set: {
259
- [key: string]: any
260
- }
261
- }
262
-
263
- /**
264
- * A Sanity `unset` patch mutation operation
265
- * Unsets the entire value of the given path
127
+ * Generates an array of mutations for Sanity, based on the differences between
128
+ * the two passed documents/trees.
266
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
267
134
  * @public
268
135
  */
269
- export declare interface SanityUnsetPatch {
270
- id: string
271
- unset: string[]
272
- }
273
-
136
+ export declare function diffPatch(source: DocumentStub, target: DocumentStub, options?: PatchOptions): SanityPatchMutation[];
274
137
  /**
275
- * A `set` operation
276
- * Replaces the current path, does not merge
277
- * Note: NOT a serializable mutation, see {@link SanitySetPatch} for that
138
+ * Generates an array of patch operation objects for Sanity, based on the
139
+ * differences between the two passed values
278
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
279
145
  * @public
280
146
  */
281
- export declare interface SetPatch {
282
- op: 'set'
283
- path: Path
284
- value: unknown
285
- }
286
-
147
+ export declare function diffValue(source: unknown, target: unknown, basePath?: Path): SanityPatchOperations[];
287
148
  /**
288
- * A `unset` operation
289
- * Unsets the entire value of the given path
290
- * Note: NOT a serializable mutation, see {@link SanityUnsetPatch} for that
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.
291
152
  *
292
153
  * @public
293
154
  */
294
- export declare interface UnsetPatch {
295
- op: 'unset'
296
- path: Path
155
+ export declare class DiffError extends Error {
156
+ path: Path;
157
+ value: unknown;
158
+ serializedPath: string;
159
+ constructor(message: string, path: Path, value?: unknown);
297
160
  }
298
-
299
- export {}
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"}