@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 CHANGED
@@ -1,6 +1,9 @@
1
1
  # @sanity/diff-patch
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@sanity/diff-patch.svg?style=flat-square)](https://www.npmjs.com/package/@sanity/diff-patch)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/@sanity/diff-patch?style=flat-square)](https://bundlephobia.com/result?p=@sanity/diff-patch)[![npm weekly downloads](https://img.shields.io/npm/dw/@sanity/diff-patch.svg?style=flat-square)](https://www.npmjs.com/package/@sanity/diff-patch)
3
+ [![Latest version](https://npmx.dev/api/registry/badge/version/@sanity/diff-patch?color=69E3EE)](https://npmx.dev/package/@sanity/diff-patch)
4
+ [![Number of dependencies](https://npmx.dev/api/registry/badge/dependencies/@sanity/diff-patch?color=69E3EE)](https://npmx.dev/package/@sanity/diff-patch)
5
+ [![Supported node versions](https://npmx.dev/api/registry/badge/engines/@sanity/diff-patch?color=69E3EE)](https://npmx.dev/package/@sanity/diff-patch)
6
+ [![Downloads per month](https://npmx.dev/api/registry/badge/downloads/@sanity/diff-patch?color=69E3EE)](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
- * 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
- * Generates an array of mutations for Sanity, based on the differences between
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
- export declare function diffPatch(
26
- source: DocumentStub,
27
- target: DocumentStub,
28
- options?: PatchOptions,
29
- ): SanityPatchMutation[]
30
-
14
+ type Path = PathSegment[];
31
15
  /**
32
- * Generates an array of patch operation objects for Sanity, based on the
33
- * differences between the two passed values
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
- export declare function diffValue(
42
- source: unknown,
43
- target: unknown,
44
- basePath?: Path,
45
- ): SanityPatchOperations[]
46
-
21
+ interface SanitySetPatchOperation {
22
+ set: Record<string, unknown>;
23
+ }
47
24
  /**
48
- * Represents a partial Sanity document (eg a "stub").
25
+ * A Sanity `unset` patch mutation operation
26
+ * Unsets the entire value of the given path
49
27
  *
50
28
  * @public
51
29
  */
52
- export declare interface DocumentStub {
53
- _id?: string
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
- * Options for the patch generator
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
- export declare interface PatchOptions {
67
- /**
68
- * Document ID to apply the patch to.
69
- *
70
- * @defaultValue `undefined` - tries to extract `_id` from passed document
71
- */
72
- id?: string
73
- /**
74
- * Base path to apply the patch to - useful if diffing sub-branches of a document.
75
- *
76
- * @defaultValue `[]` - eg root of the document
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
- * An array of path segments representing a path in a document
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
- export declare type Path = PathSegment[]
95
-
57
+ interface SanityDiffMatchPatchOperation {
58
+ diffMatchPatch: Record<string, string>;
59
+ }
96
60
  /**
97
- * A segment of a path
61
+ * Serializable patch operations that can be applied to a Sanity document.
98
62
  *
99
63
  * @public
100
64
  */
101
- export declare type PathSegment =
102
- | string
103
- | number
104
- | {
105
- _key: string
106
- }
107
- | [number | '', number | '']
108
-
65
+ type SanityPatchOperations = Partial<SanitySetPatchOperation & SanityUnsetPatchOperation & SanityInsertPatchOperation & SanityDiffMatchPatchOperation>;
109
66
  /**
110
- * A Sanity `diffMatchPatch` patch mutation operation
111
- * Patches the given path with the given unidiff string.
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
- declare interface SanityDiffMatchPatchOperation {
116
- diffMatchPatch: Record<string, string>
74
+ interface SanityPatch extends SanityPatchOperations {
75
+ id: string;
76
+ ifRevisionID?: string;
117
77
  }
118
-
119
78
  /**
120
- * A Sanity `insert` patch mutation operation
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
- declare interface SanityInsertPatchOperation {
126
- insert:
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
- * 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).
87
+ * Represents a partial Sanity document (eg a "stub").
146
88
  *
147
89
  * @public
148
90
  */
149
- export declare interface SanityPatch extends SanityPatchOperations {
150
- id: string
151
- ifRevisionID?: string
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
- * A mutation containing a single patch
100
+ * Options for the patch generator
156
101
  *
157
102
  * @public
158
103
  */
159
- export declare interface SanityPatchMutation {
160
- patch: SanityPatch
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
- * Serializable patch operations that can be applied to a Sanity document.
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 type SanityPatchOperations = Partial<
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
- * A Sanity `set` patch mutation operation
177
- * Replaces the current path, does not merge
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 interface SanitySetPatchOperation {
182
- set: Record<string, unknown>
183
- }
184
-
147
+ export declare function diffValue(source: unknown, target: unknown, basePath?: Path): SanityPatchOperations[];
185
148
  /**
186
- * A Sanity `unset` patch mutation operation
187
- * Unsets the entire value of the given path
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 interface SanityUnsetPatchOperation {
192
- unset: string[]
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
- 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"}