@sanity/sdk 2.4.0 → 2.5.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.
Files changed (47) hide show
  1. package/dist/index.d.ts +35 -90
  2. package/dist/index.js +237 -111
  3. package/dist/index.js.map +1 -1
  4. package/package.json +9 -8
  5. package/src/auth/authStore.test.ts +13 -13
  6. package/src/auth/refreshStampedToken.test.ts +16 -16
  7. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
  8. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
  9. package/src/comlink/controller/actions/destroyController.test.ts +2 -2
  10. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
  11. package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
  12. package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
  13. package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
  14. package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
  15. package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
  16. package/src/comlink/node/actions/releaseNode.test.ts +2 -2
  17. package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
  18. package/src/config/sanityConfig.ts +8 -3
  19. package/src/document/actions.ts +11 -7
  20. package/src/document/applyDocumentActions.test.ts +9 -6
  21. package/src/document/applyDocumentActions.ts +9 -49
  22. package/src/document/documentStore.test.ts +128 -115
  23. package/src/document/documentStore.ts +40 -10
  24. package/src/document/permissions.test.ts +9 -9
  25. package/src/document/permissions.ts +17 -7
  26. package/src/document/processActions.test.ts +248 -0
  27. package/src/document/processActions.ts +173 -0
  28. package/src/document/reducers.ts +13 -6
  29. package/src/presence/presenceStore.ts +13 -7
  30. package/src/preview/previewStore.test.ts +10 -2
  31. package/src/preview/previewStore.ts +2 -1
  32. package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
  33. package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
  34. package/src/projection/projectionStore.test.ts +18 -2
  35. package/src/projection/projectionStore.ts +2 -1
  36. package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
  37. package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
  38. package/src/releases/getPerspectiveState.ts +2 -2
  39. package/src/releases/releasesStore.ts +10 -4
  40. package/src/store/createActionBinder.test.ts +8 -6
  41. package/src/store/createActionBinder.ts +44 -29
  42. package/src/store/createStateSourceAction.test.ts +12 -11
  43. package/src/store/createStateSourceAction.ts +6 -6
  44. package/src/store/createStoreInstance.test.ts +29 -16
  45. package/src/store/createStoreInstance.ts +6 -5
  46. package/src/store/defineStore.test.ts +1 -1
  47. package/src/store/defineStore.ts +12 -7
@@ -143,7 +143,9 @@ export function createDocument<
143
143
  return {
144
144
  type: 'document.create',
145
145
  ...doc,
146
- ...(doc.documentId && {documentId: getPublishedId(doc.documentId)}),
146
+ ...(doc.documentId && {
147
+ documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId),
148
+ }),
147
149
  ...(initialValue && {initialValue}),
148
150
  }
149
151
  }
@@ -164,7 +166,7 @@ export function deleteDocument<
164
166
  return {
165
167
  type: 'document.delete',
166
168
  ...doc,
167
- documentId: getPublishedId(doc.documentId),
169
+ documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId),
168
170
  }
169
171
  }
170
172
 
@@ -228,12 +230,14 @@ export function editDocument<
228
230
  doc: DocumentHandle<TDocumentType, TDataset, TProjectId>,
229
231
  patches?: PatchOperations | PatchOperations[] | SanityMutatePatchMutation,
230
232
  ): EditDocumentAction<TDocumentType, TDataset, TProjectId> {
233
+ const documentId = doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
234
+
231
235
  if (isSanityMutatePatch(patches)) {
232
236
  const converted = convertSanityMutatePatch(patches) ?? []
233
237
  return {
234
238
  ...doc,
235
239
  type: 'document.edit',
236
- documentId: getPublishedId(doc.documentId),
240
+ documentId,
237
241
  patches: converted,
238
242
  }
239
243
  }
@@ -241,7 +245,7 @@ export function editDocument<
241
245
  return {
242
246
  ...doc,
243
247
  type: 'document.edit',
244
- documentId: getPublishedId(doc.documentId),
248
+ documentId,
245
249
  ...(patches && {patches: Array.isArray(patches) ? patches : [patches]}),
246
250
  }
247
251
  }
@@ -262,7 +266,7 @@ export function publishDocument<
262
266
  return {
263
267
  type: 'document.publish',
264
268
  ...doc,
265
- documentId: getPublishedId(doc.documentId),
269
+ documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId),
266
270
  }
267
271
  }
268
272
 
@@ -282,7 +286,7 @@ export function unpublishDocument<
282
286
  return {
283
287
  type: 'document.unpublish',
284
288
  ...doc,
285
- documentId: getPublishedId(doc.documentId),
289
+ documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId),
286
290
  }
287
291
  }
288
292
 
@@ -302,6 +306,6 @@ export function discardDocument<
302
306
  return {
303
307
  type: 'document.discard',
304
308
  ...doc,
305
- documentId: getPublishedId(doc.documentId),
309
+ documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId),
306
310
  }
307
311
  }
@@ -48,11 +48,11 @@ describe('applyDocumentActions', () => {
48
48
  }
49
49
  state = createStoreState(initialState)
50
50
  instance = createSanityInstance({projectId: 'p', dataset: 'd'})
51
+ const key = {name: 'p.d', projectId: 'p', dataset: 'd'}
51
52
 
52
53
  vi.mocked(bindActionByDataset).mockImplementation(
53
- (_storeDef, action) =>
54
- (instanceParam: SanityInstance, ...params: unknown[]) =>
55
- action({instance: instanceParam, state}, ...params),
54
+ (_storeDef, action) => (instanceParam: SanityInstance, options) =>
55
+ action({instance: instanceParam, state, key}, options),
56
56
  )
57
57
  // Import dynamically to ensure mocks are set up before the module under test is loaded
58
58
  const module = await import('./applyDocumentActions')
@@ -72,7 +72,8 @@ describe('applyDocumentActions', () => {
72
72
  }
73
73
 
74
74
  // Call applyDocumentActions with a fixed transactionId for reproducibility.
75
- const applyPromise = applyDocumentActions(instance, action, {
75
+ const applyPromise = applyDocumentActions(instance, {
76
+ actions: [action],
76
77
  transactionId: 'txn-success',
77
78
  })
78
79
 
@@ -128,7 +129,8 @@ describe('applyDocumentActions', () => {
128
129
  }
129
130
 
130
131
  // Call applyDocumentActions with a fixed transactionId.
131
- const applyPromise = applyDocumentActions(instance, action, {
132
+ const applyPromise = applyDocumentActions(instance, {
133
+ actions: [action],
132
134
  transactionId: 'txn-error',
133
135
  })
134
136
 
@@ -160,7 +162,8 @@ describe('applyDocumentActions', () => {
160
162
  dataset: 'd',
161
163
  }
162
164
  // Call applyDocumentActions with the context using childInstance, but with action requiring parent's config
163
- const applyPromise = applyDocumentActions(childInstance, action, {
165
+ const applyPromise = applyDocumentActions(childInstance, {
166
+ actions: [action],
164
167
  transactionId: 'txn-child-match',
165
168
  })
166
169
 
@@ -24,6 +24,11 @@ export interface ActionsResult<TDocument extends SanityDocument = SanityDocument
24
24
 
25
25
  /** @beta */
26
26
  export interface ApplyDocumentActionsOptions {
27
+ /**
28
+ * List of actions to apply.
29
+ */
30
+ actions: DocumentAction[]
31
+
27
32
  /**
28
33
  * Optionally provide an ID to be used as this transaction ID
29
34
  */
@@ -41,16 +46,12 @@ export function applyDocumentActions<
41
46
  TProjectId extends string = string,
42
47
  >(
43
48
  instance: SanityInstance,
44
- action:
45
- | DocumentAction<TDocumentType, TDataset, TProjectId>
46
- | DocumentAction<TDocumentType, TDataset, TProjectId>[],
47
- options?: ApplyDocumentActionsOptions,
49
+ options: ApplyDocumentActionsOptions,
48
50
  ): Promise<ActionsResult<SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>>>
49
51
  /** @beta */
50
52
  export function applyDocumentActions(
51
53
  instance: SanityInstance,
52
- action: DocumentAction | DocumentAction[],
53
- options?: ApplyDocumentActionsOptions,
54
+ options: ApplyDocumentActionsOptions,
54
55
  ): Promise<ActionsResult>
55
56
 
56
57
  /** @beta */
@@ -64,50 +65,9 @@ const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocum
64
65
 
65
66
  /** @internal */
66
67
  async function _applyDocumentActions(
67
- {instance, state}: StoreContext<DocumentStoreState>,
68
- actionOrActions: DocumentAction | DocumentAction[],
69
- {transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions = {},
68
+ {state}: StoreContext<DocumentStoreState>,
69
+ {actions, transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions,
70
70
  ): Promise<ActionsResult> {
71
- const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]
72
-
73
- let projectId
74
- let dataset
75
- for (const action of actions) {
76
- if (action.projectId) {
77
- if (!projectId) projectId = action.projectId
78
- if (action.projectId !== projectId) {
79
- throw new Error(
80
- `Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`,
81
- )
82
- }
83
-
84
- if (action.dataset) {
85
- if (!dataset) dataset = action.dataset
86
- if (action.dataset !== dataset) {
87
- throw new Error(
88
- `Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
89
- )
90
- }
91
- }
92
- }
93
- }
94
-
95
- if (
96
- (projectId && projectId !== instance.config.projectId) ||
97
- (dataset && dataset !== instance.config.dataset)
98
- ) {
99
- const matchedInstance = instance.match({projectId, dataset})
100
- if (!matchedInstance) {
101
- throw new Error(
102
- `Could not find a matching instance for projectId: "${projectId}" and dataset: "${dataset}"`,
103
- )
104
- }
105
- return boundApplyDocumentActions(matchedInstance, actionOrActions, {
106
- disableBatching,
107
- transactionId,
108
- })
109
- }
110
-
111
71
  const {events} = state.get()
112
72
 
113
73
  const transaction: QueuedTransaction = {