@sanity/client 6.25.0 → 6.26.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.
@@ -1,4 +1,4 @@
1
- import {DRAFTS_PREFIX, getPublishedId} from './getPublishedId'
1
+ import {getPublishedId, isDraftId} from './draftUtils'
2
2
  import {jsonPathToStudioPath} from './jsonPath'
3
3
  import * as studioPath from './studioPath'
4
4
  import type {CreateEditUrlOptions, EditIntentUrl, StudioBaseUrl} from './types'
@@ -56,7 +56,7 @@ export function createEditUrl(options: CreateEditUrlOptions): `${StudioBaseUrl}$
56
56
  if (dataset) {
57
57
  searchParams.set('dataset', dataset)
58
58
  }
59
- if (_id.startsWith(DRAFTS_PREFIX)) {
59
+ if (isDraftId(_id)) {
60
60
  searchParams.set('isDraft', '')
61
61
  }
62
62
 
@@ -0,0 +1,81 @@
1
+ // nominal/opaque type hack
2
+ type Opaque<T, K> = T & {__opaqueId__: K}
3
+
4
+ /** @internal */
5
+ export type DraftId = Opaque<string, 'draftId'>
6
+
7
+ /** @internal */
8
+ export type PublishedId = Opaque<string, 'publishedId'>
9
+
10
+ /** @internal */
11
+ export const DRAFTS_FOLDER = 'drafts'
12
+
13
+ /** @internal */
14
+ export const VERSION_FOLDER = 'versions'
15
+
16
+ const PATH_SEPARATOR = '.'
17
+ const DRAFTS_PREFIX = `${DRAFTS_FOLDER}${PATH_SEPARATOR}`
18
+ const VERSION_PREFIX = `${VERSION_FOLDER}${PATH_SEPARATOR}`
19
+
20
+ /** @internal */
21
+ export function isDraftId(id: string): id is DraftId {
22
+ return id.startsWith(DRAFTS_PREFIX)
23
+ }
24
+
25
+ /** @internal */
26
+ export function isVersionId(id: string): boolean {
27
+ return id.startsWith(VERSION_PREFIX)
28
+ }
29
+
30
+ /** @internal */
31
+ export function isPublishedId(id: string): id is PublishedId {
32
+ return !isDraftId(id) && !isVersionId(id)
33
+ }
34
+
35
+ /** @internal */
36
+ export function getDraftId(id: string): DraftId {
37
+ if (isVersionId(id)) {
38
+ const publishedId = getPublishedId(id)
39
+ return (DRAFTS_PREFIX + publishedId) as DraftId
40
+ }
41
+
42
+ return isDraftId(id) ? id : ((DRAFTS_PREFIX + id) as DraftId)
43
+ }
44
+
45
+ /** @internal */
46
+ export function getVersionId(id: string, version: string): string {
47
+ if (version === 'drafts' || version === 'published') {
48
+ throw new Error('Version can not be "published" or "drafts"')
49
+ }
50
+
51
+ return `${VERSION_PREFIX}${version}${PATH_SEPARATOR}${getPublishedId(id)}`
52
+ }
53
+
54
+ /**
55
+ * @internal
56
+ * Given an id, returns the versionId if it exists.
57
+ * e.g. `versions.summer-drop.foo` = `summer-drop`
58
+ * e.g. `drafts.foo` = `undefined`
59
+ * e.g. `foo` = `undefined`
60
+ */
61
+ export function getVersionFromId(id: string): string | undefined {
62
+ if (!isVersionId(id)) return undefined
63
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
64
+ const [_versionPrefix, versionId, ..._publishedId] = id.split(PATH_SEPARATOR)
65
+
66
+ return versionId
67
+ }
68
+
69
+ /** @internal */
70
+ export function getPublishedId(id: string): PublishedId {
71
+ if (isVersionId(id)) {
72
+ // make sure to only remove the versions prefix and the bundle name
73
+ return id.split(PATH_SEPARATOR).slice(2).join(PATH_SEPARATOR) as PublishedId as PublishedId
74
+ }
75
+
76
+ if (isDraftId(id)) {
77
+ return id.slice(DRAFTS_PREFIX.length) as PublishedId
78
+ }
79
+
80
+ return id as PublishedId
81
+ }
package/src/csm/index.ts CHANGED
@@ -2,7 +2,19 @@ import * as studioPath from './studioPath'
2
2
  export {studioPath}
3
3
  export * from './applySourceDocuments'
4
4
  export {createEditUrl} from './createEditUrl'
5
- export {getPublishedId} from './getPublishedId'
5
+ export {
6
+ type DraftId,
7
+ DRAFTS_FOLDER,
8
+ getDraftId,
9
+ getPublishedId,
10
+ getVersionFromId,
11
+ getVersionId,
12
+ isDraftId,
13
+ isPublishedId,
14
+ isVersionId,
15
+ type PublishedId,
16
+ VERSION_FOLDER,
17
+ } from './draftUtils'
6
18
  export {jsonPath, jsonPathToStudioPath, parseJsonPath, studioPathToJsonPath} from './jsonPath'
7
19
  export {resolvedKeyedSourcePath} from './resolvedKeyedSourcePath'
8
20
  export {resolveEditInfo} from './resolveEditInfo'
@@ -3619,9 +3619,15 @@ ${selectionOpts}`);
3619
3619
  });
3620
3620
  });
3621
3621
  }
3622
- const DRAFTS_PREFIX = "drafts.";
3622
+ const DRAFTS_FOLDER = "drafts", VERSION_FOLDER = "versions", PATH_SEPARATOR = ".", DRAFTS_PREFIX = `${DRAFTS_FOLDER}${PATH_SEPARATOR}`, VERSION_PREFIX = `${VERSION_FOLDER}${PATH_SEPARATOR}`;
3623
+ function isDraftId(id) {
3624
+ return id.startsWith(DRAFTS_PREFIX);
3625
+ }
3626
+ function isVersionId(id) {
3627
+ return id.startsWith(VERSION_PREFIX);
3628
+ }
3623
3629
  function getPublishedId(id) {
3624
- return id.startsWith(DRAFTS_PREFIX) ? id.slice(DRAFTS_PREFIX.length) : id;
3630
+ return isVersionId(id) ? id.split(PATH_SEPARATOR).slice(2).join(PATH_SEPARATOR) : isDraftId(id) ? id.slice(DRAFTS_PREFIX.length) : id;
3625
3631
  }
3626
3632
  function createEditUrl(options) {
3627
3633
  const {
@@ -3648,7 +3654,7 @@ ${selectionOpts}`);
3648
3654
  type,
3649
3655
  path: stringifiedPath
3650
3656
  });
3651
- workspace && searchParams.set("workspace", workspace), tool && searchParams.set("tool", tool), projectId && searchParams.set("projectId", projectId), dataset && searchParams.set("dataset", dataset), _id.startsWith(DRAFTS_PREFIX) && searchParams.set("isDraft", "");
3657
+ workspace && searchParams.set("workspace", workspace), tool && searchParams.set("tool", tool), projectId && searchParams.set("projectId", projectId), dataset && searchParams.set("dataset", dataset), isDraftId(_id) && searchParams.set("isDraft", "");
3652
3658
  const segments = [baseUrl === "/" ? "" : baseUrl];
3653
3659
  workspace && segments.push(workspace);
3654
3660
  const routerParams = [