@sanity/client 6.7.0 → 6.7.1-canary.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/dist/csm.cjs ADDED
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ const ESCAPE = {
7
+ "\f": "\\f",
8
+ "\n": "\\n",
9
+ "\r": "\\r",
10
+ " ": "\\t",
11
+ "'": "\\'",
12
+ "\\": "\\\\"
13
+ };
14
+ const UNESCAPE = {
15
+ "\\f": "\f",
16
+ "\\n": "\n",
17
+ "\\r": "\r",
18
+ "\\t": " ",
19
+ "\\'": "'",
20
+ "\\\\": "\\"
21
+ };
22
+ function jsonPath(path, opts) {
23
+ return "$".concat(path.map(segment => {
24
+ if (typeof segment === "string") {
25
+ const escapedKey = segment.replace(/[\f\n\r\t'\\]/g, match => {
26
+ return ESCAPE[match];
27
+ });
28
+ return "['".concat(escapedKey, "']");
29
+ }
30
+ if (typeof segment === "number") {
31
+ return "[".concat(segment, "]");
32
+ }
33
+ if ((opts == null ? void 0 : opts.keyArraySelectors) && segment.key !== "") {
34
+ const escapedKey = segment.key.replace(/['\\]/g, match => {
35
+ return ESCAPE[match];
36
+ });
37
+ return "[?(@._key=='".concat(escapedKey, "')]");
38
+ }
39
+ return "[".concat(segment.index, "]");
40
+ }).join(""));
41
+ }
42
+ function parseJsonPath(path) {
43
+ const parsed = [];
44
+ const parseRe = /\['(.*?)'\]|\[(\d+)\]|\[\?\(@\._key=='(.*?)'\)\]/g;
45
+ let match;
46
+ while ((match = parseRe.exec(path)) !== null) {
47
+ if (match[1] !== void 0) {
48
+ const key = match[1].replace(/\\(\\|f|n|r|t|')/g, m => {
49
+ return UNESCAPE[m];
50
+ });
51
+ parsed.push(key);
52
+ continue;
53
+ }
54
+ if (match[2] !== void 0) {
55
+ parsed.push(parseInt(match[2], 10));
56
+ continue;
57
+ }
58
+ if (match[3] !== void 0) {
59
+ const key = match[3].replace(/\\(\\')/g, m => {
60
+ return UNESCAPE[m];
61
+ });
62
+ parsed.push({
63
+ key,
64
+ index: -1
65
+ });
66
+ continue;
67
+ }
68
+ }
69
+ return parsed;
70
+ }
71
+ function encodeJsonPathToUriComponent(path) {
72
+ const sourcePath = Array.isArray(path) ? path : parseJsonPath(path);
73
+ return encodeURIComponent(sourcePath.map((key, i) =>
74
+ // eslint-disable-next-line no-nested-ternary
75
+ typeof key === "number" ? "[".concat(key, "]") : i > 0 ? ".".concat(key) : key).join(""));
76
+ }
77
+ function isRecord(value) {
78
+ return typeof value === "object" && value !== null;
79
+ }
80
+ function isArray(value) {
81
+ return value !== null && Array.isArray(value);
82
+ }
83
+ function walkMap(value, mappingFn) {
84
+ let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
85
+ if (isArray(value)) {
86
+ return value.map((v, idx) => {
87
+ if (isRecord(v)) {
88
+ const key = v["_key"];
89
+ if (typeof key === "string") {
90
+ return walkMap(v, mappingFn, path.concat({
91
+ key,
92
+ index: idx
93
+ }));
94
+ }
95
+ }
96
+ return walkMap(v, mappingFn, path.concat(idx));
97
+ });
98
+ }
99
+ if (isRecord(value)) {
100
+ return Object.fromEntries(Object.entries(value).map(_ref => {
101
+ let [k, v] = _ref;
102
+ return [k, walkMap(v, mappingFn, path.concat(k))];
103
+ }));
104
+ }
105
+ return mappingFn(value, path);
106
+ }
107
+ function resolveMapping(resultPath, csm) {
108
+ const resultJsonPath = jsonPath(resultPath);
109
+ if (csm.mappings[resultJsonPath] !== void 0) {
110
+ return [csm.mappings[resultJsonPath], resultJsonPath, ""];
111
+ }
112
+ const mappings = Object.entries(csm.mappings).filter(_ref2 => {
113
+ let [key] = _ref2;
114
+ return resultJsonPath.startsWith(key);
115
+ }).sort((_ref3, _ref4) => {
116
+ let [key1] = _ref3;
117
+ let [key2] = _ref4;
118
+ return key2.length - key1.length;
119
+ });
120
+ if (mappings.length == 0) {
121
+ return void 0;
122
+ }
123
+ const [matchedPath, mapping] = mappings[0];
124
+ const pathSuffix = resultJsonPath.substring(matchedPath.length);
125
+ return [mapping, matchedPath, pathSuffix];
126
+ }
127
+ exports.encodeJsonPathToUriComponent = encodeJsonPathToUriComponent;
128
+ exports.jsonPath = jsonPath;
129
+ exports.resolveMapping = resolveMapping;
130
+ exports.walkMap = walkMap;
131
+ //# sourceMappingURL=csm.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csm.cjs","sources":["../src/csm/jsonpath.ts","../src/csm/editIntent.ts","../src/csm/sourcemap.ts"],"sourcesContent":["import type { PathSegment } from './types'\n\nconst ESCAPE: Record<string, string> = {\n '\\f': '\\\\f',\n '\\n': '\\\\n',\n '\\r': '\\\\r',\n '\\t': '\\\\t',\n \"'\": \"\\\\'\",\n '\\\\': '\\\\\\\\',\n}\n\nconst UNESCAPE: Record<string, string> = {\n '\\\\f': '\\f',\n '\\\\n': '\\n',\n '\\\\r': '\\r',\n '\\\\t': '\\t',\n \"\\\\'\": \"'\",\n '\\\\\\\\': '\\\\',\n}\n\n/** @internal */\nexport function jsonPath(\n path: PathSegment[],\n opts?: {\n keyArraySelectors: boolean\n },\n): string {\n return `$${path\n .map((segment) => {\n if (typeof segment === 'string') {\n const escapedKey = segment.replace(/[\\f\\n\\r\\t'\\\\]/g, (match) => {\n return ESCAPE[match]\n })\n return `['${escapedKey}']`\n }\n\n if (typeof segment === 'number') {\n return `[${segment}]`\n }\n\n if (opts?.keyArraySelectors && segment.key !== '') {\n const escapedKey = segment.key.replace(/['\\\\]/g, (match) => {\n return ESCAPE[match]\n })\n return `[?(@._key=='${escapedKey}')]`\n }\n\n return `[${segment.index}]`\n })\n .join('')}`\n}\n\n/** @internal */\nexport function parseJsonPath(path: string): PathSegment[] {\n const parsed: PathSegment[] = []\n\n const parseRe = /\\['(.*?)'\\]|\\[(\\d+)\\]|\\[\\?\\(@\\._key=='(.*?)'\\)\\]/g\n let match: RegExpExecArray | null\n\n while ((match = parseRe.exec(path)) !== null) {\n if (match[1] !== undefined) {\n const key = match[1].replace(/\\\\(\\\\|f|n|r|t|')/g, (m) => {\n return UNESCAPE[m]\n })\n\n parsed.push(key)\n continue\n }\n\n if (match[2] !== undefined) {\n parsed.push(parseInt(match[2], 10))\n continue\n }\n\n if (match[3] !== undefined) {\n const key = match[3].replace(/\\\\(\\\\')/g, (m) => {\n return UNESCAPE[m]\n })\n\n parsed.push({\n key,\n index: -1,\n })\n continue\n }\n }\n\n return parsed\n}\n","import {ContentSourceMapDocument, ContentSourceMapDocuments} from '../types'\nimport {parseJsonPath} from './jsonpath'\nimport type {PathSegment, StudioUrl} from './types'\n\n/** @alpha */\nexport type EditLink = `/intent/edit/id=${string};type=${string};path=${string}`\n\n/** @alpha */\nexport interface EditLinkProps {\n studioUrl: StudioUrl\n document: ContentSourceMapDocument\n}\n\n/** @alpha */\nexport type DefineEditLink = (\n studioUrl: StudioUrl,\n) => (sourceDocument: ContentSourceMapDocuments[number]) => `${StudioUrl}${EditLink}`\n\n/** @alpha */\nexport function defineEditLink(\n _studioUrl: StudioUrl,\n): (\n sourceDocument: ContentSourceMapDocuments[number],\n path: string | PathSegment[],\n) => `${StudioUrl}${EditLink}` {\n const studioUrl = _studioUrl.replace(/\\/$/, '')\n return ({_id, _type}, path) =>\n `${studioUrl}/intent/edit/id=${_id};type=${_type};path=${encodeJsonPathToUriComponent(path)}`\n}\n\n/** @alpha */\nexport function encodeJsonPathToUriComponent(path: string | PathSegment[]): string {\n const sourcePath = Array.isArray(path) ? path : parseJsonPath(path)\n return encodeURIComponent(\n sourcePath\n .map((key, i) =>\n // eslint-disable-next-line no-nested-ternary\n typeof key === 'number' ? `[${key}]` : i > 0 ? `.${key}` : key,\n )\n .join(''),\n )\n}\n","import type {ContentSourceMap, ContentSourceMapDocuments, ContentSourceMapMapping} from '../types'\nimport {jsonPath, parseJsonPath} from './jsonpath'\nimport type {PathSegment} from './types'\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\nfunction isArray(value: unknown): value is Array<unknown> {\n return value !== null && Array.isArray(value)\n}\n\n/** @alpha */\nexport type Encoder<E> = (\n value: string,\n sourceDocument: ContentSourceMapDocuments[number],\n path: PathSegment[],\n) => E\n\n/** @alpha */\nexport function encode<R, E>(\n result: R,\n csm: ContentSourceMap,\n encoder: Encoder<E>,\n options?: {keyArraySelectors: boolean},\n): R {\n return encodeIntoResult(result, csm, encoder, options) as R\n}\n\n/** @alpha */\nexport function encodeIntoResult<R>(\n result: R,\n csm: ContentSourceMap,\n encoder: Encoder<unknown>,\n options?: {keyArraySelectors: boolean},\n): ReturnType<Encoder<unknown>> {\n return walkMap(result, (value, path) => {\n // Only map strings, we could extend this in the future to support other types like integers...\n if (typeof value !== 'string') {\n return value\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const resolveMappingResult = resolveMapping(path, csm)\n if (!resolveMappingResult) {\n return value\n }\n\n const [mapping, matchedPath, 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 =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n csm.documents[mapping.source.document!]\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const sourcePath = csm.paths[mapping.source.path]\n\n if (options?.keyArraySelectors) {\n const matchPathSegments = parseJsonPath(matchedPath)\n const sourcePathSegments = parseJsonPath(sourcePath)\n const fullSourceSegments = sourcePathSegments.concat(path.slice(matchPathSegments.length))\n\n return encoder(value, sourceDocument, fullSourceSegments)\n }\n\n return encoder(value, sourceDocument, parseJsonPath(sourcePath + pathSuffix))\n })\n}\n\n/** @alpha */\nexport type WalkMapFn = (value: unknown, path: PathSegment[]) => unknown\n\n/** generic way to walk a nested object or array and apply a mapping function to each value\n * @alpha\n */\nexport function walkMap(value: unknown, mappingFn: WalkMapFn, path: PathSegment[] = []): unknown {\n if (isArray(value)) {\n return value.map((v, idx) => {\n if (isRecord(v)) {\n const key = v['_key']\n if (typeof key === 'string') {\n return walkMap(v, mappingFn, path.concat({key, index: idx}))\n }\n }\n\n return walkMap(v, mappingFn, path.concat(idx))\n })\n }\n\n if (isRecord(value)) {\n return Object.fromEntries(\n Object.entries(value).map(([k, v]) => [k, walkMap(v, mappingFn, path.concat(k))]),\n )\n }\n\n return mappingFn(value, path)\n}\n\n/** @alpha */\nexport function resolveMapping(\n resultPath: PathSegment[],\n csm: ContentSourceMap,\n): [ContentSourceMapMapping, string, string] | undefined {\n const resultJsonPath = jsonPath(resultPath)\n\n if (csm.mappings[resultJsonPath] !== undefined) {\n return [csm.mappings[resultJsonPath], resultJsonPath, '']\n }\n\n const mappings = Object.entries(csm.mappings)\n .filter(([key]) => resultJsonPath.startsWith(key))\n .sort(([key1], [key2]) => key2.length - key1.length)\n\n if (mappings.length == 0) {\n return undefined\n }\n\n const [matchedPath, mapping] = mappings[0]\n const pathSuffix = resultJsonPath.substring(matchedPath.length)\n return [mapping, matchedPath, pathSuffix]\n}\n"],"names":["ESCAPE","UNESCAPE","jsonPath","path","opts","concat","map","segment","escapedKey","replace","match","keyArraySelectors","key","index","join","parseJsonPath","parsed","parseRe","exec","m","push","parseInt","encodeJsonPathToUriComponent","sourcePath","Array","isArray","encodeURIComponent","i","isRecord","value","walkMap","mappingFn","arguments","length","undefined","v","idx","Object","fromEntries","entries","_ref","k","resolveMapping","resultPath","csm","resultJsonPath","mappings","filter","_ref2","startsWith","sort","_ref3","_ref4","key1","key2","matchedPath","mapping","pathSuffix","substring"],"mappings":";;;;;AAEA,MAAMA,MAAiC,GAAA;EACrC,IAAM,EAAA,KAAA;EACN,IAAM,EAAA,KAAA;EACN,IAAM,EAAA,KAAA;EACN,GAAM,EAAA,KAAA;EACN,GAAK,EAAA,KAAA;EACL,IAAM,EAAA;AACR,CAAA;AAEA,MAAMC,QAAmC,GAAA;EACvC,KAAO,EAAA,IAAA;EACP,KAAO,EAAA,IAAA;EACP,KAAO,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UAAU,CAAA;EAE1C,IAAIC,GAAI,CAAAE,QAAA,CAASD,cAAc,CAAA,KAAM,KAAW,CAAA,EAAA;IAC9C,OAAO,CAACD,GAAI,CAAAE,QAAA,CAASD,cAAc,CAAA,EAAGA,gBAAgB,EAAE,CAAA;EAC1D;EAEM,MAAAC,QAAA,GAAWT,MAAO,CAAAE,OAAA,CAAQK,GAAI,CAAAE,QAAQ,CACzC,CAAAC,MAAA,CAAOC,KAAA;IAAA,IAAC,CAACpC,GAAG,CAAA,GAAAoC,KAAA;IAAA,OAAMH,cAAe,CAAAI,UAAA,CAAWrC,GAAG,CAAC;EAAA,EAChD,CAAAsC,IAAA,CAAK,CAAAC,KAAA,EAAAC,KAAA;IAAA,IAAC,CAACC,IAAI,CAAA,GAAAF,KAAA;IAAA,IAAG,CAACG,IAAI,CAAM,GAAAF,KAAA;IAAA,OAAAE,IAAA,CAAKrB,MAAS,GAAAoB,IAAA,CAAKpB,MAAM;EAAA,EAAA;EAEjD,IAAAa,QAAA,CAASb,UAAU,CAAG,EAAA;IACjB,OAAA,KAAA,CAAA;EACT;EAEA,MAAM,CAACsB,WAAA,EAAaC,OAAO,CAAA,GAAIV,SAAS,CAAC,CAAA;EACzC,MAAMW,UAAa,GAAAZ,cAAA,CAAea,SAAU,CAAAH,WAAA,CAAYtB,MAAM,CAAA;EACvD,OAAA,CAACuB,OAAS,EAAAD,WAAA,EAAaE,UAAU,CAAA;AAC1C;;;;"}
package/dist/csm.d.ts ADDED
@@ -0,0 +1,126 @@
1
+ /// <reference types="node" />
2
+
3
+ /** @public */
4
+ export declare interface ContentSourceMap {
5
+ mappings: ContentSourceMapMappings
6
+ documents: ContentSourceMapDocuments
7
+ paths: ContentSourceMapPaths
8
+ }
9
+
10
+ /** @public */
11
+ export declare interface ContentSourceMapDocument extends ContentSourceMapDocumentBase {
12
+ _projectId?: undefined
13
+ _dataset?: undefined
14
+ }
15
+
16
+ /** @public */
17
+ export declare interface ContentSourceMapDocumentBase {
18
+ _id: string
19
+ _type: string
20
+ }
21
+
22
+ /** @public */
23
+ export declare type ContentSourceMapDocuments = (
24
+ | ContentSourceMapDocument
25
+ | ContentSourceMapRemoteDocument
26
+ )[]
27
+
28
+ /**
29
+ * DocumentValueSource is a path to a value within a document
30
+ * @public
31
+ */
32
+ export declare interface ContentSourceMapDocumentValueSource {
33
+ type: 'documentValue'
34
+ document: number
35
+ path: number
36
+ }
37
+
38
+ /**
39
+ * When a value is not from a source, its a literal
40
+ * @public
41
+ */
42
+ export declare interface ContentSourceMapLiteralSource {
43
+ type: 'literal'
44
+ }
45
+
46
+ /** @public */
47
+ export declare type ContentSourceMapMapping = ContentSourceMapValueMapping
48
+
49
+ /** @public */
50
+ export declare type ContentSourceMapMappings = Record<string, ContentSourceMapMapping>
51
+
52
+ /** @public */
53
+ export declare type ContentSourceMapPaths = string[]
54
+
55
+ /** @public */
56
+ export declare interface ContentSourceMapRemoteDocument extends ContentSourceMapDocumentBase {
57
+ _projectId: string
58
+ _dataset: string
59
+ }
60
+
61
+ /** @public */
62
+ export declare type ContentSourceMapSource =
63
+ | ContentSourceMapDocumentValueSource
64
+ | ContentSourceMapLiteralSource
65
+ | ContentSourceMapUnknownSource
66
+
67
+ /**
68
+ * When a field source is unknown
69
+ * @public
70
+ */
71
+ export declare interface ContentSourceMapUnknownSource {
72
+ type: 'unknown'
73
+ }
74
+
75
+ /**
76
+ * ValueMapping is a mapping when for value that is from a single source value
77
+ * It may refer to a field within a document or a literal value
78
+ * @public
79
+ */
80
+ export declare interface ContentSourceMapValueMapping {
81
+ type: 'value'
82
+ source: ContentSourceMapSource
83
+ }
84
+
85
+ /** @alpha */
86
+ export declare function encodeJsonPathToUriComponent(path: string | PathSegment[]): string
87
+
88
+ /** @internal */
89
+ export declare function jsonPath(
90
+ path: PathSegment[],
91
+ opts?: {
92
+ keyArraySelectors: boolean
93
+ },
94
+ ): string
95
+
96
+ /** @public */
97
+ export declare type KeyedSegment = {
98
+ key: string
99
+ index: number
100
+ }
101
+
102
+ /** @public */
103
+ export declare type PathSegment = string | number | KeyedSegment
104
+
105
+ /** @alpha */
106
+ export declare function resolveMapping(
107
+ resultPath: PathSegment[],
108
+ csm: ContentSourceMap,
109
+ ): [ContentSourceMapMapping, string, string] | undefined
110
+
111
+ /** @public */
112
+ export declare type StudioUrl =
113
+ | `/${string}`
114
+ | `${string}.sanity.studio`
115
+ | `https://${string}`
116
+ | string
117
+
118
+ /** generic way to walk a nested object or array and apply a mapping function to each value
119
+ * @alpha
120
+ */
121
+ export declare function walkMap(value: unknown, mappingFn: WalkMapFn, path?: PathSegment[]): unknown
122
+
123
+ /** @alpha */
124
+ export declare type WalkMapFn = (value: unknown, path: PathSegment[]) => unknown
125
+
126
+ export {}
package/dist/csm.js ADDED
@@ -0,0 +1,123 @@
1
+ const ESCAPE = {
2
+ "\f": "\\f",
3
+ "\n": "\\n",
4
+ "\r": "\\r",
5
+ " ": "\\t",
6
+ "'": "\\'",
7
+ "\\": "\\\\"
8
+ };
9
+ const UNESCAPE = {
10
+ "\\f": "\f",
11
+ "\\n": "\n",
12
+ "\\r": "\r",
13
+ "\\t": " ",
14
+ "\\'": "'",
15
+ "\\\\": "\\"
16
+ };
17
+ function jsonPath(path, opts) {
18
+ return "$".concat(path.map(segment => {
19
+ if (typeof segment === "string") {
20
+ const escapedKey = segment.replace(/[\f\n\r\t'\\]/g, match => {
21
+ return ESCAPE[match];
22
+ });
23
+ return "['".concat(escapedKey, "']");
24
+ }
25
+ if (typeof segment === "number") {
26
+ return "[".concat(segment, "]");
27
+ }
28
+ if ((opts == null ? void 0 : opts.keyArraySelectors) && segment.key !== "") {
29
+ const escapedKey = segment.key.replace(/['\\]/g, match => {
30
+ return ESCAPE[match];
31
+ });
32
+ return "[?(@._key=='".concat(escapedKey, "')]");
33
+ }
34
+ return "[".concat(segment.index, "]");
35
+ }).join(""));
36
+ }
37
+ function parseJsonPath(path) {
38
+ const parsed = [];
39
+ const parseRe = /\['(.*?)'\]|\[(\d+)\]|\[\?\(@\._key=='(.*?)'\)\]/g;
40
+ let match;
41
+ while ((match = parseRe.exec(path)) !== null) {
42
+ if (match[1] !== void 0) {
43
+ const key = match[1].replace(/\\(\\|f|n|r|t|')/g, m => {
44
+ return UNESCAPE[m];
45
+ });
46
+ parsed.push(key);
47
+ continue;
48
+ }
49
+ if (match[2] !== void 0) {
50
+ parsed.push(parseInt(match[2], 10));
51
+ continue;
52
+ }
53
+ if (match[3] !== void 0) {
54
+ const key = match[3].replace(/\\(\\')/g, m => {
55
+ return UNESCAPE[m];
56
+ });
57
+ parsed.push({
58
+ key,
59
+ index: -1
60
+ });
61
+ continue;
62
+ }
63
+ }
64
+ return parsed;
65
+ }
66
+ function encodeJsonPathToUriComponent(path) {
67
+ const sourcePath = Array.isArray(path) ? path : parseJsonPath(path);
68
+ return encodeURIComponent(sourcePath.map((key, i) =>
69
+ // eslint-disable-next-line no-nested-ternary
70
+ typeof key === "number" ? "[".concat(key, "]") : i > 0 ? ".".concat(key) : key).join(""));
71
+ }
72
+ function isRecord(value) {
73
+ return typeof value === "object" && value !== null;
74
+ }
75
+ function isArray(value) {
76
+ return value !== null && Array.isArray(value);
77
+ }
78
+ function walkMap(value, mappingFn) {
79
+ let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
80
+ if (isArray(value)) {
81
+ return value.map((v, idx) => {
82
+ if (isRecord(v)) {
83
+ const key = v["_key"];
84
+ if (typeof key === "string") {
85
+ return walkMap(v, mappingFn, path.concat({
86
+ key,
87
+ index: idx
88
+ }));
89
+ }
90
+ }
91
+ return walkMap(v, mappingFn, path.concat(idx));
92
+ });
93
+ }
94
+ if (isRecord(value)) {
95
+ return Object.fromEntries(Object.entries(value).map(_ref => {
96
+ let [k, v] = _ref;
97
+ return [k, walkMap(v, mappingFn, path.concat(k))];
98
+ }));
99
+ }
100
+ return mappingFn(value, path);
101
+ }
102
+ function resolveMapping(resultPath, csm) {
103
+ const resultJsonPath = jsonPath(resultPath);
104
+ if (csm.mappings[resultJsonPath] !== void 0) {
105
+ return [csm.mappings[resultJsonPath], resultJsonPath, ""];
106
+ }
107
+ const mappings = Object.entries(csm.mappings).filter(_ref2 => {
108
+ let [key] = _ref2;
109
+ return resultJsonPath.startsWith(key);
110
+ }).sort((_ref3, _ref4) => {
111
+ let [key1] = _ref3;
112
+ let [key2] = _ref4;
113
+ return key2.length - key1.length;
114
+ });
115
+ if (mappings.length == 0) {
116
+ return void 0;
117
+ }
118
+ const [matchedPath, mapping] = mappings[0];
119
+ const pathSuffix = resultJsonPath.substring(matchedPath.length);
120
+ return [mapping, matchedPath, pathSuffix];
121
+ }
122
+ export { encodeJsonPathToUriComponent, jsonPath, resolveMapping, walkMap };
123
+ //# sourceMappingURL=csm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"csm.js","sources":["../src/csm/jsonpath.ts","../src/csm/editIntent.ts","../src/csm/sourcemap.ts"],"sourcesContent":["import type { PathSegment } from './types'\n\nconst ESCAPE: Record<string, string> = {\n '\\f': '\\\\f',\n '\\n': '\\\\n',\n '\\r': '\\\\r',\n '\\t': '\\\\t',\n \"'\": \"\\\\'\",\n '\\\\': '\\\\\\\\',\n}\n\nconst UNESCAPE: Record<string, string> = {\n '\\\\f': '\\f',\n '\\\\n': '\\n',\n '\\\\r': '\\r',\n '\\\\t': '\\t',\n \"\\\\'\": \"'\",\n '\\\\\\\\': '\\\\',\n}\n\n/** @internal */\nexport function jsonPath(\n path: PathSegment[],\n opts?: {\n keyArraySelectors: boolean\n },\n): string {\n return `$${path\n .map((segment) => {\n if (typeof segment === 'string') {\n const escapedKey = segment.replace(/[\\f\\n\\r\\t'\\\\]/g, (match) => {\n return ESCAPE[match]\n })\n return `['${escapedKey}']`\n }\n\n if (typeof segment === 'number') {\n return `[${segment}]`\n }\n\n if (opts?.keyArraySelectors && segment.key !== '') {\n const escapedKey = segment.key.replace(/['\\\\]/g, (match) => {\n return ESCAPE[match]\n })\n return `[?(@._key=='${escapedKey}')]`\n }\n\n return `[${segment.index}]`\n })\n .join('')}`\n}\n\n/** @internal */\nexport function parseJsonPath(path: string): PathSegment[] {\n const parsed: PathSegment[] = []\n\n const parseRe = /\\['(.*?)'\\]|\\[(\\d+)\\]|\\[\\?\\(@\\._key=='(.*?)'\\)\\]/g\n let match: RegExpExecArray | null\n\n while ((match = parseRe.exec(path)) !== null) {\n if (match[1] !== undefined) {\n const key = match[1].replace(/\\\\(\\\\|f|n|r|t|')/g, (m) => {\n return UNESCAPE[m]\n })\n\n parsed.push(key)\n continue\n }\n\n if (match[2] !== undefined) {\n parsed.push(parseInt(match[2], 10))\n continue\n }\n\n if (match[3] !== undefined) {\n const key = match[3].replace(/\\\\(\\\\')/g, (m) => {\n return UNESCAPE[m]\n })\n\n parsed.push({\n key,\n index: -1,\n })\n continue\n }\n }\n\n return parsed\n}\n","import {ContentSourceMapDocument, ContentSourceMapDocuments} from '../types'\nimport {parseJsonPath} from './jsonpath'\nimport type {PathSegment, StudioUrl} from './types'\n\n/** @alpha */\nexport type EditLink = `/intent/edit/id=${string};type=${string};path=${string}`\n\n/** @alpha */\nexport interface EditLinkProps {\n studioUrl: StudioUrl\n document: ContentSourceMapDocument\n}\n\n/** @alpha */\nexport type DefineEditLink = (\n studioUrl: StudioUrl,\n) => (sourceDocument: ContentSourceMapDocuments[number]) => `${StudioUrl}${EditLink}`\n\n/** @alpha */\nexport function defineEditLink(\n _studioUrl: StudioUrl,\n): (\n sourceDocument: ContentSourceMapDocuments[number],\n path: string | PathSegment[],\n) => `${StudioUrl}${EditLink}` {\n const studioUrl = _studioUrl.replace(/\\/$/, '')\n return ({_id, _type}, path) =>\n `${studioUrl}/intent/edit/id=${_id};type=${_type};path=${encodeJsonPathToUriComponent(path)}`\n}\n\n/** @alpha */\nexport function encodeJsonPathToUriComponent(path: string | PathSegment[]): string {\n const sourcePath = Array.isArray(path) ? path : parseJsonPath(path)\n return encodeURIComponent(\n sourcePath\n .map((key, i) =>\n // eslint-disable-next-line no-nested-ternary\n typeof key === 'number' ? `[${key}]` : i > 0 ? `.${key}` : key,\n )\n .join(''),\n )\n}\n","import type {ContentSourceMap, ContentSourceMapDocuments, ContentSourceMapMapping} from '../types'\nimport {jsonPath, parseJsonPath} from './jsonpath'\nimport type {PathSegment} from './types'\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\nfunction isArray(value: unknown): value is Array<unknown> {\n return value !== null && Array.isArray(value)\n}\n\n/** @alpha */\nexport type Encoder<E> = (\n value: string,\n sourceDocument: ContentSourceMapDocuments[number],\n path: PathSegment[],\n) => E\n\n/** @alpha */\nexport function encode<R, E>(\n result: R,\n csm: ContentSourceMap,\n encoder: Encoder<E>,\n options?: {keyArraySelectors: boolean},\n): R {\n return encodeIntoResult(result, csm, encoder, options) as R\n}\n\n/** @alpha */\nexport function encodeIntoResult<R>(\n result: R,\n csm: ContentSourceMap,\n encoder: Encoder<unknown>,\n options?: {keyArraySelectors: boolean},\n): ReturnType<Encoder<unknown>> {\n return walkMap(result, (value, path) => {\n // Only map strings, we could extend this in the future to support other types like integers...\n if (typeof value !== 'string') {\n return value\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const resolveMappingResult = resolveMapping(path, csm)\n if (!resolveMappingResult) {\n return value\n }\n\n const [mapping, matchedPath, 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 =\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n csm.documents[mapping.source.document!]\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const sourcePath = csm.paths[mapping.source.path]\n\n if (options?.keyArraySelectors) {\n const matchPathSegments = parseJsonPath(matchedPath)\n const sourcePathSegments = parseJsonPath(sourcePath)\n const fullSourceSegments = sourcePathSegments.concat(path.slice(matchPathSegments.length))\n\n return encoder(value, sourceDocument, fullSourceSegments)\n }\n\n return encoder(value, sourceDocument, parseJsonPath(sourcePath + pathSuffix))\n })\n}\n\n/** @alpha */\nexport type WalkMapFn = (value: unknown, path: PathSegment[]) => unknown\n\n/** generic way to walk a nested object or array and apply a mapping function to each value\n * @alpha\n */\nexport function walkMap(value: unknown, mappingFn: WalkMapFn, path: PathSegment[] = []): unknown {\n if (isArray(value)) {\n return value.map((v, idx) => {\n if (isRecord(v)) {\n const key = v['_key']\n if (typeof key === 'string') {\n return walkMap(v, mappingFn, path.concat({key, index: idx}))\n }\n }\n\n return walkMap(v, mappingFn, path.concat(idx))\n })\n }\n\n if (isRecord(value)) {\n return Object.fromEntries(\n Object.entries(value).map(([k, v]) => [k, walkMap(v, mappingFn, path.concat(k))]),\n )\n }\n\n return mappingFn(value, path)\n}\n\n/** @alpha */\nexport function resolveMapping(\n resultPath: PathSegment[],\n csm: ContentSourceMap,\n): [ContentSourceMapMapping, string, string] | undefined {\n const resultJsonPath = jsonPath(resultPath)\n\n if (csm.mappings[resultJsonPath] !== undefined) {\n return [csm.mappings[resultJsonPath], resultJsonPath, '']\n }\n\n const mappings = Object.entries(csm.mappings)\n .filter(([key]) => resultJsonPath.startsWith(key))\n .sort(([key1], [key2]) => key2.length - key1.length)\n\n if (mappings.length == 0) {\n return undefined\n }\n\n const [matchedPath, mapping] = mappings[0]\n const pathSuffix = resultJsonPath.substring(matchedPath.length)\n return [mapping, matchedPath, pathSuffix]\n}\n"],"names":["ESCAPE","UNESCAPE","jsonPath","path","opts","concat","map","segment","escapedKey","replace","match","keyArraySelectors","key","index","join","parseJsonPath","parsed","parseRe","exec","m","push","parseInt","encodeJsonPathToUriComponent","sourcePath","Array","isArray","encodeURIComponent","i","isRecord","value","walkMap","mappingFn","arguments","length","undefined","v","idx","Object","fromEntries","entries","_ref","k","resolveMapping","resultPath","csm","resultJsonPath","mappings","filter","_ref2","startsWith","sort","_ref3","_ref4","key1","key2","matchedPath","mapping","pathSuffix","substring"],"mappings":"AAEA,MAAMA,MAAiC,GAAA;EACrC,IAAM,EAAA,KAAA;EACN,IAAM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,OAAOoC,CAAC,CAAC,CAAC,CAAC;IAAA,EAAA,CAClF;EACF;EAEO,OAAAV,SAAA,CAAUF,OAAO1B,IAAI,CAAA;AAC9B;AAGgB,SAAAuC,cAAAA,CACdC,YACAC,GACuD,EAAA;EACjD,MAAAC,cAAA,GAAiB3C,SAASyC,UAAU,CAAA;EAE1C,IAAIC,GAAI,CAAAE,QAAA,CAASD,cAAc,CAAA,KAAM,KAAW,CAAA,EAAA;IAC9C,OAAO,CAACD,GAAI,CAAAE,QAAA,CAASD,cAAc,CAAA,EAAGA,gBAAgB,EAAE,CAAA;EAC1D;EAEM,MAAAC,QAAA,GAAWT,MAAO,CAAAE,OAAA,CAAQK,GAAI,CAAAE,QAAQ,CACzC,CAAAC,MAAA,CAAOC,KAAA;IAAA,IAAC,CAACpC,GAAG,CAAA,GAAAoC,KAAA;IAAA,OAAMH,cAAe,CAAAI,UAAA,CAAWrC,GAAG,CAAC;EAAA,EAChD,CAAAsC,IAAA,CAAK,CAAAC,KAAA,EAAAC,KAAA;IAAA,IAAC,CAACC,IAAI,CAAA,GAAAF,KAAA;IAAA,IAAG,CAACG,IAAI,CAAM,GAAAF,KAAA;IAAA,OAAAE,IAAA,CAAKrB,MAAS,GAAAoB,IAAA,CAAKpB,MAAM;EAAA,EAAA;EAEjD,IAAAa,QAAA,CAASb,UAAU,CAAG,EAAA;IACjB,OAAA,KAAA,CAAA;EACT;EAEA,MAAM,CAACsB,WAAA,EAAaC,OAAO,CAAA,GAAIV,SAAS,CAAC,CAAA;EACzC,MAAMW,UAAa,GAAAZ,cAAA,CAAea,SAAU,CAAAH,WAAA,CAAYtB,MAAM,CAAA;EACvD,OAAA,CAACuB,OAAS,EAAAD,WAAA,EAAaE,UAAU,CAAA;AAC1C;"}
package/dist/index.cjs CHANGED
@@ -8,7 +8,7 @@ var getIt = require('get-it');
8
8
  var rxjs = require('rxjs');
9
9
  var operators = require('rxjs/operators');
10
10
  var name = "@sanity/client";
11
- var version = "6.7.0";
11
+ var version = "6.7.1-canary.1";
12
12
  const middleware = [middleware$1.debug({
13
13
  verbose: true,
14
14
  namespace: "sanity:client"
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ export { adapter as unstable__adapter, environment as unstable__environment } fr
4
4
  import { Observable, lastValueFrom } from 'rxjs';
5
5
  import { map, filter } from 'rxjs/operators';
6
6
  var name = "@sanity/client";
7
- var version = "6.7.0";
7
+ var version = "6.7.1-canary.1";
8
8
  const middleware = [debug({
9
9
  verbose: true,
10
10
  namespace: "sanity:client"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "6.7.0",
3
+ "version": "6.7.1-canary.1",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -44,6 +44,13 @@
44
44
  },
45
45
  "default": "./dist/index.js"
46
46
  },
47
+ "./csm": {
48
+ "types": "./dist/csm.d.ts",
49
+ "source": "./src/csm/index.ts",
50
+ "import": "./dist/csm.js",
51
+ "require": "./dist/csm.cjs",
52
+ "default": "./dist/csm.js"
53
+ },
47
54
  "./package.json": "./package.json"
48
55
  },
49
56
  "main": "./dist/index.cjs",
@@ -55,6 +62,13 @@
55
62
  "./dist/index.js": "./dist/index.browser.js"
56
63
  },
57
64
  "types": "./dist/index.d.ts",
65
+ "typesVersions": {
66
+ "*": {
67
+ "csm": [
68
+ "./dist/csm.d.ts"
69
+ ]
70
+ }
71
+ },
58
72
  "files": [
59
73
  "dist",
60
74
  "src",
@@ -91,33 +105,33 @@
91
105
  },
92
106
  "dependencies": {
93
107
  "@sanity/eventsource": "^5.0.0",
94
- "get-it": "^8.4.3",
108
+ "get-it": "^8.4.4",
95
109
  "rxjs": "^7.0.0"
96
110
  },
97
111
  "devDependencies": {
98
- "@edge-runtime/types": "^2.2.4",
99
- "@edge-runtime/vm": "^3.1.4",
100
- "@rollup/plugin-commonjs": "^25.0.5",
112
+ "@edge-runtime/types": "^2.2.6",
113
+ "@edge-runtime/vm": "^3.1.6",
114
+ "@rollup/plugin-commonjs": "^25.0.7",
101
115
  "@rollup/plugin-node-resolve": "^15.2.3",
102
- "@sanity/pkg-utils": "^3.0.0",
103
- "@types/node": "^20.8.4",
104
- "@typescript-eslint/eslint-plugin": "^6.7.5",
105
- "@typescript-eslint/parser": "^6.7.5",
116
+ "@sanity/pkg-utils": "^3.2.1",
117
+ "@types/node": "^20.8.8",
118
+ "@typescript-eslint/eslint-plugin": "^6.9.0",
119
+ "@typescript-eslint/parser": "^6.9.0",
106
120
  "@vitest/coverage-v8": "^0.34.6",
107
- "eslint": "^8.51.0",
121
+ "eslint": "^8.52.0",
108
122
  "eslint-config-prettier": "^9.0.0",
109
123
  "eslint-plugin-prettier": "^5.0.1",
110
124
  "eslint-plugin-simple-import-sort": "^10.0.0",
111
125
  "faucet": "^0.0.4",
112
- "happy-dom": "^12.9.1",
126
+ "happy-dom": "^12.10.1",
113
127
  "ls-engines": "^0.9.0",
114
- "nock": "^13.3.4",
128
+ "nock": "^13.3.6",
115
129
  "prettier": "^3.0.3",
116
130
  "prettier-plugin-packagejson": "^2.4.6",
117
131
  "rimraf": "^5.0.1",
118
- "rollup": "^4.0.2",
132
+ "rollup": "^4.1.4",
119
133
  "sse-channel": "^4.0.0",
120
- "terser": "^5.21.0",
134
+ "terser": "^5.22.0",
121
135
  "typescript": "^5.2.2",
122
136
  "vitest": "^0.34.6",
123
137
  "vitest-github-actions-reporter": "^0.10.0"
@@ -0,0 +1,42 @@
1
+ import {ContentSourceMapDocument, ContentSourceMapDocuments} from '../types'
2
+ import {parseJsonPath} from './jsonpath'
3
+ import type {PathSegment, StudioUrl} from './types'
4
+
5
+ /** @alpha */
6
+ export type EditLink = `/intent/edit/id=${string};type=${string};path=${string}`
7
+
8
+ /** @alpha */
9
+ export interface EditLinkProps {
10
+ studioUrl: StudioUrl
11
+ document: ContentSourceMapDocument
12
+ }
13
+
14
+ /** @alpha */
15
+ export type DefineEditLink = (
16
+ studioUrl: StudioUrl,
17
+ ) => (sourceDocument: ContentSourceMapDocuments[number]) => `${StudioUrl}${EditLink}`
18
+
19
+ /** @alpha */
20
+ export function defineEditLink(
21
+ _studioUrl: StudioUrl,
22
+ ): (
23
+ sourceDocument: ContentSourceMapDocuments[number],
24
+ path: string | PathSegment[],
25
+ ) => `${StudioUrl}${EditLink}` {
26
+ const studioUrl = _studioUrl.replace(/\/$/, '')
27
+ return ({_id, _type}, path) =>
28
+ `${studioUrl}/intent/edit/id=${_id};type=${_type};path=${encodeJsonPathToUriComponent(path)}`
29
+ }
30
+
31
+ /** @alpha */
32
+ export function encodeJsonPathToUriComponent(path: string | PathSegment[]): string {
33
+ const sourcePath = Array.isArray(path) ? path : parseJsonPath(path)
34
+ return encodeURIComponent(
35
+ sourcePath
36
+ .map((key, i) =>
37
+ // eslint-disable-next-line no-nested-ternary
38
+ typeof key === 'number' ? `[${key}]` : i > 0 ? `.${key}` : key,
39
+ )
40
+ .join(''),
41
+ )
42
+ }
@@ -0,0 +1,19 @@
1
+ export type {
2
+ ContentSourceMap,
3
+ ContentSourceMapDocument,
4
+ ContentSourceMapDocumentBase,
5
+ ContentSourceMapDocuments,
6
+ ContentSourceMapDocumentValueSource,
7
+ ContentSourceMapLiteralSource,
8
+ ContentSourceMapMapping,
9
+ ContentSourceMapMappings,
10
+ ContentSourceMapPaths,
11
+ ContentSourceMapRemoteDocument,
12
+ ContentSourceMapSource,
13
+ ContentSourceMapUnknownSource,
14
+ ContentSourceMapValueMapping,
15
+ } from '../types'
16
+ export {encodeJsonPathToUriComponent} from './editIntent'
17
+ export {jsonPath} from './jsonpath'
18
+ export {resolveMapping, walkMap, type WalkMapFn} from './sourcemap'
19
+ export type * from './types'
@@ -0,0 +1,40 @@
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(
7
+ "$['foo']['bar'][0]['baz']",
8
+ )
9
+ })
10
+
11
+ test('formats normalised JSON Paths with escaped characters', () => {
12
+ expect(jsonPath(['foo', 'bar', 0, 'baz', "it's a 'test'"])).toBe(
13
+ "$['foo']['bar'][0]['baz']['it\\'s a \\'test\\'']",
14
+ )
15
+ })
16
+
17
+ test('parses normalised JSON Paths', () => {
18
+ expect(parseJsonPath("$['foo']['bar'][0]['baz']")).toEqual([
19
+ 'foo',
20
+ 'bar',
21
+ 0,
22
+ 'baz',
23
+ ])
24
+ })
25
+
26
+ test('parses normalised JSON Paths with escaped characters', () => {
27
+ expect(
28
+ parseJsonPath("$['foo']['bar'][0]['baz']['it\\'s a \\'test\\'']"),
29
+ ).toEqual(['foo', 'bar', 0, 'baz', "it's a 'test'"])
30
+ })
31
+
32
+ test('parses normalised JSON Paths with key array filter selectors', () => {
33
+ expect(parseJsonPath("$['foo'][?(@._key=='section-1')][0]['baz'][?(@._key=='section-2')]")).toEqual([
34
+ 'foo',
35
+ { key: 'section-1', index: -1 },
36
+ 0,
37
+ 'baz',
38
+ { key: 'section-2', index: -1},
39
+ ])
40
+ })