@sanity/client 7.1.0 → 7.2.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.
@@ -1,3 +1,4 @@
1
+ import {getVersionFromId, getVersionId, isDraftId} from '@sanity/client/csm'
1
2
  import {from, type MonoTypeOperatorFunction, Observable} from 'rxjs'
2
3
  import {combineLatestWith, filter, map} from 'rxjs/operators'
3
4
 
@@ -12,6 +13,8 @@ import type {
12
13
  Any,
13
14
  BaseActionOptions,
14
15
  BaseMutationOptions,
16
+ CreateVersionAction,
17
+ DiscardVersionAction,
15
18
  FirstDocumentIdMutationOptions,
16
19
  FirstDocumentMutationOptions,
17
20
  HttpRequest,
@@ -25,11 +28,13 @@ import type {
25
28
  MutationSelection,
26
29
  QueryOptions,
27
30
  RawQueryResponse,
31
+ ReplaceVersionAction,
28
32
  RequestObservableOptions,
29
33
  RequestOptions,
30
34
  SanityDocument,
31
35
  SingleActionResult,
32
36
  SingleMutationResult,
37
+ UnpublishVersionAction,
33
38
  } from '../types'
34
39
  import {getSelection} from '../util/getSelection'
35
40
  import * as validate from '../validators'
@@ -132,10 +137,36 @@ export function _getDocument<R extends Record<string, Any>>(
132
137
  client: Client,
133
138
  httpRequest: HttpRequest,
134
139
  id: string,
135
- opts: {signal?: AbortSignal; tag?: string} = {},
140
+ opts: {signal?: AbortSignal; tag?: string; releaseId?: string} = {},
136
141
  ): Observable<SanityDocument<R> | undefined> {
142
+ const getDocId = () => {
143
+ if (!opts.releaseId) {
144
+ return id
145
+ }
146
+
147
+ const versionId = getVersionFromId(id)
148
+ if (!versionId) {
149
+ if (isDraftId(id)) {
150
+ throw new Error(
151
+ `The document ID (\`${id}\`) is a draft, but \`options.releaseId\` is set as \`${opts.releaseId}\``,
152
+ )
153
+ }
154
+
155
+ return getVersionId(id, opts.releaseId)
156
+ }
157
+
158
+ if (versionId !== opts.releaseId) {
159
+ throw new Error(
160
+ `The document ID (\`${id}\`) is already a version of \`${versionId}\` release, but this does not match the provided \`options.releaseId\` (\`${opts.releaseId}\`)`,
161
+ )
162
+ }
163
+
164
+ return id
165
+ }
166
+ const docId = getDocId()
167
+
137
168
  const options = {
138
- uri: _getDataUrl(client, 'doc', id),
169
+ uri: _getDataUrl(client, 'doc', docId),
139
170
  json: true,
140
171
  tag: opts.tag,
141
172
  signal: opts.signal,
@@ -168,6 +199,27 @@ export function _getDocuments<R extends Record<string, Any>>(
168
199
  )
169
200
  }
170
201
 
202
+ /** @internal */
203
+ export function _getReleaseDocuments<R extends Record<string, Any>>(
204
+ client: ObservableSanityClient | SanityClient,
205
+ httpRequest: HttpRequest,
206
+ releaseId: string,
207
+ opts: BaseMutationOptions = {},
208
+ ): Observable<RawQueryResponse<SanityDocument<R>[]>> {
209
+ return _dataRequest(
210
+ client,
211
+ httpRequest,
212
+ 'query',
213
+ {
214
+ query: '*[sanity::partOfRelease($releaseId)]',
215
+ params: {
216
+ releaseId,
217
+ },
218
+ },
219
+ opts,
220
+ )
221
+ }
222
+
171
223
  /** @internal */
172
224
  export function _createIfNotExists<R extends Record<string, Any>>(
173
225
  client: Client,
@@ -204,6 +256,26 @@ export function _createOrReplace<R extends Record<string, Any>>(
204
256
  return _create<R>(client, httpRequest, doc, 'createOrReplace', options)
205
257
  }
206
258
 
259
+ /** @internal */
260
+ export function _createVersion<R extends Record<string, Any>>(
261
+ client: ObservableSanityClient | SanityClient,
262
+ httpRequest: HttpRequest,
263
+ doc: IdentifiedSanityDocumentStub<R>,
264
+ publishedId: string,
265
+ options?: BaseActionOptions,
266
+ ): Observable<SingleActionResult> {
267
+ validators.requireDocumentId('createVersion', doc)
268
+ validators.requireDocumentType('createVersion', doc)
269
+
270
+ const createVersionAction: CreateVersionAction = {
271
+ actionType: 'sanity.action.document.version.create',
272
+ publishedId,
273
+ document: doc,
274
+ }
275
+
276
+ return _action(client, httpRequest, createVersionAction, options)
277
+ }
278
+
207
279
  /** @internal */
208
280
  export function _delete<R extends Record<string, Any>>(
209
281
  client: Client,
@@ -227,6 +299,58 @@ export function _delete<R extends Record<string, Any>>(
227
299
  )
228
300
  }
229
301
 
302
+ /** @internal */
303
+ export function _discardVersion(
304
+ client: ObservableSanityClient | SanityClient,
305
+ httpRequest: HttpRequest,
306
+ versionId: string,
307
+ purge: boolean = false,
308
+ options?: BaseActionOptions,
309
+ ): Observable<SingleActionResult> {
310
+ const discardVersionAction: DiscardVersionAction = {
311
+ actionType: 'sanity.action.document.version.discard',
312
+ versionId,
313
+ purge,
314
+ }
315
+
316
+ return _action(client, httpRequest, discardVersionAction, options)
317
+ }
318
+
319
+ /** @internal */
320
+ export function _replaceVersion<R extends Record<string, Any>>(
321
+ client: ObservableSanityClient | SanityClient,
322
+ httpRequest: HttpRequest,
323
+ doc: IdentifiedSanityDocumentStub<R>,
324
+ options?: BaseActionOptions,
325
+ ): Observable<SingleActionResult> {
326
+ validators.requireDocumentId('replaceVersion', doc)
327
+ validators.requireDocumentType('replaceVersion', doc)
328
+
329
+ const replaceVersionAction: ReplaceVersionAction = {
330
+ actionType: 'sanity.action.document.version.replace',
331
+ document: doc,
332
+ }
333
+
334
+ return _action(client, httpRequest, replaceVersionAction, options)
335
+ }
336
+
337
+ /** @internal */
338
+ export function _unpublishVersion(
339
+ client: ObservableSanityClient | SanityClient,
340
+ httpRequest: HttpRequest,
341
+ versionId: string,
342
+ publishedId: string,
343
+ options?: BaseActionOptions,
344
+ ): Observable<SingleActionResult> {
345
+ const unpublishVersionAction: UnpublishVersionAction = {
346
+ actionType: 'sanity.action.document.version.unpublish',
347
+ versionId,
348
+ publishedId,
349
+ }
350
+
351
+ return _action(client, httpRequest, unpublishVersionAction, options)
352
+ }
353
+
230
354
  /** @internal */
231
355
  export function _mutate<R extends Record<string, Any>>(
232
356
  client: Client,