@sanity/sdk 0.0.0-chore-react-18-compat.1 → 0.0.0-chore-react-18-compat.3

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 (134) hide show
  1. package/dist/index.d.ts +441 -322
  2. package/dist/index.js +1685 -1481
  3. package/dist/index.js.map +1 -1
  4. package/package.json +13 -15
  5. package/src/_exports/index.ts +32 -30
  6. package/src/auth/authStore.test.ts +149 -104
  7. package/src/auth/authStore.ts +51 -100
  8. package/src/auth/handleAuthCallback.test.ts +67 -34
  9. package/src/auth/handleAuthCallback.ts +8 -7
  10. package/src/auth/logout.test.ts +61 -29
  11. package/src/auth/logout.ts +26 -28
  12. package/src/auth/refreshStampedToken.test.ts +197 -91
  13. package/src/auth/refreshStampedToken.ts +170 -59
  14. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
  15. package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
  16. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
  17. package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
  18. package/src/client/clientStore.test.ts +131 -67
  19. package/src/client/clientStore.ts +117 -116
  20. package/src/comlink/controller/actions/destroyController.test.ts +38 -13
  21. package/src/comlink/controller/actions/destroyController.ts +11 -15
  22. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
  23. package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
  24. package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
  25. package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
  26. package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
  27. package/src/comlink/controller/actions/releaseChannel.ts +22 -21
  28. package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
  29. package/src/comlink/controller/comlinkControllerStore.ts +44 -5
  30. package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
  31. package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
  32. package/src/comlink/node/actions/releaseNode.test.ts +75 -55
  33. package/src/comlink/node/actions/releaseNode.ts +19 -21
  34. package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
  35. package/src/comlink/node/comlinkNodeStore.ts +22 -5
  36. package/src/config/authConfig.ts +79 -0
  37. package/src/config/sanityConfig.ts +48 -0
  38. package/src/datasets/datasets.test.ts +2 -2
  39. package/src/datasets/datasets.ts +18 -5
  40. package/src/document/actions.test.ts +22 -10
  41. package/src/document/actions.ts +44 -56
  42. package/src/document/applyDocumentActions.test.ts +96 -36
  43. package/src/document/applyDocumentActions.ts +140 -99
  44. package/src/document/documentStore.test.ts +103 -155
  45. package/src/document/documentStore.ts +247 -238
  46. package/src/document/listen.ts +56 -55
  47. package/src/document/patchOperations.ts +0 -43
  48. package/src/document/permissions.test.ts +25 -12
  49. package/src/document/permissions.ts +11 -4
  50. package/src/document/processActions.test.ts +41 -8
  51. package/src/document/reducers.test.ts +87 -16
  52. package/src/document/reducers.ts +2 -2
  53. package/src/document/sharedListener.test.ts +34 -16
  54. package/src/document/sharedListener.ts +33 -11
  55. package/src/preview/getPreviewState.test.ts +40 -39
  56. package/src/preview/getPreviewState.ts +68 -56
  57. package/src/preview/previewConstants.ts +43 -0
  58. package/src/preview/previewQuery.test.ts +1 -1
  59. package/src/preview/previewQuery.ts +4 -5
  60. package/src/preview/previewStore.test.ts +13 -58
  61. package/src/preview/previewStore.ts +7 -21
  62. package/src/preview/resolvePreview.test.ts +33 -104
  63. package/src/preview/resolvePreview.ts +11 -21
  64. package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
  65. package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
  66. package/src/preview/util.ts +1 -0
  67. package/src/project/project.test.ts +3 -3
  68. package/src/project/project.ts +28 -5
  69. package/src/projection/getProjectionState.test.ts +188 -72
  70. package/src/projection/getProjectionState.ts +92 -62
  71. package/src/projection/projectionQuery.test.ts +114 -12
  72. package/src/projection/projectionQuery.ts +75 -32
  73. package/src/projection/projectionStore.test.ts +13 -51
  74. package/src/projection/projectionStore.ts +6 -43
  75. package/src/projection/resolveProjection.test.ts +32 -127
  76. package/src/projection/resolveProjection.ts +16 -28
  77. package/src/projection/subscribeToStateAndFetchBatches.test.ts +203 -116
  78. package/src/projection/subscribeToStateAndFetchBatches.ts +140 -85
  79. package/src/projection/types.ts +50 -0
  80. package/src/projection/util.ts +3 -1
  81. package/src/projects/projects.test.ts +13 -4
  82. package/src/projects/projects.ts +6 -1
  83. package/src/query/queryStore.test.ts +10 -47
  84. package/src/query/queryStore.ts +151 -133
  85. package/src/query/queryStoreConstants.ts +2 -0
  86. package/src/store/createActionBinder.test.ts +153 -0
  87. package/src/store/createActionBinder.ts +176 -0
  88. package/src/store/createSanityInstance.test.ts +84 -0
  89. package/src/store/createSanityInstance.ts +124 -0
  90. package/src/store/createStateSourceAction.test.ts +196 -0
  91. package/src/store/createStateSourceAction.ts +260 -0
  92. package/src/store/createStoreInstance.test.ts +81 -0
  93. package/src/store/createStoreInstance.ts +80 -0
  94. package/src/store/createStoreState.test.ts +85 -0
  95. package/src/store/createStoreState.ts +92 -0
  96. package/src/store/defineStore.test.ts +18 -0
  97. package/src/store/defineStore.ts +81 -0
  98. package/src/users/reducers.test.ts +318 -0
  99. package/src/users/reducers.ts +88 -0
  100. package/src/users/types.ts +46 -4
  101. package/src/users/usersConstants.ts +4 -0
  102. package/src/users/usersStore.test.ts +350 -223
  103. package/src/users/usersStore.ts +285 -149
  104. package/src/utils/createFetcherStore.test.ts +6 -7
  105. package/src/utils/createFetcherStore.ts +150 -153
  106. package/src/utils/createGroqSearchFilter.test.ts +75 -0
  107. package/src/utils/createGroqSearchFilter.ts +85 -0
  108. package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
  109. package/dist/index.cjs +0 -4888
  110. package/dist/index.cjs.map +0 -1
  111. package/dist/index.d.cts +0 -2121
  112. package/src/auth/fetchLoginUrls.test.ts +0 -163
  113. package/src/auth/fetchLoginUrls.ts +0 -74
  114. package/src/common/createLiveEventSubscriber.test.ts +0 -121
  115. package/src/common/createLiveEventSubscriber.ts +0 -55
  116. package/src/common/types.ts +0 -4
  117. package/src/instance/identity.test.ts +0 -46
  118. package/src/instance/identity.ts +0 -29
  119. package/src/instance/sanityInstance.test.ts +0 -77
  120. package/src/instance/sanityInstance.ts +0 -57
  121. package/src/instance/types.ts +0 -37
  122. package/src/preview/getPreviewProjection.ts +0 -45
  123. package/src/resources/README.md +0 -370
  124. package/src/resources/createAction.test.ts +0 -101
  125. package/src/resources/createAction.ts +0 -44
  126. package/src/resources/createResource.test.ts +0 -112
  127. package/src/resources/createResource.ts +0 -102
  128. package/src/resources/createStateSourceAction.test.ts +0 -114
  129. package/src/resources/createStateSourceAction.ts +0 -83
  130. package/src/resources/createStore.test.ts +0 -67
  131. package/src/resources/createStore.ts +0 -46
  132. package/src/store/createStore.test.ts +0 -108
  133. package/src/store/createStore.ts +0 -106
  134. /package/src/{common/util.ts → utils/hashString.ts} +0 -0
@@ -2,8 +2,9 @@ import {type SanityClient} from '@sanity/client'
2
2
  import {type SanityDocument} from '@sanity/types'
3
3
  import {distinctUntilChanged, filter, first, firstValueFrom, map, race} from 'rxjs'
4
4
 
5
- import {type SanityInstance} from '../instance/types'
6
- import {type ActionContext, createAction} from '../resources/createAction'
5
+ import {bindActionByDataset} from '../store/createActionBinder'
6
+ import {type SanityInstance} from '../store/createSanityInstance'
7
+ import {type StoreContext} from '../store/defineStore'
7
8
  import {type DocumentAction} from './actions'
8
9
  import {documentStore, type DocumentStoreState} from './documentStore'
9
10
  import {type DocumentSet} from './processMutations'
@@ -35,120 +36,160 @@ export interface ApplyDocumentActionsOptions {
35
36
 
36
37
  /** @beta */
37
38
  export function applyDocumentActions<TDocument extends SanityDocument>(
38
- instance: SanityInstance | ActionContext<DocumentStoreState>,
39
+ instance: SanityInstance,
39
40
  action: DocumentAction<TDocument> | DocumentAction<TDocument>[],
40
41
  options?: ApplyDocumentActionsOptions,
41
42
  ): Promise<ActionsResult<TDocument>>
42
43
  /** @beta */
43
44
  export function applyDocumentActions(
44
- instance: SanityInstance | ActionContext<DocumentStoreState>,
45
+ instance: SanityInstance,
45
46
  action: DocumentAction | DocumentAction[],
46
47
  options?: ApplyDocumentActionsOptions,
47
48
  ): Promise<ActionsResult>
48
49
  /** @beta */
49
50
  export function applyDocumentActions(
50
- ...args: Parameters<typeof _applyDocumentActions>
51
- ): ReturnType<typeof _applyDocumentActions> {
52
- return _applyDocumentActions(...args)
51
+ ...args: Parameters<typeof boundApplyDocumentActions>
52
+ ): ReturnType<typeof boundApplyDocumentActions> {
53
+ return boundApplyDocumentActions(...args)
53
54
  }
54
55
 
55
- const _applyDocumentActions = createAction(documentStore, ({state}) => {
56
- const {events} = state.get()
57
-
58
- return async function (
59
- action: DocumentAction | DocumentAction[],
60
- {transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions = {},
61
- ): Promise<ActionsResult> {
62
- const actions = Array.isArray(action) ? action : [action]
63
-
64
- const transaction: QueuedTransaction = {
65
- transactionId,
66
- actions,
67
- ...(disableBatching && {disableBatching}),
68
- }
56
+ const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocumentActions)
57
+
58
+ /** @internal */
59
+ async function _applyDocumentActions(
60
+ {instance, state}: StoreContext<DocumentStoreState>,
61
+ actionOrActions: DocumentAction | DocumentAction[],
62
+ {transactionId = crypto.randomUUID(), disableBatching}: ApplyDocumentActionsOptions = {},
63
+ ): Promise<ActionsResult> {
64
+ const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]
65
+
66
+ let projectId
67
+ let dataset
68
+ for (const action of actions) {
69
+ if (action.projectId) {
70
+ if (!projectId) projectId = action.projectId
71
+ if (action.projectId !== projectId) {
72
+ throw new Error(
73
+ `Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`,
74
+ )
75
+ }
69
76
 
70
- const fatalError$ = state.observable.pipe(
71
- map((s) => s.error),
72
- first(Boolean),
73
- map((error) => ({type: 'error', error}) as const),
74
- )
75
-
76
- const transactionError$ = events.pipe(
77
- filter((e) => e.type === 'error'),
78
- first((e) => e.transactionId === transactionId),
79
- )
80
-
81
- const appliedTransaction$ = state.observable.pipe(
82
- map((s) => s.applied),
83
- distinctUntilChanged(),
84
- map((applied) => applied.find((t) => t.transactionId === transactionId)),
85
- first(Boolean),
86
- )
87
-
88
- const successfulTransaction$ = events.pipe(
89
- filter((e) => e.type === 'accepted'),
90
- first((e) => e.outgoing.batchedTransactionIds.includes(transactionId)),
91
- )
92
-
93
- const rejectedTransaction$ = events.pipe(
94
- filter((e) => e.type === 'reverted'),
95
- first((e) => e.outgoing.batchedTransactionIds.includes(transactionId)),
96
- )
97
-
98
- const appliedTransactionOrError = firstValueFrom(
99
- race([fatalError$, transactionError$, appliedTransaction$]),
100
- )
101
- const acceptedOrRejectedTransaction = firstValueFrom(
102
- race([successfulTransaction$, rejectedTransaction$, transactionError$]),
103
- )
104
-
105
- state.set('queueTransaction', (prev) => queueTransaction(prev, transaction))
106
-
107
- const result = await appliedTransactionOrError
108
- if ('type' in result && result.type === 'error') throw result.error
109
-
110
- const {working: documents, previous, previousRevs} = result as AppliedTransaction
111
- const existingIds = new Set(
112
- Object.entries(previous)
113
- .filter(([, value]) => !!value)
114
- .map(([key]) => key),
115
- )
116
- const resultingIds = new Set(
117
- Object.entries(documents)
118
- .filter(([, value]) => !!value)
119
- .map(([key]) => key),
120
- )
121
- const allIds = new Set([...existingIds, ...resultingIds])
122
-
123
- const updated: string[] = []
124
- const appeared: string[] = []
125
- const disappeared: string[] = []
126
-
127
- for (const id of allIds) {
128
- if (existingIds.has(id) && resultingIds.has(id)) {
129
- updated.push(id)
130
- } else if (!existingIds.has(id) && resultingIds.has(id)) {
131
- appeared.push(id)
132
- } else if (!resultingIds.has(id) && existingIds.has(id)) {
133
- disappeared.push(id)
77
+ if (action.dataset) {
78
+ if (!dataset) dataset = action.dataset
79
+ if (action.dataset !== dataset) {
80
+ throw new Error(
81
+ `Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
82
+ )
83
+ }
134
84
  }
135
85
  }
86
+ }
136
87
 
137
- async function submitted() {
138
- const raceResult = await acceptedOrRejectedTransaction
139
- if (raceResult.type !== 'accepted') throw raceResult.error
140
- return raceResult.result
88
+ if (
89
+ (projectId && projectId !== instance.config.projectId) ||
90
+ (dataset && dataset !== instance.config.dataset)
91
+ ) {
92
+ const matchedInstance = instance.match({projectId, dataset})
93
+ if (!matchedInstance) {
94
+ throw new Error(
95
+ `Could not find a matching instance for projectId: "${projectId}" and dataset: "${dataset}"`,
96
+ )
141
97
  }
142
-
143
- return {
98
+ return boundApplyDocumentActions(matchedInstance, actionOrActions, {
99
+ disableBatching,
144
100
  transactionId,
145
- documents,
146
- previous,
147
- previousRevs,
148
- appeared,
149
- updated,
150
- disappeared,
151
- submitted,
101
+ })
102
+ }
103
+
104
+ const {events} = state.get()
105
+
106
+ const transaction: QueuedTransaction = {
107
+ transactionId,
108
+ actions,
109
+ ...(disableBatching && {disableBatching}),
110
+ }
111
+
112
+ const fatalError$ = state.observable.pipe(
113
+ map((s) => s.error),
114
+ first(Boolean),
115
+ map((error) => ({type: 'error', error}) as const),
116
+ )
117
+
118
+ const transactionError$ = events.pipe(
119
+ filter((e) => e.type === 'error'),
120
+ first((e) => e.transactionId === transactionId),
121
+ )
122
+
123
+ const appliedTransaction$ = state.observable.pipe(
124
+ map((s) => s.applied),
125
+ distinctUntilChanged(),
126
+ map((applied) => applied.find((t) => t.transactionId === transactionId)),
127
+ first(Boolean),
128
+ )
129
+
130
+ const successfulTransaction$ = events.pipe(
131
+ filter((e) => e.type === 'accepted'),
132
+ first((e) => e.outgoing.batchedTransactionIds.includes(transactionId)),
133
+ )
134
+
135
+ const rejectedTransaction$ = events.pipe(
136
+ filter((e) => e.type === 'reverted'),
137
+ first((e) => e.outgoing.batchedTransactionIds.includes(transactionId)),
138
+ )
139
+
140
+ const appliedTransactionOrError = firstValueFrom(
141
+ race([fatalError$, transactionError$, appliedTransaction$]),
142
+ )
143
+ const acceptedOrRejectedTransaction = firstValueFrom(
144
+ race([successfulTransaction$, rejectedTransaction$, transactionError$]),
145
+ )
146
+
147
+ state.set('queueTransaction', (prev) => queueTransaction(prev, transaction))
148
+
149
+ const result = await appliedTransactionOrError
150
+ if ('type' in result && result.type === 'error') throw result.error
151
+
152
+ const {working: documents, previous, previousRevs} = result as AppliedTransaction
153
+ const existingIds = new Set(
154
+ Object.entries(previous)
155
+ .filter(([, value]) => !!value)
156
+ .map(([key]) => key),
157
+ )
158
+ const resultingIds = new Set(
159
+ Object.entries(documents)
160
+ .filter(([, value]) => !!value)
161
+ .map(([key]) => key),
162
+ )
163
+ const allIds = new Set([...existingIds, ...resultingIds])
164
+
165
+ const updated: string[] = []
166
+ const appeared: string[] = []
167
+ const disappeared: string[] = []
168
+
169
+ for (const id of allIds) {
170
+ if (existingIds.has(id) && resultingIds.has(id)) {
171
+ updated.push(id)
172
+ } else if (!existingIds.has(id) && resultingIds.has(id)) {
173
+ appeared.push(id)
174
+ } else if (!resultingIds.has(id) && existingIds.has(id)) {
175
+ disappeared.push(id)
152
176
  }
153
177
  }
154
- })
178
+
179
+ async function submitted() {
180
+ const raceResult = await acceptedOrRejectedTransaction
181
+ if (raceResult.type !== 'accepted') throw raceResult.error
182
+ return raceResult.result
183
+ }
184
+
185
+ return {
186
+ transactionId,
187
+ documents,
188
+ previous,
189
+ previousRevs,
190
+ appeared,
191
+ updated,
192
+ disappeared,
193
+ submitted,
194
+ }
195
+ }