@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.
- package/dist/index.d.ts +35 -90
- package/dist/index.js +237 -111
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
- package/src/auth/authStore.test.ts +13 -13
- package/src/auth/refreshStampedToken.test.ts +16 -16
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
- package/src/comlink/controller/actions/destroyController.test.ts +2 -2
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
- package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
- package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
- package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
- package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
- package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
- package/src/comlink/node/actions/releaseNode.test.ts +2 -2
- package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
- package/src/config/sanityConfig.ts +8 -3
- package/src/document/actions.ts +11 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +128 -115
- package/src/document/documentStore.ts +40 -10
- package/src/document/permissions.test.ts +9 -9
- package/src/document/permissions.ts +17 -7
- package/src/document/processActions.test.ts +248 -0
- package/src/document/processActions.ts +173 -0
- package/src/document/reducers.ts +13 -6
- package/src/presence/presenceStore.ts +13 -7
- package/src/preview/previewStore.test.ts +10 -2
- package/src/preview/previewStore.ts +2 -1
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
- package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/projection/projectionStore.test.ts +18 -2
- package/src/projection/projectionStore.ts +2 -1
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
- package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/releases/getPerspectiveState.ts +2 -2
- package/src/releases/releasesStore.ts +10 -4
- package/src/store/createActionBinder.test.ts +8 -6
- package/src/store/createActionBinder.ts +44 -29
- package/src/store/createStateSourceAction.test.ts +12 -11
- package/src/store/createStateSourceAction.ts +6 -6
- package/src/store/createStoreInstance.test.ts +29 -16
- package/src/store/createStoreInstance.ts +6 -5
- package/src/store/defineStore.test.ts +1 -1
- package/src/store/defineStore.ts +12 -7
package/src/document/actions.ts
CHANGED
|
@@ -143,7 +143,9 @@ export function createDocument<
|
|
|
143
143
|
return {
|
|
144
144
|
type: 'document.create',
|
|
145
145
|
...doc,
|
|
146
|
-
...(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
|
|
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
|
|
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
|
-
(
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
{
|
|
68
|
-
|
|
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 = {
|