@sanity/sdk 2.3.1 → 2.5.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 +173 -105
- package/dist/index.js +354 -122
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/src/_exports/index.ts +30 -0
- package/src/agent/agentActions.test.ts +81 -0
- package/src/agent/agentActions.ts +139 -0
- package/src/auth/authStore.test.ts +13 -13
- package/src/auth/refreshStampedToken.test.ts +16 -16
- package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
- package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
- package/src/auth/utils.ts +36 -0
- package/src/client/clientStore.test.ts +151 -0
- package/src/client/clientStore.ts +39 -1
- package/src/comlink/controller/actions/destroyController.test.ts +2 -2
- package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
- package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
- package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
- package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
- package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
- package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
- package/src/comlink/node/actions/releaseNode.test.ts +2 -2
- package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
- package/src/config/sanityConfig.ts +49 -3
- package/src/document/actions.test.ts +34 -0
- package/src/document/actions.ts +31 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +148 -107
- package/src/document/documentStore.ts +40 -10
- package/src/document/permissions.test.ts +9 -9
- package/src/document/permissions.ts +17 -7
- package/src/document/processActions.test.ts +345 -0
- package/src/document/processActions.ts +185 -2
- package/src/document/reducers.ts +13 -6
- package/src/presence/presenceStore.ts +13 -7
- package/src/preview/previewStore.test.ts +10 -2
- package/src/preview/previewStore.ts +2 -1
- package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
- package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/projection/projectionStore.test.ts +18 -2
- package/src/projection/projectionStore.ts +2 -1
- package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
- package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
- package/src/query/queryStore.ts +7 -4
- package/src/releases/getPerspectiveState.ts +2 -2
- package/src/releases/releasesStore.ts +10 -4
- package/src/store/createActionBinder.test.ts +8 -6
- package/src/store/createActionBinder.ts +50 -14
- package/src/store/createStateSourceAction.test.ts +12 -11
- package/src/store/createStateSourceAction.ts +6 -6
- package/src/store/createStoreInstance.test.ts +29 -16
- package/src/store/createStoreInstance.ts +6 -5
- package/src/store/defineStore.test.ts +1 -1
- package/src/store/defineStore.ts +12 -7
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { createClient, CorsOriginError } from "@sanity/client";
|
|
2
1
|
import { Observable, share, map, distinctUntilChanged, skip, filter, exhaustMap, from, timer, switchMap, takeWhile, firstValueFrom, fromEvent, EMPTY, defer, asapScheduler, combineLatest, of, concatMap, withLatestFrom, concat, throwError, first as first$1, Subject, takeUntil, partition, merge, shareReplay, tap as tap$1, catchError as catchError$1, startWith as startWith$1, pairwise as pairwise$1, groupBy as groupBy$1, mergeMap as mergeMap$1, throttle, race, NEVER, Subscription, retry, debounceTime as debounceTime$1 } from "rxjs";
|
|
2
|
+
import { createClient, CorsOriginError } from "@sanity/client";
|
|
3
|
+
import { pick, omit, isEqual, isObject } from "lodash-es";
|
|
3
4
|
import { devtools } from "zustand/middleware";
|
|
4
5
|
import { createStore } from "zustand/vanilla";
|
|
5
|
-
import { pick, omit, isEqual, isObject } from "lodash-es";
|
|
6
6
|
import { first, switchMap as switchMap$1, groupBy, mergeMap, startWith, pairwise, filter as filter$1, map as map$1, delay, tap, catchError, scan, share as share$1, take, debounceTime } from "rxjs/operators";
|
|
7
7
|
import { createController, createNode } from "@sanity/comlink";
|
|
8
8
|
import { createSelector } from "reselect";
|
|
@@ -17,7 +17,16 @@ import { isKeySegment, isKeyedObject } from "@sanity/types";
|
|
|
17
17
|
import { createDocumentLoaderFromClient } from "@sanity/mutate/_unstable_store";
|
|
18
18
|
import { SDK_CHANNEL_NAME, SDK_NODE_NAME } from "@sanity/message-protocol";
|
|
19
19
|
import { fromUrl } from "@sanity/bifur-client";
|
|
20
|
-
|
|
20
|
+
const SOURCE_ID = "__sanity_internal_sourceId";
|
|
21
|
+
function datasetSource(projectId, dataset) {
|
|
22
|
+
return { [SOURCE_ID]: { projectId, dataset } };
|
|
23
|
+
}
|
|
24
|
+
function mediaLibrarySource(id) {
|
|
25
|
+
return { [SOURCE_ID]: ["media-library", id] };
|
|
26
|
+
}
|
|
27
|
+
function canvasSource(id) {
|
|
28
|
+
return { [SOURCE_ID]: ["canvas", id] };
|
|
29
|
+
}
|
|
21
30
|
function getPublishedId(id) {
|
|
22
31
|
const draftsPrefix = "drafts.";
|
|
23
32
|
return id.startsWith(draftsPrefix) ? id.slice(draftsPrefix.length) : id;
|
|
@@ -88,11 +97,11 @@ function createStoreState(initialState, devToolsOptions) {
|
|
|
88
97
|
})
|
|
89
98
|
};
|
|
90
99
|
}
|
|
91
|
-
function createStoreInstance(instance, { name, getInitialState: getInitialState2, initialize }) {
|
|
92
|
-
const state = createStoreState(getInitialState2(instance), {
|
|
100
|
+
function createStoreInstance(instance, key, { name, getInitialState: getInitialState2, initialize }) {
|
|
101
|
+
const state = createStoreState(getInitialState2(instance, key), {
|
|
93
102
|
enabled: !!getEnv("DEV"),
|
|
94
|
-
name: `${name}-${
|
|
95
|
-
}), dispose = initialize?.({ state, instance }), disposed = { current: !1 };
|
|
103
|
+
name: `${name}-${key.name}`
|
|
104
|
+
}), dispose = initialize?.({ state, instance, key }), disposed = { current: !1 };
|
|
96
105
|
return {
|
|
97
106
|
state,
|
|
98
107
|
dispose: () => {
|
|
@@ -105,21 +114,32 @@ function createActionBinder(keyFn) {
|
|
|
105
114
|
const instanceRegistry = /* @__PURE__ */ new Map(), storeRegistry = /* @__PURE__ */ new Map();
|
|
106
115
|
return function(storeDefinition, action) {
|
|
107
116
|
return function(instance, ...params) {
|
|
108
|
-
const
|
|
117
|
+
const key = keyFn(instance, ...params), compositeKey = storeDefinition.name + (key.name ? `:${key.name}` : "");
|
|
109
118
|
let instances = instanceRegistry.get(compositeKey);
|
|
110
119
|
instances || (instances = /* @__PURE__ */ new Set(), instanceRegistry.set(compositeKey, instances)), instances.has(instance.instanceId) || (instances.add(instance.instanceId), instance.onDispose(() => {
|
|
111
120
|
instances.delete(instance.instanceId), instances.size === 0 && (storeRegistry.get(compositeKey)?.dispose(), storeRegistry.delete(compositeKey), instanceRegistry.delete(compositeKey));
|
|
112
121
|
}));
|
|
113
122
|
let storeInstance = storeRegistry.get(compositeKey);
|
|
114
|
-
return storeInstance || (storeInstance = createStoreInstance(instance, storeDefinition), storeRegistry.set(compositeKey, storeInstance)), action({ instance, state: storeInstance.state }, ...params);
|
|
123
|
+
return storeInstance || (storeInstance = createStoreInstance(instance, key, storeDefinition), storeRegistry.set(compositeKey, storeInstance)), action({ instance, state: storeInstance.state, key }, ...params);
|
|
115
124
|
};
|
|
116
125
|
};
|
|
117
126
|
}
|
|
118
|
-
const bindActionByDataset = createActionBinder((
|
|
127
|
+
const bindActionByDataset = createActionBinder((instance, options) => {
|
|
128
|
+
const projectId = options?.projectId ?? instance.config.projectId, dataset = options?.dataset ?? instance.config.dataset;
|
|
119
129
|
if (!projectId || !dataset)
|
|
120
130
|
throw new Error("This API requires a project ID and dataset configured.");
|
|
121
|
-
return `${projectId}.${dataset}
|
|
122
|
-
}),
|
|
131
|
+
return { name: `${projectId}.${dataset}`, projectId, dataset };
|
|
132
|
+
}), bindActionBySource = createActionBinder((instance, { source }) => {
|
|
133
|
+
if (source) {
|
|
134
|
+
const id = source[SOURCE_ID];
|
|
135
|
+
if (!id) throw new Error("Invalid source (missing ID information)");
|
|
136
|
+
return Array.isArray(id) ? { name: id.join(":") } : { name: `${id.projectId}.${id.dataset}` };
|
|
137
|
+
}
|
|
138
|
+
const { projectId, dataset } = instance.config;
|
|
139
|
+
if (!projectId || !dataset)
|
|
140
|
+
throw new Error("This API requires a project ID and dataset configured.");
|
|
141
|
+
return { name: `${projectId}.${dataset}` };
|
|
142
|
+
}), bindActionGlobally = createActionBinder((..._rest) => ({ name: "global" }));
|
|
123
143
|
function createStateSourceAction(options) {
|
|
124
144
|
const selector = typeof options == "function" ? options : options.selector, subscribeHandler = options && "onSubscribe" in options ? options.onSubscribe : void 0, isEqual2 = options && "isEqual" in options ? options.isEqual ?? Object.is : Object.is, selectorContextCache = /* @__PURE__ */ new WeakMap();
|
|
125
145
|
function stateSourceAction(context, ...params) {
|
|
@@ -167,7 +187,8 @@ function createStateSourceAction(options) {
|
|
|
167
187
|
}
|
|
168
188
|
return stateSourceAction;
|
|
169
189
|
}
|
|
170
|
-
|
|
190
|
+
var AuthStateType = /* @__PURE__ */ ((AuthStateType2) => (AuthStateType2.LOGGED_IN = "logged-in", AuthStateType2.LOGGING_IN = "logging-in", AuthStateType2.ERROR = "error", AuthStateType2.LOGGED_OUT = "logged-out", AuthStateType2))(AuthStateType || {});
|
|
191
|
+
const DEFAULT_BASE = "http://localhost", AUTH_CODE_PARAM = "sid", DEFAULT_API_VERSION$1 = "2021-06-07", REQUEST_TAG_PREFIX = "sanity.sdk.auth", REFRESH_INTERVAL = 720 * 60 * 1e3, LOCK_NAME = "sanity-token-refresh-lock";
|
|
171
192
|
function getLastRefreshTime(storageArea, storageKey) {
|
|
172
193
|
try {
|
|
173
194
|
const data = storageArea?.getItem(`${storageKey}_last_refresh`), parsed = data ? parseInt(data, 10) : 0;
|
|
@@ -389,6 +410,21 @@ function getCleanedUrl(locationUrl) {
|
|
|
389
410
|
}
|
|
390
411
|
return loc.searchParams.delete("sid"), loc.searchParams.delete("url"), loc.toString();
|
|
391
412
|
}
|
|
413
|
+
function getClientErrorApiBody(error) {
|
|
414
|
+
const body = error.response?.body;
|
|
415
|
+
return body && typeof body == "object" ? body : void 0;
|
|
416
|
+
}
|
|
417
|
+
function getClientErrorApiType(error) {
|
|
418
|
+
const body = getClientErrorApiBody(error);
|
|
419
|
+
return body?.error?.type ?? body?.type;
|
|
420
|
+
}
|
|
421
|
+
function getClientErrorApiDescription(error) {
|
|
422
|
+
const body = getClientErrorApiBody(error);
|
|
423
|
+
return body?.error?.description ?? body?.description;
|
|
424
|
+
}
|
|
425
|
+
function isProjectUserNotFoundClientError(error) {
|
|
426
|
+
return getClientErrorApiType(error) === "projectUserNotFoundError";
|
|
427
|
+
}
|
|
392
428
|
async function checkForCookieAuth(projectId, clientFactory) {
|
|
393
429
|
if (!projectId) return !1;
|
|
394
430
|
try {
|
|
@@ -584,13 +620,7 @@ const authStore = {
|
|
|
584
620
|
}) : currentAuthState.type !== AuthStateType.LOGGED_OUT && state.set("setToken", {
|
|
585
621
|
authState: { type: AuthStateType.LOGGED_OUT, isDestroyingSession: !1 }
|
|
586
622
|
});
|
|
587
|
-
})
|
|
588
|
-
function compareProjectOrganization(projectId, projectOrganizationId, currentDashboardOrgId) {
|
|
589
|
-
return projectOrganizationId !== currentDashboardOrgId ? {
|
|
590
|
-
error: `Project ${projectId} belongs to Organization ${projectOrganizationId ?? "unknown"}, but the Dashboard has Organization ${currentDashboardOrgId} selected`
|
|
591
|
-
} : { error: null };
|
|
592
|
-
}
|
|
593
|
-
const DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.sdk", allowedKeys = Object.keys({
|
|
623
|
+
}), DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.sdk", allowedKeys = Object.keys({
|
|
594
624
|
apiHost: null,
|
|
595
625
|
useCdn: null,
|
|
596
626
|
token: null,
|
|
@@ -605,7 +635,8 @@ const DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.s
|
|
|
605
635
|
apiVersion: null,
|
|
606
636
|
requestTagPrefix: null,
|
|
607
637
|
useProjectHostname: null,
|
|
608
|
-
"~experimental_resource": null
|
|
638
|
+
"~experimental_resource": null,
|
|
639
|
+
source: null
|
|
609
640
|
}), DEFAULT_CLIENT_CONFIG = {
|
|
610
641
|
apiVersion: DEFAULT_API_VERSION,
|
|
611
642
|
useCdn: !1,
|
|
@@ -631,6 +662,10 @@ const DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.s
|
|
|
631
662
|
}), getClientConfigKey = (options) => JSON.stringify(pick(options, ...allowedKeys)), getClient = bindActionGlobally(
|
|
632
663
|
clientStore,
|
|
633
664
|
({ state, instance }, options) => {
|
|
665
|
+
if (!options || typeof options != "object")
|
|
666
|
+
throw new Error(
|
|
667
|
+
'getClient() requires a configuration object with at least an "apiVersion" property. Example: getClient(instance, { apiVersion: "2024-11-12" })'
|
|
668
|
+
);
|
|
634
669
|
const disallowedKeys = Object.keys(options).filter((key2) => !allowedKeys.includes(key2));
|
|
635
670
|
if (disallowedKeys.length > 0) {
|
|
636
671
|
const listFormatter = new Intl.ListFormat("en", { style: "long", type: "conjunction" });
|
|
@@ -638,16 +673,22 @@ const DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.s
|
|
|
638
673
|
`The client options provided contains unsupported properties: ${listFormatter.format(disallowedKeys)}. Allowed keys are: ${listFormatter.format(allowedKeys)}.`
|
|
639
674
|
);
|
|
640
675
|
}
|
|
641
|
-
const tokenFromState = state.get().token, { clients, authMethod } = state.get(),
|
|
676
|
+
const tokenFromState = state.get().token, { clients, authMethod } = state.get(), hasSource = !!options.source;
|
|
677
|
+
let sourceId = options.source?.[SOURCE_ID], resource;
|
|
678
|
+
Array.isArray(sourceId) && (resource = { type: sourceId[0], id: sourceId[1] }, sourceId = void 0);
|
|
679
|
+
const projectId = options.projectId ?? instance.config.projectId, dataset = options.dataset ?? instance.config.dataset, apiHost = options.apiHost ?? instance.config.auth?.apiHost, effectiveOptions = {
|
|
642
680
|
...DEFAULT_CLIENT_CONFIG,
|
|
643
|
-
...(options.scope === "global" || !projectId) && { useProjectHostname: !1 },
|
|
681
|
+
...(options.scope === "global" || !projectId || hasSource) && { useProjectHostname: !1 },
|
|
644
682
|
token: authMethod === "cookie" ? void 0 : tokenFromState ?? void 0,
|
|
645
683
|
...options,
|
|
646
684
|
...projectId && { projectId },
|
|
647
685
|
...dataset && { dataset },
|
|
648
|
-
...apiHost && { apiHost }
|
|
686
|
+
...apiHost && { apiHost },
|
|
687
|
+
...resource && { "~experimental_resource": resource }
|
|
649
688
|
};
|
|
650
|
-
|
|
689
|
+
hasSource && ((options.projectId || options.dataset) && console.warn(
|
|
690
|
+
"Both source and explicit projectId/dataset are provided. The source will be used and projectId/dataset will be ignored."
|
|
691
|
+
), delete effectiveOptions.projectId, delete effectiveOptions.dataset), effectiveOptions.token === null || typeof effectiveOptions.token > "u" ? (delete effectiveOptions.token, authMethod === "cookie" && (effectiveOptions.withCredentials = !0)) : delete effectiveOptions.withCredentials;
|
|
651
692
|
const key = getClientConfigKey(effectiveOptions);
|
|
652
693
|
if (clients[key]) return clients[key];
|
|
653
694
|
const client = createClient(effectiveOptions);
|
|
@@ -656,7 +697,47 @@ const DEFAULT_API_VERSION = "2024-11-12", DEFAULT_REQUEST_TAG_PREFIX = "sanity.s
|
|
|
656
697
|
), getClientState = bindActionGlobally(
|
|
657
698
|
clientStore,
|
|
658
699
|
createStateSourceAction(({ instance }, options) => getClient(instance, options))
|
|
659
|
-
);
|
|
700
|
+
), API_VERSION$6 = "vX";
|
|
701
|
+
function agentGenerate(instance, options) {
|
|
702
|
+
return getClientState(instance, {
|
|
703
|
+
apiVersion: API_VERSION$6,
|
|
704
|
+
projectId: instance.config.projectId,
|
|
705
|
+
dataset: instance.config.dataset
|
|
706
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.generate(options)));
|
|
707
|
+
}
|
|
708
|
+
function agentTransform(instance, options) {
|
|
709
|
+
return getClientState(instance, {
|
|
710
|
+
apiVersion: API_VERSION$6,
|
|
711
|
+
projectId: instance.config.projectId,
|
|
712
|
+
dataset: instance.config.dataset
|
|
713
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.transform(options)));
|
|
714
|
+
}
|
|
715
|
+
function agentTranslate(instance, options) {
|
|
716
|
+
return getClientState(instance, {
|
|
717
|
+
apiVersion: API_VERSION$6,
|
|
718
|
+
projectId: instance.config.projectId,
|
|
719
|
+
dataset: instance.config.dataset
|
|
720
|
+
}).observable.pipe(switchMap((client) => client.observable.agent.action.translate(options)));
|
|
721
|
+
}
|
|
722
|
+
function agentPrompt(instance, options) {
|
|
723
|
+
return getClientState(instance, {
|
|
724
|
+
apiVersion: API_VERSION$6,
|
|
725
|
+
projectId: instance.config.projectId,
|
|
726
|
+
dataset: instance.config.dataset
|
|
727
|
+
}).observable.pipe(switchMap((client) => from(client.agent.action.prompt(options))));
|
|
728
|
+
}
|
|
729
|
+
function agentPatch(instance, options) {
|
|
730
|
+
return getClientState(instance, {
|
|
731
|
+
apiVersion: API_VERSION$6,
|
|
732
|
+
projectId: instance.config.projectId,
|
|
733
|
+
dataset: instance.config.dataset
|
|
734
|
+
}).observable.pipe(switchMap((client) => from(client.agent.action.patch(options))));
|
|
735
|
+
}
|
|
736
|
+
function compareProjectOrganization(projectId, projectOrganizationId, currentDashboardOrgId) {
|
|
737
|
+
return projectOrganizationId !== currentDashboardOrgId ? {
|
|
738
|
+
error: `Project ${projectId} belongs to Organization ${projectOrganizationId ?? "unknown"}, but the Dashboard has Organization ${currentDashboardOrgId} selected`
|
|
739
|
+
} : { error: null };
|
|
740
|
+
}
|
|
660
741
|
function createFetcherStore({
|
|
661
742
|
name,
|
|
662
743
|
fetcher: getObservable,
|
|
@@ -1078,18 +1159,21 @@ const API_VERSION$4 = "v2025-02-19", datasets = createFetcherStore({
|
|
|
1078
1159
|
useProjectHostname: !0
|
|
1079
1160
|
}).observable.pipe(switchMap((client) => client.observable.datasets.list()))
|
|
1080
1161
|
}), getDatasetsState = datasets.getState, resolveDatasets = datasets.resolveState, isSanityMutatePatch = (value) => !(typeof value != "object" || !value || !("type" in value) || typeof value.type != "string" || value.type !== "patch" || !("id" in value) || typeof value.id != "string" || !("patches" in value) || !Array.isArray(value.patches));
|
|
1081
|
-
function createDocument(doc) {
|
|
1162
|
+
function createDocument(doc, initialValue) {
|
|
1082
1163
|
return {
|
|
1083
1164
|
type: "document.create",
|
|
1084
1165
|
...doc,
|
|
1085
|
-
...doc.documentId && {
|
|
1166
|
+
...doc.documentId && {
|
|
1167
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1168
|
+
},
|
|
1169
|
+
...initialValue && { initialValue }
|
|
1086
1170
|
};
|
|
1087
1171
|
}
|
|
1088
1172
|
function deleteDocument(doc) {
|
|
1089
1173
|
return {
|
|
1090
1174
|
type: "document.delete",
|
|
1091
1175
|
...doc,
|
|
1092
|
-
documentId: getPublishedId(doc.documentId)
|
|
1176
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1093
1177
|
};
|
|
1094
1178
|
}
|
|
1095
1179
|
function convertSanityMutatePatch(sanityPatchMutation) {
|
|
@@ -1099,19 +1183,20 @@ function convertSanityMutatePatch(sanityPatchMutation) {
|
|
|
1099
1183
|
});
|
|
1100
1184
|
}
|
|
1101
1185
|
function editDocument(doc, patches) {
|
|
1186
|
+
const documentId = doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId);
|
|
1102
1187
|
if (isSanityMutatePatch(patches)) {
|
|
1103
1188
|
const converted = convertSanityMutatePatch(patches) ?? [];
|
|
1104
1189
|
return {
|
|
1105
1190
|
...doc,
|
|
1106
1191
|
type: "document.edit",
|
|
1107
|
-
documentId
|
|
1192
|
+
documentId,
|
|
1108
1193
|
patches: converted
|
|
1109
1194
|
};
|
|
1110
1195
|
}
|
|
1111
1196
|
return {
|
|
1112
1197
|
...doc,
|
|
1113
1198
|
type: "document.edit",
|
|
1114
|
-
documentId
|
|
1199
|
+
documentId,
|
|
1115
1200
|
...patches && { patches: Array.isArray(patches) ? patches : [patches] }
|
|
1116
1201
|
};
|
|
1117
1202
|
}
|
|
@@ -1119,21 +1204,21 @@ function publishDocument(doc) {
|
|
|
1119
1204
|
return {
|
|
1120
1205
|
type: "document.publish",
|
|
1121
1206
|
...doc,
|
|
1122
|
-
documentId: getPublishedId(doc.documentId)
|
|
1207
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1123
1208
|
};
|
|
1124
1209
|
}
|
|
1125
1210
|
function unpublishDocument(doc) {
|
|
1126
1211
|
return {
|
|
1127
1212
|
type: "document.unpublish",
|
|
1128
1213
|
...doc,
|
|
1129
|
-
documentId: getPublishedId(doc.documentId)
|
|
1214
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1130
1215
|
};
|
|
1131
1216
|
}
|
|
1132
1217
|
function discardDocument(doc) {
|
|
1133
1218
|
return {
|
|
1134
1219
|
type: "document.discard",
|
|
1135
1220
|
...doc,
|
|
1136
|
-
documentId: getPublishedId(doc.documentId)
|
|
1221
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1137
1222
|
};
|
|
1138
1223
|
}
|
|
1139
1224
|
const DOCUMENT_STATE_CLEAR_DELAY = 1e3, INITIAL_OUTGOING_THROTTLE_TIME = 1e3, API_VERSION$3 = "v2025-05-06";
|
|
@@ -1143,13 +1228,13 @@ function generateArrayKey(length = 12) {
|
|
|
1143
1228
|
}
|
|
1144
1229
|
function memoize(fn) {
|
|
1145
1230
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
1146
|
-
return (input) => {
|
|
1231
|
+
return ((input) => {
|
|
1147
1232
|
if (!input || typeof input != "object") return fn(input);
|
|
1148
1233
|
const cached = cache.get(input);
|
|
1149
1234
|
if (cached) return cached;
|
|
1150
1235
|
const result = fn(input);
|
|
1151
1236
|
return cache.set(input, result), result;
|
|
1152
|
-
};
|
|
1237
|
+
});
|
|
1153
1238
|
}
|
|
1154
1239
|
const ensureArrayKeysDeep = memoize((input) => {
|
|
1155
1240
|
if (!input || typeof input != "object") return input;
|
|
@@ -1662,11 +1747,11 @@ function createGrantsLookup(datasetAcl) {
|
|
|
1662
1747
|
const documentsCache = new MultiKeyWeakMap(), actionsCache = /* @__PURE__ */ new WeakMap(), nullReplacer = {}, documentsSelector = createSelector(
|
|
1663
1748
|
[
|
|
1664
1749
|
({ state: { documentStates } }) => documentStates,
|
|
1665
|
-
(_context, actions) => actions
|
|
1750
|
+
(_context, { actions }) => actions
|
|
1666
1751
|
],
|
|
1667
1752
|
(documentStates, actions) => {
|
|
1668
1753
|
const documentIds = new Set(
|
|
1669
|
-
|
|
1754
|
+
actions.map((action) => typeof action.documentId != "string" ? [] : action.liveEdit ? [action.documentId] : [getPublishedId(action.documentId), getDraftId(action.documentId)]).flat()
|
|
1670
1755
|
), documents = {};
|
|
1671
1756
|
for (const documentId of documentIds) {
|
|
1672
1757
|
const local = documentStates[documentId]?.local;
|
|
@@ -1683,7 +1768,7 @@ const documentsCache = new MultiKeyWeakMap(), actionsCache = /* @__PURE__ */ new
|
|
|
1683
1768
|
), memoizedActionsSelector = createSelector(
|
|
1684
1769
|
[
|
|
1685
1770
|
documentsSelector,
|
|
1686
|
-
(_state, actions) => actions
|
|
1771
|
+
(_state, { actions }) => actions
|
|
1687
1772
|
],
|
|
1688
1773
|
(documents, actions) => {
|
|
1689
1774
|
if (!documents) return;
|
|
@@ -1737,7 +1822,7 @@ const _calculatePermissions = createSelector(
|
|
|
1737
1822
|
}
|
|
1738
1823
|
for (const action of actions)
|
|
1739
1824
|
if (action.type === "document.edit" && !action.patches?.length) {
|
|
1740
|
-
const docId = action.documentId, doc = documents[getDraftId(docId)] ?? documents[getPublishedId(docId)];
|
|
1825
|
+
const docId = action.documentId, doc = action.liveEdit ? documents[docId] : documents[getDraftId(docId)] ?? documents[getPublishedId(docId)];
|
|
1741
1826
|
doc ? checkGrant$1(grants.update, doc) || reasons.push({
|
|
1742
1827
|
type: "access",
|
|
1743
1828
|
message: `You are not allowed to edit the document with ID "${docId}".`,
|
|
@@ -1784,14 +1869,56 @@ function processActions({
|
|
|
1784
1869
|
for (const action of actions)
|
|
1785
1870
|
switch (action.type) {
|
|
1786
1871
|
case "document.create": {
|
|
1787
|
-
const documentId = getId(action.documentId)
|
|
1872
|
+
const documentId = getId(action.documentId);
|
|
1873
|
+
if (action.liveEdit) {
|
|
1874
|
+
if (working[documentId])
|
|
1875
|
+
throw new ActionError({
|
|
1876
|
+
documentId,
|
|
1877
|
+
transactionId,
|
|
1878
|
+
message: "This document already exists."
|
|
1879
|
+
});
|
|
1880
|
+
const newDocBase2 = { _type: action.documentType, _id: documentId }, newDocWorking2 = { _type: action.documentType, _id: documentId }, mutations2 = [{ create: newDocWorking2 }];
|
|
1881
|
+
if (base = processMutations({
|
|
1882
|
+
documents: base,
|
|
1883
|
+
transactionId,
|
|
1884
|
+
mutations: [{ create: newDocBase2 }],
|
|
1885
|
+
timestamp
|
|
1886
|
+
}), working = processMutations({
|
|
1887
|
+
documents: working,
|
|
1888
|
+
transactionId,
|
|
1889
|
+
mutations: mutations2,
|
|
1890
|
+
timestamp
|
|
1891
|
+
}), !checkGrant(grants.create, working[documentId]))
|
|
1892
|
+
throw new PermissionActionError({
|
|
1893
|
+
documentId,
|
|
1894
|
+
transactionId,
|
|
1895
|
+
message: `You do not have permission to create document "${documentId}".`
|
|
1896
|
+
});
|
|
1897
|
+
outgoingMutations.push(...mutations2), outgoingActions.push({
|
|
1898
|
+
actionType: "sanity.action.document.create",
|
|
1899
|
+
publishedId: documentId,
|
|
1900
|
+
attributes: newDocWorking2
|
|
1901
|
+
});
|
|
1902
|
+
continue;
|
|
1903
|
+
}
|
|
1904
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
1788
1905
|
if (working[draftId])
|
|
1789
1906
|
throw new ActionError({
|
|
1790
1907
|
documentId,
|
|
1791
1908
|
transactionId,
|
|
1792
1909
|
message: "A draft version of this document already exists. Please use or discard the existing draft before creating a new one."
|
|
1793
1910
|
});
|
|
1794
|
-
const newDocBase = {
|
|
1911
|
+
const newDocBase = {
|
|
1912
|
+
...base[publishedId],
|
|
1913
|
+
_type: action.documentType,
|
|
1914
|
+
_id: draftId,
|
|
1915
|
+
...action.initialValue
|
|
1916
|
+
}, newDocWorking = {
|
|
1917
|
+
...working[publishedId],
|
|
1918
|
+
_type: action.documentType,
|
|
1919
|
+
_id: draftId,
|
|
1920
|
+
...action.initialValue
|
|
1921
|
+
}, mutations = [{ create: newDocWorking }];
|
|
1795
1922
|
if (base = processMutations({
|
|
1796
1923
|
documents: base,
|
|
1797
1924
|
transactionId,
|
|
@@ -1816,7 +1943,28 @@ function processActions({
|
|
|
1816
1943
|
continue;
|
|
1817
1944
|
}
|
|
1818
1945
|
case "document.delete": {
|
|
1819
|
-
const documentId = action.documentId
|
|
1946
|
+
const documentId = action.documentId;
|
|
1947
|
+
if (action.liveEdit) {
|
|
1948
|
+
if (!working[documentId])
|
|
1949
|
+
throw new ActionError({
|
|
1950
|
+
documentId,
|
|
1951
|
+
transactionId,
|
|
1952
|
+
message: "The document you are trying to delete does not exist."
|
|
1953
|
+
});
|
|
1954
|
+
if (!checkGrant(grants.update, working[documentId]))
|
|
1955
|
+
throw new PermissionActionError({
|
|
1956
|
+
documentId,
|
|
1957
|
+
transactionId,
|
|
1958
|
+
message: "You do not have permission to delete this document."
|
|
1959
|
+
});
|
|
1960
|
+
const mutations2 = [{ delete: { id: documentId } }];
|
|
1961
|
+
base = processMutations({ documents: base, transactionId, mutations: mutations2, timestamp }), working = processMutations({ documents: working, transactionId, mutations: mutations2, timestamp }), outgoingMutations.push(...mutations2), outgoingActions.push({
|
|
1962
|
+
actionType: "sanity.action.document.delete",
|
|
1963
|
+
publishedId: documentId
|
|
1964
|
+
});
|
|
1965
|
+
continue;
|
|
1966
|
+
}
|
|
1967
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
1820
1968
|
if (!working[publishedId])
|
|
1821
1969
|
throw new ActionError({
|
|
1822
1970
|
documentId,
|
|
@@ -1839,7 +1987,14 @@ function processActions({
|
|
|
1839
1987
|
continue;
|
|
1840
1988
|
}
|
|
1841
1989
|
case "document.discard": {
|
|
1842
|
-
const documentId = getId(action.documentId)
|
|
1990
|
+
const documentId = getId(action.documentId);
|
|
1991
|
+
if (action.liveEdit)
|
|
1992
|
+
throw new ActionError({
|
|
1993
|
+
documentId,
|
|
1994
|
+
transactionId,
|
|
1995
|
+
message: `Cannot discard changes for liveEdit document "${documentId}". LiveEdit documents do not support drafts.`
|
|
1996
|
+
});
|
|
1997
|
+
const draftId = getDraftId(documentId), mutations = [{ delete: { id: draftId } }];
|
|
1843
1998
|
if (!working[draftId])
|
|
1844
1999
|
throw new ActionError({
|
|
1845
2000
|
documentId,
|
|
@@ -1859,7 +2014,50 @@ function processActions({
|
|
|
1859
2014
|
continue;
|
|
1860
2015
|
}
|
|
1861
2016
|
case "document.edit": {
|
|
1862
|
-
const documentId = getId(action.documentId)
|
|
2017
|
+
const documentId = getId(action.documentId);
|
|
2018
|
+
if (action.liveEdit) {
|
|
2019
|
+
const userPatches2 = action.patches?.map((patch) => ({ patch: { id: documentId, ...patch } }));
|
|
2020
|
+
if (!userPatches2?.length) continue;
|
|
2021
|
+
if (!working[documentId] || !base[documentId])
|
|
2022
|
+
throw new ActionError({
|
|
2023
|
+
documentId,
|
|
2024
|
+
transactionId,
|
|
2025
|
+
message: "Cannot edit document because it does not exist."
|
|
2026
|
+
});
|
|
2027
|
+
const baseBefore2 = base[documentId];
|
|
2028
|
+
userPatches2 && (base = processMutations({
|
|
2029
|
+
documents: base,
|
|
2030
|
+
transactionId,
|
|
2031
|
+
mutations: userPatches2,
|
|
2032
|
+
timestamp
|
|
2033
|
+
}));
|
|
2034
|
+
const baseAfter2 = base[documentId], patches2 = diffValue(baseBefore2, baseAfter2), workingBefore2 = working[documentId];
|
|
2035
|
+
if (!checkGrant(grants.update, workingBefore2))
|
|
2036
|
+
throw new PermissionActionError({
|
|
2037
|
+
documentId,
|
|
2038
|
+
transactionId,
|
|
2039
|
+
message: `You do not have permission to edit document "${documentId}".`
|
|
2040
|
+
});
|
|
2041
|
+
const workingMutations2 = patches2.map((patch) => ({ patch: { id: documentId, ...patch } }));
|
|
2042
|
+
working = processMutations({
|
|
2043
|
+
documents: working,
|
|
2044
|
+
transactionId,
|
|
2045
|
+
mutations: workingMutations2,
|
|
2046
|
+
timestamp
|
|
2047
|
+
}), outgoingMutations.push(...workingMutations2), outgoingActions.push(
|
|
2048
|
+
...patches2.map(
|
|
2049
|
+
(patch) => ({
|
|
2050
|
+
actionType: "sanity.action.document.edit",
|
|
2051
|
+
// Server requires draftId to have drafts. prefix for validation, even for liveEdit
|
|
2052
|
+
draftId: getDraftId(documentId),
|
|
2053
|
+
publishedId: documentId,
|
|
2054
|
+
patch
|
|
2055
|
+
})
|
|
2056
|
+
)
|
|
2057
|
+
);
|
|
2058
|
+
continue;
|
|
2059
|
+
}
|
|
2060
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId), userPatches = action.patches?.map((patch) => ({ patch: { id: draftId, ...patch } }));
|
|
1863
2061
|
if (!userPatches?.length) continue;
|
|
1864
2062
|
if (!working[draftId] && !working[publishedId] || !base[draftId] && !base[publishedId])
|
|
1865
2063
|
throw new ActionError({
|
|
@@ -1912,7 +2110,14 @@ function processActions({
|
|
|
1912
2110
|
continue;
|
|
1913
2111
|
}
|
|
1914
2112
|
case "document.publish": {
|
|
1915
|
-
const documentId = getId(action.documentId)
|
|
2113
|
+
const documentId = getId(action.documentId);
|
|
2114
|
+
if (action.liveEdit)
|
|
2115
|
+
throw new ActionError({
|
|
2116
|
+
documentId,
|
|
2117
|
+
transactionId,
|
|
2118
|
+
message: `Cannot publish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`
|
|
2119
|
+
});
|
|
2120
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId), workingDraft = working[draftId], baseDraft = base[draftId];
|
|
1916
2121
|
if (!workingDraft || !baseDraft)
|
|
1917
2122
|
throw new ActionError({
|
|
1918
2123
|
documentId,
|
|
@@ -1955,7 +2160,14 @@ function processActions({
|
|
|
1955
2160
|
continue;
|
|
1956
2161
|
}
|
|
1957
2162
|
case "document.unpublish": {
|
|
1958
|
-
const documentId = getId(action.documentId)
|
|
2163
|
+
const documentId = getId(action.documentId);
|
|
2164
|
+
if (action.liveEdit)
|
|
2165
|
+
throw new ActionError({
|
|
2166
|
+
documentId,
|
|
2167
|
+
transactionId,
|
|
2168
|
+
message: `Cannot unpublish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`
|
|
2169
|
+
});
|
|
2170
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
1959
2171
|
if (!working[publishedId] && !base[publishedId])
|
|
1960
2172
|
throw new ActionError({
|
|
1961
2173
|
documentId,
|
|
@@ -2277,13 +2489,13 @@ function removeSubscriptionIdFromDocument(prev, documentId, subscriptionId) {
|
|
|
2277
2489
|
}
|
|
2278
2490
|
} : { ...prev, documentStates: omit(prev.documentStates, documentId) } : prev;
|
|
2279
2491
|
}
|
|
2280
|
-
function manageSubscriberIds({ state }, documentId) {
|
|
2281
|
-
const documentIds = Array.from(
|
|
2492
|
+
function manageSubscriberIds({ state }, documentId, options) {
|
|
2493
|
+
const expandDraftPublished = options?.expandDraftPublished ?? !0, documentIds = Array.from(
|
|
2282
2494
|
new Set(
|
|
2283
|
-
(Array.isArray(documentId) ? documentId : [documentId]).flatMap((id) => [
|
|
2495
|
+
expandDraftPublished ? (Array.isArray(documentId) ? documentId : [documentId]).flatMap((id) => [
|
|
2284
2496
|
getPublishedId$1(id),
|
|
2285
2497
|
getDraftId(id)
|
|
2286
|
-
])
|
|
2498
|
+
]) : Array.isArray(documentId) ? documentId : [documentId]
|
|
2287
2499
|
)
|
|
2288
2500
|
), subscriptionId = insecureRandomId();
|
|
2289
2501
|
return state.set(
|
|
@@ -2304,8 +2516,7 @@ function manageSubscriberIds({ state }, documentId) {
|
|
|
2304
2516
|
}, DOCUMENT_STATE_CLEAR_DELAY);
|
|
2305
2517
|
};
|
|
2306
2518
|
}
|
|
2307
|
-
function getDocumentIdsFromActions(
|
|
2308
|
-
const actions = Array.isArray(action) ? action : [action];
|
|
2519
|
+
function getDocumentIdsFromActions(actions) {
|
|
2309
2520
|
return Array.from(
|
|
2310
2521
|
new Set(
|
|
2311
2522
|
actions.map((i) => i.documentId).filter((i) => typeof i == "string").flatMap((documentId) => [getPublishedId$1(documentId), getDraftId(documentId)])
|
|
@@ -2417,8 +2628,17 @@ const _getDocumentState = bindActionByDataset(
|
|
|
2417
2628
|
documentStore,
|
|
2418
2629
|
createStateSourceAction({
|
|
2419
2630
|
selector: ({ state: { error, documentStates } }, options) => {
|
|
2420
|
-
const { documentId, path } = options;
|
|
2631
|
+
const { documentId, path, liveEdit } = options;
|
|
2421
2632
|
if (error) throw error;
|
|
2633
|
+
if (liveEdit) {
|
|
2634
|
+
const document22 = documentStates[documentId]?.local;
|
|
2635
|
+
if (document22 === void 0) return;
|
|
2636
|
+
if (!path) return document22;
|
|
2637
|
+
const result2 = jsonMatch(document22, path).next();
|
|
2638
|
+
if (result2.done) return;
|
|
2639
|
+
const { value: value2 } = result2.value;
|
|
2640
|
+
return value2;
|
|
2641
|
+
}
|
|
2422
2642
|
const draftId = getDraftId(documentId), publishedId = getPublishedId$1(documentId), draft = documentStates[draftId]?.local, published = documentStates[publishedId]?.local;
|
|
2423
2643
|
if (draft === void 0 || published === void 0) return;
|
|
2424
2644
|
const document2 = draft ?? published;
|
|
@@ -2428,7 +2648,7 @@ const _getDocumentState = bindActionByDataset(
|
|
|
2428
2648
|
const { value } = result.value;
|
|
2429
2649
|
return value;
|
|
2430
2650
|
},
|
|
2431
|
-
onSubscribe: (context, options) => manageSubscriberIds(context, options.documentId)
|
|
2651
|
+
onSubscribe: (context, options) => manageSubscriberIds(context, options.documentId, { expandDraftPublished: !options.liveEdit })
|
|
2432
2652
|
})
|
|
2433
2653
|
);
|
|
2434
2654
|
function resolveDocument(...args) {
|
|
@@ -2448,6 +2668,8 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2448
2668
|
selector: ({ state: { error, documentStates: documents, outgoing, applied, queued } }, doc) => {
|
|
2449
2669
|
const documentId = typeof doc == "string" ? doc : doc.documentId;
|
|
2450
2670
|
if (error) throw error;
|
|
2671
|
+
if (doc.liveEdit)
|
|
2672
|
+
return documents[documentId] === void 0 ? void 0 : !queued.length && !applied.length && !outgoing;
|
|
2451
2673
|
const draftId = getDraftId(documentId), publishedId = getPublishedId$1(documentId), draft = documents[draftId], published = documents[publishedId];
|
|
2452
2674
|
if (!(draft === void 0 || published === void 0))
|
|
2453
2675
|
return !queued.length && !applied.length && !outgoing;
|
|
@@ -2458,12 +2680,12 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2458
2680
|
documentStore,
|
|
2459
2681
|
createStateSourceAction({
|
|
2460
2682
|
selector: calculatePermissions,
|
|
2461
|
-
onSubscribe: (context, actions) => manageSubscriberIds(context, getDocumentIdsFromActions(actions))
|
|
2683
|
+
onSubscribe: (context, { actions }) => manageSubscriberIds(context, getDocumentIdsFromActions(actions))
|
|
2462
2684
|
})
|
|
2463
2685
|
), resolvePermissions = bindActionByDataset(
|
|
2464
2686
|
documentStore,
|
|
2465
|
-
({ instance },
|
|
2466
|
-
getPermissionsState(instance,
|
|
2687
|
+
({ instance }, options) => firstValueFrom(
|
|
2688
|
+
getPermissionsState(instance, options).observable.pipe(filter((i) => i !== void 0))
|
|
2467
2689
|
)
|
|
2468
2690
|
), subscribeDocumentEvents = bindActionByDataset(
|
|
2469
2691
|
documentStore,
|
|
@@ -2569,51 +2791,25 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2569
2791
|
).subscribe({ error: (error) => state.set("setError", { error }) });
|
|
2570
2792
|
}, subscribeToClientAndFetchDatasetAcl = ({
|
|
2571
2793
|
instance,
|
|
2572
|
-
state
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
).
|
|
2585
|
-
|
|
2586
|
-
});
|
|
2587
|
-
};
|
|
2794
|
+
state,
|
|
2795
|
+
key: { projectId, dataset }
|
|
2796
|
+
}) => getClientState(instance, { apiVersion: API_VERSION$3 }).observable.pipe(
|
|
2797
|
+
switchMap(
|
|
2798
|
+
(client) => client.observable.request({
|
|
2799
|
+
uri: `/projects/${projectId}/datasets/${dataset}/acl`,
|
|
2800
|
+
tag: "acl.get",
|
|
2801
|
+
withCredentials: !0
|
|
2802
|
+
})
|
|
2803
|
+
),
|
|
2804
|
+
tap$1((datasetAcl) => state.set("setGrants", { grants: createGrantsLookup(datasetAcl) }))
|
|
2805
|
+
).subscribe({
|
|
2806
|
+
error: (error) => state.set("setError", { error })
|
|
2807
|
+
});
|
|
2588
2808
|
function applyDocumentActions(...args) {
|
|
2589
2809
|
return boundApplyDocumentActions(...args);
|
|
2590
2810
|
}
|
|
2591
2811
|
const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocumentActions);
|
|
2592
|
-
async function _applyDocumentActions({
|
|
2593
|
-
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions];
|
|
2594
|
-
let projectId, dataset;
|
|
2595
|
-
for (const action of actions)
|
|
2596
|
-
if (action.projectId) {
|
|
2597
|
-
if (projectId || (projectId = action.projectId), action.projectId !== projectId)
|
|
2598
|
-
throw new Error(
|
|
2599
|
-
`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`
|
|
2600
|
-
);
|
|
2601
|
-
if (action.dataset && (dataset || (dataset = action.dataset), action.dataset !== dataset))
|
|
2602
|
-
throw new Error(
|
|
2603
|
-
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`
|
|
2604
|
-
);
|
|
2605
|
-
}
|
|
2606
|
-
if (projectId && projectId !== instance.config.projectId || dataset && dataset !== instance.config.dataset) {
|
|
2607
|
-
const matchedInstance = instance.match({ projectId, dataset });
|
|
2608
|
-
if (!matchedInstance)
|
|
2609
|
-
throw new Error(
|
|
2610
|
-
`Could not find a matching instance for projectId: "${projectId}" and dataset: "${dataset}"`
|
|
2611
|
-
);
|
|
2612
|
-
return boundApplyDocumentActions(matchedInstance, actionOrActions, {
|
|
2613
|
-
disableBatching,
|
|
2614
|
-
transactionId
|
|
2615
|
-
});
|
|
2616
|
-
}
|
|
2812
|
+
async function _applyDocumentActions({ state }, { actions, transactionId = crypto.randomUUID(), disableBatching }) {
|
|
2617
2813
|
const { events } = state.get(), transaction = {
|
|
2618
2814
|
transactionId,
|
|
2619
2815
|
actions,
|
|
@@ -2981,8 +3177,14 @@ const handleIncomingMessage = (event) => {
|
|
|
2981
3177
|
name: "presence",
|
|
2982
3178
|
getInitialState,
|
|
2983
3179
|
initialize: (context) => {
|
|
2984
|
-
const {
|
|
2985
|
-
|
|
3180
|
+
const {
|
|
3181
|
+
instance,
|
|
3182
|
+
state,
|
|
3183
|
+
key: { projectId, dataset }
|
|
3184
|
+
} = context, sessionId = crypto.randomUUID(), client = getClient(instance, {
|
|
3185
|
+
apiVersion: "2022-06-30",
|
|
3186
|
+
projectId,
|
|
3187
|
+
dataset
|
|
2986
3188
|
}), token$ = getTokenState(instance).observable.pipe(distinctUntilChanged()), [incomingEvents$, dispatch] = createBifurTransport({
|
|
2987
3189
|
client,
|
|
2988
3190
|
token$,
|
|
@@ -3024,8 +3226,8 @@ const handleIncomingMessage = (event) => {
|
|
|
3024
3226
|
), getPresence = bindActionByDataset(
|
|
3025
3227
|
presenceStore,
|
|
3026
3228
|
createStateSourceAction({
|
|
3027
|
-
selector: (context) => selectPresence(context.state),
|
|
3028
|
-
onSubscribe: (context) => {
|
|
3229
|
+
selector: (context, _) => selectPresence(context.state),
|
|
3230
|
+
onSubscribe: (context, _) => {
|
|
3029
3231
|
const subscription = context.state.observable.pipe(
|
|
3030
3232
|
map(
|
|
3031
3233
|
(state) => Array.from(state.locations.values()).map((l) => l.userId).filter((id) => !!id)
|
|
@@ -3039,7 +3241,7 @@ const handleIncomingMessage = (event) => {
|
|
|
3039
3241
|
(userId) => getUserState(context.instance, {
|
|
3040
3242
|
userId,
|
|
3041
3243
|
resourceType: "project",
|
|
3042
|
-
projectId: context.
|
|
3244
|
+
projectId: context.key.projectId
|
|
3043
3245
|
}).pipe(filter((v) => !!v))
|
|
3044
3246
|
);
|
|
3045
3247
|
return combineLatest(userObservables);
|
|
@@ -3120,11 +3322,17 @@ const ARCHIVED_RELEASE_STATES = ["archived", "published"], releasesStore = {
|
|
|
3120
3322
|
}, getActiveReleasesState = bindActionByDataset(
|
|
3121
3323
|
releasesStore,
|
|
3122
3324
|
createStateSourceAction({
|
|
3123
|
-
selector: ({ state }) => state.activeReleases
|
|
3325
|
+
selector: ({ state }, _) => state.activeReleases
|
|
3124
3326
|
})
|
|
3125
|
-
), RELEASES_QUERY = "releases::all()", QUERY_PARAMS = {}, subscribeToReleases = ({
|
|
3327
|
+
), RELEASES_QUERY = "releases::all()", QUERY_PARAMS = {}, subscribeToReleases = ({
|
|
3328
|
+
instance,
|
|
3329
|
+
state,
|
|
3330
|
+
key: { projectId, dataset }
|
|
3331
|
+
}) => getClientState(instance, {
|
|
3126
3332
|
apiVersion: "2025-04-10",
|
|
3127
|
-
perspective: "raw"
|
|
3333
|
+
perspective: "raw",
|
|
3334
|
+
projectId,
|
|
3335
|
+
dataset
|
|
3128
3336
|
}).observable.pipe(
|
|
3129
3337
|
switchMap(
|
|
3130
3338
|
(client) => (
|
|
@@ -3152,7 +3360,7 @@ const ARCHIVED_RELEASE_STATES = ["archived", "published"], releasesStore = {
|
|
|
3152
3360
|
function isReleasePerspective(perspective) {
|
|
3153
3361
|
return typeof perspective == "object" && perspective !== null && "releaseName" in perspective;
|
|
3154
3362
|
}
|
|
3155
|
-
const DEFAULT_PERSPECTIVE = "drafts", optionsCache = /* @__PURE__ */ new Map(), selectInstancePerspective = (context) => context.instance.config.perspective, selectActiveReleases = (context) => context.state.activeReleases, selectOptions = (_context, options) => options, memoizedOptionsSelector = createSelector(
|
|
3363
|
+
const DEFAULT_PERSPECTIVE = "drafts", optionsCache = /* @__PURE__ */ new Map(), selectInstancePerspective = (context, _) => context.instance.config.perspective, selectActiveReleases = (context) => context.state.activeReleases, selectOptions = (_context, options) => options, memoizedOptionsSelector = createSelector(
|
|
3156
3364
|
[selectActiveReleases, selectOptions],
|
|
3157
3365
|
(activeReleases, options) => {
|
|
3158
3366
|
if (!options || !activeReleases) return options;
|
|
@@ -3252,6 +3460,7 @@ const queryStore = {
|
|
|
3252
3460
|
projectId,
|
|
3253
3461
|
dataset,
|
|
3254
3462
|
tag,
|
|
3463
|
+
source,
|
|
3255
3464
|
perspective: perspectiveFromOptions,
|
|
3256
3465
|
...restOptions
|
|
3257
3466
|
} = parseQueryKey(group$.key), perspective$ = getPerspectiveState(instance, {
|
|
@@ -3259,7 +3468,8 @@ const queryStore = {
|
|
|
3259
3468
|
}).observable.pipe(filter(Boolean)), client$ = getClientState(instance, {
|
|
3260
3469
|
apiVersion: QUERY_STORE_API_VERSION,
|
|
3261
3470
|
projectId,
|
|
3262
|
-
dataset
|
|
3471
|
+
dataset,
|
|
3472
|
+
source
|
|
3263
3473
|
}).observable;
|
|
3264
3474
|
return combineLatest([lastLiveEventId$, client$, perspective$]).pipe(
|
|
3265
3475
|
switchMap(
|
|
@@ -3322,7 +3532,7 @@ const queryStore = {
|
|
|
3322
3532
|
function getQueryState(...args) {
|
|
3323
3533
|
return _getQueryState(...args);
|
|
3324
3534
|
}
|
|
3325
|
-
const _getQueryState =
|
|
3535
|
+
const _getQueryState = bindActionBySource(
|
|
3326
3536
|
queryStore,
|
|
3327
3537
|
createStateSourceAction({
|
|
3328
3538
|
selector: ({ state, instance }, options) => {
|
|
@@ -3345,7 +3555,7 @@ const _getQueryState = bindActionByDataset(
|
|
|
3345
3555
|
function resolveQuery(...args) {
|
|
3346
3556
|
return _resolveQuery(...args);
|
|
3347
3557
|
}
|
|
3348
|
-
const _resolveQuery =
|
|
3558
|
+
const _resolveQuery = bindActionBySource(
|
|
3349
3559
|
queryStore,
|
|
3350
3560
|
({ state, instance }, { signal, ...options }) => {
|
|
3351
3561
|
const normalized = normalizeOptionsWithPerspective(instance, options), { getCurrent } = getQueryState(instance, normalized), key = getQueryKey(normalized), aborted$ = signal ? new Observable((observer) => {
|
|
@@ -3470,7 +3680,8 @@ function createPreviewQuery(documentIds) {
|
|
|
3470
3680
|
}
|
|
3471
3681
|
const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size && Array.from(a).every((i) => b.has(i)), subscribeToStateAndFetchBatches$1 = ({
|
|
3472
3682
|
state,
|
|
3473
|
-
instance
|
|
3683
|
+
instance,
|
|
3684
|
+
key: { projectId, dataset }
|
|
3474
3685
|
}) => state.observable.pipe(
|
|
3475
3686
|
map(({ subscriptions }) => new Set(Object.keys(subscriptions))),
|
|
3476
3687
|
distinctUntilChanged(isSetEqual$1),
|
|
@@ -3498,14 +3709,18 @@ const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size &&
|
|
|
3498
3709
|
query,
|
|
3499
3710
|
params,
|
|
3500
3711
|
tag: PREVIEW_TAG,
|
|
3501
|
-
perspective: PREVIEW_PERSPECTIVE
|
|
3712
|
+
perspective: PREVIEW_PERSPECTIVE,
|
|
3713
|
+
projectId,
|
|
3714
|
+
dataset
|
|
3502
3715
|
}), subscription = defer(() => getCurrent() === void 0 ? from(
|
|
3503
3716
|
resolveQuery(instance, {
|
|
3504
3717
|
query,
|
|
3505
3718
|
params,
|
|
3506
3719
|
tag: PREVIEW_TAG,
|
|
3507
3720
|
perspective: PREVIEW_PERSPECTIVE,
|
|
3508
|
-
signal: controller.signal
|
|
3721
|
+
signal: controller.signal,
|
|
3722
|
+
projectId,
|
|
3723
|
+
dataset
|
|
3509
3724
|
})
|
|
3510
3725
|
).pipe(switchMap(() => observable)) : observable).pipe(filter((result) => result !== void 0)).subscribe(observer);
|
|
3511
3726
|
return () => {
|
|
@@ -3515,8 +3730,8 @@ const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size &&
|
|
|
3515
3730
|
}),
|
|
3516
3731
|
map(({ ids, data }) => ({
|
|
3517
3732
|
values: processPreviewQuery({
|
|
3518
|
-
projectId
|
|
3519
|
-
dataset
|
|
3733
|
+
projectId,
|
|
3734
|
+
dataset,
|
|
3520
3735
|
ids,
|
|
3521
3736
|
results: data
|
|
3522
3737
|
})
|
|
@@ -3633,7 +3848,8 @@ function processProjectionQuery({ ids, results }) {
|
|
|
3633
3848
|
}
|
|
3634
3849
|
const BATCH_DEBOUNCE_TIME = 50, isSetEqual = (a, b) => a.size === b.size && Array.from(a).every((i) => b.has(i)), subscribeToStateAndFetchBatches = ({
|
|
3635
3850
|
state,
|
|
3636
|
-
instance
|
|
3851
|
+
instance,
|
|
3852
|
+
key: { projectId, dataset }
|
|
3637
3853
|
}) => {
|
|
3638
3854
|
const documentProjections$ = state.observable.pipe(
|
|
3639
3855
|
map((s) => s.documentProjections),
|
|
@@ -3676,12 +3892,16 @@ const BATCH_DEBOUNCE_TIME = 50, isSetEqual = (a, b) => a.size === b.size && Arra
|
|
|
3676
3892
|
const { getCurrent, observable } = getQueryState(instance, {
|
|
3677
3893
|
query,
|
|
3678
3894
|
params,
|
|
3895
|
+
projectId,
|
|
3896
|
+
dataset,
|
|
3679
3897
|
tag: PROJECTION_TAG,
|
|
3680
3898
|
perspective: PROJECTION_PERSPECTIVE
|
|
3681
3899
|
}), subscription = defer(() => getCurrent() === void 0 ? from(
|
|
3682
3900
|
resolveQuery(instance, {
|
|
3683
3901
|
query,
|
|
3684
3902
|
params,
|
|
3903
|
+
projectId,
|
|
3904
|
+
dataset,
|
|
3685
3905
|
tag: PROJECTION_TAG,
|
|
3686
3906
|
perspective: PROJECTION_PERSPECTIVE,
|
|
3687
3907
|
signal: controller.signal
|
|
@@ -3821,12 +4041,12 @@ const _resolveProjection = bindActionByDataset(
|
|
|
3821
4041
|
});
|
|
3822
4042
|
})
|
|
3823
4043
|
)
|
|
3824
|
-
}), getProjectsState = projects.getState, resolveProjects = projects.resolveState,
|
|
4044
|
+
}), getProjectsState = projects.getState, resolveProjects = projects.resolveState, TOKEN_REGEX = /(?:[^\s"]+|"[^"]*")+/g;
|
|
3825
4045
|
function isNegationToken(token) {
|
|
3826
|
-
return typeof token < "u" && token.trim().startsWith(
|
|
4046
|
+
return typeof token < "u" && token.trim().startsWith("-");
|
|
3827
4047
|
}
|
|
3828
4048
|
function isPrefixToken(token) {
|
|
3829
|
-
return typeof token < "u" && token.trim().endsWith(
|
|
4049
|
+
return typeof token < "u" && token.trim().endsWith("*");
|
|
3830
4050
|
}
|
|
3831
4051
|
function isExactMatchToken(token) {
|
|
3832
4052
|
return !!token && token.length >= 2 && token.startsWith('"') && token.endsWith('"');
|
|
@@ -3841,7 +4061,7 @@ function createGroqSearchFilter(query) {
|
|
|
3841
4061
|
return finalIncrementalToken !== void 0 && !isPrefixToken(finalIncrementalToken) && processedTokens.splice(
|
|
3842
4062
|
finalIncrementalTokenIndex,
|
|
3843
4063
|
1,
|
|
3844
|
-
`${finalIncrementalToken}
|
|
4064
|
+
`${finalIncrementalToken}*`
|
|
3845
4065
|
), `[@] match text::query("${processedTokens.join(" ").replace(/"/g, '\\"')}")`;
|
|
3846
4066
|
}
|
|
3847
4067
|
function defineIntent(intent) {
|
|
@@ -3902,12 +4122,18 @@ function getCorsErrorProjectId(error) {
|
|
|
3902
4122
|
const projMatch = (error.message || "").match(/manage\/project\/([^/?#]+)/);
|
|
3903
4123
|
return projMatch ? projMatch[1] : null;
|
|
3904
4124
|
}
|
|
3905
|
-
var version = "2.
|
|
4125
|
+
var version = "2.5.0";
|
|
3906
4126
|
const CORE_SDK_VERSION = getEnv("PKG_VERSION") || `${version}-development`;
|
|
3907
4127
|
export {
|
|
3908
4128
|
AuthStateType,
|
|
3909
4129
|
CORE_SDK_VERSION,
|
|
4130
|
+
agentGenerate,
|
|
4131
|
+
agentPatch,
|
|
4132
|
+
agentPrompt,
|
|
4133
|
+
agentTransform,
|
|
4134
|
+
agentTranslate,
|
|
3910
4135
|
applyDocumentActions,
|
|
4136
|
+
canvasSource,
|
|
3911
4137
|
createDatasetHandle,
|
|
3912
4138
|
createDocument,
|
|
3913
4139
|
createDocumentHandle,
|
|
@@ -3915,6 +4141,7 @@ export {
|
|
|
3915
4141
|
createGroqSearchFilter,
|
|
3916
4142
|
createProjectHandle,
|
|
3917
4143
|
createSanityInstance,
|
|
4144
|
+
datasetSource,
|
|
3918
4145
|
defineIntent,
|
|
3919
4146
|
deleteDocument,
|
|
3920
4147
|
destroyController,
|
|
@@ -3923,6 +4150,9 @@ export {
|
|
|
3923
4150
|
getActiveReleasesState,
|
|
3924
4151
|
getAuthState,
|
|
3925
4152
|
getClient,
|
|
4153
|
+
getClientErrorApiBody,
|
|
4154
|
+
getClientErrorApiDescription,
|
|
4155
|
+
getClientErrorApiType,
|
|
3926
4156
|
getClientState,
|
|
3927
4157
|
getCorsErrorProjectId,
|
|
3928
4158
|
getCurrentUserState,
|
|
@@ -3953,10 +4183,12 @@ export {
|
|
|
3953
4183
|
getUsersKey,
|
|
3954
4184
|
getUsersState,
|
|
3955
4185
|
handleAuthCallback,
|
|
4186
|
+
isProjectUserNotFoundClientError,
|
|
3956
4187
|
joinPaths,
|
|
3957
4188
|
jsonMatch2 as jsonMatch,
|
|
3958
4189
|
loadMoreUsers,
|
|
3959
4190
|
logout,
|
|
4191
|
+
mediaLibrarySource,
|
|
3960
4192
|
observeOrganizationVerificationState,
|
|
3961
4193
|
parseQueryKey,
|
|
3962
4194
|
parseUsersKey,
|