@sanity/sdk 0.0.0-alpha.21 → 0.0.0-alpha.23
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 +428 -325
- package/dist/index.js +1618 -1553
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/_exports/index.ts +31 -30
- package/src/auth/authStore.test.ts +149 -104
- package/src/auth/authStore.ts +51 -100
- package/src/auth/handleAuthCallback.test.ts +67 -34
- package/src/auth/handleAuthCallback.ts +8 -7
- package/src/auth/logout.test.ts +61 -29
- package/src/auth/logout.ts +26 -28
- package/src/auth/refreshStampedToken.test.ts +9 -9
- package/src/auth/refreshStampedToken.ts +62 -56
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +5 -5
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +45 -47
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -5
- package/src/auth/subscribeToStorageEventsAndSetToken.ts +22 -24
- package/src/client/clientStore.test.ts +131 -67
- package/src/client/clientStore.ts +117 -116
- package/src/comlink/controller/actions/destroyController.test.ts +38 -13
- package/src/comlink/controller/actions/destroyController.ts +11 -15
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +56 -27
- package/src/comlink/controller/actions/getOrCreateChannel.ts +37 -35
- package/src/comlink/controller/actions/getOrCreateController.test.ts +27 -16
- package/src/comlink/controller/actions/getOrCreateController.ts +23 -22
- package/src/comlink/controller/actions/releaseChannel.test.ts +37 -13
- package/src/comlink/controller/actions/releaseChannel.ts +22 -21
- package/src/comlink/controller/comlinkControllerStore.test.ts +65 -36
- package/src/comlink/controller/comlinkControllerStore.ts +44 -5
- package/src/comlink/node/actions/getOrCreateNode.test.ts +31 -15
- package/src/comlink/node/actions/getOrCreateNode.ts +30 -29
- package/src/comlink/node/actions/releaseNode.test.ts +75 -55
- package/src/comlink/node/actions/releaseNode.ts +19 -21
- package/src/comlink/node/comlinkNodeStore.test.ts +6 -11
- package/src/comlink/node/comlinkNodeStore.ts +22 -5
- package/src/config/authConfig.ts +79 -0
- package/src/config/sanityConfig.ts +48 -0
- package/src/datasets/datasets.test.ts +2 -2
- package/src/datasets/datasets.ts +18 -5
- package/src/document/actions.test.ts +22 -10
- package/src/document/actions.ts +44 -56
- package/src/document/applyDocumentActions.test.ts +96 -36
- package/src/document/applyDocumentActions.ts +140 -99
- package/src/document/documentStore.test.ts +103 -155
- package/src/document/documentStore.ts +247 -237
- package/src/document/listen.ts +56 -55
- package/src/document/patchOperations.ts +0 -43
- package/src/document/permissions.test.ts +25 -12
- package/src/document/permissions.ts +11 -4
- package/src/document/processActions.test.ts +41 -8
- package/src/document/reducers.test.ts +87 -16
- package/src/document/reducers.ts +2 -2
- package/src/document/sharedListener.test.ts +34 -16
- package/src/document/sharedListener.ts +33 -11
- package/src/preview/getPreviewState.test.ts +40 -39
- package/src/preview/getPreviewState.ts +68 -56
- package/src/preview/previewConstants.ts +43 -0
- package/src/preview/previewQuery.test.ts +1 -1
- package/src/preview/previewQuery.ts +4 -5
- package/src/preview/previewStore.test.ts +13 -58
- package/src/preview/previewStore.ts +7 -21
- package/src/preview/resolvePreview.test.ts +33 -104
- package/src/preview/resolvePreview.ts +11 -21
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +96 -97
- package/src/preview/subscribeToStateAndFetchBatches.ts +85 -81
- package/src/preview/util.ts +1 -0
- package/src/project/project.test.ts +3 -3
- package/src/project/project.ts +28 -5
- package/src/projection/getProjectionState.test.ts +69 -49
- package/src/projection/getProjectionState.ts +42 -50
- package/src/projection/projectionQuery.ts +1 -1
- package/src/projection/projectionStore.test.ts +13 -51
- package/src/projection/projectionStore.ts +6 -18
- package/src/projection/resolveProjection.test.ts +32 -127
- package/src/projection/resolveProjection.ts +15 -28
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +105 -90
- package/src/projection/subscribeToStateAndFetchBatches.ts +94 -81
- package/src/projection/util.ts +2 -0
- package/src/projects/projects.test.ts +13 -4
- package/src/projects/projects.ts +6 -1
- package/src/query/queryStore.test.ts +10 -47
- package/src/query/queryStore.ts +151 -133
- package/src/query/queryStoreConstants.ts +2 -0
- package/src/store/createActionBinder.test.ts +153 -0
- package/src/store/createActionBinder.ts +176 -0
- package/src/store/createSanityInstance.test.ts +84 -0
- package/src/store/createSanityInstance.ts +124 -0
- package/src/store/createStateSourceAction.test.ts +196 -0
- package/src/store/createStateSourceAction.ts +260 -0
- package/src/store/createStoreInstance.test.ts +81 -0
- package/src/store/createStoreInstance.ts +80 -0
- package/src/store/createStoreState.test.ts +85 -0
- package/src/store/createStoreState.ts +92 -0
- package/src/store/defineStore.test.ts +18 -0
- package/src/store/defineStore.ts +81 -0
- package/src/users/reducers.test.ts +318 -0
- package/src/users/reducers.ts +88 -0
- package/src/users/types.ts +46 -4
- package/src/users/usersConstants.ts +4 -0
- package/src/users/usersStore.test.ts +350 -223
- package/src/users/usersStore.ts +285 -149
- package/src/utils/createFetcherStore.test.ts +6 -7
- package/src/utils/createFetcherStore.ts +150 -153
- package/src/{common/util.test.ts → utils/hashString.test.ts} +1 -1
- package/src/auth/fetchLoginUrls.test.ts +0 -163
- package/src/auth/fetchLoginUrls.ts +0 -74
- package/src/common/createLiveEventSubscriber.test.ts +0 -121
- package/src/common/createLiveEventSubscriber.ts +0 -55
- package/src/common/types.ts +0 -4
- package/src/instance/identity.test.ts +0 -46
- package/src/instance/identity.ts +0 -29
- package/src/instance/sanityInstance.test.ts +0 -77
- package/src/instance/sanityInstance.ts +0 -57
- package/src/instance/types.ts +0 -37
- package/src/preview/getPreviewProjection.ts +0 -45
- package/src/resources/README.md +0 -370
- package/src/resources/createAction.test.ts +0 -101
- package/src/resources/createAction.ts +0 -44
- package/src/resources/createResource.test.ts +0 -112
- package/src/resources/createResource.ts +0 -102
- package/src/resources/createStateSourceAction.test.ts +0 -114
- package/src/resources/createStateSourceAction.ts +0 -83
- package/src/resources/createStore.test.ts +0 -67
- package/src/resources/createStore.ts +0 -46
- package/src/store/createStore.test.ts +0 -108
- package/src/store/createStore.ts +0 -106
- /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 {
|
|
6
|
-
import {type
|
|
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
|
|
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
|
|
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
|
|
51
|
-
): ReturnType<typeof
|
|
52
|
-
return
|
|
51
|
+
...args: Parameters<typeof boundApplyDocumentActions>
|
|
52
|
+
): ReturnType<typeof boundApplyDocumentActions> {
|
|
53
|
+
return boundApplyDocumentActions(...args)
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
):
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
98
|
+
return boundApplyDocumentActions(matchedInstance, actionOrActions, {
|
|
99
|
+
disableBatching,
|
|
144
100
|
transactionId,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
}
|