@sanity/sdk 2.4.0 → 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.
Files changed (47) hide show
  1. package/dist/index.d.ts +35 -90
  2. package/dist/index.js +237 -111
  3. package/dist/index.js.map +1 -1
  4. package/package.json +9 -8
  5. package/src/auth/authStore.test.ts +13 -13
  6. package/src/auth/refreshStampedToken.test.ts +16 -16
  7. package/src/auth/subscribeToStateAndFetchCurrentUser.test.ts +6 -6
  8. package/src/auth/subscribeToStorageEventsAndSetToken.test.ts +4 -4
  9. package/src/comlink/controller/actions/destroyController.test.ts +2 -2
  10. package/src/comlink/controller/actions/getOrCreateChannel.test.ts +6 -6
  11. package/src/comlink/controller/actions/getOrCreateController.test.ts +5 -5
  12. package/src/comlink/controller/actions/getOrCreateController.ts +1 -1
  13. package/src/comlink/controller/actions/releaseChannel.test.ts +3 -2
  14. package/src/comlink/controller/comlinkControllerStore.test.ts +4 -4
  15. package/src/comlink/node/actions/getOrCreateNode.test.ts +7 -7
  16. package/src/comlink/node/actions/releaseNode.test.ts +2 -2
  17. package/src/comlink/node/comlinkNodeStore.test.ts +4 -3
  18. package/src/config/sanityConfig.ts +8 -3
  19. package/src/document/actions.ts +11 -7
  20. package/src/document/applyDocumentActions.test.ts +9 -6
  21. package/src/document/applyDocumentActions.ts +9 -49
  22. package/src/document/documentStore.test.ts +128 -115
  23. package/src/document/documentStore.ts +40 -10
  24. package/src/document/permissions.test.ts +9 -9
  25. package/src/document/permissions.ts +17 -7
  26. package/src/document/processActions.test.ts +248 -0
  27. package/src/document/processActions.ts +173 -0
  28. package/src/document/reducers.ts +13 -6
  29. package/src/presence/presenceStore.ts +13 -7
  30. package/src/preview/previewStore.test.ts +10 -2
  31. package/src/preview/previewStore.ts +2 -1
  32. package/src/preview/subscribeToStateAndFetchBatches.test.ts +8 -5
  33. package/src/preview/subscribeToStateAndFetchBatches.ts +9 -3
  34. package/src/projection/projectionStore.test.ts +18 -2
  35. package/src/projection/projectionStore.ts +2 -1
  36. package/src/projection/subscribeToStateAndFetchBatches.test.ts +6 -5
  37. package/src/projection/subscribeToStateAndFetchBatches.ts +9 -3
  38. package/src/releases/getPerspectiveState.ts +2 -2
  39. package/src/releases/releasesStore.ts +10 -4
  40. package/src/store/createActionBinder.test.ts +8 -6
  41. package/src/store/createActionBinder.ts +44 -29
  42. package/src/store/createStateSourceAction.test.ts +12 -11
  43. package/src/store/createStateSourceAction.ts +6 -6
  44. package/src/store/createStoreInstance.test.ts +29 -16
  45. package/src/store/createStoreInstance.ts +6 -5
  46. package/src/store/defineStore.test.ts +1 -1
  47. package/src/store/defineStore.ts +12 -7
package/dist/index.d.ts CHANGED
@@ -105,12 +105,18 @@ interface DatasetHandle<TDataset extends string = string, TProjectId extends str
105
105
  /**
106
106
  * Identifies a specific document type within a Sanity dataset and project.
107
107
  * Includes `projectId`, `dataset`, and `documentType`.
108
- * Optionally includes a `documentId`, useful for referencing a specific document type context, potentially without a specific document ID.
108
+ * Optionally includes a `documentId` and `liveEdit` flag.
109
109
  * @public
110
110
  */
111
111
  interface DocumentTypeHandle<TDocumentType extends string = string, TDataset extends string = string, TProjectId extends string = string> extends DatasetHandle<TDataset, TProjectId> {
112
112
  documentId?: string;
113
113
  documentType: TDocumentType;
114
+ /**
115
+ * Indicates whether this document uses liveEdit mode.
116
+ * When `true`, the document does not use the draft/published model and edits are applied directly to the document.
117
+ * @see https://www.sanity.io/docs/content-lake/drafts#ca0663a8f002
118
+ */
119
+ liveEdit?: boolean;
114
120
  }
115
121
  /**
116
122
  * Uniquely identifies a specific document within a Sanity dataset and project.
@@ -308,29 +314,6 @@ declare enum AuthStateType {
308
314
  * Represents a store action that has been bound to a specific store instance
309
315
  */
310
316
  type BoundStoreAction<_TState, TParams extends unknown[], TReturn> = (instance: SanityInstance, ...params: TParams) => TReturn;
311
- /**
312
- * Creates an action binder function that uses the provided key function
313
- * to determine how store instances are shared between Sanity instances
314
- *
315
- * @param keyFn - Function that generates a key from a Sanity config
316
- * @returns A function that binds store actions to Sanity instances
317
- *
318
- * @remarks
319
- * Action binders determine how store instances are shared across multiple
320
- * Sanity instances. The key function determines which instances share state.
321
- *
322
- * @example
323
- * ```ts
324
- * // Create a custom binder that uses a tenant ID for isolation
325
- * const bindActionByTenant = createActionBinder(config => config.tenantId || 'default')
326
- *
327
- * // Use the custom binder with a store definition
328
- * const getTenantUsers = bindActionByTenant(
329
- * userStore,
330
- * ({state}) => state.get().users
331
- * )
332
- * ```
333
- */
334
317
  /**
335
318
  * Represents the various states the authentication can be in.
336
319
  *
@@ -414,10 +397,6 @@ declare const getCurrentUserState: BoundStoreAction<AuthStoreState, [], StateSou
414
397
  * @public
415
398
  */
416
399
  declare const getTokenState: BoundStoreAction<AuthStoreState, [], StateSource<string | null>>;
417
- /**
418
- * @internal
419
- */
420
-
421
400
  /**
422
401
  * @public
423
402
  */
@@ -448,10 +427,6 @@ declare const setAuthToken: BoundStoreAction<AuthStoreState, [token: string | nu
448
427
  interface OrgVerificationResult {
449
428
  error: string | null;
450
429
  }
451
- /**
452
- * Compares a project's actual organization ID with the expected organization ID.
453
- * @public
454
- */
455
430
  /**
456
431
  * Creates an observable that emits the organization verification state for a given instance.
457
432
  * It combines the dashboard organization ID (from auth context) with the
@@ -826,21 +801,6 @@ declare function discardDocument<TDocumentType extends string = string, TDataset
826
801
  * Documents that don't exist have a `null` value.
827
802
  */
828
803
  type DocumentSet<TDocument extends SanityDocument$2 = SanityDocument$2> = { [TDocumentId in string]?: TDocument | null };
829
- /**
830
- * Implements ID generation:
831
- *
832
- * A create mutation creates a new document. It takes the literal document
833
- * content as its argument. The rules for the new document's identifier are as
834
- * follows:
835
- *
836
- * - If the `_id` attribute is missing, then a new, random, unique ID is
837
- * generated.
838
- * - If the `_id` attribute is present but ends with `.`, then it is used as a
839
- * prefix for a new, random, unique ID.
840
- * - If the _id attribute is present, it is used as-is.
841
- *
842
- * [- source](https://www.sanity.io/docs/http-mutations#c732f27330a4)
843
- */
844
804
  /** @beta */
845
805
  interface ActionsResult<TDocument extends SanityDocument$3 = SanityDocument$3> {
846
806
  transactionId: string;
@@ -856,6 +816,10 @@ interface ActionsResult<TDocument extends SanityDocument$3 = SanityDocument$3> {
856
816
  }
857
817
  /** @beta */
858
818
  interface ApplyDocumentActionsOptions {
819
+ /**
820
+ * List of actions to apply.
821
+ */
822
+ actions: DocumentAction[];
859
823
  /**
860
824
  * Optionally provide an ID to be used as this transaction ID
861
825
  */
@@ -866,9 +830,9 @@ interface ApplyDocumentActionsOptions {
866
830
  disableBatching?: boolean;
867
831
  }
868
832
  /** @beta */
869
- declare function applyDocumentActions<TDocumentType extends string = string, TDataset extends string = string, TProjectId extends string = string>(instance: SanityInstance, action: DocumentAction<TDocumentType, TDataset, TProjectId> | DocumentAction<TDocumentType, TDataset, TProjectId>[], options?: ApplyDocumentActionsOptions): Promise<ActionsResult<SanityDocument$3<TDocumentType, `${TProjectId}.${TDataset}`>>>;
833
+ declare function applyDocumentActions<TDocumentType extends string = string, TDataset extends string = string, TProjectId extends string = string>(instance: SanityInstance, options: ApplyDocumentActionsOptions): Promise<ActionsResult<SanityDocument$3<TDocumentType, `${TProjectId}.${TDataset}`>>>;
870
834
  /** @beta */
871
- declare function applyDocumentActions(instance: SanityInstance, action: DocumentAction | DocumentAction[], options?: ApplyDocumentActionsOptions): Promise<ActionsResult>;
835
+ declare function applyDocumentActions(instance: SanityInstance, options: ApplyDocumentActionsOptions): Promise<ActionsResult>;
872
836
  /**
873
837
  * Represents a reactive state source that provides synchronized access to store data
874
838
  *
@@ -942,11 +906,9 @@ interface SelectorContext<TState> {
942
906
  * @public
943
907
  */
944
908
  type Selector<TState, TParams extends unknown[], TReturn> = (context: SelectorContext<TState>, ...params: TParams) => TReturn;
945
- /**
946
- * Configuration options for creating a state source action
947
- */
948
909
  type ActionMap = {
949
910
  create: 'sanity.action.document.version.create';
911
+ createLiveEdit: 'sanity.action.document.create';
950
912
  discard: 'sanity.action.document.version.discard';
951
913
  unpublish: 'sanity.action.document.unpublish';
952
914
  delete: 'sanity.action.document.delete';
@@ -961,6 +923,10 @@ type HttpAction = {
961
923
  actionType: ActionMap['create'];
962
924
  publishedId: string;
963
925
  attributes: SanityDocumentLike;
926
+ } | {
927
+ actionType: ActionMap['createLiveEdit'];
928
+ publishedId: string;
929
+ attributes: SanityDocumentLike;
964
930
  } | {
965
931
  actionType: ActionMap['discard'];
966
932
  versionId: string;
@@ -1218,11 +1184,6 @@ TTail extends readonly (string | number)[] ? TTail : []> : never;
1218
1184
  * @beta
1219
1185
  */
1220
1186
  type JsonMatch<TDocument, TPath extends string> = DeepGet<TDocument, PathParts<TPath>>;
1221
- /**
1222
- * Recursively traverse a value. When an array is encountered, ensure that
1223
- * each object item has a _key property. Memoized such that sub-objects that
1224
- * have not changed aren't re-computed.
1225
- */
1226
1187
  interface SharedListener {
1227
1188
  events: Observable<ListenEvent<SanityDocument$1>>;
1228
1189
  dispose: () => void;
@@ -1291,10 +1252,13 @@ declare function resolveDocument<TDocumentType extends string = string, TDataset
1291
1252
  declare function resolveDocument<TData extends SanityDocument$3>(instance: SanityInstance, docHandle: DocumentHandle<string, string, string>): Promise<TData | null>;
1292
1253
  /** @beta */
1293
1254
  declare const getDocumentSyncStatus: BoundStoreAction<DocumentStoreState, [doc: DocumentHandle<string, string, string>], StateSource<boolean | undefined>>;
1255
+ type PermissionsStateOptions = {
1256
+ actions: DocumentAction[];
1257
+ };
1294
1258
  /** @beta */
1295
- declare const getPermissionsState: BoundStoreAction<DocumentStoreState, [DocumentAction | DocumentAction[]], StateSource<DocumentPermissionsResult | undefined>>;
1259
+ declare const getPermissionsState: BoundStoreAction<DocumentStoreState, [PermissionsStateOptions], StateSource<DocumentPermissionsResult | undefined>>;
1296
1260
  /** @beta */
1297
- declare const resolvePermissions: BoundStoreAction<DocumentStoreState, [actions: DocumentAction | DocumentAction[]], Promise<DocumentPermissionsResult>>;
1261
+ declare const resolvePermissions: BoundStoreAction<DocumentStoreState, [options: PermissionsStateOptions], Promise<DocumentPermissionsResult>>;
1298
1262
  /** @beta */
1299
1263
  declare const subscribeDocumentEvents: BoundStoreAction<DocumentStoreState, [eventHandler: (e: DocumentEvent) => void], () => void>;
1300
1264
  /**
@@ -1431,8 +1395,6 @@ interface UserPresence {
1431
1395
  locations: PresenceLocation[];
1432
1396
  sessionId: string;
1433
1397
  }
1434
- /** @public */
1435
-
1436
1398
  /** @public */
1437
1399
  type TransportEvent = RollCallEvent | StateEvent | DisconnectEvent;
1438
1400
  /** @public */
@@ -1456,7 +1418,6 @@ interface DisconnectEvent {
1456
1418
  sessionId: string;
1457
1419
  timestamp: string;
1458
1420
  }
1459
- /** @public */
1460
1421
  type PresenceStoreState = {
1461
1422
  locations: Map<string, {
1462
1423
  userId: string;
@@ -1467,7 +1428,10 @@ type PresenceStoreState = {
1467
1428
  /** @public */
1468
1429
 
1469
1430
  /** @public */
1470
- declare const getPresence: BoundStoreAction<PresenceStoreState, [], StateSource<UserPresence[]>>;
1431
+ declare const getPresence: BoundStoreAction<PresenceStoreState, [((object & {
1432
+ projectId?: string;
1433
+ dataset?: string;
1434
+ }) | undefined)?, ...unknown[]], StateSource<UserPresence[]>>;
1471
1435
  /**
1472
1436
  * Represents a media asset in a preview.
1473
1437
  *
@@ -1542,9 +1506,6 @@ declare function getPreviewState<TResult extends object>(instance: SanityInstanc
1542
1506
  * @beta
1543
1507
  */
1544
1508
  declare function getPreviewState(instance: SanityInstance, options: GetPreviewStateOptions): StateSource<ValuePending<PreviewValue>>;
1545
- /**
1546
- * @beta
1547
- */
1548
1509
  /**
1549
1510
  * @beta
1550
1511
  */
@@ -1587,9 +1548,6 @@ declare function getProjectionState<TData extends object>(instance: SanityInstan
1587
1548
  * @beta
1588
1549
  */
1589
1550
  declare function getProjectionState(instance: SanityInstance, options: ProjectionOptions): StateSource<ProjectionValuePending<Record<string, unknown>> | undefined>;
1590
- /**
1591
- * @beta
1592
- */
1593
1551
  /** @beta */
1594
1552
  declare function resolveProjection<TProjection extends string = string, TDocumentType extends string = string, TDataset extends string = string, TProjectId extends string = string>(instance: SanityInstance, options: ProjectionOptions<TProjection, TDocumentType, TDataset, TProjectId>): Promise<ProjectionValuePending<SanityProjectionResult<TProjection, TDocumentType, `${TProjectId}.${TDataset}`>>>;
1595
1553
  /** @beta */
@@ -1689,7 +1647,10 @@ interface ReleasesStoreState {
1689
1647
  * Get the active releases from the store.
1690
1648
  * @internal
1691
1649
  */
1692
- declare const getActiveReleasesState: BoundStoreAction<ReleasesStoreState, [], StateSource<ReleaseDocument[] | undefined>>;
1650
+ declare const getActiveReleasesState: BoundStoreAction<ReleasesStoreState, [((object & {
1651
+ projectId?: string;
1652
+ dataset?: string;
1653
+ }) | undefined)?, ...unknown[]], StateSource<ReleaseDocument[] | undefined>>;
1693
1654
  /**
1694
1655
  * Provides a subscribable state source for a "perspective" for the Sanity client,
1695
1656
  * which is used to fetch documents as though certain Content Releases are active.
@@ -1702,7 +1663,10 @@ declare const getActiveReleasesState: BoundStoreAction<ReleasesStoreState, [], S
1702
1663
  *
1703
1664
  * @public
1704
1665
  */
1705
- declare const getPerspectiveState: BoundStoreAction<ReleasesStoreState, [options?: PerspectiveHandle | undefined], StateSource<string[] | "raw" | "previewDrafts" | "published" | "drafts" | undefined>>;
1666
+ declare const getPerspectiveState: BoundStoreAction<ReleasesStoreState, [_?: (PerspectiveHandle & {
1667
+ projectId?: string;
1668
+ dataset?: string;
1669
+ }) | undefined], StateSource<string[] | "raw" | "previewDrafts" | "published" | "drafts" | undefined>>;
1706
1670
  /** @internal */
1707
1671
  declare const getUsersKey: (instance: SanityInstance, {
1708
1672
  resourceType,
@@ -1807,25 +1771,6 @@ interface FetcherStore<TParams extends unknown[], TData> {
1807
1771
  getState: BoundStoreAction<FetcherStoreState<TParams, TData>, TParams, StateSource<TData | undefined>>;
1808
1772
  resolveState: BoundStoreAction<FetcherStoreState<TParams, TData>, TParams, Promise<TData>>;
1809
1773
  }
1810
- /**
1811
- * Creates a store from a function that returns an observable that fetches data
1812
- * that supports parameterized state caching.
1813
- *
1814
- * This function creates a resource store keyed by parameter values (using the
1815
- * provided `getKey` function) and returns a state source (via `getState`)
1816
- * that components can subscribe to. When a new subscription is added, and if
1817
- * enough time has passed since the last fetch (controlled by
1818
- * `fetchThrottleInternal`), it invokes the observable factory (via
1819
- * `getObservable`) to fetch fresh data. The data is stored in state and can be
1820
- * accessed reactively.
1821
- *
1822
- * Additionally, the store provides a `resolveState` function that returns a
1823
- * Promise resolving with the next non-undefined value from the state source.
1824
- *
1825
- * State expiration is implemented: after the last subscription for a key is
1826
- * removed, its state is cleared after `stateExpirationDelay` ms, causing
1827
- * components to suspend until fresh data is fetched.
1828
- */
1829
1774
  /**
1830
1775
  * Creates a GROQ search filter string (`[@] match text::query("...")`)
1831
1776
  * from a raw search query string.