@sanity/sdk-react 2.12.0 → 2.14.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 +77 -0
- package/dist/index.js +36 -31
- package/dist/index.js.map +1 -1
- package/package.json +15 -15
- package/src/_exports/sdk-react.ts +1 -0
- package/src/hooks/document/useApplyDocumentActions.test.tsx +6 -161
- package/src/hooks/document/useApplyDocumentActions.ts +3 -49
- package/src/hooks/helpers/useApplyActions.test.tsx +220 -0
- package/src/hooks/helpers/useApplyActions.ts +68 -0
- package/src/hooks/projection/useDocumentProjection.test.tsx +41 -0
- package/src/hooks/projection/useDocumentProjection.ts +6 -0
- package/src/hooks/releases/useApplyReleaseActions.test.tsx +66 -0
- package/src/hooks/releases/useApplyReleaseActions.ts +82 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {applyDocumentActions, createSanityInstance} from '@sanity/sdk'
|
|
2
|
+
import {describe, it} from 'vitest'
|
|
3
|
+
|
|
4
|
+
import {renderHook} from '../../../test/test-utils'
|
|
5
|
+
import {useSanityInstance} from '../context/useSanityInstance'
|
|
6
|
+
import {useApplyReleaseActions} from './useApplyReleaseActions'
|
|
7
|
+
|
|
8
|
+
// Resource resolution, mismatch detection, and context fallback are covered
|
|
9
|
+
// by hooks/helpers/useApplyActions.test.tsx — both this hook and
|
|
10
|
+
// useApplyDocumentActions are typed wrappers over that shared implementation.
|
|
11
|
+
// These tests just verify the wrapper forwards release actions through and
|
|
12
|
+
// supports batching them in a single transaction.
|
|
13
|
+
|
|
14
|
+
vi.mock('@sanity/sdk', async (importOriginal) => {
|
|
15
|
+
const original = await importOriginal<typeof import('@sanity/sdk')>()
|
|
16
|
+
return {...original, applyDocumentActions: vi.fn()}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
vi.mock('../context/useSanityInstance')
|
|
20
|
+
|
|
21
|
+
const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
|
|
22
|
+
|
|
23
|
+
describe('useApplyReleaseActions', () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
vi.resetAllMocks()
|
|
26
|
+
vi.mocked(useSanityInstance).mockReturnValueOnce(instance)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('forwards a release action to applyDocumentActions with the resolved resource', () => {
|
|
30
|
+
const {result} = renderHook(() => useApplyReleaseActions())
|
|
31
|
+
result.current({type: 'release.create', releaseId: 'r1', metadata: {releaseType: 'asap'}})
|
|
32
|
+
|
|
33
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
34
|
+
actions: [
|
|
35
|
+
{
|
|
36
|
+
type: 'release.create',
|
|
37
|
+
releaseId: 'r1',
|
|
38
|
+
metadata: {releaseType: 'asap'},
|
|
39
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('forwards an array of release actions as a single transaction', () => {
|
|
47
|
+
const {result} = renderHook(() => useApplyReleaseActions())
|
|
48
|
+
result.current([
|
|
49
|
+
{type: 'release.create', releaseId: 'r1', metadata: {releaseType: 'asap'}},
|
|
50
|
+
{type: 'release.publish', releaseId: 'r1'},
|
|
51
|
+
])
|
|
52
|
+
|
|
53
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
54
|
+
actions: [
|
|
55
|
+
{
|
|
56
|
+
type: 'release.create',
|
|
57
|
+
releaseId: 'r1',
|
|
58
|
+
metadata: {releaseType: 'asap'},
|
|
59
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
60
|
+
},
|
|
61
|
+
{type: 'release.publish', releaseId: 'r1', resource: {projectId: 'test', dataset: 'test'}},
|
|
62
|
+
],
|
|
63
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
})
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {type ActionsResult, type ReleaseAction} from '@sanity/sdk'
|
|
2
|
+
|
|
3
|
+
import {type ResourceHandle} from '../../config/handles'
|
|
4
|
+
import {useApplyActions} from '../helpers/useApplyActions'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
interface UseApplyReleaseActions {
|
|
10
|
+
(): (action: ReleaseAction | ReleaseAction[], options?: ResourceHandle) => Promise<ActionsResult>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*
|
|
16
|
+
* Provides a stable callback function for applying one or more release actions.
|
|
17
|
+
*
|
|
18
|
+
* This hook wraps the core `applyDocumentActions` functionality from `@sanity/sdk`,
|
|
19
|
+
* integrating it with the React component lifecycle and {@link SanityInstance}.
|
|
20
|
+
* It accepts release-lifecycle actions generated by {@link createRelease},
|
|
21
|
+
* {@link editRelease}, {@link publishRelease}, {@link scheduleRelease},
|
|
22
|
+
* {@link unscheduleRelease}, {@link archiveRelease}, {@link unarchiveRelease},
|
|
23
|
+
* and {@link deleteRelease}.
|
|
24
|
+
*
|
|
25
|
+
* Note that actions submitted via this hook will cascade to the documents in the release.
|
|
26
|
+
* For example, if you create a release and then publish it, the documents in the release will be published.
|
|
27
|
+
* If you delete a published release, the version documents will be deleted.
|
|
28
|
+
*
|
|
29
|
+
* Features:
|
|
30
|
+
* - Applies one or multiple `ReleaseAction` objects.
|
|
31
|
+
* - Supports optimistic updates for create/edit/delete: local release state
|
|
32
|
+
* reflects changes immediately while in-flight.
|
|
33
|
+
* - Handles batching: multiple actions passed together are submitted as a
|
|
34
|
+
* single atomic transaction.
|
|
35
|
+
*
|
|
36
|
+
* Release actions cannot be combined with `liveEdit` document actions in the
|
|
37
|
+
* same transaction. Submit them as separate transactions if you need both.
|
|
38
|
+
*
|
|
39
|
+
* @category Releases
|
|
40
|
+
* @returns A stable callback. When called with a single `ReleaseAction` or an
|
|
41
|
+
* array of `ReleaseAction`s, it returns a promise that resolves to an
|
|
42
|
+
* {@link ActionsResult}.
|
|
43
|
+
*
|
|
44
|
+
* @example Create and schedule a release
|
|
45
|
+
* ```tsx
|
|
46
|
+
* import {
|
|
47
|
+
* createRelease,
|
|
48
|
+
* scheduleRelease,
|
|
49
|
+
* useApplyReleaseActions,
|
|
50
|
+
* type ReleaseHandle,
|
|
51
|
+
* } from '@sanity/sdk-react'
|
|
52
|
+
*
|
|
53
|
+
* function ScheduleReleaseButton({release}: {release: ReleaseHandle}) {
|
|
54
|
+
* const applyRelease = useApplyReleaseActions()
|
|
55
|
+
*
|
|
56
|
+
* const handleSchedule = () =>
|
|
57
|
+
* applyRelease([
|
|
58
|
+
* createRelease(release, {title: 'Summer drop', releaseType: 'asap'}),
|
|
59
|
+
* scheduleRelease(release, '2026-06-01T00:00:00Z'),
|
|
60
|
+
* ])
|
|
61
|
+
*
|
|
62
|
+
* return <button onClick={handleSchedule}>Schedule</button>
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @example Publish a release
|
|
67
|
+
* ```tsx
|
|
68
|
+
* import {publishRelease, useApplyReleaseActions} from '@sanity/sdk-react'
|
|
69
|
+
*
|
|
70
|
+
* function PublishButton({releaseId}: {releaseId: string}) {
|
|
71
|
+
* const applyRelease = useApplyReleaseActions()
|
|
72
|
+
* return (
|
|
73
|
+
* <button onClick={() => applyRelease(publishRelease({releaseId}))}>
|
|
74
|
+
* Publish all documents in the release
|
|
75
|
+
* </button>
|
|
76
|
+
* )
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export const useApplyReleaseActions: UseApplyReleaseActions = () => {
|
|
81
|
+
return useApplyActions() as ReturnType<UseApplyReleaseActions>
|
|
82
|
+
}
|