@sanity/client 6.7.1-pink-lizard.4 → 6.7.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.
- package/README.md +2 -79
- package/dist/index.browser.cjs +1868 -13
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +1858 -3
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +1888 -13
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1878 -3
- package/dist/index.js.map +1 -1
- package/package.json +8 -48
- package/src/config.ts +0 -6
- package/umd/sanityClient.js +1 -5
- package/umd/sanityClient.min.js +1 -1
- package/dist/_chunks/browserMiddleware-Oa7zKwEN.js +0 -1862
- package/dist/_chunks/browserMiddleware-Oa7zKwEN.js.map +0 -1
- package/dist/_chunks/browserMiddleware-zle5A-pb.cjs +0 -1877
- package/dist/_chunks/browserMiddleware-zle5A-pb.cjs.map +0 -1
- package/dist/_chunks/createEditLink-_BA4GZaY.cjs +0 -222
- package/dist/_chunks/createEditLink-_BA4GZaY.cjs.map +0 -1
- package/dist/_chunks/createEditLink-tWkBWOk4.js +0 -212
- package/dist/_chunks/createEditLink-tWkBWOk4.js.map +0 -1
- package/dist/_chunks/nodeMiddleware-_lHo6xcC.js +0 -1882
- package/dist/_chunks/nodeMiddleware-_lHo6xcC.js.map +0 -1
- package/dist/_chunks/nodeMiddleware-qVoQZE_z.cjs +0 -1897
- package/dist/_chunks/nodeMiddleware-qVoQZE_z.cjs.map +0 -1
- package/dist/csm.cjs +0 -71
- package/dist/csm.cjs.map +0 -1
- package/dist/csm.d.ts +0 -248
- package/dist/csm.js +0 -58
- package/dist/csm.js.map +0 -1
- package/dist/stega.browser.cjs +0 -629
- package/dist/stega.browser.cjs.map +0 -1
- package/dist/stega.browser.js +0 -598
- package/dist/stega.browser.js.map +0 -1
- package/dist/stega.cjs +0 -424
- package/dist/stega.cjs.js +0 -21
- package/dist/stega.cjs.map +0 -1
- package/dist/stega.d.ts +0 -2872
- package/dist/stega.js +0 -393
- package/dist/stega.js.map +0 -1
- package/src/csm/applySourceDocuments.test.ts +0 -820
- package/src/csm/applySourceDocuments.ts +0 -87
- package/src/csm/createEditLink.ts +0 -75
- package/src/csm/getPublishedId.ts +0 -12
- package/src/csm/index.ts +0 -9
- package/src/csm/isArray.ts +0 -3
- package/src/csm/isRecord.ts +0 -3
- package/src/csm/jsonpath.test.ts +0 -33
- package/src/csm/jsonpath.ts +0 -93
- package/src/csm/resolveMapping.ts +0 -41
- package/src/csm/resolvedKeyedSourcePath.ts +0 -23
- package/src/csm/simplifyPath.ts +0 -20
- package/src/csm/types.ts +0 -72
- package/src/csm/walkMap.ts +0 -30
- package/src/stega/SanityStegaClient.ts +0 -244
- package/src/stega/config.ts +0 -56
- package/src/stega/encodeIntoResult.test.ts +0 -268
- package/src/stega/encodeIntoResult.ts +0 -59
- package/src/stega/filterDefault.ts +0 -49
- package/src/stega/index.browser.ts +0 -19
- package/src/stega/index.ts +0 -19
- package/src/stega/shared.ts +0 -4
- package/src/stega/stegaEncodeSourceMap.ts +0 -128
- package/src/stega/types.ts +0 -139
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import {parseJsonPath} from './jsonpath'
|
|
2
|
-
import {resolveMapping} from './resolveMapping'
|
|
3
|
-
import type {
|
|
4
|
-
Any,
|
|
5
|
-
ApplySourceDocumentsUpdateFunction,
|
|
6
|
-
ContentSourceMap,
|
|
7
|
-
ContentSourceMapDocuments,
|
|
8
|
-
PathSegment,
|
|
9
|
-
SanityDocument,
|
|
10
|
-
} from './types'
|
|
11
|
-
import {walkMap} from './walkMap'
|
|
12
|
-
|
|
13
|
-
const defaultUpdateFunction = <T = unknown>(changed: T): T => changed
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Optimistically applies source documents to a result, using the content source map to trace fields.
|
|
17
|
-
* Can be used to apply mutations to documents being edited in a Studio, or any mutation on Content Lake, to a result with extremely low latency.
|
|
18
|
-
* @alpha
|
|
19
|
-
*/
|
|
20
|
-
export function applySourceDocuments<Result = unknown>(
|
|
21
|
-
result: Result,
|
|
22
|
-
resultSourceMap: ContentSourceMap | undefined,
|
|
23
|
-
getCachedDocument: (
|
|
24
|
-
sourceDocument: ContentSourceMapDocuments[number],
|
|
25
|
-
) => SanityDocument | undefined,
|
|
26
|
-
updateFn: ApplySourceDocumentsUpdateFunction = defaultUpdateFunction,
|
|
27
|
-
): Result {
|
|
28
|
-
if (!resultSourceMap) return result
|
|
29
|
-
|
|
30
|
-
return walkMap(result, (value, path) => {
|
|
31
|
-
const resolveMappingResult = resolveMapping(path, resultSourceMap)
|
|
32
|
-
if (!resolveMappingResult) {
|
|
33
|
-
return value
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const {mapping, pathSuffix} = resolveMappingResult
|
|
37
|
-
if (mapping.type !== 'value') {
|
|
38
|
-
return value
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (mapping.source.type !== 'documentValue') {
|
|
42
|
-
return value
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const sourceDocument = resultSourceMap.documents[mapping.source.document]
|
|
46
|
-
const sourcePath = resultSourceMap.paths[mapping.source.path]
|
|
47
|
-
|
|
48
|
-
if (sourceDocument) {
|
|
49
|
-
const cachedDocument = getCachedDocument(sourceDocument)
|
|
50
|
-
if (!cachedDocument) {
|
|
51
|
-
return value
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const parsedPath = parseJsonPath(sourcePath + pathSuffix)
|
|
55
|
-
const changedValue = cachedDocument ? getField(cachedDocument, parsedPath) ?? value : value
|
|
56
|
-
return value === changedValue
|
|
57
|
-
? value
|
|
58
|
-
: updateFn<Result[keyof Result]>(changedValue, {
|
|
59
|
-
cachedDocument,
|
|
60
|
-
previousValue: value as Result[keyof Result],
|
|
61
|
-
sourceDocument,
|
|
62
|
-
sourcePath: parsedPath,
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return value
|
|
67
|
-
}) as Result
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function getField(obj: SanityDocument, path: PathSegment[]): Any {
|
|
71
|
-
let value = obj as SanityDocument | SanityDocument[keyof SanityDocument]
|
|
72
|
-
for (const segment of path) {
|
|
73
|
-
if (typeof segment === 'string') {
|
|
74
|
-
value = value[segment]
|
|
75
|
-
} else {
|
|
76
|
-
const match =
|
|
77
|
-
typeof segment === 'object'
|
|
78
|
-
? value.find((item: SanityDocument[keyof SanityDocument]) => item._key === segment.key)
|
|
79
|
-
: value[segment]
|
|
80
|
-
value = match || null
|
|
81
|
-
}
|
|
82
|
-
if (value === null || value === undefined) {
|
|
83
|
-
break
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return value
|
|
87
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import {getPublishedId} from './getPublishedId'
|
|
2
|
-
import {parseJsonPath} from './jsonpath'
|
|
3
|
-
import {resolvedKeyedSourcePath} from './resolvedKeyedSourcePath'
|
|
4
|
-
import {resolveMapping} from './resolveMapping'
|
|
5
|
-
import {simplifyPath} from './simplifyPath'
|
|
6
|
-
import type {CreateEditLinkOptions, EditIntentLink, PathSegment, StudioUrl} from './types'
|
|
7
|
-
|
|
8
|
-
/** @public */
|
|
9
|
-
export function createEditLink(
|
|
10
|
-
options: CreateEditLinkOptions,
|
|
11
|
-
): `${StudioUrl}${EditIntentLink}` | undefined {
|
|
12
|
-
const {studioUrl: resolveStudioUrl, resultSourceMap: csm, resultPath} = options
|
|
13
|
-
const {mapping, pathSuffix} = resolveMapping(resultPath, csm) || {}
|
|
14
|
-
|
|
15
|
-
if (!mapping) {
|
|
16
|
-
// console.warn('no mapping for path', { path: resultPath, sourceMap: csm })
|
|
17
|
-
return undefined
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (mapping.source.type === 'literal') {
|
|
21
|
-
return undefined
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (mapping.source.type === 'unknown') {
|
|
25
|
-
return undefined
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const sourceDoc = csm.documents[mapping.source.document]
|
|
29
|
-
const sourceBasePath = csm.paths[mapping.source.path]
|
|
30
|
-
|
|
31
|
-
if (sourceDoc && sourceBasePath) {
|
|
32
|
-
const studioUrl =
|
|
33
|
-
typeof resolveStudioUrl === 'function'
|
|
34
|
-
? resolveStudioUrl(sourceDoc, parseJsonPath(sourceBasePath))
|
|
35
|
-
: resolveStudioUrl
|
|
36
|
-
if (!studioUrl) return undefined
|
|
37
|
-
const _studioUrl = studioUrl.replace(/\/$/, '')
|
|
38
|
-
const _path = encodeURIComponent(
|
|
39
|
-
simplifyPath(
|
|
40
|
-
resolvedKeyedSourcePath({
|
|
41
|
-
keyedResultPath: parseJsonPath(sourceBasePath),
|
|
42
|
-
pathSuffix,
|
|
43
|
-
sourceBasePath,
|
|
44
|
-
}),
|
|
45
|
-
),
|
|
46
|
-
)
|
|
47
|
-
const {_id, _type} = sourceDoc
|
|
48
|
-
const params = new URLSearchParams()
|
|
49
|
-
if (sourceDoc._projectId) {
|
|
50
|
-
params.set('projectId', sourceDoc._projectId)
|
|
51
|
-
}
|
|
52
|
-
if (sourceDoc._dataset) {
|
|
53
|
-
params.set('dataset', sourceDoc._dataset)
|
|
54
|
-
}
|
|
55
|
-
params.set('id', sourceDoc._id)
|
|
56
|
-
params.set('type', sourceDoc._type)
|
|
57
|
-
params.set('path', decodeURIComponent(_path))
|
|
58
|
-
params.set('baseUrl', _studioUrl)
|
|
59
|
-
return `${_studioUrl}/intent/edit/id=${getPublishedId(
|
|
60
|
-
_id,
|
|
61
|
-
)};type=${_type};path=${_path}?${params}`
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return undefined
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** @internal */
|
|
68
|
-
export function encodeJsonPathToUriComponent(path: string | PathSegment[]): string {
|
|
69
|
-
const sourcePath = Array.isArray(path) ? path : parseJsonPath(path)
|
|
70
|
-
return encodeURIComponent(
|
|
71
|
-
sourcePath
|
|
72
|
-
.map((key, i) => (typeof key === 'number' ? `[${key}]` : i > 0 ? `.${key}` : key))
|
|
73
|
-
.join(''),
|
|
74
|
-
)
|
|
75
|
-
}
|
package/src/csm/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from './applySourceDocuments'
|
|
2
|
-
export {createEditLink, encodeJsonPathToUriComponent} from './createEditLink'
|
|
3
|
-
export {getPublishedId} from './getPublishedId'
|
|
4
|
-
export {jsonPath, parseJsonPath} from './jsonpath'
|
|
5
|
-
export {resolvedKeyedSourcePath} from './resolvedKeyedSourcePath'
|
|
6
|
-
export {resolveMapping} from './resolveMapping'
|
|
7
|
-
export {simplifyPath} from './simplifyPath'
|
|
8
|
-
export type * from './types'
|
|
9
|
-
export {walkMap} from './walkMap'
|
package/src/csm/isArray.ts
DELETED
package/src/csm/isRecord.ts
DELETED
package/src/csm/jsonpath.test.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {expect, test} from 'vitest'
|
|
2
|
-
|
|
3
|
-
import {jsonPath, parseJsonPath} from './jsonpath'
|
|
4
|
-
|
|
5
|
-
test('formats normalised JSON Paths', () => {
|
|
6
|
-
expect(jsonPath(['foo', 'bar', 0, 'baz'])).toBe("$['foo']['bar'][0]['baz']")
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
test('formats normalised JSON Paths with escaped characters', () => {
|
|
10
|
-
expect(jsonPath(['foo', 'bar', 0, 'baz', "it's a 'test'"])).toBe(
|
|
11
|
-
"$['foo']['bar'][0]['baz']['it\\'s a \\'test\\'']",
|
|
12
|
-
)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
test('parses normalised JSON Paths', () => {
|
|
16
|
-
expect(parseJsonPath("$['foo']['bar'][0]['baz']")).toEqual(['foo', 'bar', 0, 'baz'])
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test('parses normalised JSON Paths with escaped characters', () => {
|
|
20
|
-
expect(parseJsonPath("$['foo']['bar'][0]['baz']['it\\'s a \\'test\\'']")).toEqual([
|
|
21
|
-
'foo',
|
|
22
|
-
'bar',
|
|
23
|
-
0,
|
|
24
|
-
'baz',
|
|
25
|
-
"it's a 'test'",
|
|
26
|
-
])
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
test('parses normalised JSON Paths with key array filter selectors', () => {
|
|
30
|
-
expect(
|
|
31
|
-
parseJsonPath("$['foo'][?(@._key=='section-1')][0]['baz'][?(@._key=='section-2')]"),
|
|
32
|
-
).toEqual(['foo', {key: 'section-1', index: -1}, 0, 'baz', {key: 'section-2', index: -1}])
|
|
33
|
-
})
|
package/src/csm/jsonpath.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import type {PathSegment} from './types'
|
|
2
|
-
|
|
3
|
-
const ESCAPE: Record<string, string> = {
|
|
4
|
-
'\f': '\\f',
|
|
5
|
-
'\n': '\\n',
|
|
6
|
-
'\r': '\\r',
|
|
7
|
-
'\t': '\\t',
|
|
8
|
-
"'": "\\'",
|
|
9
|
-
'\\': '\\\\',
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const UNESCAPE: Record<string, string> = {
|
|
13
|
-
'\\f': '\f',
|
|
14
|
-
'\\n': '\n',
|
|
15
|
-
'\\r': '\r',
|
|
16
|
-
'\\t': '\t',
|
|
17
|
-
"\\'": "'",
|
|
18
|
-
'\\\\': '\\',
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
export function jsonPath(
|
|
25
|
-
path: PathSegment[],
|
|
26
|
-
opts?: {
|
|
27
|
-
keyArraySelectors: boolean
|
|
28
|
-
},
|
|
29
|
-
): string {
|
|
30
|
-
return `$${path
|
|
31
|
-
.map((segment) => {
|
|
32
|
-
if (typeof segment === 'string') {
|
|
33
|
-
const escapedKey = segment.replace(/[\f\n\r\t'\\]/g, (match) => {
|
|
34
|
-
return ESCAPE[match]
|
|
35
|
-
})
|
|
36
|
-
return `['${escapedKey}']`
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (typeof segment === 'number') {
|
|
40
|
-
return `[${segment}]`
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (opts?.keyArraySelectors && segment.key !== '') {
|
|
44
|
-
const escapedKey = segment.key.replace(/['\\]/g, (match) => {
|
|
45
|
-
return ESCAPE[match]
|
|
46
|
-
})
|
|
47
|
-
return `[?(@._key=='${escapedKey}')]`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return `[${segment.index}]`
|
|
51
|
-
})
|
|
52
|
-
.join('')}`
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @internal
|
|
57
|
-
*/
|
|
58
|
-
export function parseJsonPath(path: string): PathSegment[] {
|
|
59
|
-
const parsed: PathSegment[] = []
|
|
60
|
-
|
|
61
|
-
const parseRe = /\['(.*?)'\]|\[(\d+)\]|\[\?\(@\._key=='(.*?)'\)\]/g
|
|
62
|
-
let match: RegExpExecArray | null
|
|
63
|
-
|
|
64
|
-
while ((match = parseRe.exec(path)) !== null) {
|
|
65
|
-
if (match[1] !== undefined) {
|
|
66
|
-
const key = match[1].replace(/\\(\\|f|n|r|t|')/g, (m) => {
|
|
67
|
-
return UNESCAPE[m]
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
parsed.push(key)
|
|
71
|
-
continue
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (match[2] !== undefined) {
|
|
75
|
-
parsed.push(parseInt(match[2], 10))
|
|
76
|
-
continue
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (match[3] !== undefined) {
|
|
80
|
-
const key = match[3].replace(/\\(\\')/g, (m) => {
|
|
81
|
-
return UNESCAPE[m]
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
parsed.push({
|
|
85
|
-
key,
|
|
86
|
-
index: -1,
|
|
87
|
-
})
|
|
88
|
-
continue
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return parsed
|
|
93
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {jsonPath} from './jsonpath'
|
|
2
|
-
import type {ContentSourceMap, ContentSourceMapMapping, PathSegment} from './types'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export function resolveMapping(
|
|
8
|
-
resultPath: PathSegment[],
|
|
9
|
-
csm?: ContentSourceMap,
|
|
10
|
-
):
|
|
11
|
-
| {
|
|
12
|
-
mapping: ContentSourceMapMapping
|
|
13
|
-
matchedPath: string
|
|
14
|
-
pathSuffix: string
|
|
15
|
-
}
|
|
16
|
-
| undefined {
|
|
17
|
-
if (!csm?.mappings) {
|
|
18
|
-
return undefined
|
|
19
|
-
}
|
|
20
|
-
const resultJsonPath = jsonPath(resultPath)
|
|
21
|
-
|
|
22
|
-
if (csm.mappings[resultJsonPath] !== undefined) {
|
|
23
|
-
return {
|
|
24
|
-
mapping: csm.mappings[resultJsonPath],
|
|
25
|
-
matchedPath: resultJsonPath,
|
|
26
|
-
pathSuffix: '',
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const mappings = Object.entries(csm.mappings)
|
|
31
|
-
.filter(([key]) => resultJsonPath.startsWith(key))
|
|
32
|
-
.sort(([key1], [key2]) => key2.length - key1.length)
|
|
33
|
-
|
|
34
|
-
if (mappings.length == 0) {
|
|
35
|
-
return undefined
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const [matchedPath, mapping] = mappings[0]
|
|
39
|
-
const pathSuffix = resultJsonPath.substring(matchedPath.length)
|
|
40
|
-
return {mapping, matchedPath, pathSuffix}
|
|
41
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {jsonPath, parseJsonPath} from './jsonpath'
|
|
2
|
-
import type {PathSegment} from './types'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export function resolvedKeyedSourcePath(options: {
|
|
8
|
-
keyedResultPath: PathSegment[]
|
|
9
|
-
pathSuffix?: string
|
|
10
|
-
sourceBasePath: string
|
|
11
|
-
}): PathSegment[] {
|
|
12
|
-
const {keyedResultPath, pathSuffix, sourceBasePath} = options
|
|
13
|
-
|
|
14
|
-
const inferredResultPath = pathSuffix === undefined ? [] : parseJsonPath(pathSuffix)
|
|
15
|
-
|
|
16
|
-
const inferredPath = keyedResultPath.slice(keyedResultPath.length - inferredResultPath.length)
|
|
17
|
-
|
|
18
|
-
const inferredPathSuffix = inferredPath.length
|
|
19
|
-
? jsonPath(inferredPath, {keyArraySelectors: true}).slice(1)
|
|
20
|
-
: ''
|
|
21
|
-
|
|
22
|
-
return parseJsonPath(sourceBasePath + inferredPathSuffix)
|
|
23
|
-
}
|
package/src/csm/simplifyPath.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type {PathSegment} from './types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @internal
|
|
5
|
-
*/
|
|
6
|
-
export function simplifyPath(path: PathSegment[]): string {
|
|
7
|
-
return path
|
|
8
|
-
.map((segment, index) => {
|
|
9
|
-
if (typeof segment === 'number') {
|
|
10
|
-
return `[${segment}]`
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (typeof segment === 'string') {
|
|
14
|
-
return index === 0 ? segment : `.${segment}`
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return `[_key=="${segment.key}"]`
|
|
18
|
-
})
|
|
19
|
-
.join('')
|
|
20
|
-
}
|
package/src/csm/types.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import type {ContentSourceMap, ContentSourceMapDocuments, SanityDocument} from '../types'
|
|
2
|
-
|
|
3
|
-
export type {
|
|
4
|
-
Any,
|
|
5
|
-
ContentSourceMap,
|
|
6
|
-
ContentSourceMapDocument,
|
|
7
|
-
ContentSourceMapDocumentBase,
|
|
8
|
-
ContentSourceMapDocuments,
|
|
9
|
-
ContentSourceMapDocumentValueSource,
|
|
10
|
-
ContentSourceMapLiteralSource,
|
|
11
|
-
ContentSourceMapMapping,
|
|
12
|
-
ContentSourceMapMappings,
|
|
13
|
-
ContentSourceMapPaths,
|
|
14
|
-
ContentSourceMapRemoteDocument,
|
|
15
|
-
ContentSourceMapSource,
|
|
16
|
-
ContentSourceMapUnknownSource,
|
|
17
|
-
ContentSourceMapValueMapping,
|
|
18
|
-
SanityDocument,
|
|
19
|
-
} from '../types'
|
|
20
|
-
|
|
21
|
-
/** @public */
|
|
22
|
-
export type Path = PathSegment[]
|
|
23
|
-
|
|
24
|
-
/** @public */
|
|
25
|
-
export type PathSegment = string | number | KeyedSegment
|
|
26
|
-
|
|
27
|
-
/** @public */
|
|
28
|
-
export type KeyedSegment = {key: string; index: number}
|
|
29
|
-
|
|
30
|
-
/** @public */
|
|
31
|
-
export type StudioUrl = `/${string}` | `${string}.sanity.studio` | `https://${string}` | string
|
|
32
|
-
|
|
33
|
-
/** @public */
|
|
34
|
-
export type ResolveStudioUrl = (
|
|
35
|
-
sourceDocument: ContentSourceMapDocuments[number],
|
|
36
|
-
sourcePath: Path,
|
|
37
|
-
) => StudioUrl
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Equivalent to `import type {Exclude<Path, IndexTuple>} from 'sanity'`
|
|
41
|
-
* The `IndexTuple` piece is omitted since it's irrelavent to CSM source links.
|
|
42
|
-
* @public
|
|
43
|
-
*/
|
|
44
|
-
export type StudioPathLike = (string | number | {_key: string})[]
|
|
45
|
-
|
|
46
|
-
/** @public */
|
|
47
|
-
export type EditIntentLink = `/intent/edit/id=${string};type=${string};path=${string}`
|
|
48
|
-
|
|
49
|
-
/** @public */
|
|
50
|
-
export interface CreateEditLinkOptions {
|
|
51
|
-
studioUrl: StudioUrl | ResolveStudioUrl
|
|
52
|
-
resultSourceMap: ContentSourceMap
|
|
53
|
-
resultPath: Path
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @alpha
|
|
58
|
-
*/
|
|
59
|
-
export type ApplySourceDocumentsUpdateFunction = <T = unknown>(
|
|
60
|
-
changedValue: T,
|
|
61
|
-
context: {
|
|
62
|
-
cachedDocument: SanityDocument
|
|
63
|
-
previousValue: T
|
|
64
|
-
sourceDocument: ContentSourceMapDocuments[number]
|
|
65
|
-
sourcePath: PathSegment[]
|
|
66
|
-
},
|
|
67
|
-
) => T
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @internal
|
|
71
|
-
*/
|
|
72
|
-
export type WalkMapFn = (value: unknown, path: PathSegment[]) => unknown
|
package/src/csm/walkMap.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {isArray} from './isArray'
|
|
2
|
-
import {isRecord} from './isRecord'
|
|
3
|
-
import type {PathSegment, WalkMapFn} from './types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* generic way to walk a nested object or array and apply a mapping function to each value
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export function walkMap(value: unknown, mappingFn: WalkMapFn, path: PathSegment[] = []): unknown {
|
|
10
|
-
if (isArray(value)) {
|
|
11
|
-
return value.map((v, idx) => {
|
|
12
|
-
if (isRecord(v)) {
|
|
13
|
-
const key = v['_key']
|
|
14
|
-
if (typeof key === 'string') {
|
|
15
|
-
return walkMap(v, mappingFn, path.concat({key, index: idx}))
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return walkMap(v, mappingFn, path.concat(idx))
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (isRecord(value)) {
|
|
24
|
-
return Object.fromEntries(
|
|
25
|
-
Object.entries(value).map(([k, v]) => [k, walkMap(v, mappingFn, path.concat(k))]),
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return mappingFn(value, path)
|
|
30
|
-
}
|