@sanity/sdk 2.18.0 → 3.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/_chunks-dts/createGroqSearchFilter.d.ts +233 -12
  2. package/dist/_chunks-dts/createGroqSearchFilter.d.ts.map +1 -1
  3. package/dist/_chunks-es/_internal.js +1 -0
  4. package/dist/_chunks-es/_internal.js.map +1 -1
  5. package/dist/_chunks-es/createGroqSearchFilter.js +284 -6
  6. package/dist/_chunks-es/createGroqSearchFilter.js.map +1 -1
  7. package/dist/_chunks-es/telemetryManager.js +2 -2
  8. package/dist/_exports/_internal.d.ts +2 -2
  9. package/dist/_exports/_internal.d.ts.map +1 -1
  10. package/dist/_exports/_internal.js +3 -1
  11. package/dist/index.d.ts +599 -185
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +492 -337
  14. package/dist/index.js.map +1 -1
  15. package/package.json +7 -7
  16. package/src/_exports/_internal.ts +11 -0
  17. package/src/_exports/index.ts +57 -12
  18. package/src/access/checkPermissions.test.ts +55 -0
  19. package/src/access/checkPermissions.ts +54 -0
  20. package/src/applications/applications.test-d.ts +83 -0
  21. package/src/applications/applications.test.ts +130 -0
  22. package/src/applications/applications.ts +364 -0
  23. package/src/applications/userApplications.test.ts +126 -0
  24. package/src/applications/userApplications.ts +261 -0
  25. package/src/auth/getOrganizationVerificationState.test.ts +7 -7
  26. package/src/auth/getOrganizationVerificationState.ts +4 -4
  27. package/src/document/documentStore.concurrency.test.ts +313 -22
  28. package/src/installations/installations.test-d.ts +68 -0
  29. package/src/installations/installations.test.ts +62 -0
  30. package/src/installations/installations.ts +202 -0
  31. package/src/organization/organization.test-d.ts +18 -84
  32. package/src/organization/organization.test.ts +11 -11
  33. package/src/organization/organization.ts +26 -54
  34. package/src/organizations/organizations.test-d.ts +11 -32
  35. package/src/organizations/organizations.test.ts +8 -8
  36. package/src/organizations/organizations.ts +29 -54
  37. package/src/project/project.test-d.ts +19 -75
  38. package/src/project/project.test.ts +57 -10
  39. package/src/project/project.ts +33 -52
  40. package/src/projects/projects.test-d.ts +15 -21
  41. package/src/projects/projects.test.ts +8 -8
  42. package/src/projects/projects.ts +25 -49
  43. package/src/store/fetcherStore.test-d.ts +121 -0
  44. package/src/store/fetcherStore.test.ts +299 -0
  45. package/src/store/fetcherStore.ts +801 -0
  46. package/src/utils/buildQuery.test.ts +14 -0
  47. package/src/utils/buildQuery.ts +17 -0
  48. package/src/utils/includeShape.ts +36 -0
  49. package/src/utils/setCleanupTimeout.ts +21 -8
  50. package/dist/_chunks-es/version.js +0 -7
  51. package/dist/_chunks-es/version.js.map +0 -1
  52. package/src/utils/defineIntent.test.ts +0 -477
  53. package/src/utils/defineIntent.ts +0 -244
@@ -0,0 +1,261 @@
1
+ import {switchMap} from 'rxjs'
2
+
3
+ import {getClientState} from '../client/clientStore'
4
+ import {defineFetcher, defineMutation} from '../store/fetcherStore'
5
+ import {buildQuery} from '../utils/buildQuery'
6
+ import {type ApplicationVisibility, type DeletedResult} from './applications'
7
+
8
+ // The version the CLI pins for this API family; verify against the deployed
9
+ // API before promoting these stores past @internal.
10
+ const API_VERSION = 'v2024-08-01'
11
+
12
+ /**
13
+ * A deployment of a user application, embedded when `activeDeployment` is
14
+ * included on the User Applications API. Distinct from the org Applications
15
+ * API's {@link ApplicationDeployment} summary.
16
+ *
17
+ * @see https://www.sanity.io/docs/http-reference/applications-api
18
+ * @public
19
+ */
20
+ export interface UserApplicationDeployment {
21
+ id: string
22
+ version: string | null
23
+ isActiveDeployment: boolean
24
+ userApplicationId: string
25
+ isAutoUpdating: boolean | null
26
+ size: number | null
27
+ deployedAt: string
28
+ deployedBy: string | null
29
+ createdAt: string
30
+ updatedAt: string
31
+ manifest: unknown
32
+ }
33
+
34
+ /**
35
+ * A user application (deployed studio or custom app) as returned by
36
+ * `/user-applications`.
37
+ *
38
+ * @see https://www.sanity.io/docs/http-reference/applications-api
39
+ * @public
40
+ */
41
+ export interface UserApplication {
42
+ id: string
43
+ projectId: string | null
44
+ organizationId: string | null
45
+ title: string | null
46
+ type: string
47
+ urlType: string
48
+ appHost: string
49
+ dashboardStatus: string
50
+ createdAt: string
51
+ updatedAt: string
52
+ autoUpdatingVersion: string | null
53
+ activeDeployment: UserApplicationDeployment | null
54
+ manifestData: unknown
55
+ config: Record<string, unknown> | null
56
+ manifest: unknown
57
+ }
58
+
59
+ /**
60
+ * Options for listing an organization's user applications.
61
+ *
62
+ * @see https://www.sanity.io/docs/http-reference/applications-api
63
+ * @public
64
+ */
65
+ export interface UserApplicationsOptions {
66
+ organizationId: string
67
+ }
68
+
69
+ /**
70
+ * Fetcher for an organization's user applications (`GET /user-applications`),
71
+ * on the shared fetcher cache.
72
+ *
73
+ * @see https://www.sanity.io/docs/http-reference/applications-api
74
+ * @internal
75
+ */
76
+ export const userApplications = defineFetcher<
77
+ [options: UserApplicationsOptions],
78
+ UserApplication[]
79
+ >({
80
+ name: 'userApplications',
81
+ getKey: (_instance, options) => options.organizationId,
82
+ fetch: (instance) => (options) =>
83
+ getClientState(instance, {apiVersion: API_VERSION, scope: 'global'}).observable.pipe(
84
+ switchMap((client) =>
85
+ client.observable.request<UserApplication[]>({
86
+ uri: '/user-applications',
87
+ query: buildQuery({organizationId: options.organizationId}),
88
+ tag: 'user-applications.list',
89
+ }),
90
+ ),
91
+ ),
92
+ tags: (data) => [
93
+ {type: 'user-application', id: 'LIST'},
94
+ ...data.map((app) => ({type: 'user-application', id: app.id})),
95
+ ],
96
+ })
97
+
98
+ /**
99
+ * Fetcher for a single user application (`GET /user-applications/:id`), on the
100
+ * shared fetcher cache. No pre-seeding from {@link userApplications}: its list
101
+ * keys include the organization, which this fetcher's params can't
102
+ * reconstruct.
103
+ *
104
+ * @see https://www.sanity.io/docs/http-reference/applications-api
105
+ * @internal
106
+ */
107
+ export const userApplication = defineFetcher<[userApplicationId: string], UserApplication>({
108
+ name: 'userApplication',
109
+ getKey: (_instance, userApplicationId) => userApplicationId,
110
+ fetch: (instance) => (userApplicationId) =>
111
+ getClientState(instance, {apiVersion: API_VERSION, scope: 'global'}).observable.pipe(
112
+ switchMap((client) =>
113
+ client.observable.request<UserApplication>({
114
+ uri: `/user-applications/${userApplicationId}`,
115
+ tag: 'user-applications.get',
116
+ }),
117
+ ),
118
+ ),
119
+ tags: (data) => [{type: 'user-application', id: data.id}],
120
+ })
121
+
122
+ /**
123
+ * Fields you can set when creating or updating a user application.
124
+ *
125
+ * @see https://www.sanity.io/docs/http-reference/applications-api
126
+ * @internal
127
+ */
128
+ export interface UserApplicationWriteFields {
129
+ /** Human-friendly display name; `null` clears it */
130
+ title?: string | null
131
+ /** `internal` for Sanity-hosted studios, `external` for externally hosted */
132
+ urlType?: 'internal' | 'external'
133
+ /** Studio subdomain (internal) or full `https` URL (external) */
134
+ appHost?: string
135
+ /** Internal apps only: make this the default app for the deployment */
136
+ isDefaultForDeployment?: boolean
137
+ /** Dashboard visibility */
138
+ dashboardStatus?: ApplicationVisibility
139
+ /** Auto-update channel or a pinned semver */
140
+ autoUpdatingVersion?: string | null
141
+ }
142
+
143
+ /**
144
+ * Input for {@link createUserApplication}: the writable fields, with `urlType`
145
+ * and `appHost` required. `organizationId` and `appType` are sent as query
146
+ * parameters; the rest form the request body.
147
+ *
148
+ * @see https://www.sanity.io/docs/http-reference/applications-api
149
+ * @internal
150
+ */
151
+ export interface CreateUserApplicationInput extends UserApplicationWriteFields {
152
+ /** Query param: owning organization */
153
+ organizationId?: string
154
+ /** Query param: `studio` or `coreApp` */
155
+ appType?: 'studio' | 'coreApp'
156
+ urlType: 'internal' | 'external'
157
+ appHost: string
158
+ /** Create does not accept a `null` title */
159
+ title?: string
160
+ }
161
+
162
+ /**
163
+ * Mutation to create a user application (`POST /user-applications`). Invalidates
164
+ * the list so it reconverges to include the new entry.
165
+ *
166
+ * @see https://www.sanity.io/docs/http-reference/applications-api
167
+ * @internal
168
+ */
169
+ export const createUserApplication = defineMutation<CreateUserApplicationInput, UserApplication>({
170
+ name: 'createUserApplication',
171
+ mutationFn:
172
+ (instance) =>
173
+ ({organizationId, appType, ...body}) =>
174
+ getClientState(instance, {apiVersion: API_VERSION, scope: 'global'}).observable.pipe(
175
+ switchMap((client) =>
176
+ client.observable.request<UserApplication>({
177
+ uri: '/user-applications',
178
+ method: 'POST',
179
+ query: buildQuery({organizationId, appType}),
180
+ body,
181
+ tag: 'user-applications.create',
182
+ }),
183
+ ),
184
+ ),
185
+ invalidates: [{type: 'user-application', id: 'LIST'}],
186
+ })
187
+
188
+ /**
189
+ * Input for {@link updateUserApplication}: the writable fields (all optional),
190
+ * keyed by id.
191
+ *
192
+ * @see https://www.sanity.io/docs/http-reference/applications-api
193
+ * @internal
194
+ */
195
+ export interface UpdateUserApplicationInput extends UserApplicationWriteFields {
196
+ userApplicationId: string
197
+ }
198
+
199
+ /**
200
+ * Mutation to update a user application (`PATCH /user-applications/:id`).
201
+ * Invalidates the updated entry and the list.
202
+ *
203
+ * @see https://www.sanity.io/docs/http-reference/applications-api
204
+ * @internal
205
+ */
206
+ export const updateUserApplication = defineMutation<UpdateUserApplicationInput, UserApplication>({
207
+ name: 'updateUserApplication',
208
+ mutationFn:
209
+ (instance) =>
210
+ ({userApplicationId, ...body}) =>
211
+ getClientState(instance, {apiVersion: API_VERSION, scope: 'global'}).observable.pipe(
212
+ switchMap((client) =>
213
+ client.observable.request<UserApplication>({
214
+ uri: `/user-applications/${userApplicationId}`,
215
+ method: 'PATCH',
216
+ body,
217
+ tag: 'user-applications.update',
218
+ }),
219
+ ),
220
+ ),
221
+ invalidates: (_result, {userApplicationId}) => [
222
+ {type: 'user-application', id: 'LIST'},
223
+ {type: 'user-application', id: userApplicationId},
224
+ ],
225
+ })
226
+
227
+ /**
228
+ * Input for {@link deleteUserApplication}.
229
+ *
230
+ * @internal
231
+ */
232
+ export interface DeleteUserApplicationInput {
233
+ userApplicationId: string
234
+ }
235
+
236
+ /**
237
+ * Mutation to delete a user application (`DELETE /user-applications/:id`).
238
+ * Invalidates the deleted entry and the list.
239
+ *
240
+ * @see https://www.sanity.io/docs/http-reference/applications-api
241
+ * @internal
242
+ */
243
+ export const deleteUserApplication = defineMutation<DeleteUserApplicationInput, DeletedResult>({
244
+ name: 'deleteUserApplication',
245
+ mutationFn:
246
+ (instance) =>
247
+ ({userApplicationId}) =>
248
+ getClientState(instance, {apiVersion: API_VERSION, scope: 'global'}).observable.pipe(
249
+ switchMap((client) =>
250
+ client.observable.request<DeletedResult>({
251
+ uri: `/user-applications/${userApplicationId}`,
252
+ method: 'DELETE',
253
+ tag: 'user-applications.delete',
254
+ }),
255
+ ),
256
+ ),
257
+ invalidates: (_result, {userApplicationId}) => [
258
+ {type: 'user-application', id: 'LIST'},
259
+ {type: 'user-application', id: userApplicationId},
260
+ ],
261
+ })
@@ -1,4 +1,4 @@
1
- import {type Observable} from 'rxjs'
1
+ import {map, type Observable} from 'rxjs'
2
2
  import {TestScheduler} from 'rxjs/testing'
3
3
  import {beforeEach, describe, expect, it, vi} from 'vitest'
4
4
 
@@ -6,7 +6,7 @@ import {
6
6
  compareProjectOrganization,
7
7
  type OrgVerificationResult,
8
8
  } from '../project/organizationVerification'
9
- import {getProjectState} from '../project/project'
9
+ import {project} from '../project/project'
10
10
  import {type SanityInstance} from '../store/createSanityInstance'
11
11
  import {type StateSource} from '../store/createStateSourceAction'
12
12
  import {getDashboardOrganizationId} from './dashboardUtils'
@@ -17,7 +17,7 @@ vi.mock('./dashboardUtils', () => ({
17
17
  getDashboardOrganizationId: vi.fn(),
18
18
  }))
19
19
  vi.mock('../project/project', () => ({
20
- getProjectState: vi.fn(),
20
+ project: {getState: vi.fn()},
21
21
  }))
22
22
  // Mock the comparison function to check its inputs
23
23
  vi.mock('../project/organizationVerification', async (importOriginal) => {
@@ -54,11 +54,11 @@ describe('observeOrganizationVerificationState', () => {
54
54
  } as any) // Cast to any to bypass strict type checking in mock
55
55
  }
56
56
 
57
- // Helper to mock getProjectState
57
+ // Helper to mock the project fetcher's state; wraps plain values in the snapshot envelope
58
58
  const mockProjectOrgId = (observable: Observable<{organizationId: string | null} | null>) => {
59
- vi.mocked(getProjectState).mockReturnValue({
59
+ vi.mocked(project.getState).mockReturnValue({
60
60
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
- observable: observable as any, // Cast needed due to complex type
61
+ observable: observable.pipe(map((value) => ({data: value ?? undefined}))) as any,
62
62
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
63
  } as StateSource<any>)
64
64
  }
@@ -110,7 +110,7 @@ describe('observeOrganizationVerificationState', () => {
110
110
  expectObservable(result$).toBe(expectedMarble, expectedValues)
111
111
  })
112
112
  // No project fetch or comparison should occur
113
- expect(getProjectState).not.toHaveBeenCalled()
113
+ expect(project.getState).not.toHaveBeenCalled()
114
114
  expect(compareProjectOrganization).not.toHaveBeenCalled()
115
115
  })
116
116
 
@@ -4,14 +4,14 @@ import {
4
4
  compareProjectOrganization,
5
5
  type OrgVerificationResult,
6
6
  } from '../project/organizationVerification'
7
- import {getProjectState} from '../project/project'
7
+ import {project} from '../project/project'
8
8
  import {type SanityInstance} from '../store/createSanityInstance'
9
9
  import {getDashboardOrganizationId} from './dashboardUtils'
10
10
 
11
11
  /**
12
12
  * Creates an observable that emits the organization verification state for a given instance.
13
13
  * It combines the dashboard organization ID (from auth context) with the
14
- * project's actual organization ID (fetched via getProjectState) and compares them.
14
+ * project's actual organization ID (fetched via the project fetcher) and compares them.
15
15
  * @public
16
16
  */
17
17
  export function observeOrganizationVerificationState(
@@ -24,8 +24,8 @@ export function observeOrganizationVerificationState(
24
24
 
25
25
  // Create observables for each project's org ID
26
26
  const projectOrgIdObservables = projectIds.map((id) =>
27
- getProjectState(instance, {projectId: id}).observable.pipe(
28
- map((project) => ({projectId: id, orgId: project?.organizationId ?? null})),
27
+ project.getState(instance, {projectId: id}).observable.pipe(
28
+ map((snapshot) => ({projectId: id, orgId: snapshot.data?.organizationId ?? null})),
29
29
  // Ensure we only proceed if the orgId is loaded, distinct prevents unnecessary checks
30
30
  distinctUntilChanged((prev, curr) => prev.orgId === curr.orgId),
31
31
  ),