Skip to content

Commit 1a064d4

Browse files
authoredMar 14, 2025··
fix(core): updating document version actions to match deviated API (#8913)
1 parent 6cffca3 commit 1a064d4

File tree

2 files changed

+191
-116
lines changed

2 files changed

+191
-116
lines changed
 

‎packages/sanity/src/core/releases/store/__tests__/createReleaseOperationsStore.test.ts

+165-95
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,38 @@ describe('createReleaseOperationsStore', () => {
205205
},
206206
})
207207

208-
expect(mockClient.create).toHaveBeenNthCalledWith(
209-
1,
210-
{
211-
_id: `versions.${revertReleaseId}.doc1`,
208+
expect(mockClient.request).toHaveBeenNthCalledWith(1, {
209+
body: {
210+
actions: [
211+
{
212+
actionType: 'sanity.action.release.create',
213+
metadata: {
214+
description: 'A reverted release',
215+
releaseType: 'asap',
216+
title: 'Revert Release',
217+
},
218+
releaseId: 'revert-release-id',
219+
},
220+
],
212221
},
213-
undefined,
214-
)
215-
expect(mockClient.create).toHaveBeenNthCalledWith(
216-
2,
217-
{
218-
_id: `versions.${revertReleaseId}.doc2`,
222+
method: 'POST',
223+
uri: '/data/actions/test-dataset',
224+
})
225+
expect(mockClient.request).toHaveBeenNthCalledWith(2, {
226+
body: {
227+
actions: [
228+
{
229+
actionType: 'sanity.action.document.version.create',
230+
document: {
231+
_id: 'versions.revert-release-id.doc1',
232+
},
233+
publishedId: 'doc1',
234+
},
235+
],
219236
},
220-
undefined,
221-
)
237+
method: 'POST',
238+
uri: '/data/actions/test-dataset',
239+
})
222240

223241
expect(mockClient.request).toHaveBeenCalledWith({
224242
uri: '/data/actions/test-dataset',
@@ -256,8 +274,7 @@ describe('createReleaseOperationsStore', () => {
256274
},
257275
})
258276

259-
expect(mockClient.create).toHaveBeenCalledTimes(2)
260-
expect(mockClient.request).toHaveBeenCalledTimes(1)
277+
expect(mockClient.request).toHaveBeenCalledTimes(3)
261278
})
262279

263280
it('should fail if a document does not exist and no initial value is provided', async () => {
@@ -284,21 +301,55 @@ describe('createReleaseOperationsStore', () => {
284301
)
285302

286303
expect(result).toBeUndefined()
287-
expect(mockClient.create).toHaveBeenCalledTimes(2)
288-
expect(mockClient.create).toHaveBeenNthCalledWith(
289-
1,
290-
{
291-
_id: `versions.${revertReleaseId}.doc1`,
304+
expect(mockClient.request).toHaveBeenCalledTimes(3)
305+
expect(mockClient.request).toHaveBeenNthCalledWith(1, {
306+
body: {
307+
actions: [
308+
{
309+
actionType: 'sanity.action.release.create',
310+
metadata: {
311+
description: 'A reverted release',
312+
releaseType: 'asap',
313+
title: 'Revert Release',
314+
},
315+
releaseId: 'revert-release-id',
316+
},
317+
],
292318
},
293-
undefined,
294-
)
295-
expect(mockClient.create).toHaveBeenNthCalledWith(
296-
2,
297-
{
298-
_id: `versions.${revertReleaseId}.doc2`,
319+
method: 'POST',
320+
uri: '/data/actions/test-dataset',
321+
})
322+
323+
expect(mockClient.request).toHaveBeenNthCalledWith(2, {
324+
body: {
325+
actions: [
326+
{
327+
actionType: 'sanity.action.document.version.create',
328+
document: {
329+
_id: 'versions.revert-release-id.doc1',
330+
},
331+
publishedId: 'doc1',
332+
},
333+
],
299334
},
300-
undefined,
301-
)
335+
method: 'POST',
336+
uri: '/data/actions/test-dataset',
337+
})
338+
expect(mockClient.request).toHaveBeenNthCalledWith(3, {
339+
body: {
340+
actions: [
341+
{
342+
actionType: 'sanity.action.document.version.create',
343+
document: {
344+
_id: 'versions.revert-release-id.doc2',
345+
},
346+
publishedId: 'doc2',
347+
},
348+
],
349+
},
350+
method: 'POST',
351+
uri: '/data/actions/test-dataset',
352+
})
302353
})
303354

304355
it('should throw an error if creating the release fails', async () => {
@@ -314,14 +365,23 @@ describe('createReleaseOperationsStore', () => {
314365
const store = createStore()
315366
mockClient.getDocument.mockResolvedValue({_id: 'doc-id', data: 'example'})
316367
await store.createVersion('release-id', 'doc-id', {newData: 'value'})
317-
expect(mockClient.create).toHaveBeenCalledWith(
318-
{
319-
_id: `versions.release-id.doc-id`,
320-
data: 'example',
321-
newData: 'value',
368+
expect(mockClient.request).toHaveBeenCalledWith({
369+
body: {
370+
actions: [
371+
{
372+
actionType: 'sanity.action.document.version.create',
373+
document: {
374+
_id: `versions.release-id.doc-id`,
375+
data: 'example',
376+
newData: 'value',
377+
},
378+
publishedId: 'doc-id',
379+
},
380+
],
322381
},
323-
undefined,
324-
)
382+
method: 'POST',
383+
uri: '/data/actions/test-dataset',
384+
})
325385
})
326386

327387
it('should omit _weak from reference fields if _strengthenOnPublish is present when it creates a version of a document', async () => {
@@ -411,85 +471,94 @@ describe('createReleaseOperationsStore', () => {
411471

412472
await store.createVersion('release-id', 'doc-id')
413473

414-
expect(mockClient.create).toHaveBeenCalledWith(
415-
{
416-
_id: `versions.release-id.doc-id`,
417-
artist: {
418-
_ref: 'some-artist-id',
419-
_strengthenOnPublish: {
420-
template: {
421-
id: 'artist',
422-
},
423-
type: 'artist',
424-
},
425-
_type: 'reference',
426-
},
427-
expectedWeakReference: {
428-
_ref: 'expected-weak-reference',
429-
_type: 'reference',
430-
_weak: true,
431-
_strengthenOnPublish: {
432-
template: {
433-
id: 'some-document',
434-
},
435-
type: 'some-document',
436-
weak: true,
437-
},
438-
},
439-
plants: [
474+
expect(mockClient.request).toHaveBeenCalledWith({
475+
body: {
476+
actions: [
440477
{
441-
_ref: 'some-plant-id',
442-
_strengthenOnPublish: {
443-
template: {
444-
id: 'plant',
478+
actionType: 'sanity.action.document.version.create',
479+
document: {
480+
_id: `versions.release-id.doc-id`,
481+
artist: {
482+
_ref: 'some-artist-id',
483+
_strengthenOnPublish: {
484+
template: {
485+
id: 'artist',
486+
},
487+
type: 'artist',
488+
},
489+
_type: 'reference',
445490
},
446-
type: 'plant',
447-
},
448-
_type: 'reference',
449-
},
450-
{
451-
_ref: 'some-plant-id',
452-
_strengthenOnPublish: {
453-
template: {
454-
id: 'plant',
491+
expectedWeakReference: {
492+
_ref: 'expected-weak-reference',
493+
_type: 'reference',
494+
_weak: true,
495+
_strengthenOnPublish: {
496+
template: {
497+
id: 'some-document',
498+
},
499+
type: 'some-document',
500+
weak: true,
501+
},
455502
},
456-
type: 'plant',
457-
},
458-
_type: 'reference',
459-
},
460-
],
461-
stores: [
462-
{
463-
name: 'some-store',
464-
inventory: {
465-
products: [
503+
plants: [
466504
{
467-
_ref: 'some-product-id',
505+
_ref: 'some-plant-id',
468506
_strengthenOnPublish: {
469507
template: {
470-
id: 'product',
508+
id: 'plant',
471509
},
472-
type: 'product',
510+
type: 'plant',
473511
},
474512
_type: 'reference',
475513
},
476514
{
477-
_ref: 'some-product-id',
515+
_ref: 'some-plant-id',
478516
_strengthenOnPublish: {
479517
template: {
480-
id: 'product',
518+
id: 'plant',
481519
},
482-
type: 'product',
520+
type: 'plant',
483521
},
484522
_type: 'reference',
485523
},
486524
],
525+
stores: [
526+
{
527+
name: 'some-store',
528+
inventory: {
529+
products: [
530+
{
531+
_ref: 'some-product-id',
532+
_strengthenOnPublish: {
533+
template: {
534+
id: 'product',
535+
},
536+
type: 'product',
537+
},
538+
_type: 'reference',
539+
},
540+
{
541+
_ref: 'some-product-id',
542+
_strengthenOnPublish: {
543+
template: {
544+
id: 'product',
545+
},
546+
type: 'product',
547+
},
548+
_type: 'reference',
549+
},
550+
],
551+
},
552+
},
553+
],
487554
},
555+
publishedId: 'doc-id',
488556
},
489557
],
490558
},
491-
undefined,
492-
)
559+
method: 'POST',
560+
uri: '/data/actions/test-dataset',
561+
})
493562
})
494563

495564
it('should discard a version of a document', async () => {
@@ -501,8 +570,9 @@ describe('createReleaseOperationsStore', () => {
501570
body: {
502571
actions: [
503572
{
504-
actionType: 'sanity.action.document.discard',
505-
draftId: 'versions.release-id.doc-id',
573+
actionType: 'sanity.action.document.version.discard',
574+
versionId: 'versions.release-id.doc-id',
575+
purge: false,
506576
},
507577
],
508578
},
@@ -511,15 +581,15 @@ describe('createReleaseOperationsStore', () => {
511581

512582
it('should unpublish a version of a document', async () => {
513583
const store = createStore()
514-
await store.unpublishVersion('doc-id')
584+
await store.unpublishVersion('versions.release-id.doc-id')
515585
expect(mockClient.request).toHaveBeenCalledWith({
516586
uri: '/data/actions/test-dataset',
517587
method: 'POST',
518588
body: {
519589
actions: [
520590
{
521591
actionType: 'sanity.action.document.version.unpublish',
522-
draftId: 'doc-id',
592+
versionId: 'versions.release-id.doc-id',
523593
publishedId: `doc-id`,
524594
},
525595
],

‎packages/sanity/src/core/releases/store/createReleaseOperationStore.ts

+26-21
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export interface ReleaseOperationsStore {
4444
unpublishVersion: (documentId: string, opts?: operationsOptions) => Promise<void>
4545
}
4646

47-
const IS_CREATE_VERSION_ACTION_SUPPORTED = false
4847
const METADATA_PROPERTY_NAME = 'metadata'
4948

5049
export function createReleaseOperationsStore(options: {
@@ -185,28 +184,27 @@ export function createReleaseOperationsStore(options: {
185184
_id: getVersionId(documentId, releaseId),
186185
}) as IdentifiedSanityDocumentStub
187186

188-
await (IS_CREATE_VERSION_ACTION_SUPPORTED
189-
? requestAction(
190-
client,
191-
[
192-
{
193-
actionType: 'sanity.action.document.createVersion',
194-
releaseId,
195-
attributes: versionDocument,
196-
},
197-
],
198-
opts,
199-
)
200-
: client.create(versionDocument, opts))
187+
await requestAction(
188+
client,
189+
[
190+
{
191+
actionType: 'sanity.action.document.version.create',
192+
publishedId: getPublishedId(documentId),
193+
document: versionDocument,
194+
},
195+
],
196+
opts,
197+
)
201198
}
202199

203200
const handleDiscardVersion = (releaseId: string, documentId: string, opts?: operationsOptions) =>
204201
requestAction(
205202
client,
206203
[
207204
{
208-
actionType: 'sanity.action.document.discard',
209-
draftId: getVersionId(documentId, releaseId),
205+
actionType: 'sanity.action.document.version.discard',
206+
versionId: getVersionId(documentId, releaseId),
207+
purge: false, // keep document history
210208
},
211209
],
212210
opts,
@@ -216,7 +214,7 @@ export function createReleaseOperationsStore(options: {
216214
requestAction(client, [
217215
{
218216
actionType: 'sanity.action.document.version.unpublish',
219-
draftId: documentId,
217+
versionId: documentId,
220218
publishedId: getPublishedId(documentId),
221219
},
222220
])
@@ -301,17 +299,23 @@ interface CreateReleaseApiAction {
301299
}
302300

303301
interface CreateVersionReleaseApiAction {
304-
actionType: 'sanity.action.document.createVersion'
305-
releaseId: string
306-
attributes: IdentifiedSanityDocumentStub
302+
actionType: 'sanity.action.document.version.create'
303+
publishedId: string
304+
document: IdentifiedSanityDocumentStub
307305
}
308306

309307
interface UnpublishVersionReleaseApiAction {
310308
actionType: 'sanity.action.document.version.unpublish'
311-
draftId: string
309+
versionId: string
312310
publishedId: string
313311
}
314312

313+
interface DiscardVersionReleaseApiAction {
314+
actionType: 'sanity.action.document.version.discard'
315+
versionId: string
316+
purge?: boolean
317+
}
318+
315319
interface EditReleaseApiAction {
316320
actionType: 'sanity.action.release.edit'
317321
releaseId: string
@@ -335,6 +339,7 @@ type ReleaseAction =
335339
| DeleteApiAction
336340
| CreateVersionReleaseApiAction
337341
| UnpublishVersionReleaseApiAction
342+
| DiscardVersionReleaseApiAction
338343

339344
export function createRequestAction(
340345
onReleaseLimitReached: ReleasesUpsellContextValue['onReleaseLimitReached'],

0 commit comments

Comments
 (0)
Please sign in to comment.