@sanity/sdk 3.0.0-rc.0 → 3.0.0-rc.1
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/_chunks-dts/createGroqSearchFilter.d.ts +1147 -0
- package/dist/_chunks-dts/createGroqSearchFilter.d.ts.map +1 -0
- package/dist/_chunks-es/_internal.js +134 -0
- package/dist/_chunks-es/_internal.js.map +1 -0
- package/dist/_chunks-es/createGroqSearchFilter.js +1864 -0
- package/dist/_chunks-es/createGroqSearchFilter.js.map +1 -0
- package/dist/_chunks-es/telemetryManager.js +93 -0
- package/dist/_chunks-es/telemetryManager.js.map +1 -0
- package/dist/_exports/_internal.d.ts +95 -0
- package/dist/_exports/_internal.d.ts.map +1 -0
- package/dist/_exports/_internal.js +24 -0
- package/dist/_exports/_internal.js.map +1 -0
- package/dist/index.d.ts +1079 -1076
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1967 -2302
- package/dist/index.js.map +1 -1
- package/package.json +48 -47
- package/src/_exports/_internal.ts +26 -0
- package/src/_exports/index.ts +120 -11
- package/src/access/checkPermissions.test.ts +55 -0
- package/src/access/checkPermissions.ts +54 -0
- package/src/agent/agentActions.test.ts +16 -60
- package/src/agent/agentActions.ts +25 -38
- package/src/applications/applications.test-d.ts +83 -0
- package/src/applications/applications.test.ts +130 -0
- package/src/applications/applications.ts +364 -0
- package/src/applications/userApplications.test.ts +126 -0
- package/src/applications/userApplications.ts +261 -0
- package/src/auth/authLogger.ts +30 -0
- package/src/auth/authMode.test.ts +25 -0
- package/src/auth/authMode.ts +6 -3
- package/src/auth/authStore.test.ts +194 -13
- package/src/auth/authStore.ts +59 -26
- package/src/auth/getOrganizationVerificationState.test.ts +18 -17
- package/src/auth/getOrganizationVerificationState.ts +4 -4
- package/src/auth/handleAuthCallback.test.ts +35 -1
- package/src/auth/handleAuthCallback.ts +25 -6
- package/src/auth/logout.test.ts +75 -2
- package/src/auth/logout.ts +23 -4
- package/src/auth/refreshStampedToken.test.ts +219 -469
- package/src/auth/refreshStampedToken.ts +79 -195
- package/src/auth/studioAuth.ts +1 -1
- package/src/auth/studioModeAuth.ts +2 -1
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +146 -13
- package/src/auth/subscribeToStateAndFetchCurrentUser.ts +37 -6
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +2 -0
- package/src/client/clientStore.test.ts +62 -37
- package/src/client/clientStore.ts +45 -75
- package/src/client/liveEvents.test.ts +151 -0
- package/src/client/liveEvents.ts +107 -0
- package/src/comlink/controller/actions/destroyController.test.ts +4 -1
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +4 -1
- package/src/comlink/controller/actions/getOrCreateChannel.ts +2 -2
- package/src/comlink/controller/actions/getOrCreateController.test.ts +4 -1
- package/src/comlink/controller/actions/releaseChannel.test.ts +1 -1
- package/src/comlink/controller/comlinkControllerStore.test.ts +4 -1
- package/src/comlink/node/actions/getOrCreateNode.test.ts +9 -3
- package/src/comlink/node/actions/getOrCreateNode.ts +2 -2
- package/src/comlink/node/actions/releaseNode.test.ts +7 -4
- package/src/comlink/node/comlinkNodeStore.test.ts +2 -2
- package/src/comlink/node/getNodeState.test.ts +1 -1
- package/src/comlink/node/getNodeState.ts +2 -1
- package/src/config/__tests__/handles.test.ts +18 -12
- package/src/config/handles.ts +25 -7
- package/src/config/sanityConfig.ts +109 -79
- package/src/datasets/datasets.test.ts +2 -2
- package/src/datasets/datasets.ts +10 -4
- package/src/document/actions.test.ts +115 -33
- package/src/document/actions.ts +212 -15
- package/src/document/applyDocumentActions.test.ts +38 -12
- package/src/document/applyDocumentActions.ts +30 -9
- package/src/document/documentConstants.ts +7 -0
- package/src/document/documentStore.concurrency.test.ts +1306 -0
- package/src/document/documentStore.test.ts +673 -278
- package/src/document/documentStore.ts +162 -71
- package/src/document/events.test.ts +57 -2
- package/src/document/events.ts +82 -26
- package/src/document/listen.test.ts +56 -0
- package/src/document/listen.ts +74 -21
- package/src/document/listenerEventOperators.test.ts +311 -0
- package/src/document/listenerEventOperators.ts +217 -0
- package/src/document/patchOperations.test.ts +49 -2
- package/src/document/patchOperations.ts +50 -0
- package/src/document/permissions.test.ts +105 -60
- package/src/document/permissions.ts +44 -19
- package/src/document/processActions/create.ts +138 -0
- package/src/document/processActions/delete.ts +100 -0
- package/src/document/processActions/discard.ts +63 -0
- package/src/document/processActions/edit.ts +154 -0
- package/src/document/processActions/processActions.ts +218 -0
- package/src/document/processActions/publish.ts +120 -0
- package/src/document/processActions/releaseArchive.ts +77 -0
- package/src/document/processActions/releaseCreate.ts +59 -0
- package/src/document/processActions/releaseDelete.ts +65 -0
- package/src/document/processActions/releaseEdit.ts +37 -0
- package/src/document/processActions/releasePublish.ts +45 -0
- package/src/document/processActions/releaseSchedule.ts +87 -0
- package/src/document/processActions/releaseUtil.ts +31 -0
- package/src/document/processActions/shared.ts +308 -0
- package/src/document/processActions/unpublish.ts +85 -0
- package/src/document/processActions.test.ts +1129 -121
- package/src/document/reducers.test.ts +639 -64
- package/src/document/reducers.ts +187 -48
- package/src/document/resourceRules.test.ts +178 -0
- package/src/document/resourceRules.ts +117 -0
- package/src/document/sharedListener.test.ts +16 -14
- package/src/document/sharedListener.ts +24 -8
- package/src/document/util.ts +14 -0
- package/src/favorites/favorites.test.ts +5 -6
- package/src/installations/installations.test-d.ts +68 -0
- package/src/installations/installations.test.ts +62 -0
- package/src/installations/installations.ts +202 -0
- package/src/organization/organization.test-d.ts +36 -0
- package/src/organization/organization.test.ts +138 -0
- package/src/organization/organization.ts +138 -0
- package/src/organizations/organizations.test-d.ts +56 -0
- package/src/organizations/organizations.test.ts +150 -0
- package/src/organizations/organizations.ts +107 -0
- package/src/presence/bifurTransport.test.ts +46 -6
- package/src/presence/bifurTransport.ts +19 -2
- package/src/presence/presenceStore.test.ts +94 -23
- package/src/presence/presenceStore.ts +85 -29
- package/src/preview/getPreviewState.test.ts +137 -0
- package/src/preview/getPreviewState.ts +69 -0
- package/src/preview/previewProjectionUtils.test.ts +9 -22
- package/src/preview/previewProjectionUtils.ts +7 -3
- package/src/preview/resolvePreview.test.ts +64 -0
- package/src/preview/resolvePreview.ts +43 -0
- package/src/preview/types.ts +10 -0
- package/src/project/project.test-d.ts +37 -0
- package/src/project/project.test.ts +157 -12
- package/src/project/project.ts +138 -25
- package/src/projection/getProjectionState.test.ts +18 -20
- package/src/projection/getProjectionState.ts +23 -3
- package/src/projection/projectionQuery.ts +5 -5
- package/src/projection/projectionStore.test.ts +1 -1
- package/src/projection/resolveProjection.test.ts +1 -2
- package/src/projection/resolveProjection.ts +16 -0
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +1 -1
- package/src/projection/subscribeToStateAndFetchBatches.ts +32 -23
- package/src/projection/types.ts +8 -0
- package/src/projects/projects.test-d.ts +32 -0
- package/src/projects/projects.test.ts +108 -42
- package/src/projects/projects.ts +63 -27
- package/src/query/queryStore.test.ts +259 -123
- package/src/query/queryStore.ts +78 -63
- package/src/query/reducers.test.ts +0 -42
- package/src/query/reducers.ts +2 -18
- package/src/releases/getPerspectiveState.test.ts +17 -23
- package/src/releases/getPerspectiveState.ts +8 -20
- package/src/releases/observeReleases.test.ts +127 -0
- package/src/releases/observeReleases.ts +102 -0
- package/src/releases/releasesStore.test.ts +100 -53
- package/src/releases/releasesStore.ts +69 -44
- package/src/releases/utils/sortReleases.test.ts +38 -2
- package/src/releases/utils/sortReleases.ts +15 -10
- package/src/store/createActionBinder.test.ts +87 -90
- package/src/store/createActionBinder.ts +79 -25
- package/src/store/createSanityInstance.test.ts +83 -13
- package/src/store/createSanityInstance.ts +77 -2
- package/src/store/createStateSourceAction.test.ts +64 -2
- package/src/store/createStateSourceAction.ts +39 -44
- package/src/store/createStoreInstance.test.ts +4 -2
- package/src/store/fetcherStore.test-d.ts +121 -0
- package/src/store/fetcherStore.test.ts +299 -0
- package/src/store/fetcherStore.ts +801 -0
- package/src/telemetry/environment.test.ts +119 -0
- package/src/telemetry/environment.ts +92 -0
- package/src/telemetry/events.ts +42 -0
- package/src/telemetry/initTelemetry.test.ts +432 -0
- package/src/telemetry/initTelemetry.ts +218 -0
- package/src/telemetry/telemetryManager.test.ts +327 -0
- package/src/telemetry/telemetryManager.ts +199 -0
- package/src/users/reducers.test.ts +6 -1
- package/src/users/reducers.ts +5 -6
- package/src/users/types.ts +4 -4
- package/src/users/usersStore.test.ts +16 -12
- package/src/users/usersStore.ts +5 -1
- package/src/utils/buildQuery.test.ts +14 -0
- package/src/utils/buildQuery.ts +17 -0
- package/src/utils/createFetcherStore.test.ts +7 -5
- package/src/utils/createFetcherStore.ts +8 -5
- package/src/utils/getStagingApiHost.test.ts +21 -0
- package/src/utils/getStagingApiHost.ts +14 -0
- package/src/utils/ids.test.ts +1 -29
- package/src/utils/ids.ts +0 -10
- package/src/utils/includeShape.ts +36 -0
- package/src/utils/isImportError.test.ts +72 -0
- package/src/utils/isImportError.ts +34 -0
- package/src/utils/logger.test.ts +12 -0
- package/src/utils/logger.ts +8 -3
- package/src/utils/object.test.ts +95 -0
- package/src/utils/object.ts +142 -0
- package/src/utils/setCleanupTimeout.ts +37 -0
- package/src/document/processActions.ts +0 -707
- package/src/utils/defineIntent.test.ts +0 -477
- package/src/utils/defineIntent.ts +0 -244
|
@@ -1,707 +0,0 @@
|
|
|
1
|
-
import {diffValue} from '@sanity/diff-patch'
|
|
2
|
-
import {
|
|
3
|
-
type Mutation,
|
|
4
|
-
type PatchOperations,
|
|
5
|
-
type Reference,
|
|
6
|
-
type SanityDocument,
|
|
7
|
-
} from '@sanity/types'
|
|
8
|
-
import {evaluateSync, type ExprNode} from 'groq-js'
|
|
9
|
-
import {isEqual} from 'lodash-es'
|
|
10
|
-
|
|
11
|
-
import {getDraftId, getPublishedId} from '../utils/ids'
|
|
12
|
-
import {type DocumentAction} from './actions'
|
|
13
|
-
import {type Grant} from './permissions'
|
|
14
|
-
import {type DocumentSet, getId, processMutations} from './processMutations'
|
|
15
|
-
import {type HttpAction} from './reducers'
|
|
16
|
-
|
|
17
|
-
function checkGrant(grantExpr: ExprNode, document: SanityDocument): boolean {
|
|
18
|
-
const value = evaluateSync(grantExpr, {params: {document}})
|
|
19
|
-
return value.type === 'boolean' && value.data
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
interface ProcessActionsOptions {
|
|
23
|
-
/**
|
|
24
|
-
* The ID of this transaction. This will become the resulting `_rev` for all
|
|
25
|
-
* documents affected by changes derived from the current set of actions.
|
|
26
|
-
*/
|
|
27
|
-
transactionId: string
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The actions to apply to the given documents
|
|
31
|
-
*/
|
|
32
|
-
actions: DocumentAction[]
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The set of documents these actions were intended to be applied to. These
|
|
36
|
-
* set of documents should be captured right before a queued action is
|
|
37
|
-
* applied.
|
|
38
|
-
*/
|
|
39
|
-
base: DocumentSet
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The current "working" set of documents. A patch will be created by applying
|
|
43
|
-
* the actions to the base. This patch will then be applied to the working
|
|
44
|
-
* set for conflict resolution. Initially, this value should match the base
|
|
45
|
-
* set.
|
|
46
|
-
*/
|
|
47
|
-
working: DocumentSet
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* The timestamp to use for `_updateAt` and other similar timestamps for this
|
|
51
|
-
* transaction
|
|
52
|
-
*/
|
|
53
|
-
timestamp: string
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* the lookup with pre-parsed GROQ expressions
|
|
57
|
-
*/
|
|
58
|
-
grants: Record<Grant, ExprNode>
|
|
59
|
-
|
|
60
|
-
// // TODO: implement initial values from the schema?
|
|
61
|
-
// initialValues?: {[TDocumentType in string]?: {_type: string}}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface ProcessActionsResult {
|
|
65
|
-
/**
|
|
66
|
-
* The resulting document set after the actions have been applied. This is
|
|
67
|
-
* derived from the working documents.
|
|
68
|
-
*/
|
|
69
|
-
working: DocumentSet
|
|
70
|
-
/**
|
|
71
|
-
* The document set before the actions have been applied. This is simply the
|
|
72
|
-
* input of the `working` document set.
|
|
73
|
-
*/
|
|
74
|
-
previous: DocumentSet
|
|
75
|
-
/**
|
|
76
|
-
* The outgoing action that were collected when applying the actions. These
|
|
77
|
-
* are sent to the Actions HTTP API
|
|
78
|
-
*/
|
|
79
|
-
outgoingActions: HttpAction[]
|
|
80
|
-
/**
|
|
81
|
-
* The outgoing mutations that were collected when applying the actions. These
|
|
82
|
-
* are here for debugging purposes.
|
|
83
|
-
*/
|
|
84
|
-
outgoingMutations: Mutation[]
|
|
85
|
-
/**
|
|
86
|
-
* The previous revisions of the given documents before the actions were applied.
|
|
87
|
-
*/
|
|
88
|
-
previousRevs: {[TDocumentId in string]?: string}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
interface ActionErrorOptions {
|
|
92
|
-
message: string
|
|
93
|
-
documentId: string
|
|
94
|
-
transactionId: string
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Thrown when a precondition for an action failed.
|
|
99
|
-
*/
|
|
100
|
-
export class ActionError extends Error implements ActionErrorOptions {
|
|
101
|
-
documentId!: string
|
|
102
|
-
transactionId!: string
|
|
103
|
-
|
|
104
|
-
constructor(options: ActionErrorOptions) {
|
|
105
|
-
super(options.message)
|
|
106
|
-
Object.assign(this, options)
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export class PermissionActionError extends ActionError {}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Applies the given set of actions to the working set of documents and converts
|
|
114
|
-
* high-level actions into lower-level outgoing mutations/actions that respect
|
|
115
|
-
* the current state of the working documents.
|
|
116
|
-
*
|
|
117
|
-
* Supports a "base" and "working" set of documents to allow actions to be
|
|
118
|
-
* applied on top of a different working set of documents in a 3-way merge
|
|
119
|
-
*
|
|
120
|
-
* Actions are applied to the base set of documents first. The difference
|
|
121
|
-
* between the base before and after is used to create a patch. This patch is
|
|
122
|
-
* then applied to the working set of documents and is set as the outgoing patch
|
|
123
|
-
* sent to the server.
|
|
124
|
-
*/
|
|
125
|
-
export function processActions({
|
|
126
|
-
actions,
|
|
127
|
-
transactionId,
|
|
128
|
-
working: initialWorking,
|
|
129
|
-
base: initialBase,
|
|
130
|
-
timestamp,
|
|
131
|
-
grants,
|
|
132
|
-
}: ProcessActionsOptions): ProcessActionsResult {
|
|
133
|
-
let working: DocumentSet = {...initialWorking}
|
|
134
|
-
let base: DocumentSet = {...initialBase}
|
|
135
|
-
|
|
136
|
-
const outgoingActions: HttpAction[] = []
|
|
137
|
-
const outgoingMutations: Mutation[] = []
|
|
138
|
-
|
|
139
|
-
for (const action of actions) {
|
|
140
|
-
switch (action.type) {
|
|
141
|
-
case 'document.create': {
|
|
142
|
-
const documentId = getId(action.documentId)
|
|
143
|
-
|
|
144
|
-
if (action.liveEdit) {
|
|
145
|
-
// For liveEdit documents, create directly without draft/published logic
|
|
146
|
-
if (working[documentId]) {
|
|
147
|
-
throw new ActionError({
|
|
148
|
-
documentId,
|
|
149
|
-
transactionId,
|
|
150
|
-
message: `This document already exists.`,
|
|
151
|
-
})
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const newDocBase = {_type: action.documentType, _id: documentId}
|
|
155
|
-
const newDocWorking = {_type: action.documentType, _id: documentId}
|
|
156
|
-
const mutations: Mutation[] = [{create: newDocWorking}]
|
|
157
|
-
|
|
158
|
-
base = processMutations({
|
|
159
|
-
documents: base,
|
|
160
|
-
transactionId,
|
|
161
|
-
mutations: [{create: newDocBase}],
|
|
162
|
-
timestamp,
|
|
163
|
-
})
|
|
164
|
-
working = processMutations({
|
|
165
|
-
documents: working,
|
|
166
|
-
transactionId,
|
|
167
|
-
mutations,
|
|
168
|
-
timestamp,
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
if (!checkGrant(grants.create, working[documentId] as SanityDocument)) {
|
|
172
|
-
throw new PermissionActionError({
|
|
173
|
-
documentId,
|
|
174
|
-
transactionId,
|
|
175
|
-
message: `You do not have permission to create document "${documentId}".`,
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
outgoingMutations.push(...mutations)
|
|
180
|
-
outgoingActions.push({
|
|
181
|
-
actionType: 'sanity.action.document.create',
|
|
182
|
-
publishedId: documentId,
|
|
183
|
-
attributes: newDocWorking,
|
|
184
|
-
})
|
|
185
|
-
continue
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Standard draft/published logic
|
|
189
|
-
const draftId = getDraftId(documentId)
|
|
190
|
-
const publishedId = getPublishedId(documentId)
|
|
191
|
-
|
|
192
|
-
if (working[draftId]) {
|
|
193
|
-
throw new ActionError({
|
|
194
|
-
documentId,
|
|
195
|
-
transactionId,
|
|
196
|
-
message: `A draft version of this document already exists. Please use or discard the existing draft before creating a new one.`,
|
|
197
|
-
})
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Spread the (possibly undefined) published version directly.
|
|
201
|
-
const newDocBase = {
|
|
202
|
-
...base[publishedId],
|
|
203
|
-
_type: action.documentType,
|
|
204
|
-
_id: draftId,
|
|
205
|
-
...action.initialValue,
|
|
206
|
-
}
|
|
207
|
-
const newDocWorking = {
|
|
208
|
-
...working[publishedId],
|
|
209
|
-
_type: action.documentType,
|
|
210
|
-
_id: draftId,
|
|
211
|
-
...action.initialValue,
|
|
212
|
-
}
|
|
213
|
-
const mutations: Mutation[] = [{create: newDocWorking}]
|
|
214
|
-
|
|
215
|
-
base = processMutations({
|
|
216
|
-
documents: base,
|
|
217
|
-
transactionId,
|
|
218
|
-
mutations: [{create: newDocBase}],
|
|
219
|
-
timestamp,
|
|
220
|
-
})
|
|
221
|
-
working = processMutations({
|
|
222
|
-
documents: working,
|
|
223
|
-
transactionId,
|
|
224
|
-
mutations,
|
|
225
|
-
timestamp,
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
if (!checkGrant(grants.create, working[draftId] as SanityDocument)) {
|
|
229
|
-
throw new PermissionActionError({
|
|
230
|
-
documentId,
|
|
231
|
-
transactionId,
|
|
232
|
-
message: `You do not have permission to create a draft for document "${documentId}".`,
|
|
233
|
-
})
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
outgoingMutations.push(...mutations)
|
|
237
|
-
outgoingActions.push({
|
|
238
|
-
actionType: 'sanity.action.document.version.create',
|
|
239
|
-
publishedId,
|
|
240
|
-
attributes: newDocWorking,
|
|
241
|
-
})
|
|
242
|
-
continue
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
case 'document.delete': {
|
|
246
|
-
const documentId = action.documentId
|
|
247
|
-
|
|
248
|
-
if (action.liveEdit) {
|
|
249
|
-
// For liveEdit documents, delete directly
|
|
250
|
-
if (!working[documentId]) {
|
|
251
|
-
throw new ActionError({
|
|
252
|
-
documentId,
|
|
253
|
-
transactionId,
|
|
254
|
-
message: 'The document you are trying to delete does not exist.',
|
|
255
|
-
})
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (!checkGrant(grants.update, working[documentId])) {
|
|
259
|
-
throw new PermissionActionError({
|
|
260
|
-
documentId,
|
|
261
|
-
transactionId,
|
|
262
|
-
message: `You do not have permission to delete this document.`,
|
|
263
|
-
})
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const mutations: Mutation[] = [{delete: {id: documentId}}]
|
|
267
|
-
|
|
268
|
-
base = processMutations({documents: base, transactionId, mutations, timestamp})
|
|
269
|
-
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
270
|
-
|
|
271
|
-
outgoingMutations.push(...mutations)
|
|
272
|
-
outgoingActions.push({
|
|
273
|
-
actionType: 'sanity.action.document.delete',
|
|
274
|
-
publishedId: documentId,
|
|
275
|
-
})
|
|
276
|
-
continue
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// Standard draft/published logic
|
|
280
|
-
const draftId = getDraftId(documentId)
|
|
281
|
-
const publishedId = getPublishedId(documentId)
|
|
282
|
-
|
|
283
|
-
if (!working[publishedId]) {
|
|
284
|
-
throw new ActionError({
|
|
285
|
-
documentId,
|
|
286
|
-
transactionId,
|
|
287
|
-
message: working[draftId]
|
|
288
|
-
? 'Cannot delete a document without a published version.'
|
|
289
|
-
: 'The document you are trying to delete does not exist.',
|
|
290
|
-
})
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
const cantDeleteDraft = working[draftId] && !checkGrant(grants.update, working[draftId])
|
|
294
|
-
const cantDeletePublished =
|
|
295
|
-
working[publishedId] && !checkGrant(grants.update, working[publishedId])
|
|
296
|
-
|
|
297
|
-
if (cantDeleteDraft || cantDeletePublished) {
|
|
298
|
-
throw new PermissionActionError({
|
|
299
|
-
documentId,
|
|
300
|
-
transactionId,
|
|
301
|
-
message: `You do not have permission to delete this document.`,
|
|
302
|
-
})
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const mutations: Mutation[] = [{delete: {id: publishedId}}, {delete: {id: draftId}}]
|
|
306
|
-
const includeDrafts = working[draftId] ? [draftId] : undefined
|
|
307
|
-
|
|
308
|
-
base = processMutations({documents: base, transactionId, mutations, timestamp})
|
|
309
|
-
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
310
|
-
|
|
311
|
-
outgoingMutations.push(...mutations)
|
|
312
|
-
outgoingActions.push({
|
|
313
|
-
actionType: 'sanity.action.document.delete',
|
|
314
|
-
publishedId,
|
|
315
|
-
...(includeDrafts ? {includeDrafts} : {}),
|
|
316
|
-
})
|
|
317
|
-
continue
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
case 'document.discard': {
|
|
321
|
-
const documentId = getId(action.documentId)
|
|
322
|
-
|
|
323
|
-
if (action.liveEdit) {
|
|
324
|
-
throw new ActionError({
|
|
325
|
-
documentId,
|
|
326
|
-
transactionId,
|
|
327
|
-
message: `Cannot discard changes for liveEdit document "${documentId}". LiveEdit documents do not support drafts.`,
|
|
328
|
-
})
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// Standard draft/published logic
|
|
332
|
-
const draftId = getDraftId(documentId)
|
|
333
|
-
const mutations: Mutation[] = [{delete: {id: draftId}}]
|
|
334
|
-
|
|
335
|
-
if (!working[draftId]) {
|
|
336
|
-
throw new ActionError({
|
|
337
|
-
documentId,
|
|
338
|
-
transactionId,
|
|
339
|
-
message: `There is no draft available to discard for document "${documentId}".`,
|
|
340
|
-
})
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (!checkGrant(grants.update, working[draftId])) {
|
|
344
|
-
throw new PermissionActionError({
|
|
345
|
-
documentId,
|
|
346
|
-
transactionId,
|
|
347
|
-
message: `You do not have permission to discard changes for document "${documentId}".`,
|
|
348
|
-
})
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
base = processMutations({documents: base, transactionId, mutations, timestamp})
|
|
352
|
-
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
353
|
-
|
|
354
|
-
outgoingMutations.push(...mutations)
|
|
355
|
-
outgoingActions.push({
|
|
356
|
-
actionType: 'sanity.action.document.version.discard',
|
|
357
|
-
versionId: draftId,
|
|
358
|
-
})
|
|
359
|
-
continue
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
case 'document.edit': {
|
|
363
|
-
const documentId = getId(action.documentId)
|
|
364
|
-
|
|
365
|
-
if (action.liveEdit) {
|
|
366
|
-
// For liveEdit documents, edit directly without draft logic
|
|
367
|
-
const userPatches = action.patches?.map((patch) => ({patch: {id: documentId, ...patch}}))
|
|
368
|
-
|
|
369
|
-
// skip this action if there are no associated patches
|
|
370
|
-
if (!userPatches?.length) continue
|
|
371
|
-
|
|
372
|
-
if (!working[documentId] || !base[documentId]) {
|
|
373
|
-
throw new ActionError({
|
|
374
|
-
documentId,
|
|
375
|
-
transactionId,
|
|
376
|
-
message: `Cannot edit document because it does not exist.`,
|
|
377
|
-
})
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const baseBefore = base[documentId] as SanityDocument
|
|
381
|
-
if (userPatches) {
|
|
382
|
-
base = processMutations({
|
|
383
|
-
documents: base,
|
|
384
|
-
transactionId,
|
|
385
|
-
mutations: userPatches,
|
|
386
|
-
timestamp,
|
|
387
|
-
})
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const baseAfter = base[documentId] as SanityDocument
|
|
391
|
-
const patches = diffValue(baseBefore, baseAfter)
|
|
392
|
-
|
|
393
|
-
const workingBefore = working[documentId] as SanityDocument
|
|
394
|
-
if (!checkGrant(grants.update, workingBefore)) {
|
|
395
|
-
throw new PermissionActionError({
|
|
396
|
-
documentId,
|
|
397
|
-
transactionId,
|
|
398
|
-
message: `You do not have permission to edit document "${documentId}".`,
|
|
399
|
-
})
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
const workingMutations = patches.map((patch) => ({patch: {id: documentId, ...patch}}))
|
|
403
|
-
|
|
404
|
-
working = processMutations({
|
|
405
|
-
documents: working,
|
|
406
|
-
transactionId,
|
|
407
|
-
mutations: workingMutations,
|
|
408
|
-
timestamp,
|
|
409
|
-
})
|
|
410
|
-
|
|
411
|
-
outgoingMutations.push(...workingMutations)
|
|
412
|
-
outgoingActions.push(
|
|
413
|
-
...patches.map(
|
|
414
|
-
(patch): HttpAction => ({
|
|
415
|
-
actionType: 'sanity.action.document.edit',
|
|
416
|
-
// Server requires draftId to have drafts. prefix for validation, even for liveEdit
|
|
417
|
-
draftId: getDraftId(documentId),
|
|
418
|
-
publishedId: documentId,
|
|
419
|
-
patch: patch as PatchOperations,
|
|
420
|
-
}),
|
|
421
|
-
),
|
|
422
|
-
)
|
|
423
|
-
|
|
424
|
-
continue
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
// Standard draft/published logic
|
|
428
|
-
const draftId = getDraftId(documentId)
|
|
429
|
-
const publishedId = getPublishedId(documentId)
|
|
430
|
-
const userPatches = action.patches?.map((patch) => ({patch: {id: draftId, ...patch}}))
|
|
431
|
-
|
|
432
|
-
// skip this action if there are no associated patches
|
|
433
|
-
if (!userPatches?.length) continue
|
|
434
|
-
|
|
435
|
-
if (
|
|
436
|
-
(!working[draftId] && !working[publishedId]) ||
|
|
437
|
-
(!base[draftId] && !base[publishedId])
|
|
438
|
-
) {
|
|
439
|
-
throw new ActionError({
|
|
440
|
-
documentId,
|
|
441
|
-
transactionId,
|
|
442
|
-
message: `Cannot edit document because it does not exist in draft or published form.`,
|
|
443
|
-
})
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
const baseMutations: Mutation[] = []
|
|
447
|
-
if (!base[draftId] && base[publishedId]) {
|
|
448
|
-
baseMutations.push({create: {...base[publishedId], _id: draftId}})
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// the first if statement should make this never be null or undefined
|
|
452
|
-
const baseBefore = (base[draftId] ?? base[publishedId]) as SanityDocument
|
|
453
|
-
if (userPatches) {
|
|
454
|
-
baseMutations.push(...userPatches)
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
base = processMutations({
|
|
458
|
-
documents: base,
|
|
459
|
-
transactionId,
|
|
460
|
-
mutations: baseMutations,
|
|
461
|
-
timestamp,
|
|
462
|
-
})
|
|
463
|
-
// this one will always be defined because a patch mutation will never
|
|
464
|
-
// delete an input document
|
|
465
|
-
const baseAfter = base[draftId] as SanityDocument
|
|
466
|
-
const patches = diffValue(baseBefore, baseAfter)
|
|
467
|
-
|
|
468
|
-
const workingMutations: Mutation[] = []
|
|
469
|
-
if (!working[draftId] && working[publishedId]) {
|
|
470
|
-
const newDraftFromPublished = {...working[publishedId], _id: draftId}
|
|
471
|
-
|
|
472
|
-
if (!checkGrant(grants.create, newDraftFromPublished)) {
|
|
473
|
-
throw new PermissionActionError({
|
|
474
|
-
documentId,
|
|
475
|
-
transactionId,
|
|
476
|
-
message: `You do not have permission to create a draft for editing this document.`,
|
|
477
|
-
})
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
workingMutations.push({create: newDraftFromPublished})
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// the first if statement should make this never be null or undefined
|
|
484
|
-
const workingBefore = (working[draftId] ?? working[publishedId]) as SanityDocument
|
|
485
|
-
if (!checkGrant(grants.update, workingBefore)) {
|
|
486
|
-
throw new PermissionActionError({
|
|
487
|
-
documentId,
|
|
488
|
-
transactionId,
|
|
489
|
-
message: `You do not have permission to edit document "${documentId}".`,
|
|
490
|
-
})
|
|
491
|
-
}
|
|
492
|
-
workingMutations.push(...patches.map((patch) => ({patch: {id: draftId, ...patch}})))
|
|
493
|
-
|
|
494
|
-
working = processMutations({
|
|
495
|
-
documents: working,
|
|
496
|
-
transactionId,
|
|
497
|
-
mutations: workingMutations,
|
|
498
|
-
timestamp,
|
|
499
|
-
})
|
|
500
|
-
|
|
501
|
-
outgoingMutations.push(...workingMutations)
|
|
502
|
-
outgoingActions.push(
|
|
503
|
-
...patches.map(
|
|
504
|
-
(patch): HttpAction => ({
|
|
505
|
-
actionType: 'sanity.action.document.edit',
|
|
506
|
-
draftId,
|
|
507
|
-
publishedId,
|
|
508
|
-
patch: patch as PatchOperations,
|
|
509
|
-
}),
|
|
510
|
-
),
|
|
511
|
-
)
|
|
512
|
-
|
|
513
|
-
continue
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
case 'document.publish': {
|
|
517
|
-
const documentId = getId(action.documentId)
|
|
518
|
-
|
|
519
|
-
if (action.liveEdit) {
|
|
520
|
-
throw new ActionError({
|
|
521
|
-
documentId,
|
|
522
|
-
transactionId,
|
|
523
|
-
message: `Cannot publish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`,
|
|
524
|
-
})
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// Standard draft/published logic
|
|
528
|
-
const draftId = getDraftId(documentId)
|
|
529
|
-
const publishedId = getPublishedId(documentId)
|
|
530
|
-
|
|
531
|
-
const workingDraft = working[draftId]
|
|
532
|
-
const baseDraft = base[draftId]
|
|
533
|
-
if (!workingDraft || !baseDraft) {
|
|
534
|
-
throw new ActionError({
|
|
535
|
-
documentId,
|
|
536
|
-
transactionId,
|
|
537
|
-
message: `Cannot publish because no draft version was found for document "${documentId}".`,
|
|
538
|
-
})
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// Before proceeding, verify that the working draft is identical to the base draft.
|
|
542
|
-
// TODO: is it enough just to check for the _rev or nah?
|
|
543
|
-
if (!isEqual(workingDraft, baseDraft)) {
|
|
544
|
-
throw new ActionError({
|
|
545
|
-
documentId,
|
|
546
|
-
transactionId,
|
|
547
|
-
message: `Publish aborted: The document has changed elsewhere. Please try again.`,
|
|
548
|
-
})
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
const newPublishedFromDraft = {...strengthenOnPublish(workingDraft), _id: publishedId}
|
|
552
|
-
|
|
553
|
-
const mutations: Mutation[] = [
|
|
554
|
-
{delete: {id: draftId}},
|
|
555
|
-
{createOrReplace: newPublishedFromDraft},
|
|
556
|
-
]
|
|
557
|
-
|
|
558
|
-
if (working[draftId] && !checkGrant(grants.update, working[draftId])) {
|
|
559
|
-
throw new PermissionActionError({
|
|
560
|
-
documentId,
|
|
561
|
-
transactionId,
|
|
562
|
-
message: `Publish failed: You do not have permission to update the draft for "${documentId}".`,
|
|
563
|
-
})
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
if (working[publishedId] && !checkGrant(grants.update, newPublishedFromDraft)) {
|
|
567
|
-
throw new PermissionActionError({
|
|
568
|
-
documentId,
|
|
569
|
-
transactionId,
|
|
570
|
-
message: `Publish failed: You do not have permission to update the published version of "${documentId}".`,
|
|
571
|
-
})
|
|
572
|
-
} else if (!working[publishedId] && !checkGrant(grants.create, newPublishedFromDraft)) {
|
|
573
|
-
throw new PermissionActionError({
|
|
574
|
-
documentId,
|
|
575
|
-
transactionId,
|
|
576
|
-
message: `Publish failed: You do not have permission to publish a new version of "${documentId}".`,
|
|
577
|
-
})
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
base = processMutations({documents: base, transactionId, mutations, timestamp})
|
|
581
|
-
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
582
|
-
|
|
583
|
-
outgoingMutations.push(...mutations)
|
|
584
|
-
outgoingActions.push({
|
|
585
|
-
actionType: 'sanity.action.document.publish',
|
|
586
|
-
draftId,
|
|
587
|
-
publishedId,
|
|
588
|
-
})
|
|
589
|
-
continue
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
case 'document.unpublish': {
|
|
593
|
-
const documentId = getId(action.documentId)
|
|
594
|
-
|
|
595
|
-
if (action.liveEdit) {
|
|
596
|
-
throw new ActionError({
|
|
597
|
-
documentId,
|
|
598
|
-
transactionId,
|
|
599
|
-
message: `Cannot unpublish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`,
|
|
600
|
-
})
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
// Standard draft/published logic
|
|
604
|
-
const draftId = getDraftId(documentId)
|
|
605
|
-
const publishedId = getPublishedId(documentId)
|
|
606
|
-
|
|
607
|
-
if (!working[publishedId] && !base[publishedId]) {
|
|
608
|
-
throw new ActionError({
|
|
609
|
-
documentId,
|
|
610
|
-
transactionId,
|
|
611
|
-
message: `Cannot unpublish because the document "${documentId}" is not currently published.`,
|
|
612
|
-
})
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const sourceDoc = working[publishedId] ?? (base[publishedId] as SanityDocument)
|
|
616
|
-
const newDraftFromPublished = {...sourceDoc, _id: draftId}
|
|
617
|
-
const mutations: Mutation[] = [
|
|
618
|
-
{delete: {id: publishedId}},
|
|
619
|
-
{createIfNotExists: newDraftFromPublished},
|
|
620
|
-
]
|
|
621
|
-
|
|
622
|
-
if (!checkGrant(grants.update, sourceDoc)) {
|
|
623
|
-
throw new PermissionActionError({
|
|
624
|
-
documentId,
|
|
625
|
-
transactionId,
|
|
626
|
-
message: `You do not have permission to unpublish the document "${documentId}".`,
|
|
627
|
-
})
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
if (!working[draftId] && !checkGrant(grants.create, newDraftFromPublished)) {
|
|
631
|
-
throw new PermissionActionError({
|
|
632
|
-
documentId,
|
|
633
|
-
transactionId,
|
|
634
|
-
message: `You do not have permission to create a draft from the published version of "${documentId}".`,
|
|
635
|
-
})
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
base = processMutations({
|
|
639
|
-
documents: base,
|
|
640
|
-
transactionId,
|
|
641
|
-
mutations: [
|
|
642
|
-
{delete: {id: publishedId}},
|
|
643
|
-
{createIfNotExists: {...(base[publishedId] ?? sourceDoc), _id: draftId}},
|
|
644
|
-
],
|
|
645
|
-
timestamp,
|
|
646
|
-
})
|
|
647
|
-
working = processMutations({documents: working, transactionId, mutations, timestamp})
|
|
648
|
-
|
|
649
|
-
outgoingMutations.push(...mutations)
|
|
650
|
-
outgoingActions.push({
|
|
651
|
-
actionType: 'sanity.action.document.unpublish',
|
|
652
|
-
draftId,
|
|
653
|
-
publishedId,
|
|
654
|
-
})
|
|
655
|
-
continue
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
default: {
|
|
659
|
-
throw new Error(
|
|
660
|
-
`Unknown action type: "${
|
|
661
|
-
// @ts-expect-error invalid input
|
|
662
|
-
action.type
|
|
663
|
-
}". Please contact support if this issue persists.`,
|
|
664
|
-
)
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
const previousRevs = Object.fromEntries(
|
|
670
|
-
Object.entries(initialWorking).map(([id, doc]) => [id, doc?._rev]),
|
|
671
|
-
)
|
|
672
|
-
|
|
673
|
-
return {
|
|
674
|
-
working,
|
|
675
|
-
outgoingActions,
|
|
676
|
-
outgoingMutations,
|
|
677
|
-
previous: initialWorking,
|
|
678
|
-
previousRevs,
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function strengthenOnPublish(draft: SanityDocument): SanityDocument {
|
|
683
|
-
const isStrengthenReference = (
|
|
684
|
-
value: object,
|
|
685
|
-
): value is Reference & Required<Pick<Reference, '_strengthenOnPublish'>> =>
|
|
686
|
-
'_strengthenOnPublish' in value
|
|
687
|
-
|
|
688
|
-
function strengthen(value: unknown): unknown {
|
|
689
|
-
if (typeof value !== 'object' || !value) return value
|
|
690
|
-
|
|
691
|
-
if (isStrengthenReference(value)) {
|
|
692
|
-
const {_strengthenOnPublish, _weak, ...rest} = value
|
|
693
|
-
return {
|
|
694
|
-
...rest,
|
|
695
|
-
...(_strengthenOnPublish.weak && {_weak: true}),
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
if (Array.isArray(value)) {
|
|
700
|
-
return value.map(strengthen)
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, strengthen(v)]))
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
return strengthen(draft) as SanityDocument
|
|
707
|
-
}
|