@sanity/sdk 2.4.0 → 2.6.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 +346 -110
- package/dist/index.js +428 -136
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
- package/src/_exports/index.ts +15 -3
- 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/client/clientStore.test.ts +45 -43
- package/src/client/clientStore.ts +23 -9
- 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/loggingConfig.ts +149 -0
- package/src/config/sanityConfig.ts +47 -23
- package/src/document/actions.ts +11 -7
- package/src/document/applyDocumentActions.test.ts +9 -6
- package/src/document/applyDocumentActions.ts +9 -49
- package/src/document/documentStore.test.ts +128 -115
- 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 +248 -0
- package/src/document/processActions.ts +173 -0
- 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 +3 -1
- 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 +54 -28
- package/src/store/createSanityInstance.test.ts +85 -1
- package/src/store/createSanityInstance.ts +53 -4
- 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/src/utils/logger-usage-example.md +141 -0
- package/src/utils/logger.test.ts +757 -0
- package/src/utils/logger.ts +537 -0
package/dist/index.js
CHANGED
|
@@ -17,15 +17,14 @@ 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
|
-
|
|
21
|
-
|
|
22
|
-
return { [SOURCE_ID]: { projectId, dataset } };
|
|
20
|
+
function isDatasetSource(source) {
|
|
21
|
+
return "projectId" in source && "dataset" in source;
|
|
23
22
|
}
|
|
24
|
-
function
|
|
25
|
-
return
|
|
23
|
+
function isMediaLibrarySource(source) {
|
|
24
|
+
return "mediaLibraryId" in source;
|
|
26
25
|
}
|
|
27
|
-
function
|
|
28
|
-
return
|
|
26
|
+
function isCanvasSource(source) {
|
|
27
|
+
return "canvasId" in source;
|
|
29
28
|
}
|
|
30
29
|
function getPublishedId(id) {
|
|
31
30
|
const draftsPrefix = "drafts.";
|
|
@@ -38,13 +37,152 @@ function getDraftId(id) {
|
|
|
38
37
|
function insecureRandomId() {
|
|
39
38
|
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
40
39
|
}
|
|
40
|
+
const LOG_LEVEL_PRIORITY = {
|
|
41
|
+
error: 0,
|
|
42
|
+
warn: 1,
|
|
43
|
+
info: 2,
|
|
44
|
+
debug: 3,
|
|
45
|
+
trace: 4
|
|
46
|
+
}, DEFAULT_CONFIG = {
|
|
47
|
+
level: "warn",
|
|
48
|
+
namespaces: [],
|
|
49
|
+
internal: !1,
|
|
50
|
+
timestamps: !0,
|
|
51
|
+
enableInProduction: !1,
|
|
52
|
+
handler: {
|
|
53
|
+
// eslint-disable-next-line no-console
|
|
54
|
+
error: console.error.bind(console),
|
|
55
|
+
// eslint-disable-next-line no-console
|
|
56
|
+
warn: console.warn.bind(console),
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
info: console.info.bind(console),
|
|
59
|
+
// eslint-disable-next-line no-console
|
|
60
|
+
debug: console.debug.bind(console),
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
62
|
+
trace: console.debug.bind(console)
|
|
63
|
+
// trace uses console.debug
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function parseDebugEnvVar() {
|
|
67
|
+
if (typeof process > "u" || !process.env?.DEBUG)
|
|
68
|
+
return null;
|
|
69
|
+
const debug = process.env.DEBUG;
|
|
70
|
+
if (!debug.includes("sanity"))
|
|
71
|
+
return null;
|
|
72
|
+
const config = {}, levelMatch = debug.match(/sanity:(trace|debug|info|warn|error):/), hasLevelSpecifier = !!levelMatch;
|
|
73
|
+
if (levelMatch ? config.level = levelMatch[1] : config.level = "debug", debug === "sanity")
|
|
74
|
+
config.namespaces = ["*"];
|
|
75
|
+
else if (hasLevelSpecifier && debug.match(/sanity:(trace|debug|info|warn|error):\*/))
|
|
76
|
+
config.namespaces = ["*"];
|
|
77
|
+
else if (!hasLevelSpecifier && debug.includes("sanity:*"))
|
|
78
|
+
config.namespaces = ["*"];
|
|
79
|
+
else {
|
|
80
|
+
const namespaces = debug.split(",").filter((s) => s.includes("sanity:")).map((s) => {
|
|
81
|
+
const cleaned = s.replace(/^sanity:/, "");
|
|
82
|
+
return hasLevelSpecifier && cleaned.match(/^(trace|debug|info|warn|error):/) ? cleaned.split(":").slice(1).join(":") : cleaned.split(":")[0];
|
|
83
|
+
}).filter(Boolean).filter((ns) => ns !== "*");
|
|
84
|
+
namespaces.length > 0 && (config.namespaces = namespaces);
|
|
85
|
+
}
|
|
86
|
+
return debug.includes(":internal") && (config.internal = !0), config;
|
|
87
|
+
}
|
|
88
|
+
const envConfig = parseDebugEnvVar();
|
|
89
|
+
let globalConfig = {
|
|
90
|
+
...DEFAULT_CONFIG,
|
|
91
|
+
...envConfig ?? {}
|
|
92
|
+
};
|
|
93
|
+
envConfig && (["info", "debug", "trace"].includes(globalConfig.level) || globalConfig.level === "warn") && console.info(
|
|
94
|
+
`[${(/* @__PURE__ */ new Date()).toISOString()}] [INFO] [sdk] Logging auto-configured from DEBUG environment variable`,
|
|
95
|
+
{
|
|
96
|
+
level: globalConfig.level,
|
|
97
|
+
namespaces: globalConfig.namespaces,
|
|
98
|
+
internal: globalConfig.internal,
|
|
99
|
+
source: "env:DEBUG",
|
|
100
|
+
value: typeof process < "u" ? process.env?.DEBUG : void 0
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
function configureLogging$1(config) {
|
|
104
|
+
globalConfig = {
|
|
105
|
+
...globalConfig,
|
|
106
|
+
...config,
|
|
107
|
+
handler: config.handler ?? globalConfig.handler
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function isLoggingEnabled() {
|
|
111
|
+
return typeof process < "u" && process.env?.NODE_ENV === "production" ? globalConfig.enableInProduction : !0;
|
|
112
|
+
}
|
|
113
|
+
function isNamespaceEnabled(namespace) {
|
|
114
|
+
return isLoggingEnabled() ? globalConfig.namespaces.includes("*") ? !0 : globalConfig.namespaces.includes(namespace) : !1;
|
|
115
|
+
}
|
|
116
|
+
function isLevelEnabled(level) {
|
|
117
|
+
return isLoggingEnabled() ? LOG_LEVEL_PRIORITY[level] <= LOG_LEVEL_PRIORITY[globalConfig.level] : !1;
|
|
118
|
+
}
|
|
119
|
+
function formatMessage(namespace, level, message, context) {
|
|
120
|
+
const parts = [];
|
|
121
|
+
if (globalConfig.timestamps) {
|
|
122
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
123
|
+
parts.push(`[${timestamp}]`);
|
|
124
|
+
}
|
|
125
|
+
parts.push(`[${level.toUpperCase()}]`), parts.push(`[${namespace}]`);
|
|
126
|
+
const instanceContext = context?.instanceContext;
|
|
127
|
+
return instanceContext && (instanceContext.projectId && parts.push(`[project:${instanceContext.projectId}]`), instanceContext.dataset && parts.push(`[dataset:${instanceContext.dataset}]`), instanceContext.instanceId && parts.push(`[instance:${instanceContext.instanceId.slice(0, 8)}]`)), parts.push(message), [parts.join(" "), context];
|
|
128
|
+
}
|
|
129
|
+
function sanitizeContext(context) {
|
|
130
|
+
if (!context || Object.keys(context).length === 0) return;
|
|
131
|
+
const sanitized = { ...context }, sensitiveKeys = ["token", "password", "secret", "apiKey", "authorization"];
|
|
132
|
+
for (const key of Object.keys(sanitized))
|
|
133
|
+
sensitiveKeys.some((sensitive) => key.toLowerCase().includes(sensitive)) && (sanitized[key] = "[REDACTED]");
|
|
134
|
+
return sanitized;
|
|
135
|
+
}
|
|
136
|
+
function createLogger(namespace, baseContext) {
|
|
137
|
+
const logAtLevel = (level, message, context) => {
|
|
138
|
+
if (!isNamespaceEnabled(namespace) || !isLevelEnabled(level) || context?.internal && !globalConfig.internal) return;
|
|
139
|
+
const mergedContext = { ...baseContext, ...context }, sanitized = sanitizeContext(mergedContext), [formatted, finalContext] = formatMessage(namespace, level, message, sanitized);
|
|
140
|
+
globalConfig.handler[level](formatted, finalContext);
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
namespace,
|
|
144
|
+
error: (message, context) => logAtLevel("error", message, context),
|
|
145
|
+
warn: (message, context) => logAtLevel("warn", message, context),
|
|
146
|
+
info: (message, context) => logAtLevel("info", message, context),
|
|
147
|
+
debug: (message, context) => logAtLevel("debug", message, context),
|
|
148
|
+
trace: (message, context) => logAtLevel("trace", message, { ...context, internal: !0 }),
|
|
149
|
+
isLevelEnabled: (level) => isNamespaceEnabled(namespace) && isLevelEnabled(level),
|
|
150
|
+
child: (childContext) => createLogger(namespace, { ...baseContext, ...childContext }),
|
|
151
|
+
getInstanceContext: () => baseContext?.instanceContext
|
|
152
|
+
};
|
|
153
|
+
}
|
|
41
154
|
function createSanityInstance(config = {}) {
|
|
42
|
-
const instanceId = crypto.randomUUID(), disposeListeners = /* @__PURE__ */ new Map(), disposed = { current: !1 },
|
|
155
|
+
const instanceId = crypto.randomUUID(), disposeListeners = /* @__PURE__ */ new Map(), disposed = { current: !1 }, instanceContext = {
|
|
156
|
+
instanceId,
|
|
157
|
+
projectId: config.projectId,
|
|
158
|
+
dataset: config.dataset
|
|
159
|
+
}, logger = createLogger("sdk", { instanceContext });
|
|
160
|
+
logger.info("Sanity instance created", {
|
|
161
|
+
hasProjectId: !!config.projectId,
|
|
162
|
+
hasDataset: !!config.dataset,
|
|
163
|
+
hasAuth: !!config.auth,
|
|
164
|
+
hasPerspective: !!config.perspective
|
|
165
|
+
}), logger.debug("Instance configuration", {
|
|
166
|
+
projectId: config.projectId,
|
|
167
|
+
dataset: config.dataset,
|
|
168
|
+
perspective: config.perspective,
|
|
169
|
+
studioMode: config.studioMode?.enabled,
|
|
170
|
+
hasAuthProviders: !!config.auth?.providers,
|
|
171
|
+
hasAuthToken: !!config.auth?.token
|
|
172
|
+
});
|
|
173
|
+
const instance = {
|
|
43
174
|
instanceId,
|
|
44
175
|
config,
|
|
45
176
|
isDisposed: () => disposed.current,
|
|
46
177
|
dispose: () => {
|
|
47
|
-
|
|
178
|
+
if (disposed.current) {
|
|
179
|
+
logger.trace("Dispose called on already disposed instance", { internal: !0 });
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
logger.trace("Disposing instance", {
|
|
183
|
+
internal: !0,
|
|
184
|
+
listenerCount: disposeListeners.size
|
|
185
|
+
}), disposed.current = !0, disposeListeners.forEach((listener) => listener()), disposeListeners.clear(), logger.info("Instance disposed");
|
|
48
186
|
},
|
|
49
187
|
onDispose: (cb) => {
|
|
50
188
|
const listenerId = insecureRandomId();
|
|
@@ -54,14 +192,26 @@ function createSanityInstance(config = {}) {
|
|
|
54
192
|
},
|
|
55
193
|
getParent: () => {
|
|
56
194
|
},
|
|
57
|
-
createChild: (next) =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
195
|
+
createChild: (next) => {
|
|
196
|
+
logger.debug("Creating child instance", {
|
|
197
|
+
parentInstanceId: instanceId.slice(0, 8),
|
|
198
|
+
overridingProjectId: !!next.projectId,
|
|
199
|
+
overridingDataset: !!next.dataset,
|
|
200
|
+
overridingAuth: !!next.auth
|
|
201
|
+
});
|
|
202
|
+
const child = Object.assign(
|
|
203
|
+
createSanityInstance({
|
|
204
|
+
...config,
|
|
205
|
+
...next,
|
|
206
|
+
...config.auth === next.auth ? config.auth : config.auth && next.auth && { auth: { ...config.auth, ...next.auth } }
|
|
207
|
+
}),
|
|
208
|
+
{ getParent: () => instance }
|
|
209
|
+
);
|
|
210
|
+
return logger.trace("Child instance created", {
|
|
211
|
+
internal: !0,
|
|
212
|
+
childInstanceId: child.instanceId.slice(0, 8)
|
|
213
|
+
}), child;
|
|
214
|
+
},
|
|
65
215
|
match: (targetConfig) => {
|
|
66
216
|
if (Object.entries(pick(targetConfig, "auth", "projectId", "dataset")).every(
|
|
67
217
|
([key, value]) => config[key] === value
|
|
@@ -97,11 +247,11 @@ function createStoreState(initialState, devToolsOptions) {
|
|
|
97
247
|
})
|
|
98
248
|
};
|
|
99
249
|
}
|
|
100
|
-
function createStoreInstance(instance, { name, getInitialState: getInitialState2, initialize }) {
|
|
101
|
-
const state = createStoreState(getInitialState2(instance), {
|
|
250
|
+
function createStoreInstance(instance, key, { name, getInitialState: getInitialState2, initialize }) {
|
|
251
|
+
const state = createStoreState(getInitialState2(instance, key), {
|
|
102
252
|
enabled: !!getEnv("DEV"),
|
|
103
|
-
name: `${name}-${
|
|
104
|
-
}), dispose = initialize?.({ state, instance }), disposed = { current: !1 };
|
|
253
|
+
name: `${name}-${key.name}`
|
|
254
|
+
}), dispose = initialize?.({ state, instance, key }), disposed = { current: !1 };
|
|
105
255
|
return {
|
|
106
256
|
state,
|
|
107
257
|
dispose: () => {
|
|
@@ -114,32 +264,32 @@ function createActionBinder(keyFn) {
|
|
|
114
264
|
const instanceRegistry = /* @__PURE__ */ new Map(), storeRegistry = /* @__PURE__ */ new Map();
|
|
115
265
|
return function(storeDefinition, action) {
|
|
116
266
|
return function(instance, ...params) {
|
|
117
|
-
const
|
|
267
|
+
const key = keyFn(instance, ...params), compositeKey = storeDefinition.name + (key.name ? `:${key.name}` : "");
|
|
118
268
|
let instances = instanceRegistry.get(compositeKey);
|
|
119
269
|
instances || (instances = /* @__PURE__ */ new Set(), instanceRegistry.set(compositeKey, instances)), instances.has(instance.instanceId) || (instances.add(instance.instanceId), instance.onDispose(() => {
|
|
120
270
|
instances.delete(instance.instanceId), instances.size === 0 && (storeRegistry.get(compositeKey)?.dispose(), storeRegistry.delete(compositeKey), instanceRegistry.delete(compositeKey));
|
|
121
271
|
}));
|
|
122
272
|
let storeInstance = storeRegistry.get(compositeKey);
|
|
123
|
-
return storeInstance || (storeInstance = createStoreInstance(instance, storeDefinition), storeRegistry.set(compositeKey, storeInstance)), action({ instance, state: storeInstance.state }, ...params);
|
|
273
|
+
return storeInstance || (storeInstance = createStoreInstance(instance, key, storeDefinition), storeRegistry.set(compositeKey, storeInstance)), action({ instance, state: storeInstance.state, key }, ...params);
|
|
124
274
|
};
|
|
125
275
|
};
|
|
126
276
|
}
|
|
127
|
-
const bindActionByDataset = createActionBinder((
|
|
277
|
+
const bindActionByDataset = createActionBinder((instance, options) => {
|
|
278
|
+
const projectId = options?.projectId ?? instance.config.projectId, dataset = options?.dataset ?? instance.config.dataset;
|
|
128
279
|
if (!projectId || !dataset)
|
|
129
280
|
throw new Error("This API requires a project ID and dataset configured.");
|
|
130
|
-
return `${projectId}.${dataset}
|
|
131
|
-
}), bindActionBySource = createActionBinder(
|
|
132
|
-
(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return Array.isArray(id) ? id.join(":") : `${id.projectId}.${id.dataset}`;
|
|
137
|
-
}
|
|
138
|
-
if (!projectId || !dataset)
|
|
139
|
-
throw new Error("This API requires a project ID and dataset configured.");
|
|
140
|
-
return `${projectId}.${dataset}`;
|
|
281
|
+
return { name: `${projectId}.${dataset}`, projectId, dataset };
|
|
282
|
+
}), bindActionBySource = createActionBinder((instance, { source }) => {
|
|
283
|
+
if (source) {
|
|
284
|
+
let id;
|
|
285
|
+
if (isDatasetSource(source) ? id = `${source.projectId}.${source.dataset}` : isMediaLibrarySource(source) ? id = `media-library:${source.mediaLibraryId}` : isCanvasSource(source) && (id = `canvas:${source.canvasId}`), !id) throw new Error(`Received invalid source: ${JSON.stringify(source)}`);
|
|
286
|
+
return { name: id };
|
|
141
287
|
}
|
|
142
|
-
|
|
288
|
+
const { projectId, dataset } = instance.config;
|
|
289
|
+
if (!projectId || !dataset)
|
|
290
|
+
throw new Error("This API requires a project ID and dataset configured.");
|
|
291
|
+
return { name: `${projectId}.${dataset}` };
|
|
292
|
+
}), bindActionGlobally = createActionBinder((..._rest) => ({ name: "global" }));
|
|
143
293
|
function createStateSourceAction(options) {
|
|
144
294
|
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();
|
|
145
295
|
function stateSourceAction(context, ...params) {
|
|
@@ -188,7 +338,7 @@ function createStateSourceAction(options) {
|
|
|
188
338
|
return stateSourceAction;
|
|
189
339
|
}
|
|
190
340
|
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 =
|
|
341
|
+
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";
|
|
192
342
|
function getLastRefreshTime(storageArea, storageKey) {
|
|
193
343
|
try {
|
|
194
344
|
const data = storageArea?.getItem(`${storageKey}_last_refresh`), parsed = data ? parseInt(data, 10) : 0;
|
|
@@ -673,12 +823,12 @@ const authStore = {
|
|
|
673
823
|
`The client options provided contains unsupported properties: ${listFormatter.format(disallowedKeys)}. Allowed keys are: ${listFormatter.format(allowedKeys)}.`
|
|
674
824
|
);
|
|
675
825
|
}
|
|
676
|
-
const tokenFromState = state.get().token, { clients, authMethod } = state.get()
|
|
677
|
-
let
|
|
678
|
-
|
|
826
|
+
const tokenFromState = state.get().token, { clients, authMethod } = state.get();
|
|
827
|
+
let resource;
|
|
828
|
+
options.source && (isDatasetSource(options.source) ? resource = { type: "dataset", id: `${options.source.projectId}.${options.source.dataset}` } : isMediaLibrarySource(options.source) ? resource = { type: "media-library", id: options.source.mediaLibraryId } : isCanvasSource(options.source) && (resource = { type: "canvas", id: options.source.canvasId }));
|
|
679
829
|
const projectId = options.projectId ?? instance.config.projectId, dataset = options.dataset ?? instance.config.dataset, apiHost = options.apiHost ?? instance.config.auth?.apiHost, effectiveOptions = {
|
|
680
830
|
...DEFAULT_CLIENT_CONFIG,
|
|
681
|
-
...(options.scope === "global" || !projectId ||
|
|
831
|
+
...(options.scope === "global" || !projectId || resource) && { useProjectHostname: !1 },
|
|
682
832
|
token: authMethod === "cookie" ? void 0 : tokenFromState ?? void 0,
|
|
683
833
|
...options,
|
|
684
834
|
...projectId && { projectId },
|
|
@@ -686,7 +836,7 @@ const authStore = {
|
|
|
686
836
|
...apiHost && { apiHost },
|
|
687
837
|
...resource && { "~experimental_resource": resource }
|
|
688
838
|
};
|
|
689
|
-
|
|
839
|
+
resource && ((options.projectId || options.dataset) && console.warn(
|
|
690
840
|
"Both source and explicit projectId/dataset are provided. The source will be used and projectId/dataset will be ignored."
|
|
691
841
|
), delete effectiveOptions.projectId, delete effectiveOptions.dataset), effectiveOptions.token === null || typeof effectiveOptions.token > "u" ? (delete effectiveOptions.token, authMethod === "cookie" && (effectiveOptions.withCredentials = !0)) : delete effectiveOptions.withCredentials;
|
|
692
842
|
const key = getClientConfigKey(effectiveOptions);
|
|
@@ -1144,6 +1294,21 @@ function createProjectHandle(handle) {
|
|
|
1144
1294
|
function createDatasetHandle(handle) {
|
|
1145
1295
|
return handle;
|
|
1146
1296
|
}
|
|
1297
|
+
function configureLogging(config) {
|
|
1298
|
+
configureLogging$1(config);
|
|
1299
|
+
const configLevel = config.level || "warn", shouldLog = ["info", "debug", "trace"].includes(configLevel) || configLevel === "warn";
|
|
1300
|
+
shouldLog && config.handler?.info ? config.handler.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] [INFO] [sdk] Logging configured`, {
|
|
1301
|
+
level: configLevel,
|
|
1302
|
+
namespaces: config.namespaces || [],
|
|
1303
|
+
internal: config.internal || !1,
|
|
1304
|
+
source: "programmatic"
|
|
1305
|
+
}) : shouldLog && console.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] [INFO] [sdk] Logging configured`, {
|
|
1306
|
+
level: configLevel,
|
|
1307
|
+
namespaces: config.namespaces || [],
|
|
1308
|
+
internal: config.internal || !1,
|
|
1309
|
+
source: "programmatic"
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1147
1312
|
const API_VERSION$4 = "v2025-02-19", datasets = createFetcherStore({
|
|
1148
1313
|
name: "Datasets",
|
|
1149
1314
|
getKey: (instance, options) => {
|
|
@@ -1163,7 +1328,9 @@ function createDocument(doc, initialValue) {
|
|
|
1163
1328
|
return {
|
|
1164
1329
|
type: "document.create",
|
|
1165
1330
|
...doc,
|
|
1166
|
-
...doc.documentId && {
|
|
1331
|
+
...doc.documentId && {
|
|
1332
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1333
|
+
},
|
|
1167
1334
|
...initialValue && { initialValue }
|
|
1168
1335
|
};
|
|
1169
1336
|
}
|
|
@@ -1171,7 +1338,7 @@ function deleteDocument(doc) {
|
|
|
1171
1338
|
return {
|
|
1172
1339
|
type: "document.delete",
|
|
1173
1340
|
...doc,
|
|
1174
|
-
documentId: getPublishedId(doc.documentId)
|
|
1341
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1175
1342
|
};
|
|
1176
1343
|
}
|
|
1177
1344
|
function convertSanityMutatePatch(sanityPatchMutation) {
|
|
@@ -1181,19 +1348,20 @@ function convertSanityMutatePatch(sanityPatchMutation) {
|
|
|
1181
1348
|
});
|
|
1182
1349
|
}
|
|
1183
1350
|
function editDocument(doc, patches) {
|
|
1351
|
+
const documentId = doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId);
|
|
1184
1352
|
if (isSanityMutatePatch(patches)) {
|
|
1185
1353
|
const converted = convertSanityMutatePatch(patches) ?? [];
|
|
1186
1354
|
return {
|
|
1187
1355
|
...doc,
|
|
1188
1356
|
type: "document.edit",
|
|
1189
|
-
documentId
|
|
1357
|
+
documentId,
|
|
1190
1358
|
patches: converted
|
|
1191
1359
|
};
|
|
1192
1360
|
}
|
|
1193
1361
|
return {
|
|
1194
1362
|
...doc,
|
|
1195
1363
|
type: "document.edit",
|
|
1196
|
-
documentId
|
|
1364
|
+
documentId,
|
|
1197
1365
|
...patches && { patches: Array.isArray(patches) ? patches : [patches] }
|
|
1198
1366
|
};
|
|
1199
1367
|
}
|
|
@@ -1201,21 +1369,21 @@ function publishDocument(doc) {
|
|
|
1201
1369
|
return {
|
|
1202
1370
|
type: "document.publish",
|
|
1203
1371
|
...doc,
|
|
1204
|
-
documentId: getPublishedId(doc.documentId)
|
|
1372
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1205
1373
|
};
|
|
1206
1374
|
}
|
|
1207
1375
|
function unpublishDocument(doc) {
|
|
1208
1376
|
return {
|
|
1209
1377
|
type: "document.unpublish",
|
|
1210
1378
|
...doc,
|
|
1211
|
-
documentId: getPublishedId(doc.documentId)
|
|
1379
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1212
1380
|
};
|
|
1213
1381
|
}
|
|
1214
1382
|
function discardDocument(doc) {
|
|
1215
1383
|
return {
|
|
1216
1384
|
type: "document.discard",
|
|
1217
1385
|
...doc,
|
|
1218
|
-
documentId: getPublishedId(doc.documentId)
|
|
1386
|
+
documentId: doc.liveEdit ? doc.documentId : getPublishedId(doc.documentId)
|
|
1219
1387
|
};
|
|
1220
1388
|
}
|
|
1221
1389
|
const DOCUMENT_STATE_CLEAR_DELAY = 1e3, INITIAL_OUTGOING_THROTTLE_TIME = 1e3, API_VERSION$3 = "v2025-05-06";
|
|
@@ -1225,13 +1393,13 @@ function generateArrayKey(length = 12) {
|
|
|
1225
1393
|
}
|
|
1226
1394
|
function memoize(fn) {
|
|
1227
1395
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
1228
|
-
return (input) => {
|
|
1396
|
+
return ((input) => {
|
|
1229
1397
|
if (!input || typeof input != "object") return fn(input);
|
|
1230
1398
|
const cached = cache.get(input);
|
|
1231
1399
|
if (cached) return cached;
|
|
1232
1400
|
const result = fn(input);
|
|
1233
1401
|
return cache.set(input, result), result;
|
|
1234
|
-
};
|
|
1402
|
+
});
|
|
1235
1403
|
}
|
|
1236
1404
|
const ensureArrayKeysDeep = memoize((input) => {
|
|
1237
1405
|
if (!input || typeof input != "object") return input;
|
|
@@ -1744,11 +1912,11 @@ function createGrantsLookup(datasetAcl) {
|
|
|
1744
1912
|
const documentsCache = new MultiKeyWeakMap(), actionsCache = /* @__PURE__ */ new WeakMap(), nullReplacer = {}, documentsSelector = createSelector(
|
|
1745
1913
|
[
|
|
1746
1914
|
({ state: { documentStates } }) => documentStates,
|
|
1747
|
-
(_context, actions) => actions
|
|
1915
|
+
(_context, { actions }) => actions
|
|
1748
1916
|
],
|
|
1749
1917
|
(documentStates, actions) => {
|
|
1750
1918
|
const documentIds = new Set(
|
|
1751
|
-
|
|
1919
|
+
actions.map((action) => typeof action.documentId != "string" ? [] : action.liveEdit ? [action.documentId] : [getPublishedId(action.documentId), getDraftId(action.documentId)]).flat()
|
|
1752
1920
|
), documents = {};
|
|
1753
1921
|
for (const documentId of documentIds) {
|
|
1754
1922
|
const local = documentStates[documentId]?.local;
|
|
@@ -1765,7 +1933,7 @@ const documentsCache = new MultiKeyWeakMap(), actionsCache = /* @__PURE__ */ new
|
|
|
1765
1933
|
), memoizedActionsSelector = createSelector(
|
|
1766
1934
|
[
|
|
1767
1935
|
documentsSelector,
|
|
1768
|
-
(_state, actions) => actions
|
|
1936
|
+
(_state, { actions }) => actions
|
|
1769
1937
|
],
|
|
1770
1938
|
(documents, actions) => {
|
|
1771
1939
|
if (!documents) return;
|
|
@@ -1819,7 +1987,7 @@ const _calculatePermissions = createSelector(
|
|
|
1819
1987
|
}
|
|
1820
1988
|
for (const action of actions)
|
|
1821
1989
|
if (action.type === "document.edit" && !action.patches?.length) {
|
|
1822
|
-
const docId = action.documentId, doc = documents[getDraftId(docId)] ?? documents[getPublishedId(docId)];
|
|
1990
|
+
const docId = action.documentId, doc = action.liveEdit ? documents[docId] : documents[getDraftId(docId)] ?? documents[getPublishedId(docId)];
|
|
1823
1991
|
doc ? checkGrant$1(grants.update, doc) || reasons.push({
|
|
1824
1992
|
type: "access",
|
|
1825
1993
|
message: `You are not allowed to edit the document with ID "${docId}".`,
|
|
@@ -1866,7 +2034,39 @@ function processActions({
|
|
|
1866
2034
|
for (const action of actions)
|
|
1867
2035
|
switch (action.type) {
|
|
1868
2036
|
case "document.create": {
|
|
1869
|
-
const documentId = getId(action.documentId)
|
|
2037
|
+
const documentId = getId(action.documentId);
|
|
2038
|
+
if (action.liveEdit) {
|
|
2039
|
+
if (working[documentId])
|
|
2040
|
+
throw new ActionError({
|
|
2041
|
+
documentId,
|
|
2042
|
+
transactionId,
|
|
2043
|
+
message: "This document already exists."
|
|
2044
|
+
});
|
|
2045
|
+
const newDocBase2 = { _type: action.documentType, _id: documentId }, newDocWorking2 = { _type: action.documentType, _id: documentId }, mutations2 = [{ create: newDocWorking2 }];
|
|
2046
|
+
if (base = processMutations({
|
|
2047
|
+
documents: base,
|
|
2048
|
+
transactionId,
|
|
2049
|
+
mutations: [{ create: newDocBase2 }],
|
|
2050
|
+
timestamp
|
|
2051
|
+
}), working = processMutations({
|
|
2052
|
+
documents: working,
|
|
2053
|
+
transactionId,
|
|
2054
|
+
mutations: mutations2,
|
|
2055
|
+
timestamp
|
|
2056
|
+
}), !checkGrant(grants.create, working[documentId]))
|
|
2057
|
+
throw new PermissionActionError({
|
|
2058
|
+
documentId,
|
|
2059
|
+
transactionId,
|
|
2060
|
+
message: `You do not have permission to create document "${documentId}".`
|
|
2061
|
+
});
|
|
2062
|
+
outgoingMutations.push(...mutations2), outgoingActions.push({
|
|
2063
|
+
actionType: "sanity.action.document.create",
|
|
2064
|
+
publishedId: documentId,
|
|
2065
|
+
attributes: newDocWorking2
|
|
2066
|
+
});
|
|
2067
|
+
continue;
|
|
2068
|
+
}
|
|
2069
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
1870
2070
|
if (working[draftId])
|
|
1871
2071
|
throw new ActionError({
|
|
1872
2072
|
documentId,
|
|
@@ -1908,7 +2108,28 @@ function processActions({
|
|
|
1908
2108
|
continue;
|
|
1909
2109
|
}
|
|
1910
2110
|
case "document.delete": {
|
|
1911
|
-
const documentId = action.documentId
|
|
2111
|
+
const documentId = action.documentId;
|
|
2112
|
+
if (action.liveEdit) {
|
|
2113
|
+
if (!working[documentId])
|
|
2114
|
+
throw new ActionError({
|
|
2115
|
+
documentId,
|
|
2116
|
+
transactionId,
|
|
2117
|
+
message: "The document you are trying to delete does not exist."
|
|
2118
|
+
});
|
|
2119
|
+
if (!checkGrant(grants.update, working[documentId]))
|
|
2120
|
+
throw new PermissionActionError({
|
|
2121
|
+
documentId,
|
|
2122
|
+
transactionId,
|
|
2123
|
+
message: "You do not have permission to delete this document."
|
|
2124
|
+
});
|
|
2125
|
+
const mutations2 = [{ delete: { id: documentId } }];
|
|
2126
|
+
base = processMutations({ documents: base, transactionId, mutations: mutations2, timestamp }), working = processMutations({ documents: working, transactionId, mutations: mutations2, timestamp }), outgoingMutations.push(...mutations2), outgoingActions.push({
|
|
2127
|
+
actionType: "sanity.action.document.delete",
|
|
2128
|
+
publishedId: documentId
|
|
2129
|
+
});
|
|
2130
|
+
continue;
|
|
2131
|
+
}
|
|
2132
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
1912
2133
|
if (!working[publishedId])
|
|
1913
2134
|
throw new ActionError({
|
|
1914
2135
|
documentId,
|
|
@@ -1931,7 +2152,14 @@ function processActions({
|
|
|
1931
2152
|
continue;
|
|
1932
2153
|
}
|
|
1933
2154
|
case "document.discard": {
|
|
1934
|
-
const documentId = getId(action.documentId)
|
|
2155
|
+
const documentId = getId(action.documentId);
|
|
2156
|
+
if (action.liveEdit)
|
|
2157
|
+
throw new ActionError({
|
|
2158
|
+
documentId,
|
|
2159
|
+
transactionId,
|
|
2160
|
+
message: `Cannot discard changes for liveEdit document "${documentId}". LiveEdit documents do not support drafts.`
|
|
2161
|
+
});
|
|
2162
|
+
const draftId = getDraftId(documentId), mutations = [{ delete: { id: draftId } }];
|
|
1935
2163
|
if (!working[draftId])
|
|
1936
2164
|
throw new ActionError({
|
|
1937
2165
|
documentId,
|
|
@@ -1951,7 +2179,50 @@ function processActions({
|
|
|
1951
2179
|
continue;
|
|
1952
2180
|
}
|
|
1953
2181
|
case "document.edit": {
|
|
1954
|
-
const documentId = getId(action.documentId)
|
|
2182
|
+
const documentId = getId(action.documentId);
|
|
2183
|
+
if (action.liveEdit) {
|
|
2184
|
+
const userPatches2 = action.patches?.map((patch) => ({ patch: { id: documentId, ...patch } }));
|
|
2185
|
+
if (!userPatches2?.length) continue;
|
|
2186
|
+
if (!working[documentId] || !base[documentId])
|
|
2187
|
+
throw new ActionError({
|
|
2188
|
+
documentId,
|
|
2189
|
+
transactionId,
|
|
2190
|
+
message: "Cannot edit document because it does not exist."
|
|
2191
|
+
});
|
|
2192
|
+
const baseBefore2 = base[documentId];
|
|
2193
|
+
userPatches2 && (base = processMutations({
|
|
2194
|
+
documents: base,
|
|
2195
|
+
transactionId,
|
|
2196
|
+
mutations: userPatches2,
|
|
2197
|
+
timestamp
|
|
2198
|
+
}));
|
|
2199
|
+
const baseAfter2 = base[documentId], patches2 = diffValue(baseBefore2, baseAfter2), workingBefore2 = working[documentId];
|
|
2200
|
+
if (!checkGrant(grants.update, workingBefore2))
|
|
2201
|
+
throw new PermissionActionError({
|
|
2202
|
+
documentId,
|
|
2203
|
+
transactionId,
|
|
2204
|
+
message: `You do not have permission to edit document "${documentId}".`
|
|
2205
|
+
});
|
|
2206
|
+
const workingMutations2 = patches2.map((patch) => ({ patch: { id: documentId, ...patch } }));
|
|
2207
|
+
working = processMutations({
|
|
2208
|
+
documents: working,
|
|
2209
|
+
transactionId,
|
|
2210
|
+
mutations: workingMutations2,
|
|
2211
|
+
timestamp
|
|
2212
|
+
}), outgoingMutations.push(...workingMutations2), outgoingActions.push(
|
|
2213
|
+
...patches2.map(
|
|
2214
|
+
(patch) => ({
|
|
2215
|
+
actionType: "sanity.action.document.edit",
|
|
2216
|
+
// Server requires draftId to have drafts. prefix for validation, even for liveEdit
|
|
2217
|
+
draftId: getDraftId(documentId),
|
|
2218
|
+
publishedId: documentId,
|
|
2219
|
+
patch
|
|
2220
|
+
})
|
|
2221
|
+
)
|
|
2222
|
+
);
|
|
2223
|
+
continue;
|
|
2224
|
+
}
|
|
2225
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId), userPatches = action.patches?.map((patch) => ({ patch: { id: draftId, ...patch } }));
|
|
1955
2226
|
if (!userPatches?.length) continue;
|
|
1956
2227
|
if (!working[draftId] && !working[publishedId] || !base[draftId] && !base[publishedId])
|
|
1957
2228
|
throw new ActionError({
|
|
@@ -2004,7 +2275,14 @@ function processActions({
|
|
|
2004
2275
|
continue;
|
|
2005
2276
|
}
|
|
2006
2277
|
case "document.publish": {
|
|
2007
|
-
const documentId = getId(action.documentId)
|
|
2278
|
+
const documentId = getId(action.documentId);
|
|
2279
|
+
if (action.liveEdit)
|
|
2280
|
+
throw new ActionError({
|
|
2281
|
+
documentId,
|
|
2282
|
+
transactionId,
|
|
2283
|
+
message: `Cannot publish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`
|
|
2284
|
+
});
|
|
2285
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId), workingDraft = working[draftId], baseDraft = base[draftId];
|
|
2008
2286
|
if (!workingDraft || !baseDraft)
|
|
2009
2287
|
throw new ActionError({
|
|
2010
2288
|
documentId,
|
|
@@ -2047,7 +2325,14 @@ function processActions({
|
|
|
2047
2325
|
continue;
|
|
2048
2326
|
}
|
|
2049
2327
|
case "document.unpublish": {
|
|
2050
|
-
const documentId = getId(action.documentId)
|
|
2328
|
+
const documentId = getId(action.documentId);
|
|
2329
|
+
if (action.liveEdit)
|
|
2330
|
+
throw new ActionError({
|
|
2331
|
+
documentId,
|
|
2332
|
+
transactionId,
|
|
2333
|
+
message: `Cannot unpublish liveEdit document "${documentId}". LiveEdit documents do not support drafts or publishing.`
|
|
2334
|
+
});
|
|
2335
|
+
const draftId = getDraftId(documentId), publishedId = getPublishedId(documentId);
|
|
2051
2336
|
if (!working[publishedId] && !base[publishedId])
|
|
2052
2337
|
throw new ActionError({
|
|
2053
2338
|
documentId,
|
|
@@ -2369,13 +2654,13 @@ function removeSubscriptionIdFromDocument(prev, documentId, subscriptionId) {
|
|
|
2369
2654
|
}
|
|
2370
2655
|
} : { ...prev, documentStates: omit(prev.documentStates, documentId) } : prev;
|
|
2371
2656
|
}
|
|
2372
|
-
function manageSubscriberIds({ state }, documentId) {
|
|
2373
|
-
const documentIds = Array.from(
|
|
2657
|
+
function manageSubscriberIds({ state }, documentId, options) {
|
|
2658
|
+
const expandDraftPublished = options?.expandDraftPublished ?? !0, documentIds = Array.from(
|
|
2374
2659
|
new Set(
|
|
2375
|
-
(Array.isArray(documentId) ? documentId : [documentId]).flatMap((id) => [
|
|
2660
|
+
expandDraftPublished ? (Array.isArray(documentId) ? documentId : [documentId]).flatMap((id) => [
|
|
2376
2661
|
getPublishedId$1(id),
|
|
2377
2662
|
getDraftId(id)
|
|
2378
|
-
])
|
|
2663
|
+
]) : Array.isArray(documentId) ? documentId : [documentId]
|
|
2379
2664
|
)
|
|
2380
2665
|
), subscriptionId = insecureRandomId();
|
|
2381
2666
|
return state.set(
|
|
@@ -2396,8 +2681,7 @@ function manageSubscriberIds({ state }, documentId) {
|
|
|
2396
2681
|
}, DOCUMENT_STATE_CLEAR_DELAY);
|
|
2397
2682
|
};
|
|
2398
2683
|
}
|
|
2399
|
-
function getDocumentIdsFromActions(
|
|
2400
|
-
const actions = Array.isArray(action) ? action : [action];
|
|
2684
|
+
function getDocumentIdsFromActions(actions) {
|
|
2401
2685
|
return Array.from(
|
|
2402
2686
|
new Set(
|
|
2403
2687
|
actions.map((i) => i.documentId).filter((i) => typeof i == "string").flatMap((documentId) => [getPublishedId$1(documentId), getDraftId(documentId)])
|
|
@@ -2509,8 +2793,17 @@ const _getDocumentState = bindActionByDataset(
|
|
|
2509
2793
|
documentStore,
|
|
2510
2794
|
createStateSourceAction({
|
|
2511
2795
|
selector: ({ state: { error, documentStates } }, options) => {
|
|
2512
|
-
const { documentId, path } = options;
|
|
2796
|
+
const { documentId, path, liveEdit } = options;
|
|
2513
2797
|
if (error) throw error;
|
|
2798
|
+
if (liveEdit) {
|
|
2799
|
+
const document22 = documentStates[documentId]?.local;
|
|
2800
|
+
if (document22 === void 0) return;
|
|
2801
|
+
if (!path) return document22;
|
|
2802
|
+
const result2 = jsonMatch(document22, path).next();
|
|
2803
|
+
if (result2.done) return;
|
|
2804
|
+
const { value: value2 } = result2.value;
|
|
2805
|
+
return value2;
|
|
2806
|
+
}
|
|
2514
2807
|
const draftId = getDraftId(documentId), publishedId = getPublishedId$1(documentId), draft = documentStates[draftId]?.local, published = documentStates[publishedId]?.local;
|
|
2515
2808
|
if (draft === void 0 || published === void 0) return;
|
|
2516
2809
|
const document2 = draft ?? published;
|
|
@@ -2520,7 +2813,7 @@ const _getDocumentState = bindActionByDataset(
|
|
|
2520
2813
|
const { value } = result.value;
|
|
2521
2814
|
return value;
|
|
2522
2815
|
},
|
|
2523
|
-
onSubscribe: (context, options) => manageSubscriberIds(context, options.documentId)
|
|
2816
|
+
onSubscribe: (context, options) => manageSubscriberIds(context, options.documentId, { expandDraftPublished: !options.liveEdit })
|
|
2524
2817
|
})
|
|
2525
2818
|
);
|
|
2526
2819
|
function resolveDocument(...args) {
|
|
@@ -2540,6 +2833,8 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2540
2833
|
selector: ({ state: { error, documentStates: documents, outgoing, applied, queued } }, doc) => {
|
|
2541
2834
|
const documentId = typeof doc == "string" ? doc : doc.documentId;
|
|
2542
2835
|
if (error) throw error;
|
|
2836
|
+
if (doc.liveEdit)
|
|
2837
|
+
return documents[documentId] === void 0 ? void 0 : !queued.length && !applied.length && !outgoing;
|
|
2543
2838
|
const draftId = getDraftId(documentId), publishedId = getPublishedId$1(documentId), draft = documents[draftId], published = documents[publishedId];
|
|
2544
2839
|
if (!(draft === void 0 || published === void 0))
|
|
2545
2840
|
return !queued.length && !applied.length && !outgoing;
|
|
@@ -2550,12 +2845,12 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2550
2845
|
documentStore,
|
|
2551
2846
|
createStateSourceAction({
|
|
2552
2847
|
selector: calculatePermissions,
|
|
2553
|
-
onSubscribe: (context, actions) => manageSubscriberIds(context, getDocumentIdsFromActions(actions))
|
|
2848
|
+
onSubscribe: (context, { actions }) => manageSubscriberIds(context, getDocumentIdsFromActions(actions))
|
|
2554
2849
|
})
|
|
2555
2850
|
), resolvePermissions = bindActionByDataset(
|
|
2556
2851
|
documentStore,
|
|
2557
|
-
({ instance },
|
|
2558
|
-
getPermissionsState(instance,
|
|
2852
|
+
({ instance }, options) => firstValueFrom(
|
|
2853
|
+
getPermissionsState(instance, options).observable.pipe(filter((i) => i !== void 0))
|
|
2559
2854
|
)
|
|
2560
2855
|
), subscribeDocumentEvents = bindActionByDataset(
|
|
2561
2856
|
documentStore,
|
|
@@ -2661,51 +2956,25 @@ const _resolveDocument = bindActionByDataset(
|
|
|
2661
2956
|
).subscribe({ error: (error) => state.set("setError", { error }) });
|
|
2662
2957
|
}, subscribeToClientAndFetchDatasetAcl = ({
|
|
2663
2958
|
instance,
|
|
2664
|
-
state
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
).
|
|
2677
|
-
|
|
2678
|
-
});
|
|
2679
|
-
};
|
|
2959
|
+
state,
|
|
2960
|
+
key: { projectId, dataset }
|
|
2961
|
+
}) => getClientState(instance, { apiVersion: API_VERSION$3 }).observable.pipe(
|
|
2962
|
+
switchMap(
|
|
2963
|
+
(client) => client.observable.request({
|
|
2964
|
+
uri: `/projects/${projectId}/datasets/${dataset}/acl`,
|
|
2965
|
+
tag: "acl.get",
|
|
2966
|
+
withCredentials: !0
|
|
2967
|
+
})
|
|
2968
|
+
),
|
|
2969
|
+
tap$1((datasetAcl) => state.set("setGrants", { grants: createGrantsLookup(datasetAcl) }))
|
|
2970
|
+
).subscribe({
|
|
2971
|
+
error: (error) => state.set("setError", { error })
|
|
2972
|
+
});
|
|
2680
2973
|
function applyDocumentActions(...args) {
|
|
2681
2974
|
return boundApplyDocumentActions(...args);
|
|
2682
2975
|
}
|
|
2683
2976
|
const boundApplyDocumentActions = bindActionByDataset(documentStore, _applyDocumentActions);
|
|
2684
|
-
async function _applyDocumentActions({
|
|
2685
|
-
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions];
|
|
2686
|
-
let projectId, dataset;
|
|
2687
|
-
for (const action of actions)
|
|
2688
|
-
if (action.projectId) {
|
|
2689
|
-
if (projectId || (projectId = action.projectId), action.projectId !== projectId)
|
|
2690
|
-
throw new Error(
|
|
2691
|
-
`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`
|
|
2692
|
-
);
|
|
2693
|
-
if (action.dataset && (dataset || (dataset = action.dataset), action.dataset !== dataset))
|
|
2694
|
-
throw new Error(
|
|
2695
|
-
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`
|
|
2696
|
-
);
|
|
2697
|
-
}
|
|
2698
|
-
if (projectId && projectId !== instance.config.projectId || dataset && dataset !== instance.config.dataset) {
|
|
2699
|
-
const matchedInstance = instance.match({ projectId, dataset });
|
|
2700
|
-
if (!matchedInstance)
|
|
2701
|
-
throw new Error(
|
|
2702
|
-
`Could not find a matching instance for projectId: "${projectId}" and dataset: "${dataset}"`
|
|
2703
|
-
);
|
|
2704
|
-
return boundApplyDocumentActions(matchedInstance, actionOrActions, {
|
|
2705
|
-
disableBatching,
|
|
2706
|
-
transactionId
|
|
2707
|
-
});
|
|
2708
|
-
}
|
|
2977
|
+
async function _applyDocumentActions({ state }, { actions, transactionId = crypto.randomUUID(), disableBatching }) {
|
|
2709
2978
|
const { events } = state.get(), transaction = {
|
|
2710
2979
|
transactionId,
|
|
2711
2980
|
actions,
|
|
@@ -3073,8 +3342,14 @@ const handleIncomingMessage = (event) => {
|
|
|
3073
3342
|
name: "presence",
|
|
3074
3343
|
getInitialState,
|
|
3075
3344
|
initialize: (context) => {
|
|
3076
|
-
const {
|
|
3077
|
-
|
|
3345
|
+
const {
|
|
3346
|
+
instance,
|
|
3347
|
+
state,
|
|
3348
|
+
key: { projectId, dataset }
|
|
3349
|
+
} = context, sessionId = crypto.randomUUID(), client = getClient(instance, {
|
|
3350
|
+
apiVersion: "2022-06-30",
|
|
3351
|
+
projectId,
|
|
3352
|
+
dataset
|
|
3078
3353
|
}), token$ = getTokenState(instance).observable.pipe(distinctUntilChanged()), [incomingEvents$, dispatch] = createBifurTransport({
|
|
3079
3354
|
client,
|
|
3080
3355
|
token$,
|
|
@@ -3116,8 +3391,8 @@ const handleIncomingMessage = (event) => {
|
|
|
3116
3391
|
), getPresence = bindActionByDataset(
|
|
3117
3392
|
presenceStore,
|
|
3118
3393
|
createStateSourceAction({
|
|
3119
|
-
selector: (context) => selectPresence(context.state),
|
|
3120
|
-
onSubscribe: (context) => {
|
|
3394
|
+
selector: (context, _) => selectPresence(context.state),
|
|
3395
|
+
onSubscribe: (context, _) => {
|
|
3121
3396
|
const subscription = context.state.observable.pipe(
|
|
3122
3397
|
map(
|
|
3123
3398
|
(state) => Array.from(state.locations.values()).map((l) => l.userId).filter((id) => !!id)
|
|
@@ -3131,7 +3406,7 @@ const handleIncomingMessage = (event) => {
|
|
|
3131
3406
|
(userId) => getUserState(context.instance, {
|
|
3132
3407
|
userId,
|
|
3133
3408
|
resourceType: "project",
|
|
3134
|
-
projectId: context.
|
|
3409
|
+
projectId: context.key.projectId
|
|
3135
3410
|
}).pipe(filter((v) => !!v))
|
|
3136
3411
|
);
|
|
3137
3412
|
return combineLatest(userObservables);
|
|
@@ -3212,11 +3487,17 @@ const ARCHIVED_RELEASE_STATES = ["archived", "published"], releasesStore = {
|
|
|
3212
3487
|
}, getActiveReleasesState = bindActionByDataset(
|
|
3213
3488
|
releasesStore,
|
|
3214
3489
|
createStateSourceAction({
|
|
3215
|
-
selector: ({ state }) => state.activeReleases
|
|
3490
|
+
selector: ({ state }, _) => state.activeReleases
|
|
3216
3491
|
})
|
|
3217
|
-
), RELEASES_QUERY = "releases::all()", QUERY_PARAMS = {}, subscribeToReleases = ({
|
|
3492
|
+
), RELEASES_QUERY = "releases::all()", QUERY_PARAMS = {}, subscribeToReleases = ({
|
|
3493
|
+
instance,
|
|
3494
|
+
state,
|
|
3495
|
+
key: { projectId, dataset }
|
|
3496
|
+
}) => getClientState(instance, {
|
|
3218
3497
|
apiVersion: "2025-04-10",
|
|
3219
|
-
perspective: "raw"
|
|
3498
|
+
perspective: "raw",
|
|
3499
|
+
projectId,
|
|
3500
|
+
dataset
|
|
3220
3501
|
}).observable.pipe(
|
|
3221
3502
|
switchMap(
|
|
3222
3503
|
(client) => (
|
|
@@ -3244,7 +3525,7 @@ const ARCHIVED_RELEASE_STATES = ["archived", "published"], releasesStore = {
|
|
|
3244
3525
|
function isReleasePerspective(perspective) {
|
|
3245
3526
|
return typeof perspective == "object" && perspective !== null && "releaseName" in perspective;
|
|
3246
3527
|
}
|
|
3247
|
-
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(
|
|
3528
|
+
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(
|
|
3248
3529
|
[selectActiveReleases, selectOptions],
|
|
3249
3530
|
(activeReleases, options) => {
|
|
3250
3531
|
if (!options || !activeReleases) return options;
|
|
@@ -3564,7 +3845,8 @@ function createPreviewQuery(documentIds) {
|
|
|
3564
3845
|
}
|
|
3565
3846
|
const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size && Array.from(a).every((i) => b.has(i)), subscribeToStateAndFetchBatches$1 = ({
|
|
3566
3847
|
state,
|
|
3567
|
-
instance
|
|
3848
|
+
instance,
|
|
3849
|
+
key: { projectId, dataset }
|
|
3568
3850
|
}) => state.observable.pipe(
|
|
3569
3851
|
map(({ subscriptions }) => new Set(Object.keys(subscriptions))),
|
|
3570
3852
|
distinctUntilChanged(isSetEqual$1),
|
|
@@ -3592,14 +3874,18 @@ const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size &&
|
|
|
3592
3874
|
query,
|
|
3593
3875
|
params,
|
|
3594
3876
|
tag: PREVIEW_TAG,
|
|
3595
|
-
perspective: PREVIEW_PERSPECTIVE
|
|
3877
|
+
perspective: PREVIEW_PERSPECTIVE,
|
|
3878
|
+
projectId,
|
|
3879
|
+
dataset
|
|
3596
3880
|
}), subscription = defer(() => getCurrent() === void 0 ? from(
|
|
3597
3881
|
resolveQuery(instance, {
|
|
3598
3882
|
query,
|
|
3599
3883
|
params,
|
|
3600
3884
|
tag: PREVIEW_TAG,
|
|
3601
3885
|
perspective: PREVIEW_PERSPECTIVE,
|
|
3602
|
-
signal: controller.signal
|
|
3886
|
+
signal: controller.signal,
|
|
3887
|
+
projectId,
|
|
3888
|
+
dataset
|
|
3603
3889
|
})
|
|
3604
3890
|
).pipe(switchMap(() => observable)) : observable).pipe(filter((result) => result !== void 0)).subscribe(observer);
|
|
3605
3891
|
return () => {
|
|
@@ -3609,8 +3895,8 @@ const BATCH_DEBOUNCE_TIME$1 = 50, isSetEqual$1 = (a, b) => a.size === b.size &&
|
|
|
3609
3895
|
}),
|
|
3610
3896
|
map(({ ids, data }) => ({
|
|
3611
3897
|
values: processPreviewQuery({
|
|
3612
|
-
projectId
|
|
3613
|
-
dataset
|
|
3898
|
+
projectId,
|
|
3899
|
+
dataset,
|
|
3614
3900
|
ids,
|
|
3615
3901
|
results: data
|
|
3616
3902
|
})
|
|
@@ -3727,7 +4013,8 @@ function processProjectionQuery({ ids, results }) {
|
|
|
3727
4013
|
}
|
|
3728
4014
|
const BATCH_DEBOUNCE_TIME = 50, isSetEqual = (a, b) => a.size === b.size && Array.from(a).every((i) => b.has(i)), subscribeToStateAndFetchBatches = ({
|
|
3729
4015
|
state,
|
|
3730
|
-
instance
|
|
4016
|
+
instance,
|
|
4017
|
+
key: { projectId, dataset }
|
|
3731
4018
|
}) => {
|
|
3732
4019
|
const documentProjections$ = state.observable.pipe(
|
|
3733
4020
|
map((s) => s.documentProjections),
|
|
@@ -3770,12 +4057,16 @@ const BATCH_DEBOUNCE_TIME = 50, isSetEqual = (a, b) => a.size === b.size && Arra
|
|
|
3770
4057
|
const { getCurrent, observable } = getQueryState(instance, {
|
|
3771
4058
|
query,
|
|
3772
4059
|
params,
|
|
4060
|
+
projectId,
|
|
4061
|
+
dataset,
|
|
3773
4062
|
tag: PROJECTION_TAG,
|
|
3774
4063
|
perspective: PROJECTION_PERSPECTIVE
|
|
3775
4064
|
}), subscription = defer(() => getCurrent() === void 0 ? from(
|
|
3776
4065
|
resolveQuery(instance, {
|
|
3777
4066
|
query,
|
|
3778
4067
|
params,
|
|
4068
|
+
projectId,
|
|
4069
|
+
dataset,
|
|
3779
4070
|
tag: PROJECTION_TAG,
|
|
3780
4071
|
perspective: PROJECTION_PERSPECTIVE,
|
|
3781
4072
|
signal: controller.signal
|
|
@@ -3915,12 +4206,12 @@ const _resolveProjection = bindActionByDataset(
|
|
|
3915
4206
|
});
|
|
3916
4207
|
})
|
|
3917
4208
|
)
|
|
3918
|
-
}), getProjectsState = projects.getState, resolveProjects = projects.resolveState,
|
|
4209
|
+
}), getProjectsState = projects.getState, resolveProjects = projects.resolveState, TOKEN_REGEX = /(?:[^\s"]+|"[^"]*")+/g;
|
|
3919
4210
|
function isNegationToken(token) {
|
|
3920
|
-
return typeof token < "u" && token.trim().startsWith(
|
|
4211
|
+
return typeof token < "u" && token.trim().startsWith("-");
|
|
3921
4212
|
}
|
|
3922
4213
|
function isPrefixToken(token) {
|
|
3923
|
-
return typeof token < "u" && token.trim().endsWith(
|
|
4214
|
+
return typeof token < "u" && token.trim().endsWith("*");
|
|
3924
4215
|
}
|
|
3925
4216
|
function isExactMatchToken(token) {
|
|
3926
4217
|
return !!token && token.length >= 2 && token.startsWith('"') && token.endsWith('"');
|
|
@@ -3935,7 +4226,7 @@ function createGroqSearchFilter(query) {
|
|
|
3935
4226
|
return finalIncrementalToken !== void 0 && !isPrefixToken(finalIncrementalToken) && processedTokens.splice(
|
|
3936
4227
|
finalIncrementalTokenIndex,
|
|
3937
4228
|
1,
|
|
3938
|
-
`${finalIncrementalToken}
|
|
4229
|
+
`${finalIncrementalToken}*`
|
|
3939
4230
|
), `[@] match text::query("${processedTokens.join(" ").replace(/"/g, '\\"')}")`;
|
|
3940
4231
|
}
|
|
3941
4232
|
function defineIntent(intent) {
|
|
@@ -3996,7 +4287,7 @@ function getCorsErrorProjectId(error) {
|
|
|
3996
4287
|
const projMatch = (error.message || "").match(/manage\/project\/([^/?#]+)/);
|
|
3997
4288
|
return projMatch ? projMatch[1] : null;
|
|
3998
4289
|
}
|
|
3999
|
-
var version = "2.
|
|
4290
|
+
var version = "2.6.0";
|
|
4000
4291
|
const CORE_SDK_VERSION = getEnv("PKG_VERSION") || `${version}-development`;
|
|
4001
4292
|
export {
|
|
4002
4293
|
AuthStateType,
|
|
@@ -4007,7 +4298,7 @@ export {
|
|
|
4007
4298
|
agentTransform,
|
|
4008
4299
|
agentTranslate,
|
|
4009
4300
|
applyDocumentActions,
|
|
4010
|
-
|
|
4301
|
+
configureLogging,
|
|
4011
4302
|
createDatasetHandle,
|
|
4012
4303
|
createDocument,
|
|
4013
4304
|
createDocumentHandle,
|
|
@@ -4015,7 +4306,6 @@ export {
|
|
|
4015
4306
|
createGroqSearchFilter,
|
|
4016
4307
|
createProjectHandle,
|
|
4017
4308
|
createSanityInstance,
|
|
4018
|
-
datasetSource,
|
|
4019
4309
|
defineIntent,
|
|
4020
4310
|
deleteDocument,
|
|
4021
4311
|
destroyController,
|
|
@@ -4057,12 +4347,14 @@ export {
|
|
|
4057
4347
|
getUsersKey,
|
|
4058
4348
|
getUsersState,
|
|
4059
4349
|
handleAuthCallback,
|
|
4350
|
+
isCanvasSource,
|
|
4351
|
+
isDatasetSource,
|
|
4352
|
+
isMediaLibrarySource,
|
|
4060
4353
|
isProjectUserNotFoundClientError,
|
|
4061
4354
|
joinPaths,
|
|
4062
4355
|
jsonMatch2 as jsonMatch,
|
|
4063
4356
|
loadMoreUsers,
|
|
4064
4357
|
logout,
|
|
4065
|
-
mediaLibrarySource,
|
|
4066
4358
|
observeOrganizationVerificationState,
|
|
4067
4359
|
parseQueryKey,
|
|
4068
4360
|
parseUsersKey,
|