@sanity/sdk 3.0.0-rc.1 → 3.0.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 (128) hide show
  1. package/README.md +12 -0
  2. package/dist/_exports/_internal.d.ts +71 -3
  3. package/dist/_exports/_internal.d.ts.map +1 -1
  4. package/dist/_exports/_internal.js +332 -24
  5. package/dist/_exports/_internal.js.map +1 -1
  6. package/dist/_exports/agent.d.ts +65 -0
  7. package/dist/_exports/agent.d.ts.map +1 -0
  8. package/dist/_exports/agent.js +71 -0
  9. package/dist/_exports/agent.js.map +1 -0
  10. package/dist/_exports/comlink.d.ts +118 -0
  11. package/dist/_exports/comlink.d.ts.map +1 -0
  12. package/dist/_exports/comlink.js +69 -0
  13. package/dist/_exports/comlink.js.map +1 -0
  14. package/dist/authStore-C0o1NOxW.d.ts +3165 -0
  15. package/dist/authStore-C0o1NOxW.d.ts.map +1 -0
  16. package/dist/authStore-CtE9bLHW.js +754 -0
  17. package/dist/authStore-CtE9bLHW.js.map +1 -0
  18. package/dist/clientStore-BtE2-q2i.js +90 -0
  19. package/dist/clientStore-BtE2-q2i.js.map +1 -0
  20. package/dist/createSanityInstance-DUI1bGIg.d.ts +278 -0
  21. package/dist/createSanityInstance-DUI1bGIg.d.ts.map +1 -0
  22. package/dist/createStateSourceAction-BOKtqFNm.js +300 -0
  23. package/dist/createStateSourceAction-BOKtqFNm.js.map +1 -0
  24. package/dist/getEnv-D5RGkQLu.js +8 -0
  25. package/dist/getEnv-D5RGkQLu.js.map +1 -0
  26. package/dist/getNodeState-CR3__B2D.js +67 -0
  27. package/dist/getNodeState-CR3__B2D.js.map +1 -0
  28. package/dist/index.d.ts +3 -2335
  29. package/dist/index.js +4048 -3878
  30. package/dist/index.js.map +1 -1
  31. package/dist/logger-DAtJb5RQ.js +173 -0
  32. package/dist/logger-DAtJb5RQ.js.map +1 -0
  33. package/dist/object-CxXlSVg7.js +83 -0
  34. package/dist/object-CxXlSVg7.js.map +1 -0
  35. package/dist/queryStore-C7yMHPUI.js +1346 -0
  36. package/dist/queryStore-C7yMHPUI.js.map +1 -0
  37. package/dist/rolldown-runtime-vyAXikos.js +9 -0
  38. package/dist/setCleanupTimeout-BiM6q1KF.js +31 -0
  39. package/dist/setCleanupTimeout-BiM6q1KF.js.map +1 -0
  40. package/dist/telemetryManager-UIEiMtC0.js +127 -0
  41. package/dist/telemetryManager-UIEiMtC0.js.map +1 -0
  42. package/dist/version-D8RP_Q59.js +9 -0
  43. package/dist/version-D8RP_Q59.js.map +1 -0
  44. package/package.json +11 -17
  45. package/src/_exports/_internal.ts +1 -0
  46. package/src/_exports/agent.ts +19 -0
  47. package/src/_exports/comlink.ts +16 -0
  48. package/src/_exports/index.ts +45 -90
  49. package/src/applications/applications.test-d.ts +5 -0
  50. package/src/applications/applications.ts +5 -2
  51. package/src/auth/authMode.test.ts +0 -25
  52. package/src/auth/authMode.ts +3 -6
  53. package/src/auth/authStore.test.ts +1 -1
  54. package/src/auth/authStore.ts +0 -1
  55. package/src/comments/addonDatasetStore.test.ts +215 -0
  56. package/src/comments/addonDatasetStore.ts +249 -0
  57. package/src/comments/buildCommentThreads.test.ts +83 -0
  58. package/src/comments/buildCommentThreads.ts +54 -0
  59. package/src/comments/commentActions.test.ts +540 -0
  60. package/src/comments/commentActions.ts +555 -0
  61. package/src/comments/commentFieldPath.test.ts +40 -0
  62. package/src/comments/commentFieldPath.ts +44 -0
  63. package/src/comments/commentsConstants.ts +80 -0
  64. package/src/comments/commentsStore.test.ts +419 -0
  65. package/src/comments/commentsStore.ts +483 -0
  66. package/src/comments/normalizeComment.test.ts +116 -0
  67. package/src/comments/normalizeComment.ts +45 -0
  68. package/src/comments/observeComments.test.ts +193 -0
  69. package/src/comments/observeComments.ts +113 -0
  70. package/src/comments/reducers.test.ts +277 -0
  71. package/src/comments/reducers.ts +316 -0
  72. package/src/comments/types.ts +249 -0
  73. package/src/comments/weakenReferences.test.ts +47 -0
  74. package/src/comments/weakenReferences.ts +29 -0
  75. package/src/config/sanityConfig.ts +3 -75
  76. package/src/datasets/datasets.test.ts +44 -9
  77. package/src/datasets/datasets.ts +24 -23
  78. package/src/document/applyDocumentActions.test.ts +8 -12
  79. package/src/document/documentConstants.ts +17 -0
  80. package/src/document/documentStore.test.ts +173 -19
  81. package/src/document/documentStore.ts +36 -5
  82. package/src/document/patchOperations.test.ts +27 -0
  83. package/src/document/patchOperations.ts +10 -1
  84. package/src/document/permissions.test.ts +3 -1
  85. package/src/document/reducers.test.ts +357 -0
  86. package/src/document/reducers.ts +125 -27
  87. package/src/document/util.test.ts +46 -0
  88. package/src/document/util.ts +37 -1
  89. package/src/favorites/favorites.test.ts +64 -24
  90. package/src/favorites/favorites.ts +73 -22
  91. package/src/installations/installations.test-d.ts +5 -0
  92. package/src/installations/installations.ts +4 -0
  93. package/src/presence/bifurTransport.test.ts +155 -3
  94. package/src/presence/bifurTransport.ts +79 -12
  95. package/src/presence/paths.test.ts +70 -0
  96. package/src/presence/paths.ts +41 -0
  97. package/src/presence/presenceStore.test.ts +822 -2
  98. package/src/presence/presenceStore.ts +518 -102
  99. package/src/presence/types.ts +175 -3
  100. package/src/preview/types.ts +0 -25
  101. package/src/projection/projectionStore.test.ts +0 -8
  102. package/src/projection/types.ts +0 -8
  103. package/src/query/queryStore.ts +7 -6
  104. package/src/store/createActionBinder.test.ts +51 -29
  105. package/src/store/createActionBinder.ts +16 -62
  106. package/src/store/createSanityInstance.test.ts +0 -68
  107. package/src/store/createSanityInstance.ts +0 -63
  108. package/src/store/createStateSourceAction.ts +1 -1
  109. package/src/store/fetcherStore.test.ts +23 -1
  110. package/src/store/fetcherStore.ts +3 -3
  111. package/src/users/reducers.test.ts +0 -3
  112. package/dist/_chunks-dts/createGroqSearchFilter.d.ts +0 -1147
  113. package/dist/_chunks-dts/createGroqSearchFilter.d.ts.map +0 -1
  114. package/dist/_chunks-es/_internal.js +0 -134
  115. package/dist/_chunks-es/_internal.js.map +0 -1
  116. package/dist/_chunks-es/createGroqSearchFilter.js +0 -1864
  117. package/dist/_chunks-es/createGroqSearchFilter.js.map +0 -1
  118. package/dist/_chunks-es/telemetryManager.js +0 -93
  119. package/dist/_chunks-es/telemetryManager.js.map +0 -1
  120. package/dist/index.d.ts.map +0 -1
  121. package/src/applications/userApplications.test.ts +0 -126
  122. package/src/applications/userApplications.ts +0 -261
  123. package/src/preview/getPreviewState.test.ts +0 -137
  124. package/src/preview/getPreviewState.ts +0 -69
  125. package/src/preview/resolvePreview.test.ts +0 -64
  126. package/src/preview/resolvePreview.ts +0 -43
  127. package/src/utils/createFetcherStore.test.ts +0 -171
  128. package/src/utils/createFetcherStore.ts +0 -284
@@ -1,7 +1,6 @@
1
1
  import {type SanityConfig} from '../config/sanityConfig'
2
2
  import {insecureRandomId} from '../utils/ids'
3
3
  import {createLogger, type InstanceContext} from '../utils/logger'
4
- import {pickProperties} from '../utils/object'
5
4
 
6
5
  /**
7
6
  * Represents a Sanity.io resource instance with its own configuration and lifecycle
@@ -38,36 +37,12 @@ export interface SanityInstance {
38
37
  * @returns Function to unsubscribe the callback
39
38
  */
40
39
  onDispose(cb: () => void): () => void
41
-
42
- /**
43
- * Gets the parent instance in the hierarchy
44
- * @returns Parent instance or undefined if this is the root
45
- * @deprecated The parent/child instance hierarchy is deprecated. Use a single SanityInstance instead.
46
- */
47
- getParent(): SanityInstance | undefined
48
-
49
- /**
50
- * Creates a child instance with merged configuration
51
- * @param config - Configuration to merge with parent values
52
- * @deprecated The parent/child instance hierarchy is deprecated. Use a single SanityInstance instead.
53
- */
54
- createChild(config: SanityConfig): SanityInstance
55
-
56
- /**
57
- * Traverses the instance hierarchy to find the first instance whose configuration
58
- * matches the given target config using a shallow comparison.
59
- * @param targetConfig - A partial configuration object containing key-value pairs to match.
60
- * @returns The first matching instance or undefined if no match is found.
61
- * @deprecated The parent/child instance hierarchy is deprecated. Use a single SanityInstance instead.
62
- */
63
- match(targetConfig: Partial<SanityConfig>): SanityInstance | undefined
64
40
  }
65
41
 
66
42
  /**
67
43
  * Creates a new Sanity resource instance
68
44
  * @param config - Configuration for the instance (optional)
69
45
  * @returns A configured SanityInstance
70
- * @remarks When creating child instances, configurations are merged with parent values
71
46
  *
72
47
  * @public
73
48
  */
@@ -101,7 +76,6 @@ export function createSanityInstance(config: SanityConfig = {}): SanityInstance
101
76
  perspective: config.perspective,
102
77
  hasStudioConfig: !!config.studio,
103
78
  hasStudioTokenSource: !!config.studio?.auth?.token,
104
- legacyStudioMode: config.studioMode?.enabled,
105
79
  hasAuthProviders: !!config.auth?.providers,
106
80
  hasAuthToken: !!config.auth?.token,
107
81
  })
@@ -131,43 +105,6 @@ export function createSanityInstance(config: SanityConfig = {}): SanityInstance
131
105
  disposeListeners.delete(listenerId)
132
106
  }
133
107
  },
134
- getParent: () => undefined,
135
- createChild: (next) => {
136
- logger.debug('Creating child instance', {
137
- parentInstanceId: instanceId.slice(0, 8),
138
- overridingProjectId: !!next.projectId,
139
- overridingDataset: !!next.dataset,
140
- overridingAuth: !!next.auth,
141
- })
142
- const child = Object.assign(
143
- createSanityInstance({
144
- ...config,
145
- ...next,
146
- ...(config.auth === next.auth
147
- ? config.auth
148
- : config.auth && next.auth && {auth: {...config.auth, ...next.auth}}),
149
- }),
150
- {getParent: () => instance},
151
- )
152
- logger.trace('Child instance created', {
153
- internal: true,
154
- childInstanceId: child.instanceId.slice(0, 8),
155
- })
156
- return child
157
- },
158
- match: (targetConfig) => {
159
- if (
160
- Object.entries(pickProperties(targetConfig, ['auth', 'projectId', 'dataset'])).every(
161
- ([key, value]) => config[key as keyof SanityConfig] === value,
162
- )
163
- ) {
164
- return instance
165
- }
166
-
167
- const parent = instance.getParent()
168
- if (parent) return parent.match(targetConfig)
169
- return undefined
170
- },
171
108
  }
172
109
 
173
110
  return instance
@@ -155,7 +155,7 @@ interface StateSourceOptions<TState, TParams extends unknown[], TReturn, TKey> {
155
155
  * // ...
156
156
  * })
157
157
  *
158
- * const getDocument = bindActionByDataset(
158
+ * const getDocument = bindActionByResource(
159
159
  * documentStore,
160
160
  * createStateSourceAction(({state}, documentId: string) => state.documents[documentId])
161
161
  * )
@@ -1,4 +1,4 @@
1
- import {NEVER, of, startWith, Subject, throwError} from 'rxjs'
1
+ import {concatWith, NEVER, of, startWith, Subject, throwError} from 'rxjs'
2
2
  import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
3
3
 
4
4
  import {createSanityInstance, type SanityInstance} from './createSanityInstance'
@@ -65,6 +65,28 @@ describe('fetcherStore', () => {
65
65
  unsub3()
66
66
  })
67
67
 
68
+ it('revalidates a stale entry on re-subscribe when the fetch source does not complete', async () => {
69
+ let n = 0
70
+ const fetchSpy = vi.fn(() => of(++n).pipe(concatWith(NEVER)))
71
+ const fetcher = defineFetcher<[], number>({
72
+ name: 'swr-non-completing',
73
+ staleTime: 0,
74
+ getKey: () => 'k',
75
+ fetch: () => fetchSpy,
76
+ })
77
+
78
+ const first = fetcher.getState(instance)
79
+ const unsub1 = first.subscribe()
80
+ await vi.waitFor(() => expect(first.getCurrent().data).toBe(1))
81
+ unsub1()
82
+
83
+ const second = fetcher.getState(instance)
84
+ const unsub2 = second.subscribe()
85
+ await vi.waitFor(() => expect(fetchSpy).toHaveBeenCalledTimes(2))
86
+ expect(second.getCurrent().data).toBe(2)
87
+ unsub2()
88
+ })
89
+
68
90
  it('invalidate refetches an actively-subscribed entry', async () => {
69
91
  let n = 0
70
92
  const fetcher = defineFetcher<[], number>({
@@ -1,4 +1,4 @@
1
- import {filter, firstValueFrom, map, type Observable, Subscription} from 'rxjs'
1
+ import {filter, firstValueFrom, map, type Observable, Subscription, take} from 'rxjs'
2
2
 
3
3
  import {setCleanupInterval, setCleanupTimeout} from '../utils/setCleanupTimeout'
4
4
  import {bindActionGlobally} from './createActionBinder'
@@ -490,6 +490,7 @@ const startFetch = (
490
490
  sub.add(
491
491
  def
492
492
  .fetch(instance)(...params)
493
+ .pipe(take(1))
493
494
  .subscribe({
494
495
  next: (data) => {
495
496
  const current = state.get().entries[key]
@@ -659,8 +660,7 @@ export function defineFetcher<TParams extends unknown[], TData>(
659
660
  }))
660
661
  startInterval(state, def, instance, params, key)
661
662
 
662
- // Defer the fetch decision off the subscribe call stack (see the
663
- // `delay(0)` precedent in the legacy fetcher store) to avoid a
663
+ // Defer the fetch decision off the subscribe call stack to avoid a
664
664
  // re-entrant cache write during subscription setup.
665
665
  queueMicrotask(() => {
666
666
  const entry = state.get().entries[key]
@@ -25,9 +25,6 @@ describe('Users Reducers', () => {
25
25
  isDisposed: () => false,
26
26
  dispose: () => {},
27
27
  onDispose: () => () => {},
28
- getParent: () => undefined,
29
- createChild: (_config) => mockInstance,
30
- match: () => undefined,
31
28
  }
32
29
 
33
30
  const sampleOptions: GetUsersOptions = {