@sanity/sdk 2.8.0 → 3.0.0-rc.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 (99) hide show
  1. package/dist/index.d.ts +228 -239
  2. package/dist/index.js +287 -454
  3. package/dist/index.js.map +1 -1
  4. package/package.json +4 -4
  5. package/src/_exports/index.ts +16 -17
  6. package/src/agent/agentActions.test.ts +60 -16
  7. package/src/agent/agentActions.ts +29 -20
  8. package/src/auth/authMode.test.ts +0 -25
  9. package/src/auth/authMode.ts +3 -6
  10. package/src/auth/authStore.test.ts +129 -66
  11. package/src/auth/authStore.ts +9 -11
  12. package/src/auth/dashboardAuth.ts +2 -2
  13. package/src/auth/getOrganizationVerificationState.test.ts +10 -11
  14. package/src/auth/handleAuthCallback.test.ts +0 -12
  15. package/src/auth/handleAuthCallback.ts +9 -3
  16. package/src/auth/logout.test.ts +0 -6
  17. package/src/auth/refreshStampedToken.test.ts +121 -17
  18. package/src/auth/standaloneAuth.ts +9 -3
  19. package/src/auth/studioAuth.ts +35 -8
  20. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +9 -3
  21. package/src/auth/subscribeToStateAndFetchCurrentUser.ts +1 -1
  22. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +0 -2
  23. package/src/auth/subscribeToStorageEventsAndSetToken.ts +2 -2
  24. package/src/auth/utils.ts +33 -0
  25. package/src/client/clientStore.test.ts +14 -61
  26. package/src/client/clientStore.ts +52 -28
  27. package/src/comlink/controller/actions/destroyController.test.ts +1 -4
  28. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +1 -4
  29. package/src/comlink/controller/actions/getOrCreateController.test.ts +1 -4
  30. package/src/comlink/controller/actions/releaseChannel.test.ts +1 -1
  31. package/src/comlink/controller/comlinkControllerStore.test.ts +1 -4
  32. package/src/comlink/node/actions/getOrCreateNode.test.ts +1 -4
  33. package/src/comlink/node/actions/releaseNode.test.ts +1 -4
  34. package/src/comlink/node/comlinkNodeStore.test.ts +2 -2
  35. package/src/comlink/node/getNodeState.test.ts +1 -1
  36. package/src/config/__tests__/handles.test.ts +12 -18
  37. package/src/config/handles.ts +7 -25
  38. package/src/config/sanityConfig.ts +99 -52
  39. package/src/datasets/datasets.test.ts +2 -2
  40. package/src/datasets/datasets.ts +4 -10
  41. package/src/document/actions.test.ts +33 -4
  42. package/src/document/actions.ts +3 -10
  43. package/src/document/applyDocumentActions.test.ts +17 -18
  44. package/src/document/applyDocumentActions.ts +9 -12
  45. package/src/document/documentStore.test.ts +303 -133
  46. package/src/document/documentStore.ts +70 -61
  47. package/src/document/permissions.test.ts +44 -8
  48. package/src/document/processActions.test.ts +77 -7
  49. package/src/document/reducers.test.ts +35 -3
  50. package/src/document/sharedListener.test.ts +13 -13
  51. package/src/document/sharedListener.ts +8 -3
  52. package/src/favorites/favorites.test.ts +10 -2
  53. package/src/presence/presenceStore.test.ts +34 -9
  54. package/src/presence/presenceStore.ts +29 -13
  55. package/src/preview/previewProjectionUtils.test.ts +192 -0
  56. package/src/preview/previewProjectionUtils.ts +88 -0
  57. package/src/preview/{previewStore.ts → types.ts} +6 -25
  58. package/src/project/project.test.ts +1 -1
  59. package/src/project/project.ts +14 -20
  60. package/src/projection/getProjectionState.test.ts +4 -2
  61. package/src/projection/getProjectionState.ts +2 -21
  62. package/src/projection/projectionQuery.ts +2 -3
  63. package/src/projection/projectionStore.test.ts +3 -3
  64. package/src/projection/resolveProjection.test.ts +2 -1
  65. package/src/projection/resolveProjection.ts +2 -18
  66. package/src/projection/subscribeToStateAndFetchBatches.test.ts +2 -2
  67. package/src/projection/subscribeToStateAndFetchBatches.ts +23 -36
  68. package/src/projection/types.ts +1 -9
  69. package/src/projects/projects.test.ts +1 -1
  70. package/src/query/queryStore.test.ts +86 -28
  71. package/src/query/queryStore.ts +23 -38
  72. package/src/releases/getPerspectiveState.test.ts +14 -13
  73. package/src/releases/getPerspectiveState.ts +6 -6
  74. package/src/releases/releasesStore.test.ts +21 -6
  75. package/src/releases/releasesStore.ts +18 -8
  76. package/src/store/createActionBinder.test.ts +114 -111
  77. package/src/store/createActionBinder.ts +52 -101
  78. package/src/store/createSanityInstance.test.ts +13 -83
  79. package/src/store/createSanityInstance.ts +2 -78
  80. package/src/store/createStateSourceAction.test.ts +2 -2
  81. package/src/store/createStateSourceAction.ts +5 -5
  82. package/src/store/createStoreInstance.test.ts +2 -4
  83. package/src/users/reducers.test.ts +1 -6
  84. package/src/users/reducers.ts +2 -2
  85. package/src/users/types.ts +4 -4
  86. package/src/users/usersStore.test.ts +12 -15
  87. package/src/utils/createFetcherStore.test.ts +1 -1
  88. package/src/utils/logger.test.ts +0 -12
  89. package/src/utils/logger.ts +3 -8
  90. package/src/preview/getPreviewState.test.ts +0 -120
  91. package/src/preview/getPreviewState.ts +0 -91
  92. package/src/preview/previewQuery.test.ts +0 -236
  93. package/src/preview/previewQuery.ts +0 -153
  94. package/src/preview/previewStore.test.ts +0 -36
  95. package/src/preview/resolvePreview.test.ts +0 -47
  96. package/src/preview/resolvePreview.ts +0 -20
  97. package/src/preview/subscribeToStateAndFetchBatches.test.ts +0 -221
  98. package/src/preview/subscribeToStateAndFetchBatches.ts +0 -112
  99. package/src/preview/util.ts +0 -13
@@ -2,7 +2,7 @@ import {at, patch, set, setIfMissing} from '@sanity/mutate'
2
2
  import {type PatchOperations} from '@sanity/types'
3
3
  import {describe, expect, it} from 'vitest'
4
4
 
5
- import {type DocumentHandle} from '../config/sanityConfig'
5
+ import {type DocumentHandle, type DocumentResource} from '../config/sanityConfig'
6
6
  import {
7
7
  createDocument,
8
8
  deleteDocument,
@@ -15,9 +15,18 @@ import {
15
15
  const dummyPatch: PatchOperations = {
16
16
  diffMatchPatch: {'dummy.path': 'dummy patch'},
17
17
  }
18
+ const dummyDocResource: DocumentResource = {projectId: 'test-project', dataset: 'test-dataset'}
18
19
 
19
- const dummyDocHandle: DocumentHandle = {documentId: 'drafts.abc123', documentType: 'testType'}
20
- const dummyDocString = {documentId: 'drafts.abc123', documentType: 'testType'}
20
+ const dummyDocHandle: DocumentHandle = {
21
+ documentId: 'drafts.abc123',
22
+ documentType: 'testType',
23
+ resource: dummyDocResource,
24
+ }
25
+ const dummyDocString: DocumentHandle = {
26
+ documentId: 'drafts.abc123',
27
+ documentType: 'testType',
28
+ resource: dummyDocResource,
29
+ }
21
30
 
22
31
  describe('document actions', () => {
23
32
  describe('createDocument', () => {
@@ -28,18 +37,24 @@ describe('document actions', () => {
28
37
  // getId returns the input if it does not end with a dot.
29
38
  documentId: 'abc123',
30
39
  documentType: dummyDocHandle.documentType,
40
+ resource: dummyDocResource,
31
41
  })
32
42
  })
33
43
 
34
44
  it('creates a document action from a document type handle', () => {
35
45
  // A document type handle is similar to a document handle,
36
46
  // but _id is optional.
37
- const typeHandle = {documentId: 'abc456', documentType: 'anotherType'}
47
+ const typeHandle = {
48
+ documentId: 'abc456',
49
+ documentType: 'anotherType',
50
+ resource: dummyDocResource,
51
+ }
38
52
  const action = createDocument(typeHandle)
39
53
  expect(action).toEqual({
40
54
  type: 'document.create',
41
55
  documentId: 'abc456',
42
56
  documentType: typeHandle.documentType,
57
+ resource: dummyDocResource,
43
58
  })
44
59
  })
45
60
 
@@ -55,6 +70,7 @@ describe('document actions', () => {
55
70
  documentId: 'abc123',
56
71
  documentType: dummyDocHandle.documentType,
57
72
  initialValue,
73
+ resource: dummyDocResource,
58
74
  })
59
75
  })
60
76
 
@@ -64,6 +80,7 @@ describe('document actions', () => {
64
80
  type: 'document.create',
65
81
  documentId: 'abc123',
66
82
  documentType: dummyDocHandle.documentType,
83
+ resource: dummyDocResource,
67
84
  })
68
85
  })
69
86
 
@@ -74,6 +91,7 @@ describe('document actions', () => {
74
91
  documentId: 'abc123',
75
92
  documentType: dummyDocHandle.documentType,
76
93
  initialValue: {},
94
+ resource: dummyDocResource,
77
95
  })
78
96
  })
79
97
  })
@@ -86,6 +104,7 @@ describe('document actions', () => {
86
104
  type: 'document.delete',
87
105
  documentId: 'abc123',
88
106
  documentType: dummyDocString.documentType,
107
+ resource: dummyDocResource,
89
108
  })
90
109
  })
91
110
 
@@ -95,6 +114,7 @@ describe('document actions', () => {
95
114
  type: 'document.delete',
96
115
  documentId: 'abc123',
97
116
  documentType: dummyDocHandle.documentType,
117
+ resource: dummyDocResource,
98
118
  })
99
119
  })
100
120
  })
@@ -107,6 +127,7 @@ describe('document actions', () => {
107
127
  documentId: 'abc123',
108
128
  documentType: dummyDocString.documentType,
109
129
  patches: [dummyPatch],
130
+ resource: dummyDocResource,
110
131
  })
111
132
  })
112
133
 
@@ -117,6 +138,7 @@ describe('document actions', () => {
117
138
  documentId: 'abc123',
118
139
  documentType: dummyDocHandle.documentType,
119
140
  patches: [dummyPatch],
141
+ resource: dummyDocResource,
120
142
  })
121
143
  })
122
144
 
@@ -143,6 +165,7 @@ describe('document actions', () => {
143
165
  {ifRevisionID: 'txn0', set: {'address.city': 'Oslo'}},
144
166
  ],
145
167
  type: 'document.edit',
168
+ resource: dummyDocResource,
146
169
  })
147
170
  })
148
171
  })
@@ -154,6 +177,7 @@ describe('document actions', () => {
154
177
  type: 'document.publish',
155
178
  documentId: 'abc123',
156
179
  documentType: dummyDocString.documentType,
180
+ resource: dummyDocResource,
157
181
  })
158
182
  })
159
183
 
@@ -163,6 +187,7 @@ describe('document actions', () => {
163
187
  type: 'document.publish',
164
188
  documentId: 'abc123',
165
189
  documentType: dummyDocHandle.documentType,
190
+ resource: dummyDocResource,
166
191
  })
167
192
  })
168
193
  })
@@ -174,6 +199,7 @@ describe('document actions', () => {
174
199
  type: 'document.unpublish',
175
200
  documentId: 'abc123',
176
201
  documentType: dummyDocString.documentType,
202
+ resource: dummyDocResource,
177
203
  })
178
204
  })
179
205
 
@@ -183,6 +209,7 @@ describe('document actions', () => {
183
209
  type: 'document.unpublish',
184
210
  documentId: 'abc123',
185
211
  documentType: dummyDocHandle.documentType,
212
+ resource: dummyDocResource,
186
213
  })
187
214
  })
188
215
  })
@@ -194,6 +221,7 @@ describe('document actions', () => {
194
221
  type: 'document.discard',
195
222
  documentId: 'abc123',
196
223
  documentType: dummyDocString.documentType,
224
+ resource: dummyDocResource,
197
225
  })
198
226
  })
199
227
 
@@ -203,6 +231,7 @@ describe('document actions', () => {
203
231
  type: 'document.discard',
204
232
  documentId: 'abc123',
205
233
  documentType: dummyDocHandle.documentType,
234
+ resource: dummyDocResource,
206
235
  })
207
236
  })
208
237
  })
@@ -1,7 +1,6 @@
1
1
  import {SanityEncoder} from '@sanity/mutate'
2
2
  import {type PatchMutation as SanityMutatePatchMutation} from '@sanity/mutate/_unstable_store'
3
- import {type PatchMutation, type PatchOperations} from '@sanity/types'
4
- import {type SanityDocument} from 'groq'
3
+ import {type PatchMutation, type PatchOperations, type SanityDocument} from '@sanity/types'
5
4
 
6
5
  import {type DocumentHandle, type DocumentTypeHandle} from '../config/sanityConfig'
7
6
  import {getPublishedId} from '../utils/ids'
@@ -31,10 +30,7 @@ export interface CreateDocumentAction<
31
30
  * System fields (_id, _type, _rev, _createdAt, _updatedAt) are omitted as they are set automatically.
32
31
  */
33
32
  initialValue?: Partial<
34
- Omit<
35
- SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>,
36
- '_id' | '_type' | '_rev' | '_createdAt' | '_updatedAt'
37
- >
33
+ Omit<SanityDocument, '_id' | '_type' | '_rev' | '_createdAt' | '_updatedAt'>
38
34
  >
39
35
  }
40
36
 
@@ -134,10 +130,7 @@ export function createDocument<
134
130
  >(
135
131
  doc: DocumentTypeHandle<TDocumentType, TDataset, TProjectId>,
136
132
  initialValue?: Partial<
137
- Omit<
138
- SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>,
139
- '_id' | '_type' | '_rev' | '_createdAt' | '_updatedAt'
140
- >
133
+ Omit<SanityDocument, '_id' | '_type' | '_rev' | '_createdAt' | '_updatedAt'>
141
134
  >,
142
135
  ): CreateDocumentAction<TDocumentType, TDataset, TProjectId> {
143
136
  return {
@@ -3,7 +3,7 @@ import {type SanityDocument} from '@sanity/types'
3
3
  import {Subject} from 'rxjs'
4
4
  import {describe, expect, it} from 'vitest'
5
5
 
6
- import {bindActionByDataset} from '../store/createActionBinder'
6
+ import {bindActionByResource} from '../store/createActionBinder'
7
7
  import {createSanityInstance, type SanityInstance} from '../store/createSanityInstance'
8
8
  import {} from '../store/createStateSourceAction'
9
9
  import {createStoreState, type StoreState} from '../store/createStoreState'
@@ -14,7 +14,7 @@ import {type AppliedTransaction, type OutgoingTransaction} from './reducers'
14
14
 
15
15
  vi.mock('../store/createActionBinder', async (importOriginal) => ({
16
16
  ...(await importOriginal<typeof import('../store/createActionBinder')>()),
17
- bindActionByDataset: vi.fn(),
17
+ bindActionByResource: vi.fn(),
18
18
  }))
19
19
 
20
20
  type TestState = Pick<
@@ -47,10 +47,10 @@ describe('applyDocumentActions', () => {
47
47
  events: eventsSubject,
48
48
  }
49
49
  state = createStoreState(initialState)
50
- instance = createSanityInstance({projectId: 'p', dataset: 'd'})
51
- const key = {name: 'p.d', projectId: 'p', dataset: 'd'}
50
+ instance = createSanityInstance()
51
+ const key = {name: 'p.d', resource: {projectId: 'p', dataset: 'd'}}
52
52
 
53
- vi.mocked(bindActionByDataset).mockImplementation(
53
+ vi.mocked(bindActionByResource).mockImplementation(
54
54
  (_storeDef, action) => (instanceParam: SanityInstance, options) =>
55
55
  action({instance: instanceParam, state, key}, options),
56
56
  )
@@ -69,12 +69,14 @@ describe('applyDocumentActions', () => {
69
69
  documentId: 'doc1',
70
70
  documentType: 'example',
71
71
  patches: [{set: {foo: 'bar'}}],
72
+ resource: {projectId: 'p', dataset: 'd'},
72
73
  }
73
74
 
74
75
  // Call applyDocumentActions with a fixed transactionId for reproducibility.
75
76
  const applyPromise = applyDocumentActions(instance, {
76
77
  actions: [action],
77
78
  transactionId: 'txn-success',
79
+ resource: {projectId: 'p', dataset: 'd'},
78
80
  })
79
81
 
80
82
  const appliedTx: AppliedTransaction = {
@@ -126,12 +128,14 @@ describe('applyDocumentActions', () => {
126
128
  documentId: 'doc1',
127
129
  documentType: 'example',
128
130
  patches: [{set: {foo: 'error'}}],
131
+ resource: {projectId: 'p', dataset: 'd'},
129
132
  }
130
133
 
131
134
  // Call applyDocumentActions with a fixed transactionId.
132
135
  const applyPromise = applyDocumentActions(instance, {
133
136
  actions: [action],
134
137
  transactionId: 'txn-error',
138
+ resource: {projectId: 'p', dataset: 'd'},
135
139
  })
136
140
 
137
141
  const errorEvent: DocumentEvent = {
@@ -146,25 +150,21 @@ describe('applyDocumentActions', () => {
146
150
  await expect(applyPromise).rejects.toThrow('Simulated error')
147
151
  })
148
152
 
149
- it('matches parent instance via child when action projectId and dataset do not match child config', async () => {
150
- // Create a parent instance
151
- const parentInstance = createSanityInstance({projectId: 'p', dataset: 'd'})
152
- // Create a child instance with different config
153
- const childInstance = parentInstance.createChild({projectId: 'child-p', dataset: 'child-d'})
154
- // Use the child instance in context
155
- // Create an action that refers to the parent's configuration
153
+ it('uses explicit resource even when instance default differs', async () => {
154
+ const otherInstance = createSanityInstance({
155
+ defaultResource: {projectId: 'child-p', dataset: 'child-d'},
156
+ })
156
157
  const action: DocumentAction = {
157
158
  type: 'document.edit',
158
159
  documentId: 'doc1',
159
160
  documentType: 'example',
160
161
  patches: [{set: {foo: 'childTest'}}],
161
- projectId: 'p',
162
- dataset: 'd',
162
+ resource: {projectId: 'p', dataset: 'd'},
163
163
  }
164
- // Call applyDocumentActions with the context using childInstance, but with action requiring parent's config
165
- const applyPromise = applyDocumentActions(childInstance, {
164
+ const applyPromise = applyDocumentActions(instance, {
166
165
  actions: [action],
167
166
  transactionId: 'txn-child-match',
167
+ resource: {projectId: 'p', dataset: 'd'},
168
168
  })
169
169
 
170
170
  // Simulate an applied transaction on the parent's instance
@@ -198,7 +198,6 @@ describe('applyDocumentActions', () => {
198
198
  const submittedResult = await result.submitted()
199
199
  expect(submittedResult).toEqual(acceptedResult)
200
200
 
201
- childInstance.dispose()
202
- parentInstance.dispose()
201
+ otherInstance.dispose()
203
202
  })
204
203
  })
@@ -1,8 +1,9 @@
1
1
  import {type SanityClient} from '@sanity/client'
2
- import {type SanityDocument} from 'groq'
2
+ import {type SanityDocument} from '@sanity/types'
3
3
  import {distinctUntilChanged, filter, first, firstValueFrom, map, race} from 'rxjs'
4
4
 
5
- import {bindActionByDataset} from '../store/createActionBinder'
5
+ import {type DocumentResource} from '../config/sanityConfig'
6
+ import {bindActionByResource} from '../store/createActionBinder'
6
7
  import {type SanityInstance} from '../store/createSanityInstance'
7
8
  import {type StoreContext} from '../store/defineStore'
8
9
  import {type DocumentAction} from './actions'
@@ -29,6 +30,11 @@ export interface ApplyDocumentActionsOptions {
29
30
  */
30
31
  actions: DocumentAction[]
31
32
 
33
+ /**
34
+ * The resource to which the documents being acted on belong.
35
+ */
36
+ resource: DocumentResource
37
+
32
38
  /**
33
39
  * Optionally provide an ID to be used as this transaction ID
34
40
  */
@@ -39,15 +45,6 @@ export interface ApplyDocumentActionsOptions {
39
45
  disableBatching?: boolean
40
46
  }
41
47
 
42
- /** @beta */
43
- export function applyDocumentActions<
44
- TDocumentType extends string = string,
45
- TDataset extends string = string,
46
- TProjectId extends string = string,
47
- >(
48
- instance: SanityInstance,
49
- options: ApplyDocumentActionsOptions,
50
- ): Promise<ActionsResult<SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>>>
51
48
  /** @beta */
52
49
  export function applyDocumentActions(
53
50
  instance: SanityInstance,
@@ -61,7 +58,7 @@ export function applyDocumentActions(
61
58
  return boundApplyDocumentActions(...args)
62
59
  }
63
60
 
64
- const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocumentActions)
61
+ const boundApplyDocumentActions = bindActionByResource(documentStore, _applyDocumentActions)
65
62
 
66
63
  /** @internal */
67
64
  async function _applyDocumentActions(