@sanity/client 8.1.0 → 8.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "8.1.0",
3
+ "version": "8.3.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "api",
@@ -51,7 +51,6 @@
51
51
  "dependencies": {
52
52
  "eventsource": "^5.1.0",
53
53
  "get-it": "^9.5.0",
54
- "nanoid": "^6.0.1",
55
54
  "obug": "^2.1.4",
56
55
  "rxjs": "^7.8.2"
57
56
  },
@@ -3,6 +3,10 @@ import {Observable} from 'rxjs'
3
3
 
4
4
  import {AgentActionsClient, ObservableAgentsActionClient} from './agent/actions/AgentActionsClient'
5
5
  import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
6
+ import {
7
+ CollaborationCommentsClient,
8
+ ObservableCollaborationCommentsClient,
9
+ } from './collaboration/CollaborationCommentsClient'
6
10
  import {defaultConfig, initConfig} from './config'
7
11
  import * as dataMethods from './data/dataMethods'
8
12
  import {_listen} from './data/listen'
@@ -57,10 +61,12 @@ import {deriveDocumentVersionId, getDocumentVersionId} from './util/createVersio
57
61
  export type {
58
62
  _listen,
59
63
  AssetsClient,
64
+ CollaborationCommentsClient,
60
65
  DatasetsClient,
61
66
  LiveClient,
62
67
  MediaLibraryVideoClient,
63
68
  ObservableAssetsClient,
69
+ ObservableCollaborationCommentsClient,
64
70
  ObservableDatasetsClient,
65
71
  ObservableMediaLibraryVideoClient,
66
72
  ObservableProjectsClient,
@@ -82,6 +88,10 @@ export class ObservableSanityClient {
82
88
  agent: {
83
89
  action: ObservableAgentsActionClient
84
90
  }
91
+ collaboration: {
92
+ /** @alpha */
93
+ comments: ObservableCollaborationCommentsClient
94
+ }
85
95
  functions: ObservableFunctionsClient
86
96
  releases: ObservableReleasesClient
87
97
 
@@ -112,6 +122,9 @@ export class ObservableSanityClient {
112
122
  this.agent = {
113
123
  action: new ObservableAgentsActionClient(this, this.#httpRequest),
114
124
  }
125
+ this.collaboration = {
126
+ comments: new ObservableCollaborationCommentsClient(this, this.#httpRequest),
127
+ }
115
128
  this.functions = new ObservableFunctionsClient(this, this.#httpRequest)
116
129
  this.releases = new ObservableReleasesClient(this, this.#httpRequest)
117
130
  }
@@ -1148,6 +1161,10 @@ export class SanityClient {
1148
1161
  agent: {
1149
1162
  action: AgentActionsClient
1150
1163
  }
1164
+ collaboration: {
1165
+ /** @alpha */
1166
+ comments: CollaborationCommentsClient
1167
+ }
1151
1168
  functions: FunctionsClient
1152
1169
  releases: ReleasesClient
1153
1170
 
@@ -1183,6 +1200,9 @@ export class SanityClient {
1183
1200
  this.agent = {
1184
1201
  action: new AgentActionsClient(this, this.#httpRequest),
1185
1202
  }
1203
+ this.collaboration = {
1204
+ comments: new CollaborationCommentsClient(this, this.#httpRequest),
1205
+ }
1186
1206
  this.functions = new FunctionsClient(this, this.#httpRequest)
1187
1207
  this.releases = new ReleasesClient(this, this.#httpRequest)
1188
1208
 
@@ -0,0 +1,387 @@
1
+ import {lastValueFrom, type Observable} from 'rxjs'
2
+
3
+ import type {ListenEventFromOptions} from '../data/listen'
4
+ import type {ObservableSanityClient, SanityClient} from '../SanityClient'
5
+ import type {
6
+ HttpRequest,
7
+ ListenEvent,
8
+ MultipleMutationResult,
9
+ MutationEvent,
10
+ QueryParams,
11
+ } from '../types'
12
+ import {
13
+ _addReaction,
14
+ _create,
15
+ _delete,
16
+ _fetch,
17
+ _getTargetDocumentRef,
18
+ _listen,
19
+ _removeReaction,
20
+ _update,
21
+ } from './comments'
22
+ import type {
23
+ CollaborationCommentCreate,
24
+ CollaborationCommentDocument,
25
+ CollaborationCommentReactionShortName,
26
+ CollaborationCommentsListenOptions,
27
+ CollaborationCommentsRequestOptions,
28
+ CollaborationCommentsWriteOptions,
29
+ CollaborationCommentUpdate,
30
+ } from './types'
31
+
32
+ /**
33
+ * Comments on the configured organization resource.
34
+ *
35
+ * Requires `collaboration.organizationId`, plus either `resource` or `projectId` and `dataset`.
36
+ *
37
+ * @alpha
38
+ */
39
+ export class ObservableCollaborationCommentsClient {
40
+ #client: ObservableSanityClient
41
+ #httpRequest: HttpRequest
42
+ constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
43
+ this.#client = client
44
+ this.#httpRequest = httpRequest
45
+ }
46
+
47
+ /**
48
+ * Create a comment or reply on the configured resource.
49
+ *
50
+ * A top-level comment requires `target`; a reply requires `parentCommentId` (never both).
51
+ * Replies inherit `target`, `status`, and `threadId` from the parent comment.
52
+ *
53
+ * @param body - Comment to create
54
+ * @param options - Optional request options
55
+ * @returns The created comment
56
+ */
57
+ create(
58
+ body: CollaborationCommentCreate,
59
+ options?: CollaborationCommentsWriteOptions,
60
+ ): Observable<CollaborationCommentDocument> {
61
+ return _create(this.#client, this.#httpRequest, body, options)
62
+ }
63
+
64
+ /**
65
+ * Update an existing comment.
66
+ *
67
+ * Updating `status` cascades to the comment's replies.
68
+ *
69
+ * @param id - Comment document ID
70
+ * @param body - Fields to update
71
+ * @param options - Optional request options
72
+ * @returns The updated comment
73
+ */
74
+ update(
75
+ id: string,
76
+ body: CollaborationCommentUpdate,
77
+ options?: CollaborationCommentsWriteOptions,
78
+ ): Observable<CollaborationCommentDocument> {
79
+ return _update(this.#client, this.#httpRequest, id, body, options)
80
+ }
81
+
82
+ /**
83
+ * Delete a comment and its replies.
84
+ *
85
+ * @param id - Comment document ID
86
+ * @param options - Optional request options
87
+ * @returns Mutation result, where `documentIds` covers the comment and every deleted reply
88
+ */
89
+ delete(
90
+ id: string,
91
+ options?: CollaborationCommentsWriteOptions,
92
+ ): Observable<MultipleMutationResult> {
93
+ return _delete(this.#client, this.#httpRequest, id, options)
94
+ }
95
+
96
+ /**
97
+ * Add the current user's reaction to a comment.
98
+ *
99
+ * @param id - Comment document ID
100
+ * @param shortName - Emoji short name, for example `:+1:`
101
+ * @param options - Optional request options
102
+ * @returns The comment, with the reaction applied
103
+ */
104
+ addReaction(
105
+ id: string,
106
+ shortName: CollaborationCommentReactionShortName,
107
+ options?: CollaborationCommentsWriteOptions,
108
+ ): Observable<CollaborationCommentDocument> {
109
+ return _addReaction(this.#client, this.#httpRequest, id, shortName, options)
110
+ }
111
+
112
+ /**
113
+ * Remove the current user's reaction from a comment.
114
+ *
115
+ * @param id - Comment document ID
116
+ * @param shortName - Emoji short name, for example `:+1:`
117
+ * @param options - Optional request options
118
+ * @returns The comment, with the reaction removed
119
+ */
120
+ removeReaction(
121
+ id: string,
122
+ shortName: CollaborationCommentReactionShortName,
123
+ options?: CollaborationCommentsWriteOptions,
124
+ ): Observable<CollaborationCommentDocument> {
125
+ return _removeReaction(this.#client, this.#httpRequest, id, shortName, options)
126
+ }
127
+
128
+ /**
129
+ * Build the global document reference used by `target.document._ref`, for use in
130
+ * queries and listeners.
131
+ *
132
+ * The reference is built from the configured `resource` and the published ID of
133
+ * the given document ID, since comment references always use published IDs.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * client.collaboration.comments.listen(
138
+ * '*[_type == "sanity.comment" && target.document._ref == $ref]',
139
+ * {ref: client.collaboration.comments.getTargetDocumentRef('doc-1')},
140
+ * )
141
+ * ```
142
+ *
143
+ * @param documentId - Document ID, in published, draft or version form
144
+ * @returns Global document reference, of the form `resourceType:resourceId:documentId`
145
+ */
146
+ getTargetDocumentRef(
147
+ documentId: string,
148
+ ): CollaborationCommentDocument['target']['document']['_ref'] {
149
+ return _getTargetDocumentRef(this.#client, documentId)
150
+ }
151
+
152
+ /**
153
+ * Fetch comments on the configured resource.
154
+ *
155
+ * Takes the same `query` and `params` as `client.fetch`, and switches from a
156
+ * GET to a POST for queries too large for the request URL in the same way,
157
+ * but queries the comments endpoint, which accepts none of the query options
158
+ * `client.fetch` does (`perspective`, `useCdn`, `filterResponse`,
159
+ * `resultSourceMap`, stega).
160
+ *
161
+ * The query runs against the organization store, which is not scoped to
162
+ * comments, so filter on `_type == "sanity.comment"`.
163
+ *
164
+ * @param query - GROQ-query to perform
165
+ * @param params - Optional query parameters
166
+ * @param options - Optional request options
167
+ */
168
+ fetch<R = unknown>(
169
+ query: string,
170
+ params?: QueryParams,
171
+ options?: CollaborationCommentsRequestOptions,
172
+ ): Observable<R> {
173
+ return _fetch<R>(this.#client, this.#httpRequest, query, params, options)
174
+ }
175
+
176
+ /**
177
+ * Listen for changes to comments on the configured resource.
178
+ *
179
+ * Mirrors `client.listen(query, params)`, and emits mutation events.
180
+ *
181
+ * @param query - GROQ-filter to listen to changes for
182
+ * @param params - Optional query parameters
183
+ */
184
+ listen(
185
+ query: string,
186
+ params?: QueryParams,
187
+ ): Observable<MutationEvent<CollaborationCommentDocument>>
188
+ /**
189
+ * Listen for changes to comments on the configured resource.
190
+ *
191
+ * Mirrors `client.listen(query, params, options)`.
192
+ *
193
+ * @param query - GROQ-filter to listen to changes for
194
+ * @param params - Optional query parameters
195
+ * @param options - The same listener options `client.listen` takes, forwarded
196
+ * to the organization store's listener
197
+ */
198
+ listen<Opts extends CollaborationCommentsListenOptions>(
199
+ query: string,
200
+ params: QueryParams | undefined,
201
+ options: Opts,
202
+ ): Observable<ListenEventFromOptions<CollaborationCommentDocument, Opts>>
203
+ listen(
204
+ query: string,
205
+ params?: QueryParams,
206
+ options?: CollaborationCommentsListenOptions,
207
+ ): Observable<ListenEvent<CollaborationCommentDocument>> {
208
+ return _listen(this.#client, query, params, options)
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Comments on the configured organization resource.
214
+ *
215
+ * Requires `collaboration.organizationId`, plus either `resource` or `projectId` and `dataset`.
216
+ *
217
+ * @alpha
218
+ */
219
+ export class CollaborationCommentsClient {
220
+ #client: SanityClient
221
+ #httpRequest: HttpRequest
222
+ constructor(client: SanityClient, httpRequest: HttpRequest) {
223
+ this.#client = client
224
+ this.#httpRequest = httpRequest
225
+ }
226
+
227
+ /**
228
+ * Create a comment or reply on the configured resource.
229
+ *
230
+ * A top-level comment requires `target`; a reply requires `parentCommentId` (never both).
231
+ * Replies inherit `target`, `status`, and `threadId` from the parent comment.
232
+ *
233
+ * @param body - Comment to create
234
+ * @param options - Optional request options
235
+ * @returns The created comment
236
+ */
237
+ create(
238
+ body: CollaborationCommentCreate,
239
+ options?: CollaborationCommentsWriteOptions,
240
+ ): Promise<CollaborationCommentDocument> {
241
+ return lastValueFrom(_create(this.#client, this.#httpRequest, body, options))
242
+ }
243
+
244
+ /**
245
+ * Update an existing comment.
246
+ *
247
+ * Updating `status` cascades to the comment's replies.
248
+ *
249
+ * @param id - Comment document ID
250
+ * @param body - Fields to update
251
+ * @param options - Optional request options
252
+ * @returns The updated comment
253
+ */
254
+ update(
255
+ id: string,
256
+ body: CollaborationCommentUpdate,
257
+ options?: CollaborationCommentsWriteOptions,
258
+ ): Promise<CollaborationCommentDocument> {
259
+ return lastValueFrom(_update(this.#client, this.#httpRequest, id, body, options))
260
+ }
261
+
262
+ /**
263
+ * Delete a comment and its replies.
264
+ *
265
+ * @param id - Comment document ID
266
+ * @param options - Optional request options
267
+ * @returns Mutation result, where `documentIds` covers the comment and every deleted reply
268
+ */
269
+ delete(id: string, options?: CollaborationCommentsWriteOptions): Promise<MultipleMutationResult> {
270
+ return lastValueFrom(_delete(this.#client, this.#httpRequest, id, options))
271
+ }
272
+
273
+ /**
274
+ * Add the current user's reaction to a comment.
275
+ *
276
+ * @param id - Comment document ID
277
+ * @param shortName - Emoji short name, for example `:+1:`
278
+ * @param options - Optional request options
279
+ * @returns The comment, with the reaction applied
280
+ */
281
+ addReaction(
282
+ id: string,
283
+ shortName: CollaborationCommentReactionShortName,
284
+ options?: CollaborationCommentsWriteOptions,
285
+ ): Promise<CollaborationCommentDocument> {
286
+ return lastValueFrom(_addReaction(this.#client, this.#httpRequest, id, shortName, options))
287
+ }
288
+
289
+ /**
290
+ * Remove the current user's reaction from a comment.
291
+ *
292
+ * @param id - Comment document ID
293
+ * @param shortName - Emoji short name, for example `:+1:`
294
+ * @param options - Optional request options
295
+ * @returns The comment, with the reaction removed
296
+ */
297
+ removeReaction(
298
+ id: string,
299
+ shortName: CollaborationCommentReactionShortName,
300
+ options?: CollaborationCommentsWriteOptions,
301
+ ): Promise<CollaborationCommentDocument> {
302
+ return lastValueFrom(_removeReaction(this.#client, this.#httpRequest, id, shortName, options))
303
+ }
304
+
305
+ /**
306
+ * Build the global document reference used by `target.document._ref`, for use in
307
+ * queries and listeners.
308
+ *
309
+ * The reference is built from the configured `resource` and the published ID of
310
+ * the given document ID, since comment references always use published IDs.
311
+ *
312
+ * @example
313
+ * ```ts
314
+ * const comments = await client.collaboration.comments.fetch(
315
+ * '*[_type == "sanity.comment" && target.document._ref == $ref]',
316
+ * {ref: client.collaboration.comments.getTargetDocumentRef('doc-1')},
317
+ * )
318
+ * ```
319
+ *
320
+ * @param documentId - Document ID, in published, draft or version form
321
+ * @returns Global document reference, of the form `resourceType:resourceId:documentId`
322
+ */
323
+ getTargetDocumentRef(
324
+ documentId: string,
325
+ ): CollaborationCommentDocument['target']['document']['_ref'] {
326
+ return _getTargetDocumentRef(this.#client, documentId)
327
+ }
328
+
329
+ /**
330
+ * Fetch comments on the configured resource.
331
+ *
332
+ * Takes the same `query` and `params` as `client.fetch`, and switches from a
333
+ * GET to a POST for queries too large for the request URL in the same way,
334
+ * but queries the comments endpoint, which accepts none of the query options
335
+ * `client.fetch` does (`perspective`, `useCdn`, `filterResponse`,
336
+ * `resultSourceMap`, stega).
337
+ *
338
+ * The query runs against the organization store, which is not scoped to
339
+ * comments, so filter on `_type == "sanity.comment"`.
340
+ *
341
+ * @param query - GROQ-query to perform
342
+ * @param params - Optional query parameters
343
+ * @param options - Optional request options
344
+ */
345
+ fetch<R = unknown>(
346
+ query: string,
347
+ params?: QueryParams,
348
+ options?: CollaborationCommentsRequestOptions,
349
+ ): Promise<R> {
350
+ return lastValueFrom(_fetch<R>(this.#client, this.#httpRequest, query, params, options))
351
+ }
352
+
353
+ /**
354
+ * Listen for changes to comments on the configured resource.
355
+ *
356
+ * Mirrors `client.listen(query, params)`, and emits mutation events.
357
+ *
358
+ * @param query - GROQ-filter to listen to changes for
359
+ * @param params - Optional query parameters
360
+ */
361
+ listen(
362
+ query: string,
363
+ params?: QueryParams,
364
+ ): Observable<MutationEvent<CollaborationCommentDocument>>
365
+ /**
366
+ * Listen for changes to comments on the configured resource.
367
+ *
368
+ * Mirrors `client.listen(query, params, options)`.
369
+ *
370
+ * @param query - GROQ-filter to listen to changes for
371
+ * @param params - Optional query parameters
372
+ * @param options - The same listener options `client.listen` takes, forwarded
373
+ * to the organization store's listener
374
+ */
375
+ listen<Opts extends CollaborationCommentsListenOptions>(
376
+ query: string,
377
+ params: QueryParams | undefined,
378
+ options: Opts,
379
+ ): Observable<ListenEventFromOptions<CollaborationCommentDocument, Opts>>
380
+ listen(
381
+ query: string,
382
+ params?: QueryParams,
383
+ options?: CollaborationCommentsListenOptions,
384
+ ): Observable<ListenEvent<CollaborationCommentDocument>> {
385
+ return _listen(this.#client, query, params, options)
386
+ }
387
+ }