@sanity/client 6.7.1 → 6.8.0-pink-lizard.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 (65) hide show
  1. package/README.md +79 -2
  2. package/dist/_chunks/browserMiddleware-Oa7zKwEN.js +1862 -0
  3. package/dist/_chunks/browserMiddleware-Oa7zKwEN.js.map +1 -0
  4. package/dist/_chunks/browserMiddleware-zle5A-pb.cjs +1877 -0
  5. package/dist/_chunks/browserMiddleware-zle5A-pb.cjs.map +1 -0
  6. package/dist/_chunks/createEditLink-FiifjNgi.cjs +222 -0
  7. package/dist/_chunks/createEditLink-FiifjNgi.cjs.map +1 -0
  8. package/dist/_chunks/createEditLink-f3l2nngi.js +212 -0
  9. package/dist/_chunks/createEditLink-f3l2nngi.js.map +1 -0
  10. package/dist/_chunks/nodeMiddleware-S6QZVGmT.cjs +1897 -0
  11. package/dist/_chunks/nodeMiddleware-S6QZVGmT.cjs.map +1 -0
  12. package/dist/_chunks/nodeMiddleware-XodpBhbR.js +1882 -0
  13. package/dist/_chunks/nodeMiddleware-XodpBhbR.js.map +1 -0
  14. package/dist/csm.cjs +71 -0
  15. package/dist/csm.cjs.map +1 -0
  16. package/dist/csm.d.ts +248 -0
  17. package/dist/csm.js +58 -0
  18. package/dist/csm.js.map +1 -0
  19. package/dist/index.browser.cjs +13 -1868
  20. package/dist/index.browser.cjs.map +1 -1
  21. package/dist/index.browser.js +3 -1858
  22. package/dist/index.browser.js.map +1 -1
  23. package/dist/index.cjs +13 -1888
  24. package/dist/index.cjs.js +2 -2
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +3 -1878
  27. package/dist/index.js.map +1 -1
  28. package/dist/stega.browser.cjs +629 -0
  29. package/dist/stega.browser.cjs.map +1 -0
  30. package/dist/stega.browser.js +598 -0
  31. package/dist/stega.browser.js.map +1 -0
  32. package/dist/stega.cjs +424 -0
  33. package/dist/stega.cjs.js +21 -0
  34. package/dist/stega.cjs.map +1 -0
  35. package/dist/stega.d.ts +2872 -0
  36. package/dist/stega.js +393 -0
  37. package/dist/stega.js.map +1 -0
  38. package/package.json +42 -2
  39. package/src/config.ts +6 -0
  40. package/src/csm/applySourceDocuments.test.ts +820 -0
  41. package/src/csm/applySourceDocuments.ts +87 -0
  42. package/src/csm/createEditLink.ts +75 -0
  43. package/src/csm/getPublishedId.ts +12 -0
  44. package/src/csm/index.ts +9 -0
  45. package/src/csm/isArray.ts +3 -0
  46. package/src/csm/isRecord.ts +3 -0
  47. package/src/csm/jsonpath.test.ts +33 -0
  48. package/src/csm/jsonpath.ts +93 -0
  49. package/src/csm/resolveMapping.ts +41 -0
  50. package/src/csm/resolvedKeyedSourcePath.ts +23 -0
  51. package/src/csm/simplifyPath.ts +20 -0
  52. package/src/csm/types.ts +72 -0
  53. package/src/csm/walkMap.ts +30 -0
  54. package/src/stega/SanityStegaClient.ts +244 -0
  55. package/src/stega/config.ts +56 -0
  56. package/src/stega/encodeIntoResult.test.ts +268 -0
  57. package/src/stega/encodeIntoResult.ts +59 -0
  58. package/src/stega/filterDefault.ts +49 -0
  59. package/src/stega/index.browser.ts +19 -0
  60. package/src/stega/index.ts +19 -0
  61. package/src/stega/shared.ts +4 -0
  62. package/src/stega/stegaEncodeSourceMap.ts +128 -0
  63. package/src/stega/types.ts +139 -0
  64. package/umd/sanityClient.js +5 -1
  65. package/umd/sanityClient.min.js +1 -1
package/dist/csm.d.ts ADDED
@@ -0,0 +1,248 @@
1
+ /// <reference types="node" />
2
+
3
+ /**
4
+ * Used to tag types that is set to `any` as a temporary measure, but should be replaced with proper typings in the future
5
+ * @internal
6
+ */
7
+ export declare type Any = any
8
+
9
+ /**
10
+ * Optimistically applies source documents to a result, using the content source map to trace fields.
11
+ * 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.
12
+ * @alpha
13
+ */
14
+ export declare function applySourceDocuments<Result = unknown>(
15
+ result: Result,
16
+ resultSourceMap: ContentSourceMap | undefined,
17
+ getCachedDocument: (
18
+ sourceDocument: ContentSourceMapDocuments[number],
19
+ ) => SanityDocument | undefined,
20
+ updateFn?: ApplySourceDocumentsUpdateFunction,
21
+ ): Result
22
+
23
+ /**
24
+ * @alpha
25
+ */
26
+ export declare type ApplySourceDocumentsUpdateFunction = <T = unknown>(
27
+ changedValue: T,
28
+ context: {
29
+ cachedDocument: SanityDocument
30
+ previousValue: T
31
+ sourceDocument: ContentSourceMapDocuments[number]
32
+ sourcePath: PathSegment[]
33
+ },
34
+ ) => T
35
+
36
+ /** @public */
37
+ export declare interface ContentSourceMap {
38
+ mappings: ContentSourceMapMappings
39
+ documents: ContentSourceMapDocuments
40
+ paths: ContentSourceMapPaths
41
+ }
42
+
43
+ /** @public */
44
+ export declare interface ContentSourceMapDocument extends ContentSourceMapDocumentBase {
45
+ _projectId?: undefined
46
+ _dataset?: undefined
47
+ }
48
+
49
+ /** @public */
50
+ export declare interface ContentSourceMapDocumentBase {
51
+ _id: string
52
+ _type: string
53
+ }
54
+
55
+ /** @public */
56
+ export declare type ContentSourceMapDocuments = (
57
+ | ContentSourceMapDocument
58
+ | ContentSourceMapRemoteDocument
59
+ )[]
60
+
61
+ /**
62
+ * DocumentValueSource is a path to a value within a document
63
+ * @public
64
+ */
65
+ export declare interface ContentSourceMapDocumentValueSource {
66
+ type: 'documentValue'
67
+ document: number
68
+ path: number
69
+ }
70
+
71
+ /**
72
+ * When a value is not from a source, its a literal
73
+ * @public
74
+ */
75
+ export declare interface ContentSourceMapLiteralSource {
76
+ type: 'literal'
77
+ }
78
+
79
+ /** @public */
80
+ export declare type ContentSourceMapMapping = ContentSourceMapValueMapping
81
+
82
+ /** @public */
83
+ export declare type ContentSourceMapMappings = Record<string, ContentSourceMapMapping>
84
+
85
+ /** @public */
86
+ export declare type ContentSourceMapPaths = string[]
87
+
88
+ /** @public */
89
+ export declare interface ContentSourceMapRemoteDocument extends ContentSourceMapDocumentBase {
90
+ _projectId: string
91
+ _dataset: string
92
+ }
93
+
94
+ /** @public */
95
+ export declare type ContentSourceMapSource =
96
+ | ContentSourceMapDocumentValueSource
97
+ | ContentSourceMapLiteralSource
98
+ | ContentSourceMapUnknownSource
99
+
100
+ /**
101
+ * When a field source is unknown
102
+ * @public
103
+ */
104
+ export declare interface ContentSourceMapUnknownSource {
105
+ type: 'unknown'
106
+ }
107
+
108
+ /**
109
+ * ValueMapping is a mapping when for value that is from a single source value
110
+ * It may refer to a field within a document or a literal value
111
+ * @public
112
+ */
113
+ export declare interface ContentSourceMapValueMapping {
114
+ type: 'value'
115
+ source: ContentSourceMapSource
116
+ }
117
+
118
+ /** @public */
119
+ export declare function createEditLink(
120
+ options: CreateEditLinkOptions,
121
+ ): `${StudioUrl}${EditIntentLink}` | undefined
122
+
123
+ /** @public */
124
+ export declare interface CreateEditLinkOptions {
125
+ studioUrl: StudioUrl | ResolveStudioUrl
126
+ resultSourceMap: ContentSourceMap
127
+ resultPath: Path
128
+ }
129
+
130
+ /** @public */
131
+ export declare type EditIntentLink = `/intent/edit/id=${string};type=${string};path=${string}`
132
+
133
+ /** @internal */
134
+ export declare function encodeJsonPathToUriComponent(path: string | PathSegment[]): string
135
+
136
+ /**
137
+ * @internal
138
+ */
139
+ export declare function getPublishedId(id: string): string
140
+
141
+ /**
142
+ * @internal
143
+ */
144
+ export declare function jsonPath(
145
+ path: PathSegment[],
146
+ opts?: {
147
+ keyArraySelectors: boolean
148
+ },
149
+ ): string
150
+
151
+ /** @public */
152
+ export declare type KeyedSegment = {
153
+ key: string
154
+ index: number
155
+ }
156
+
157
+ /**
158
+ * @internal
159
+ */
160
+ export declare function parseJsonPath(path: string): PathSegment[]
161
+
162
+ /** @public */
163
+ export declare type Path = PathSegment[]
164
+
165
+ /** @public */
166
+ export declare type PathSegment = string | number | KeyedSegment
167
+
168
+ /**
169
+ * @internal
170
+ */
171
+ export declare function resolvedKeyedSourcePath(options: {
172
+ keyedResultPath: PathSegment[]
173
+ pathSuffix?: string
174
+ sourceBasePath: string
175
+ }): PathSegment[]
176
+
177
+ /**
178
+ * @internal
179
+ */
180
+ export declare function resolveMapping(
181
+ resultPath: PathSegment[],
182
+ csm?: ContentSourceMap,
183
+ ):
184
+ | {
185
+ mapping: ContentSourceMapMapping
186
+ matchedPath: string
187
+ pathSuffix: string
188
+ }
189
+ | undefined
190
+
191
+ /** @public */
192
+ export declare type ResolveStudioUrl = (
193
+ sourceDocument: ContentSourceMapDocuments[number],
194
+ sourcePath: Path,
195
+ ) => StudioUrl
196
+
197
+ /** @internal */
198
+ export declare type SanityDocument<T extends Record<string, Any> = Record<string, Any>> = {
199
+ [P in keyof T]: T[P]
200
+ } & {
201
+ _id: string
202
+ _rev: string
203
+ _type: string
204
+ _createdAt: string
205
+ _updatedAt: string
206
+ /**
207
+ * Present when `perspective` is set to `previewDrafts`
208
+ */
209
+ _originalId?: string
210
+ }
211
+
212
+ /**
213
+ * @internal
214
+ */
215
+ export declare function simplifyPath(path: PathSegment[]): string
216
+
217
+ /**
218
+ * Equivalent to `import type {Exclude<Path, IndexTuple>} from 'sanity'`
219
+ * The `IndexTuple` piece is omitted since it's irrelavent to CSM source links.
220
+ * @public
221
+ */
222
+ export declare type StudioPathLike = (
223
+ | string
224
+ | number
225
+ | {
226
+ _key: string
227
+ }
228
+ )[]
229
+
230
+ /** @public */
231
+ export declare type StudioUrl =
232
+ | `/${string}`
233
+ | `${string}.sanity.studio`
234
+ | `https://${string}`
235
+ | string
236
+
237
+ /**
238
+ * generic way to walk a nested object or array and apply a mapping function to each value
239
+ * @internal
240
+ */
241
+ export declare function walkMap(value: unknown, mappingFn: WalkMapFn, path?: PathSegment[]): unknown
242
+
243
+ /**
244
+ * @internal
245
+ */
246
+ export declare type WalkMapFn = (value: unknown, path: PathSegment[]) => unknown
247
+
248
+ export {}
package/dist/csm.js ADDED
@@ -0,0 +1,58 @@
1
+ import { walkMap, resolveMapping, parseJsonPath } from './_chunks/createEditLink-f3l2nngi.js';
2
+ export { createEditLink, encodeJsonPathToUriComponent, getPublishedId, jsonPath, resolvedKeyedSourcePath, simplifyPath } from './_chunks/createEditLink-f3l2nngi.js';
3
+ const defaultUpdateFunction = changed => changed;
4
+ function applySourceDocuments(result, resultSourceMap, getCachedDocument) {
5
+ let updateFn = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultUpdateFunction;
6
+ if (!resultSourceMap) return result;
7
+ return walkMap(result, (value, path) => {
8
+ var _a;
9
+ const resolveMappingResult = resolveMapping(path, resultSourceMap);
10
+ if (!resolveMappingResult) {
11
+ return value;
12
+ }
13
+ const {
14
+ mapping,
15
+ pathSuffix
16
+ } = resolveMappingResult;
17
+ if (mapping.type !== "value") {
18
+ return value;
19
+ }
20
+ if (mapping.source.type !== "documentValue") {
21
+ return value;
22
+ }
23
+ const sourceDocument = resultSourceMap.documents[mapping.source.document];
24
+ const sourcePath = resultSourceMap.paths[mapping.source.path];
25
+ if (sourceDocument) {
26
+ const cachedDocument = getCachedDocument(sourceDocument);
27
+ if (!cachedDocument) {
28
+ return value;
29
+ }
30
+ const parsedPath = parseJsonPath(sourcePath + pathSuffix);
31
+ const changedValue = cachedDocument ? (_a = getField(cachedDocument, parsedPath)) != null ? _a : value : value;
32
+ return value === changedValue ? value : updateFn(changedValue, {
33
+ cachedDocument,
34
+ previousValue: value,
35
+ sourceDocument,
36
+ sourcePath: parsedPath
37
+ });
38
+ }
39
+ return value;
40
+ });
41
+ }
42
+ function getField(obj, path) {
43
+ let value = obj;
44
+ for (const segment of path) {
45
+ if (typeof segment === "string") {
46
+ value = value[segment];
47
+ } else {
48
+ const match = typeof segment === "object" ? value.find(item => item._key === segment.key) : value[segment];
49
+ value = match || null;
50
+ }
51
+ if (value === null || value === void 0) {
52
+ break;
53
+ }
54
+ }
55
+ return value;
56
+ }
57
+ export { applySourceDocuments, parseJsonPath, resolveMapping, walkMap };
58
+ //# sourceMappingURL=csm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csm.js","sources":["../src/csm/applySourceDocuments.ts"],"sourcesContent":["import {parseJsonPath} from './jsonpath'\nimport {resolveMapping} from './resolveMapping'\nimport type {\n Any,\n ApplySourceDocumentsUpdateFunction,\n ContentSourceMap,\n ContentSourceMapDocuments,\n PathSegment,\n SanityDocument,\n} from './types'\nimport {walkMap} from './walkMap'\n\nconst defaultUpdateFunction = <T = unknown>(changed: T): T => changed\n\n/**\n * Optimistically applies source documents to a result, using the content source map to trace fields.\n * 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.\n * @alpha\n */\nexport function applySourceDocuments<Result = unknown>(\n result: Result,\n resultSourceMap: ContentSourceMap | undefined,\n getCachedDocument: (\n sourceDocument: ContentSourceMapDocuments[number],\n ) => SanityDocument | undefined,\n updateFn: ApplySourceDocumentsUpdateFunction = defaultUpdateFunction,\n): Result {\n if (!resultSourceMap) return result\n\n return walkMap(result, (value, path) => {\n const resolveMappingResult = resolveMapping(path, resultSourceMap)\n if (!resolveMappingResult) {\n return value\n }\n\n const {mapping, pathSuffix} = resolveMappingResult\n if (mapping.type !== 'value') {\n return value\n }\n\n if (mapping.source.type !== 'documentValue') {\n return value\n }\n\n const sourceDocument = resultSourceMap.documents[mapping.source.document]\n const sourcePath = resultSourceMap.paths[mapping.source.path]\n\n if (sourceDocument) {\n const cachedDocument = getCachedDocument(sourceDocument)\n if (!cachedDocument) {\n return value\n }\n\n const parsedPath = parseJsonPath(sourcePath + pathSuffix)\n const changedValue = cachedDocument ? getField(cachedDocument, parsedPath) ?? value : value\n return value === changedValue\n ? value\n : updateFn<Result[keyof Result]>(changedValue, {\n cachedDocument,\n previousValue: value as Result[keyof Result],\n sourceDocument,\n sourcePath: parsedPath,\n })\n }\n\n return value\n }) as Result\n}\n\nfunction getField(obj: SanityDocument, path: PathSegment[]): Any {\n let value = obj as SanityDocument | SanityDocument[keyof SanityDocument]\n for (const segment of path) {\n if (typeof segment === 'string') {\n value = value[segment]\n } else {\n const match =\n typeof segment === 'object'\n ? value.find((item: SanityDocument[keyof SanityDocument]) => item._key === segment.key)\n : value[segment]\n value = match || null\n }\n if (value === null || value === undefined) {\n break\n }\n }\n return value\n}\n"],"names":["defaultUpdateFunction","changed","applySourceDocuments","result","resultSourceMap","getCachedDocument","updateFn","walkMap","value","path","_a","resolveMappingResult","resolveMapping","mapping","pathSuffix","type","source","sourceDocument","documents","document","sourcePath","paths","cachedDocument","parsedPath","parseJsonPath","changedValue","getField","previousValue","obj","segment","match","find","item","_key","key"],"mappings":";;AAYA,MAAMA,qBAAA,GAAsCC,OAAkB,IAAAA,OAAA;AAOvD,SAASC,oBACdA,CAAAC,MAAA,EACAC,eACA,EAAAC,iBAAA,EAIQ;EAAA,IADRC,+EAA+CN,qBACvC;EACR,IAAI,CAACI,eAAA,EAAwB,OAAAD,MAAA;EAE7B,OAAOI,OAAQ,CAAAJ,MAAA,EAAQ,CAACK,KAAA,EAAOC,IAAS,KAAA;IA7B1C,IAAAC,EAAA;IA8BU,MAAAC,oBAAA,GAAuBC,cAAe,CAAAH,IAAA,EAAML,eAAe,CAAA;IACjE,IAAI,CAACO,oBAAsB,EAAA;MAClB,OAAAH,KAAA;IACT;IAEM,MAAA;MAACK,OAAS;MAAAC;IAAc,CAAA,GAAAH,oBAAA;IAC1B,IAAAE,OAAA,CAAQE,SAAS,OAAS,EAAA;MACrB,OAAAP,KAAA;IACT;IAEI,IAAAK,OAAA,CAAQG,MAAO,CAAAD,IAAA,KAAS,eAAiB,EAAA;MACpC,OAAAP,KAAA;IACT;IAEA,MAAMS,cAAiB,GAAAb,eAAA,CAAgBc,SAAU,CAAAL,OAAA,CAAQG,OAAOG,QAAQ,CAAA;IACxE,MAAMC,UAAa,GAAAhB,eAAA,CAAgBiB,KAAM,CAAAR,OAAA,CAAQG,OAAOP,IAAI,CAAA;IAE5D,IAAIQ,cAAgB,EAAA;MACZ,MAAAK,cAAA,GAAiBjB,kBAAkBY,cAAc,CAAA;MACvD,IAAI,CAACK,cAAgB,EAAA;QACZ,OAAAd,KAAA;MACT;MAEM,MAAAe,UAAA,GAAaC,aAAc,CAAAJ,UAAA,GAAaN,UAAU,CAAA;MACxD,MAAMW,eAAeH,cAAiB,GAAA,CAAAZ,EAAA,GAAAgB,QAAA,CAASJ,gBAAgBC,UAAU,CAAA,KAAnC,YAAwCf,KAAQ,GAAAA,KAAA;MACtF,OAAOA,KAAU,KAAAiB,YAAA,GACbjB,KACA,GAAAF,QAAA,CAA+BmB,YAAc,EAAA;QAC3CH,cAAA;QACAK,aAAe,EAAAnB,KAAA;QACfS,cAAA;QACAG,UAAY,EAAAG;MAAA,CACb,CAAA;IACP;IAEO,OAAAf,KAAA;EAAA,CACR,CAAA;AACH;AAEA,SAASkB,QAAAA,CAASE,KAAqBnB,IAA0B,EAAA;EAC/D,IAAID,KAAQ,GAAAoB,GAAA;EACZ,KAAA,MAAWC,WAAWpB,IAAM,EAAA;IACtB,IAAA,OAAOoB,YAAY,QAAU,EAAA;MAC/BrB,KAAA,GAAQA,MAAMqB,OAAO,CAAA;IAAA,CAChB,MAAA;MACL,MAAMC,KACJ,GAAA,OAAOD,OAAY,KAAA,QAAA,GACfrB,MAAMuB,IAAK,CAACC,IAA+C,IAAAA,IAAA,CAAKC,IAAS,KAAAJ,OAAA,CAAQK,GAAG,CAAA,GACpF1B,MAAMqB,OAAO,CAAA;MACnBrB,KAAA,GAAQsB,KAAS,IAAA,IAAA;IACnB;IACI,IAAAtB,KAAA,KAAU,IAAQ,IAAAA,KAAA,KAAU,KAAW,CAAA,EAAA;MACzC;IACF;EACF;EACO,OAAAA,KAAA;AACT;"}