Skip to content

Commit

Permalink
fix(sanity): ensure Actions API client uses adequate API version (#7166)
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 authored Jul 17, 2024
1 parent 9dbb9f6 commit 25a983b
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const client = {
action: mockedActionRequest,
},
dataRequest: mockedDataRequest,
withConfig: jest.fn(() => client),
}

const idPair = {publishedId: 'publishedId', draftId: 'draftId'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../buffered-doc'
import {getPairListener, type ListenerEvent} from '../getPairListener'
import {type IdPair, type PendingMutationsEvent, type ReconnectEvent} from '../types'
import {actionsApiClient} from './utils/actionsApiClient'

const isMutationEventForDocId =
(id: string) =>
Expand Down Expand Up @@ -129,7 +130,7 @@ function commitActions(client: SanityClient, idPair: IdPair, mutationParams: Mut
return commitMutations(client, mutationParams)
}

return client.observable.action(toActions(idPair, mutationParams), {
return actionsApiClient(client).observable.action(toActions(idPair, mutationParams), {
tag: 'document.commit',
transactionId: mutationParams.transactionId,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type OperationImpl} from '../operations/types'
import {actionsApiClient} from '../utils/actionsApiClient'
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'

export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {
Expand All @@ -11,7 +12,7 @@ export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {

//the delete action requires a published doc -- discard if not present
if (!snapshots.published) {
return client.observable.action(
return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.discard',
draftId: idPair.draftId,
Expand All @@ -20,7 +21,7 @@ export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {
)
}

return client.observable.action(
return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.delete',
includeDrafts: snapshots.draft ? [idPair.draftId] : [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type OperationImpl} from '../operations/types'
import {actionsApiClient} from '../utils/actionsApiClient'

type DisabledReason = 'NO_CHANGES' | 'NOT_PUBLISHED'

Expand All @@ -13,7 +14,7 @@ export const discardChanges: OperationImpl<[], DisabledReason> = {
return false
},
execute: ({client, idPair}) => {
return client.observable.action(
return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.discard',
draftId: idPair.draftId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type OperationImpl} from '../operations/index'
import {actionsApiClient} from '../utils/actionsApiClient'
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'

type DisabledReason = 'LIVE_EDIT_ENABLED' | 'ALREADY_PUBLISHED' | 'NO_CHANGES'
Expand All @@ -19,7 +20,7 @@ export const publish: OperationImpl<[], DisabledReason> = {
throw new Error('cannot execute "publish" when draft is missing')
}

return client.observable.action(
return actionsApiClient(client).observable.action(
{
actionType: 'sanity.action.document.publish',
draftId: idPair.draftId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type OperationImpl} from '../operations/types'
import {actionsApiClient} from '../utils/actionsApiClient'
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'

type DisabledReason = 'LIVE_EDIT_ENABLED' | 'NOT_PUBLISHED'
Expand All @@ -11,7 +12,7 @@ export const unpublish: OperationImpl<[], DisabledReason> = {
return snapshots.published ? false : 'NOT_PUBLISHED'
},
execute: ({client, idPair}) =>
client.observable.action(
actionsApiClient(client).observable.action(
{
// This operation is run when "unpublish anyway" is clicked
actionType: 'sanity.action.document.unpublish',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {type SanityClient} from 'sanity'

const ACTIONS_API_MINIMUM_VERSION = '2024-05-23'

export function actionsApiClient(client: SanityClient): SanityClient {
return client.withConfig({
apiVersion: ACTIONS_API_MINIMUM_VERSION,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {map, mergeMap} from 'rxjs/operators'

import {isDev} from '../../../environment'
import {getDraftId, getPublishedId, isRecord} from '../../../util'
import {actionsApiClient} from '../document/document-pair/utils/actionsApiClient'
import {Timeline, TimelineController} from './history'

/**
Expand Down Expand Up @@ -215,7 +216,7 @@ function restore(
publishedId: documentId,
attributes: restoredDraft,
}
return client.observable.action(
return actionsApiClient(client).observable.action(
options.fromDeleted
? [
{
Expand Down

0 comments on commit 25a983b

Please sign in to comment.