@sanity/sdk 2.3.1 → 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 (55) hide show
  1. package/dist/index.d.ts +173 -105
  2. package/dist/index.js +354 -122
  3. package/dist/index.js.map +1 -1
  4. package/package.json +12 -11
  5. package/src/_exports/index.ts +30 -0
  6. package/src/agent/agentActions.test.ts +81 -0
  7. package/src/agent/agentActions.ts +139 -0
  8. package/src/auth/authStore.test.ts +13 -13
  9. package/src/auth/refreshStampedToken.test.ts +16 -16
  10. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
  11. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
  12. package/src/auth/utils.ts +36 -0
  13. package/src/client/clientStore.test.ts +151 -0
  14. package/src/client/clientStore.ts +39 -1
  15. package/src/comlink/controller/actions/destroyController.test.ts +2 -2
  16. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
  17. package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
  18. package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
  19. package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
  20. package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
  21. package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
  22. package/src/comlink/node/actions/releaseNode.test.ts +2 -2
  23. package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
  24. package/src/config/sanityConfig.ts +49 -3
  25. package/src/document/actions.test.ts +34 -0
  26. package/src/document/actions.ts +31 -7
  27. package/src/document/applyDocumentActions.test.ts +9 -6
  28. package/src/document/applyDocumentActions.ts +9 -49
  29. package/src/document/documentStore.test.ts +148 -107
  30. package/src/document/documentStore.ts +40 -10
  31. package/src/document/permissions.test.ts +9 -9
  32. package/src/document/permissions.ts +17 -7
  33. package/src/document/processActions.test.ts +345 -0
  34. package/src/document/processActions.ts +185 -2
  35. package/src/document/reducers.ts +13 -6
  36. package/src/presence/presenceStore.ts +13 -7
  37. package/src/preview/previewStore.test.ts +10 -2
  38. package/src/preview/previewStore.ts +2 -1
  39. package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
  40. package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
  41. package/src/projection/projectionStore.test.ts +18 -2
  42. package/src/projection/projectionStore.ts +2 -1
  43. package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
  44. package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
  45. package/src/query/queryStore.ts +7 -4
  46. package/src/releases/getPerspectiveState.ts +2 -2
  47. package/src/releases/releasesStore.ts +10 -4
  48. package/src/store/createActionBinder.test.ts +8 -6
  49. package/src/store/createActionBinder.ts +50 -14
  50. package/src/store/createStateSourceAction.test.ts +12 -11
  51. package/src/store/createStateSourceAction.ts +6 -6
  52. package/src/store/createStoreInstance.test.ts +29 -16
  53. package/src/store/createStoreInstance.ts +6 -5
  54. package/src/store/defineStore.test.ts +1 -1
  55. package/src/store/defineStore.ts +12 -7
@@ -75,7 +75,7 @@ describe('calculatePermissions', () => {
75
75
  const actions: DocumentAction[] = [
76
76
  {documentId: 'doc1', type: 'document.create', documentType: 'article'},
77
77
  ]
78
- const result = calculatePermissions({instance, state}, actions)
78
+ const result = calculatePermissions({instance, state}, {actions})
79
79
  expect(result).toEqual({allowed: true})
80
80
  })
81
81
 
@@ -91,7 +91,7 @@ describe('calculatePermissions', () => {
91
91
  const actions: DocumentAction[] = [
92
92
  {documentId: 'doc1', type: 'document.create', documentType: 'article'},
93
93
  ]
94
- expect(calculatePermissions({instance, state}, actions)).toBeUndefined()
94
+ expect(calculatePermissions({instance, state}, {actions})).toBeUndefined()
95
95
  })
96
96
 
97
97
  it('should catch PermissionActionError from processActions and return allowed false with a reason', () => {
@@ -107,7 +107,7 @@ describe('calculatePermissions', () => {
107
107
  const actions: DocumentAction[] = [
108
108
  {documentId: 'doc1', type: 'document.create', documentType: 'article'},
109
109
  ]
110
- const result = calculatePermissions({instance, state}, actions)
110
+ const result = calculatePermissions({instance, state}, {actions})
111
111
  expect(result).toBeDefined()
112
112
  expect(result?.allowed).toBe(false)
113
113
  expect(result?.reasons).toEqual(
@@ -135,7 +135,7 @@ describe('calculatePermissions', () => {
135
135
  const actions: DocumentAction[] = [
136
136
  {documentId: 'doc1', documentType: 'book', type: 'document.edit'},
137
137
  ]
138
- const result = calculatePermissions({instance, state}, actions)
138
+ const result = calculatePermissions({instance, state}, {actions})
139
139
  expect(result).toBeDefined()
140
140
  expect(result?.allowed).toBe(false)
141
141
  expect(result?.reasons).toEqual(
@@ -161,7 +161,7 @@ describe('calculatePermissions', () => {
161
161
  const actions: DocumentAction[] = [
162
162
  {documentId: 'doc1', documentType: 'book', type: 'document.edit'},
163
163
  ]
164
- const result = calculatePermissions({instance, state}, actions)
164
+ const result = calculatePermissions({instance, state}, {actions})
165
165
  expect(result).toBeDefined()
166
166
  expect(result?.allowed).toBe(false)
167
167
  expect(result?.reasons).toEqual(
@@ -185,7 +185,7 @@ describe('calculatePermissions', () => {
185
185
  const actions: DocumentAction[] = [
186
186
  {documentId: 'doc1', type: 'document.create', documentType: 'article'},
187
187
  ]
188
- expect(calculatePermissions({instance, state}, actions)).toBeUndefined()
188
+ expect(calculatePermissions({instance, state}, {actions})).toBeUndefined()
189
189
  })
190
190
 
191
191
  it('should catch ActionError from processActions and return a precondition error reason', () => {
@@ -200,7 +200,7 @@ describe('calculatePermissions', () => {
200
200
  const actions: DocumentAction[] = [
201
201
  {documentId: 'doc1', documentType: 'book', type: 'document.delete'},
202
202
  ]
203
- const result = calculatePermissions({instance, state}, actions)
203
+ const result = calculatePermissions({instance, state}, {actions})
204
204
  expect(result).toBeDefined()
205
205
  expect(result?.allowed).toBe(false)
206
206
  expect(result?.reasons).toEqual(
@@ -228,8 +228,8 @@ describe('calculatePermissions', () => {
228
228
  documentType: 'article',
229
229
  }
230
230
  // notice how the action is a copy
231
- const result1 = calculatePermissions({instance, state}, [{...action}])
232
- const result2 = calculatePermissions({instance, state}, [{...action}])
231
+ const result1 = calculatePermissions({instance, state}, {actions: [{...action}]})
232
+ const result2 = calculatePermissions({instance, state}, {actions: [{...action}]})
233
233
  expect(result1).toBe(result2)
234
234
  })
235
235
  })
@@ -58,15 +58,22 @@ const nullReplacer: object = {}
58
58
  const documentsSelector = createSelector(
59
59
  [
60
60
  ({state: {documentStates}}: SelectorContext<SyncTransactionState>) => documentStates,
61
- (_context: SelectorContext<SyncTransactionState>, actions: DocumentAction | DocumentAction[]) =>
61
+ (_context: SelectorContext<SyncTransactionState>, {actions}: {actions: DocumentAction[]}) =>
62
62
  actions,
63
63
  ],
64
64
  (documentStates, actions) => {
65
+ // Collect all document IDs needed for permission checks.
66
+ // Important: liveEdit documents don't have drafts, so we only fetch the single document to avoid waiting for non-existent draft documents.
65
67
  const documentIds = new Set(
66
- (Array.isArray(actions) ? actions : [actions])
67
- .map((i) => i.documentId)
68
- .filter((i) => typeof i === 'string')
69
- .flatMap((documentId) => [getPublishedId(documentId), getDraftId(documentId)]),
68
+ actions
69
+ .map((action) => {
70
+ if (typeof action.documentId !== 'string') return []
71
+ // For liveEdit documents, only fetch the single document
72
+ if (action.liveEdit) return [action.documentId]
73
+ // For standard documents, fetch both draft and published
74
+ return [getPublishedId(action.documentId), getDraftId(action.documentId)]
75
+ })
76
+ .flat(),
70
77
  )
71
78
 
72
79
  const documents: DocumentSet = {}
@@ -99,7 +106,7 @@ const documentsSelector = createSelector(
99
106
  const memoizedActionsSelector = createSelector(
100
107
  [
101
108
  documentsSelector,
102
- (_state: SelectorContext<SyncTransactionState>, actions: DocumentAction | DocumentAction[]) =>
109
+ (_state: SelectorContext<SyncTransactionState>, {actions}: {actions: DocumentAction[]}) =>
103
110
  actions,
104
111
  ],
105
112
  (documents, actions) => {
@@ -203,7 +210,10 @@ const _calculatePermissions = createSelector(
203
210
  // Check edit actions with no patches
204
211
  if (action.type === 'document.edit' && !action.patches?.length) {
205
212
  const docId = action.documentId
206
- const doc = documents[getDraftId(docId)] ?? documents[getPublishedId(docId)]
213
+ // For liveEdit documents, only check the single document
214
+ const doc = action.liveEdit
215
+ ? documents[docId]
216
+ : (documents[getDraftId(docId)] ?? documents[getPublishedId(docId)])
207
217
  if (!doc) {
208
218
  reasons.push({
209
219
  type: 'precondition',
@@ -29,6 +29,16 @@ const defaultGrants = {
29
29
  const transactionId = 'txn-123'
30
30
  const timestamp = '2025-02-02T00:00:00.000Z'
31
31
 
32
+ // Helper: Create a sample liveEdit document
33
+ const createLiveEditDoc = (id: string, title: string, rev: string = 'initial'): SanityDocument => ({
34
+ _id: id,
35
+ _type: 'liveArticle',
36
+ _createdAt: '2025-01-01T00:00:00.000Z',
37
+ _updatedAt: '2025-01-01T00:00:00.000Z',
38
+ _rev: rev,
39
+ title,
40
+ })
41
+
32
42
  describe('processActions', () => {
33
43
  describe('document.create', () => {
34
44
  it('should create a new draft document from a published document', () => {
@@ -138,6 +148,103 @@ describe('processActions', () => {
138
148
  processActions({actions, transactionId, base, working, timestamp, grants}),
139
149
  ).toThrow(/You do not have permission to create a draft for document "doc1"/)
140
150
  })
151
+
152
+ it('should create a draft document with initial values', () => {
153
+ const base: DocumentSet = {}
154
+ const working: DocumentSet = {}
155
+ const actions: DocumentAction[] = [
156
+ {
157
+ documentId: 'doc1',
158
+ type: 'document.create',
159
+ documentType: 'article',
160
+ initialValue: {
161
+ title: 'New Article',
162
+ author: 'John Doe',
163
+ count: 42,
164
+ },
165
+ },
166
+ ]
167
+ const result = processActions({
168
+ actions,
169
+ transactionId,
170
+ base,
171
+ working,
172
+ timestamp,
173
+ grants: defaultGrants,
174
+ })
175
+
176
+ const draftId = 'drafts.doc1'
177
+ const draftDoc = result.working[draftId]
178
+ expect(draftDoc).toBeDefined()
179
+ expect(draftDoc?._id).toBe(draftId)
180
+ expect(draftDoc?._type).toBe('article')
181
+ expect(draftDoc?._rev).toBe(transactionId)
182
+ // Should have the initial values:
183
+ expect(draftDoc?.['title']).toBe('New Article')
184
+ expect(draftDoc?.['author']).toBe('John Doe')
185
+ expect(draftDoc?.['count']).toBe(42)
186
+ })
187
+
188
+ it('should create a draft with initial values that override published document values', () => {
189
+ const published = createDoc('doc1', 'Original Title')
190
+ const base: DocumentSet = {doc1: published}
191
+ const working: DocumentSet = {doc1: published}
192
+ const actions: DocumentAction[] = [
193
+ {
194
+ documentId: 'doc1',
195
+ type: 'document.create',
196
+ documentType: 'article',
197
+ initialValue: {
198
+ title: 'Overridden Title',
199
+ newField: 'New Value',
200
+ },
201
+ },
202
+ ]
203
+ const result = processActions({
204
+ actions,
205
+ transactionId,
206
+ base,
207
+ working,
208
+ timestamp,
209
+ grants: defaultGrants,
210
+ })
211
+
212
+ const draftId = 'drafts.doc1'
213
+ const draftDoc = result.working[draftId]
214
+ expect(draftDoc).toBeDefined()
215
+ // Should have the overridden title from initialValue:
216
+ expect(draftDoc?.['title']).toBe('Overridden Title')
217
+ // Should have the new field:
218
+ expect(draftDoc?.['newField']).toBe('New Value')
219
+ })
220
+
221
+ it('should create a draft with empty initial values object', () => {
222
+ const base: DocumentSet = {}
223
+ const working: DocumentSet = {}
224
+ const actions: DocumentAction[] = [
225
+ {
226
+ documentId: 'doc1',
227
+ type: 'document.create',
228
+ documentType: 'article',
229
+ initialValue: {},
230
+ },
231
+ ]
232
+ const result = processActions({
233
+ actions,
234
+ transactionId,
235
+ base,
236
+ working,
237
+ timestamp,
238
+ grants: defaultGrants,
239
+ })
240
+
241
+ const draftId = 'drafts.doc1'
242
+ const draftDoc = result.working[draftId]
243
+ expect(draftDoc).toBeDefined()
244
+ expect(draftDoc?._id).toBe(draftId)
245
+ expect(draftDoc?._type).toBe('article')
246
+ expect(draftDoc?._rev).toBe(transactionId)
247
+ })
141
248
  })
142
249
 
143
250
  describe('document.delete', () => {
@@ -748,4 +855,242 @@ describe('processActions', () => {
748
855
  ).toThrow(/Unknown action type: "document.unrecognizedAction"/)
749
856
  })
750
857
  })
858
+
859
+ describe('liveEdit documents', () => {
860
+ describe('document.create', () => {
861
+ it('should create a liveEdit document directly without draft logic', () => {
862
+ const base: DocumentSet = {}
863
+ const working: DocumentSet = {}
864
+ const actions: DocumentAction[] = [
865
+ {
866
+ documentId: 'live1',
867
+ type: 'document.create',
868
+ documentType: 'liveArticle',
869
+ liveEdit: true,
870
+ },
871
+ ]
872
+
873
+ const result = processActions({
874
+ actions,
875
+ transactionId,
876
+ base,
877
+ working,
878
+ timestamp,
879
+ grants: defaultGrants,
880
+ })
881
+
882
+ const doc = result.working['live1']
883
+ expect(doc).toBeDefined()
884
+ expect(doc?._id).toBe('live1')
885
+ expect(doc?._type).toBe('liveArticle')
886
+ expect(doc?._rev).toBe(transactionId)
887
+
888
+ // Should use document.create action, not version.create
889
+ expect(result.outgoingActions).toHaveLength(1)
890
+ const action = result.outgoingActions[0]
891
+ expect(action.actionType).toBe('sanity.action.document.create')
892
+ if ('attributes' in action && 'publishedId' in action) {
893
+ expect(action.publishedId).toBe('live1')
894
+ expect(action.attributes._id).toBe('live1')
895
+ expect(action.attributes._type).toBe('liveArticle')
896
+ } else {
897
+ throw new Error('Expected action to have attributes and publishedId')
898
+ }
899
+ })
900
+
901
+ it('should throw an error if liveEdit document already exists', () => {
902
+ const existingDoc = createLiveEditDoc('live1', 'Existing')
903
+ const base: DocumentSet = {live1: existingDoc}
904
+ const working: DocumentSet = {live1: existingDoc}
905
+ const actions: DocumentAction[] = [
906
+ {
907
+ documentId: 'live1',
908
+ type: 'document.create',
909
+ documentType: 'liveArticle',
910
+ liveEdit: true,
911
+ },
912
+ ]
913
+
914
+ expect(() =>
915
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
916
+ ).toThrow('This document already exists')
917
+ })
918
+ })
919
+
920
+ describe('document.edit', () => {
921
+ it('should edit a liveEdit document directly', () => {
922
+ const doc = createLiveEditDoc('live1', 'Original Title')
923
+ const base: DocumentSet = {live1: doc}
924
+ const working: DocumentSet = {live1: doc}
925
+ const actions: DocumentAction[] = [
926
+ {
927
+ documentId: 'live1',
928
+ type: 'document.edit',
929
+ documentType: 'liveArticle',
930
+ liveEdit: true,
931
+ patches: [{set: {title: 'Updated Title'}}],
932
+ },
933
+ ]
934
+
935
+ const result = processActions({
936
+ actions,
937
+ transactionId,
938
+ base,
939
+ working,
940
+ timestamp,
941
+ grants: defaultGrants,
942
+ })
943
+
944
+ const editedDoc = result.working['live1']
945
+ expect(editedDoc).toBeDefined()
946
+ expect(editedDoc?.['title']).toBe('Updated Title')
947
+ expect(editedDoc?._id).toBe('live1')
948
+
949
+ // Should use document.edit action with draftId prefixed (for server validation) but publishedId as the actual doc
950
+ expect(result.outgoingActions[0]).toMatchObject({
951
+ actionType: 'sanity.action.document.edit',
952
+ draftId: 'drafts.live1',
953
+ publishedId: 'live1',
954
+ })
955
+ })
956
+
957
+ it('should throw an error if liveEdit document does not exist', () => {
958
+ const base: DocumentSet = {}
959
+ const working: DocumentSet = {}
960
+ const actions: DocumentAction[] = [
961
+ {
962
+ documentId: 'live1',
963
+ type: 'document.edit',
964
+ documentType: 'liveArticle',
965
+ liveEdit: true,
966
+ patches: [{set: {title: 'New Title'}}],
967
+ },
968
+ ]
969
+
970
+ expect(() =>
971
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
972
+ ).toThrow('Cannot edit document because it does not exist')
973
+ })
974
+ })
975
+
976
+ describe('document.delete', () => {
977
+ it('should delete a liveEdit document directly', () => {
978
+ const doc = createLiveEditDoc('live1', 'To Delete')
979
+ const base: DocumentSet = {live1: doc}
980
+ const working: DocumentSet = {live1: doc}
981
+ const actions: DocumentAction[] = [
982
+ {
983
+ documentId: 'live1',
984
+ type: 'document.delete',
985
+ documentType: 'liveArticle',
986
+ liveEdit: true,
987
+ },
988
+ ]
989
+
990
+ const result = processActions({
991
+ actions,
992
+ transactionId,
993
+ base,
994
+ working,
995
+ timestamp,
996
+ grants: defaultGrants,
997
+ })
998
+
999
+ expect(result.working['live1']).toBeNull()
1000
+
1001
+ expect(result.outgoingActions).toEqual([
1002
+ {
1003
+ actionType: 'sanity.action.document.delete',
1004
+ publishedId: 'live1',
1005
+ },
1006
+ ])
1007
+ })
1008
+
1009
+ it('should throw an error if liveEdit document does not exist', () => {
1010
+ const base: DocumentSet = {}
1011
+ const working: DocumentSet = {}
1012
+ const actions: DocumentAction[] = [
1013
+ {
1014
+ documentId: 'live1',
1015
+ type: 'document.delete',
1016
+ documentType: 'liveArticle',
1017
+ liveEdit: true,
1018
+ },
1019
+ ]
1020
+
1021
+ expect(() =>
1022
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1023
+ ).toThrow('The document you are trying to delete does not exist')
1024
+ })
1025
+ })
1026
+
1027
+ describe('document.publish', () => {
1028
+ it('should throw an error for liveEdit documents', () => {
1029
+ const doc = createLiveEditDoc('live1', 'Title')
1030
+ const base: DocumentSet = {live1: doc}
1031
+ const working: DocumentSet = {live1: doc}
1032
+ const actions: DocumentAction[] = [
1033
+ {
1034
+ documentId: 'live1',
1035
+ type: 'document.publish',
1036
+ documentType: 'liveArticle',
1037
+ liveEdit: true,
1038
+ },
1039
+ ]
1040
+
1041
+ expect(() =>
1042
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1043
+ ).toThrow('Cannot publish liveEdit document')
1044
+ expect(() =>
1045
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1046
+ ).toThrow('LiveEdit documents do not support drafts or publishing')
1047
+ })
1048
+ })
1049
+
1050
+ describe('document.unpublish', () => {
1051
+ it('should throw an error for liveEdit documents', () => {
1052
+ const doc = createLiveEditDoc('live1', 'Title')
1053
+ const base: DocumentSet = {live1: doc}
1054
+ const working: DocumentSet = {live1: doc}
1055
+ const actions: DocumentAction[] = [
1056
+ {
1057
+ documentId: 'live1',
1058
+ type: 'document.unpublish',
1059
+ documentType: 'liveArticle',
1060
+ liveEdit: true,
1061
+ },
1062
+ ]
1063
+
1064
+ expect(() =>
1065
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1066
+ ).toThrow('Cannot unpublish liveEdit document')
1067
+ expect(() =>
1068
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1069
+ ).toThrow('LiveEdit documents do not support drafts or publishing')
1070
+ })
1071
+ })
1072
+
1073
+ describe('document.discard', () => {
1074
+ it('should throw an error for liveEdit documents', () => {
1075
+ const doc = createLiveEditDoc('live1', 'Title')
1076
+ const base: DocumentSet = {live1: doc}
1077
+ const working: DocumentSet = {live1: doc}
1078
+ const actions: DocumentAction[] = [
1079
+ {
1080
+ documentId: 'live1',
1081
+ type: 'document.discard',
1082
+ documentType: 'liveArticle',
1083
+ liveEdit: true,
1084
+ },
1085
+ ]
1086
+
1087
+ expect(() =>
1088
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1089
+ ).toThrow('Cannot discard changes for liveEdit document')
1090
+ expect(() =>
1091
+ processActions({actions, transactionId, base, working, timestamp, grants: defaultGrants}),
1092
+ ).toThrow('LiveEdit documents do not support drafts')
1093
+ })
1094
+ })
1095
+ })
751
1096
  })