@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,244 +0,0 @@
|
|
|
1
|
-
import {Observable} from 'rxjs'
|
|
2
|
-
import {map} from 'rxjs/operators'
|
|
3
|
-
|
|
4
|
-
import {defaultConfig} from '../config'
|
|
5
|
-
import {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
6
|
-
import type {
|
|
7
|
-
Any,
|
|
8
|
-
ClientConfig,
|
|
9
|
-
FilteredResponseQueryOptions,
|
|
10
|
-
HttpRequest,
|
|
11
|
-
QueryParams,
|
|
12
|
-
RawQueryResponse,
|
|
13
|
-
UnfilteredResponseQueryOptions,
|
|
14
|
-
} from '../types'
|
|
15
|
-
import {defaultStegaConfig, initStegaConfig, splitConfig} from './config'
|
|
16
|
-
import {stegaEncodeSourceMap} from './stegaEncodeSourceMap'
|
|
17
|
-
import {ClientStegaConfig, InitializedClientStegaConfig, InitializedStegaConfig} from './types'
|
|
18
|
-
|
|
19
|
-
/** @public */
|
|
20
|
-
export class ObservableSanityStegaClient extends ObservableSanityClient {
|
|
21
|
-
/**
|
|
22
|
-
* Private properties
|
|
23
|
-
*/
|
|
24
|
-
#httpRequest: HttpRequest
|
|
25
|
-
private stegaConfig: InitializedStegaConfig
|
|
26
|
-
|
|
27
|
-
constructor(httpRequest: HttpRequest, config: ClientStegaConfig = defaultConfig) {
|
|
28
|
-
const {clientConfig, stegaConfig} = splitConfig(config)
|
|
29
|
-
super(httpRequest, clientConfig)
|
|
30
|
-
|
|
31
|
-
this.#httpRequest = httpRequest
|
|
32
|
-
this.stegaConfig = initStegaConfig(stegaConfig, defaultStegaConfig)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Clone the client - returns a new instance
|
|
37
|
-
*/
|
|
38
|
-
clone(): ObservableSanityStegaClient {
|
|
39
|
-
return new ObservableSanityStegaClient(this.#httpRequest, this.config())
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Returns the current client configuration
|
|
44
|
-
*/
|
|
45
|
-
config(): InitializedClientStegaConfig
|
|
46
|
-
/**
|
|
47
|
-
* Reconfigure the client. Note that this _mutates_ the current client.
|
|
48
|
-
*/
|
|
49
|
-
config(newConfig?: Partial<ClientStegaConfig>): this
|
|
50
|
-
config(newConfig?: Partial<ClientStegaConfig>): ClientStegaConfig | this {
|
|
51
|
-
if (newConfig === undefined) {
|
|
52
|
-
return {...super.config(), stega: {...this.stegaConfig}}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
super.config(newConfig)
|
|
56
|
-
|
|
57
|
-
const {stegaConfig} = splitConfig(newConfig)
|
|
58
|
-
|
|
59
|
-
this.stegaConfig = initStegaConfig(stegaConfig, this.stegaConfig || {})
|
|
60
|
-
return this
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Clone the client with a new (partial) configuration.
|
|
65
|
-
*
|
|
66
|
-
* @param newConfig - New client configuration properties, shallowly merged with existing configuration
|
|
67
|
-
*/
|
|
68
|
-
withConfig(newConfig?: Partial<ClientConfig>): ObservableSanityStegaClient {
|
|
69
|
-
return new ObservableSanityStegaClient(this.#httpRequest, {...this.config(), ...newConfig})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Perform a GROQ-query against the configured dataset.
|
|
74
|
-
*
|
|
75
|
-
* @param query - GROQ-query to perform
|
|
76
|
-
*/
|
|
77
|
-
fetch<R = Any>(query: string): Observable<R>
|
|
78
|
-
/**
|
|
79
|
-
* Perform a GROQ-query against the configured dataset.
|
|
80
|
-
*
|
|
81
|
-
* @param query - GROQ-query to perform
|
|
82
|
-
* @param params - Query parameters
|
|
83
|
-
*/
|
|
84
|
-
fetch<R = Any, Q = QueryParams>(query: string, params: Q): Observable<R>
|
|
85
|
-
/**
|
|
86
|
-
* Perform a GROQ-query against the configured dataset.
|
|
87
|
-
*
|
|
88
|
-
* @param query - GROQ-query to perform
|
|
89
|
-
* @param params - Query parameters
|
|
90
|
-
* @param options - Request options
|
|
91
|
-
*/
|
|
92
|
-
fetch<R = Any, Q = QueryParams>(
|
|
93
|
-
query: string,
|
|
94
|
-
params: Q | undefined,
|
|
95
|
-
options: FilteredResponseQueryOptions,
|
|
96
|
-
): Observable<R>
|
|
97
|
-
/**
|
|
98
|
-
* Perform a GROQ-query against the configured dataset.
|
|
99
|
-
*
|
|
100
|
-
* @param query - GROQ-query to perform
|
|
101
|
-
* @param params - Query parameters
|
|
102
|
-
* @param options - Request options
|
|
103
|
-
*/
|
|
104
|
-
fetch<R = Any, Q = QueryParams>(
|
|
105
|
-
query: string,
|
|
106
|
-
params: Q | undefined,
|
|
107
|
-
options: UnfilteredResponseQueryOptions,
|
|
108
|
-
): Observable<RawQueryResponse<R>>
|
|
109
|
-
fetch<R, Q extends QueryParams>(
|
|
110
|
-
query: string,
|
|
111
|
-
params?: Q,
|
|
112
|
-
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
|
|
113
|
-
): Observable<RawQueryResponse<R> | R> {
|
|
114
|
-
const {filterResponse: originalFilterResponse = true} = options
|
|
115
|
-
return super
|
|
116
|
-
.fetch<R, Q>(query, params, Object.assign({}, options as Any, {filterResponse: false}))
|
|
117
|
-
.pipe(
|
|
118
|
-
map((res: Any) => {
|
|
119
|
-
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
|
|
120
|
-
let result = _result
|
|
121
|
-
if (this.stegaConfig.enabled) {
|
|
122
|
-
result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
|
|
123
|
-
}
|
|
124
|
-
return originalFilterResponse ? result : {...res, result}
|
|
125
|
-
}),
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** @public */
|
|
131
|
-
export class SanityStegaClient extends SanityClient {
|
|
132
|
-
/**
|
|
133
|
-
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
134
|
-
*/
|
|
135
|
-
observable: ObservableSanityStegaClient
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Private properties
|
|
139
|
-
*/
|
|
140
|
-
#httpRequest: HttpRequest
|
|
141
|
-
private stegaConfig: InitializedStegaConfig
|
|
142
|
-
|
|
143
|
-
constructor(httpRequest: HttpRequest, config: ClientStegaConfig = defaultConfig) {
|
|
144
|
-
const {clientConfig, stegaConfig} = splitConfig(config)
|
|
145
|
-
super(httpRequest, clientConfig)
|
|
146
|
-
|
|
147
|
-
this.#httpRequest = httpRequest
|
|
148
|
-
this.stegaConfig = initStegaConfig(stegaConfig, defaultStegaConfig)
|
|
149
|
-
|
|
150
|
-
this.observable = new ObservableSanityStegaClient(httpRequest, config)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Clone the client - returns a new instance
|
|
155
|
-
*/
|
|
156
|
-
clone(): SanityStegaClient {
|
|
157
|
-
return new SanityStegaClient(this.#httpRequest, this.config())
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Returns the current client configuration
|
|
162
|
-
*/
|
|
163
|
-
config(): InitializedClientStegaConfig
|
|
164
|
-
/**
|
|
165
|
-
* Reconfigure the client. Note that this _mutates_ the current client.
|
|
166
|
-
*/
|
|
167
|
-
config(newConfig?: Partial<ClientStegaConfig>): this
|
|
168
|
-
config(newConfig?: Partial<ClientStegaConfig>): ClientStegaConfig | this {
|
|
169
|
-
if (newConfig === undefined) {
|
|
170
|
-
return {...super.config(), stega: {...this.stegaConfig}}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
super.config(newConfig)
|
|
174
|
-
|
|
175
|
-
const {stegaConfig} = splitConfig(newConfig)
|
|
176
|
-
|
|
177
|
-
this.stegaConfig = initStegaConfig(stegaConfig, {...(this.stegaConfig || {})})
|
|
178
|
-
return this
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Clone the client with a new (partial) configuration.
|
|
183
|
-
*
|
|
184
|
-
* @param newConfig - New client configuration properties, shallowly merged with existing configuration
|
|
185
|
-
*/
|
|
186
|
-
withConfig(newConfig?: Partial<ClientStegaConfig>): SanityStegaClient {
|
|
187
|
-
return new SanityStegaClient(this.#httpRequest, {...this.config(), ...newConfig})
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Perform a GROQ-query against the configured dataset.
|
|
192
|
-
*
|
|
193
|
-
* @param query - GROQ-query to perform
|
|
194
|
-
*/
|
|
195
|
-
fetch<R = Any>(query: string): Promise<R>
|
|
196
|
-
/**
|
|
197
|
-
* Perform a GROQ-query against the configured dataset.
|
|
198
|
-
*
|
|
199
|
-
* @param query - GROQ-query to perform
|
|
200
|
-
* @param params - Optional query parameters
|
|
201
|
-
*/
|
|
202
|
-
fetch<R = Any, Q = QueryParams>(query: string, params: Q): Promise<R>
|
|
203
|
-
/**
|
|
204
|
-
* Perform a GROQ-query against the configured dataset.
|
|
205
|
-
*
|
|
206
|
-
* @param query - GROQ-query to perform
|
|
207
|
-
* @param params - Optional query parameters
|
|
208
|
-
* @param options - Request options
|
|
209
|
-
*/
|
|
210
|
-
fetch<R = Any, Q = QueryParams>(
|
|
211
|
-
query: string,
|
|
212
|
-
params: Q | undefined,
|
|
213
|
-
options: FilteredResponseQueryOptions,
|
|
214
|
-
): Promise<R>
|
|
215
|
-
/**
|
|
216
|
-
* Perform a GROQ-query against the configured dataset.
|
|
217
|
-
*
|
|
218
|
-
* @param query - GROQ-query to perform
|
|
219
|
-
* @param params - Optional query parameters
|
|
220
|
-
* @param options - Request options
|
|
221
|
-
*/
|
|
222
|
-
fetch<R = Any, Q = QueryParams>(
|
|
223
|
-
query: string,
|
|
224
|
-
params: Q | undefined,
|
|
225
|
-
options: UnfilteredResponseQueryOptions,
|
|
226
|
-
): Promise<RawQueryResponse<R>>
|
|
227
|
-
fetch<R, Q extends QueryParams>(
|
|
228
|
-
query: string,
|
|
229
|
-
params?: Q,
|
|
230
|
-
options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {},
|
|
231
|
-
): Promise<RawQueryResponse<R> | R> {
|
|
232
|
-
const {filterResponse: originalFilterResponse = true} = options
|
|
233
|
-
return super
|
|
234
|
-
.fetch<R, Q>(query, params, Object.assign({}, options as Any, {filterResponse: false}))
|
|
235
|
-
.then((res: Any) => {
|
|
236
|
-
const {result: _result, resultSourceMap} = res as RawQueryResponse<R>
|
|
237
|
-
let result = _result
|
|
238
|
-
if (this.stegaConfig.enabled) {
|
|
239
|
-
result = stegaEncodeSourceMap(_result, resultSourceMap, this.stegaConfig)
|
|
240
|
-
}
|
|
241
|
-
return originalFilterResponse ? result : {...res, result}
|
|
242
|
-
})
|
|
243
|
-
}
|
|
244
|
-
}
|
package/src/stega/config.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type {ClientConfig} from '../types'
|
|
2
|
-
import type {ClientStegaConfig, InitializedStegaConfig, StegaConfig} from './types'
|
|
3
|
-
|
|
4
|
-
export const defaultStegaConfig: StegaConfig = {
|
|
5
|
-
enabled: false,
|
|
6
|
-
filter: (props) => props.filterDefault(props),
|
|
7
|
-
vercelStegaCombineSkip: 'auto',
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function splitConfig(config: ClientStegaConfig): {
|
|
11
|
-
clientConfig: ClientConfig
|
|
12
|
-
stegaConfig: StegaConfig
|
|
13
|
-
} {
|
|
14
|
-
const {stega = {}, ...clientConfig} = config
|
|
15
|
-
return {clientConfig, stegaConfig: stega}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const initStegaConfig = (
|
|
19
|
-
config: Partial<StegaConfig>,
|
|
20
|
-
prevConfig: Partial<StegaConfig>,
|
|
21
|
-
): InitializedStegaConfig => {
|
|
22
|
-
const specifiedConfig = Object.assign({} as StegaConfig, prevConfig, config)
|
|
23
|
-
const newConfig = Object.assign({} as InitializedStegaConfig, defaultStegaConfig, specifiedConfig)
|
|
24
|
-
|
|
25
|
-
if ('encodeSourceMap' in newConfig) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
`It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client/stega'. Did you mean 'enabled'?`,
|
|
28
|
-
)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if ('encodeSourceMapAtPath' in newConfig) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMapAtPath' is not supported in '@sanity/client/stega'. Did you mean 'filter'?`,
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (typeof newConfig.enabled !== 'boolean') {
|
|
38
|
-
throw new Error(`config.enabled must be a boolean, received ${newConfig.enabled}`)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (newConfig.enabled && newConfig.studioUrl === undefined) {
|
|
42
|
-
throw new Error(`config.studioUrl must be defined when config.enabled is true`)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
newConfig.enabled &&
|
|
47
|
-
typeof newConfig.studioUrl !== 'string' &&
|
|
48
|
-
typeof newConfig.studioUrl !== 'function'
|
|
49
|
-
) {
|
|
50
|
-
throw new Error(
|
|
51
|
-
`config.studioUrl must be a string or a function, received ${newConfig.studioUrl}`,
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return newConfig
|
|
56
|
-
}
|
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
import {expect, test, vi} from 'vitest'
|
|
2
|
-
|
|
3
|
-
import type {ContentSourceMap} from '../types'
|
|
4
|
-
import {encodeIntoResult} from './encodeIntoResult'
|
|
5
|
-
|
|
6
|
-
const encodeTestCases: {
|
|
7
|
-
name: string
|
|
8
|
-
queryResult: {
|
|
9
|
-
result: unknown
|
|
10
|
-
resultSourceMap: ContentSourceMap
|
|
11
|
-
}
|
|
12
|
-
options?: {
|
|
13
|
-
keyArraySelectors: boolean
|
|
14
|
-
}
|
|
15
|
-
expected: {
|
|
16
|
-
encoderCalls: number
|
|
17
|
-
encoderArgs: unknown[][]
|
|
18
|
-
}
|
|
19
|
-
}[] = [
|
|
20
|
-
{
|
|
21
|
-
name: 'resolves exact mappings to source',
|
|
22
|
-
queryResult: {
|
|
23
|
-
result: [
|
|
24
|
-
{
|
|
25
|
-
_id: 'foo',
|
|
26
|
-
this: 'that',
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
resultSourceMap: {
|
|
30
|
-
documents: [
|
|
31
|
-
{
|
|
32
|
-
_id: 'foo',
|
|
33
|
-
_type: 'bar',
|
|
34
|
-
},
|
|
35
|
-
],
|
|
36
|
-
paths: ["$['this']"],
|
|
37
|
-
mappings: {
|
|
38
|
-
"$[0]['this']": {
|
|
39
|
-
source: {
|
|
40
|
-
document: 0,
|
|
41
|
-
path: 0,
|
|
42
|
-
type: 'documentValue',
|
|
43
|
-
},
|
|
44
|
-
type: 'value',
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
expected: {
|
|
50
|
-
encoderCalls: 1,
|
|
51
|
-
encoderArgs: [
|
|
52
|
-
[
|
|
53
|
-
{
|
|
54
|
-
sourcePath: ['this'],
|
|
55
|
-
sourceDocument: {
|
|
56
|
-
_id: 'foo',
|
|
57
|
-
_type: 'bar',
|
|
58
|
-
},
|
|
59
|
-
resultPath: [0, 'this'],
|
|
60
|
-
value: 'that',
|
|
61
|
-
},
|
|
62
|
-
],
|
|
63
|
-
],
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
name: 'resolves aggregated mappings to source',
|
|
68
|
-
queryResult: {
|
|
69
|
-
result: [
|
|
70
|
-
{
|
|
71
|
-
_id: 'foo',
|
|
72
|
-
nested: {
|
|
73
|
-
object: {
|
|
74
|
-
this: 'that',
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
resultSourceMap: {
|
|
80
|
-
documents: [
|
|
81
|
-
{
|
|
82
|
-
_id: 'foo',
|
|
83
|
-
_type: 'bar',
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
paths: ["$['something']['nested']"],
|
|
87
|
-
mappings: {
|
|
88
|
-
"$[0]['nested']": {
|
|
89
|
-
source: {
|
|
90
|
-
document: 0,
|
|
91
|
-
path: 0,
|
|
92
|
-
type: 'documentValue',
|
|
93
|
-
},
|
|
94
|
-
type: 'value',
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
expected: {
|
|
100
|
-
encoderCalls: 1,
|
|
101
|
-
encoderArgs: [
|
|
102
|
-
[
|
|
103
|
-
{
|
|
104
|
-
sourcePath: ['something', 'nested', 'object', 'this'],
|
|
105
|
-
sourceDocument: {
|
|
106
|
-
_id: 'foo',
|
|
107
|
-
_type: 'bar',
|
|
108
|
-
},
|
|
109
|
-
resultPath: [0, 'nested', 'object', 'this'],
|
|
110
|
-
value: 'that',
|
|
111
|
-
},
|
|
112
|
-
],
|
|
113
|
-
],
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
name: 'plucks out _key to use as path segment',
|
|
118
|
-
queryResult: {
|
|
119
|
-
result: [
|
|
120
|
-
{
|
|
121
|
-
_id: 'foo',
|
|
122
|
-
nested: {
|
|
123
|
-
arr: [
|
|
124
|
-
{
|
|
125
|
-
_key: 'im_a_key',
|
|
126
|
-
value: 'that',
|
|
127
|
-
},
|
|
128
|
-
],
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
],
|
|
132
|
-
resultSourceMap: {
|
|
133
|
-
documents: [
|
|
134
|
-
{
|
|
135
|
-
_id: 'foo',
|
|
136
|
-
_type: 'bar',
|
|
137
|
-
},
|
|
138
|
-
],
|
|
139
|
-
paths: ["$['projected']['nested']"],
|
|
140
|
-
mappings: {
|
|
141
|
-
"$[0]['nested']": {
|
|
142
|
-
source: {
|
|
143
|
-
document: 0,
|
|
144
|
-
path: 0,
|
|
145
|
-
type: 'documentValue',
|
|
146
|
-
},
|
|
147
|
-
type: 'value',
|
|
148
|
-
},
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
options: {
|
|
153
|
-
keyArraySelectors: true,
|
|
154
|
-
},
|
|
155
|
-
expected: {
|
|
156
|
-
encoderCalls: 2,
|
|
157
|
-
encoderArgs: [
|
|
158
|
-
[
|
|
159
|
-
{
|
|
160
|
-
sourcePath: ['projected', 'nested', 'arr', {key: 'im_a_key', index: 0}, '_key'],
|
|
161
|
-
sourceDocument: {
|
|
162
|
-
_id: 'foo',
|
|
163
|
-
_type: 'bar',
|
|
164
|
-
},
|
|
165
|
-
resultPath: [0, 'nested', 'arr', {key: 'im_a_key', index: 0}, '_key'],
|
|
166
|
-
value: 'im_a_key',
|
|
167
|
-
},
|
|
168
|
-
],
|
|
169
|
-
[
|
|
170
|
-
{
|
|
171
|
-
sourcePath: ['projected', 'nested', 'arr', {key: 'im_a_key', index: 0}, 'value'],
|
|
172
|
-
sourceDocument: {
|
|
173
|
-
_id: 'foo',
|
|
174
|
-
_type: 'bar',
|
|
175
|
-
},
|
|
176
|
-
resultPath: [0, 'nested', 'arr', {key: 'im_a_key', index: 0}, 'value'],
|
|
177
|
-
value: 'that',
|
|
178
|
-
},
|
|
179
|
-
],
|
|
180
|
-
],
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
name: 'handles _key array filter selectors in source paths',
|
|
185
|
-
queryResult: {
|
|
186
|
-
result: [
|
|
187
|
-
{
|
|
188
|
-
_id: 'foo',
|
|
189
|
-
arr: [
|
|
190
|
-
{
|
|
191
|
-
_key: 'im_a_key',
|
|
192
|
-
value: 'that',
|
|
193
|
-
},
|
|
194
|
-
],
|
|
195
|
-
},
|
|
196
|
-
],
|
|
197
|
-
resultSourceMap: {
|
|
198
|
-
documents: [
|
|
199
|
-
{
|
|
200
|
-
_id: 'foo',
|
|
201
|
-
_type: 'bar',
|
|
202
|
-
},
|
|
203
|
-
],
|
|
204
|
-
paths: ["$['projected'][?(@._key=='fooKey')]"],
|
|
205
|
-
mappings: {
|
|
206
|
-
"$[0]['arr']": {
|
|
207
|
-
source: {
|
|
208
|
-
document: 0,
|
|
209
|
-
path: 0,
|
|
210
|
-
type: 'documentValue',
|
|
211
|
-
},
|
|
212
|
-
type: 'value',
|
|
213
|
-
},
|
|
214
|
-
},
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
options: {
|
|
218
|
-
keyArraySelectors: true,
|
|
219
|
-
},
|
|
220
|
-
expected: {
|
|
221
|
-
encoderCalls: 2,
|
|
222
|
-
encoderArgs: [
|
|
223
|
-
[
|
|
224
|
-
{
|
|
225
|
-
sourcePath: [
|
|
226
|
-
'projected',
|
|
227
|
-
{key: 'fooKey', index: -1},
|
|
228
|
-
{key: 'im_a_key', index: 0},
|
|
229
|
-
'_key',
|
|
230
|
-
],
|
|
231
|
-
sourceDocument: {
|
|
232
|
-
_id: 'foo',
|
|
233
|
-
_type: 'bar',
|
|
234
|
-
},
|
|
235
|
-
resultPath: [0, 'arr', {key: 'im_a_key', index: 0}, '_key'],
|
|
236
|
-
value: 'im_a_key',
|
|
237
|
-
},
|
|
238
|
-
],
|
|
239
|
-
[
|
|
240
|
-
{
|
|
241
|
-
sourcePath: [
|
|
242
|
-
'projected',
|
|
243
|
-
{key: 'fooKey', index: -1},
|
|
244
|
-
{key: 'im_a_key', index: 0},
|
|
245
|
-
'value',
|
|
246
|
-
],
|
|
247
|
-
sourceDocument: {
|
|
248
|
-
_id: 'foo',
|
|
249
|
-
_type: 'bar',
|
|
250
|
-
},
|
|
251
|
-
resultPath: [0, 'arr', {key: 'im_a_key', index: 0}, 'value'],
|
|
252
|
-
value: 'that',
|
|
253
|
-
},
|
|
254
|
-
],
|
|
255
|
-
],
|
|
256
|
-
},
|
|
257
|
-
},
|
|
258
|
-
]
|
|
259
|
-
|
|
260
|
-
test.each(encodeTestCases)('encode $name', ({queryResult, options, expected}) => {
|
|
261
|
-
const mockTranscoder = vi.fn().mockImplementation((input: string) => input)
|
|
262
|
-
encodeIntoResult(queryResult.result, queryResult.resultSourceMap, mockTranscoder, options)
|
|
263
|
-
|
|
264
|
-
expect(mockTranscoder).toBeCalledTimes(expected.encoderCalls)
|
|
265
|
-
for (const args of expected.encoderArgs) {
|
|
266
|
-
expect(mockTranscoder).toBeCalledWith(...args)
|
|
267
|
-
}
|
|
268
|
-
})
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import {parseJsonPath} from '../csm/jsonpath'
|
|
2
|
-
import {resolveMapping} from '../csm/resolveMapping'
|
|
3
|
-
import type {ContentSourceMap} from '../csm/types'
|
|
4
|
-
import {walkMap} from '../csm/walkMap'
|
|
5
|
-
import type {Encoder} from './types'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export function encodeIntoResult<Result>(
|
|
11
|
-
result: Result,
|
|
12
|
-
csm: ContentSourceMap,
|
|
13
|
-
encoder: Encoder,
|
|
14
|
-
options?: {keyArraySelectors: boolean},
|
|
15
|
-
): Result {
|
|
16
|
-
return walkMap(result, (value, path) => {
|
|
17
|
-
// Only map strings, we could extend this in the future to support other types like integers...
|
|
18
|
-
if (typeof value !== 'string') {
|
|
19
|
-
return value
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const resolveMappingResult = resolveMapping(path, csm)
|
|
23
|
-
if (!resolveMappingResult) {
|
|
24
|
-
return value
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const {mapping, matchedPath, pathSuffix} = resolveMappingResult
|
|
28
|
-
if (mapping.type !== 'value') {
|
|
29
|
-
return value
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (mapping.source.type !== 'documentValue') {
|
|
33
|
-
return value
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const sourceDocument = csm.documents[mapping.source.document!]
|
|
37
|
-
const sourcePath = csm.paths[mapping.source.path]
|
|
38
|
-
|
|
39
|
-
if (options?.keyArraySelectors) {
|
|
40
|
-
const matchPathSegments = parseJsonPath(matchedPath)
|
|
41
|
-
const sourcePathSegments = parseJsonPath(sourcePath)
|
|
42
|
-
const fullSourceSegments = sourcePathSegments.concat(path.slice(matchPathSegments.length))
|
|
43
|
-
|
|
44
|
-
return encoder({
|
|
45
|
-
sourcePath: fullSourceSegments,
|
|
46
|
-
sourceDocument,
|
|
47
|
-
resultPath: path,
|
|
48
|
-
value,
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return encoder({
|
|
53
|
-
sourcePath: parseJsonPath(sourcePath + pathSuffix),
|
|
54
|
-
sourceDocument,
|
|
55
|
-
resultPath: path,
|
|
56
|
-
value,
|
|
57
|
-
})
|
|
58
|
-
}) as Result
|
|
59
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type {FilterDefault} from './types'
|
|
2
|
-
|
|
3
|
-
export const filterDefault: FilterDefault = ({sourcePath}) => {
|
|
4
|
-
const endPath = sourcePath.at(-1)
|
|
5
|
-
// Never encode slugs
|
|
6
|
-
if (sourcePath.at(-2) === 'slug' && endPath === 'current') {
|
|
7
|
-
return false
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// Skip underscored keys, needs better heuristics but it works for now
|
|
11
|
-
if (typeof endPath === 'string' && endPath.startsWith('_')) {
|
|
12
|
-
return false
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Best effort infer Portable Text paths that should not be encoded.
|
|
17
|
-
* Nothing is for certain, and the below implementation may cause paths that aren't Portable Text and otherwise be safe to encode to be skipped.
|
|
18
|
-
* However, that's ok as userland can always opt-in with the `encodeSourceMapAtPath` option and mark known safe paths as such, which will override this heuristic.
|
|
19
|
-
*/
|
|
20
|
-
// If the path ends in [number].children[number].marks[number] it's likely a PortableTextSpan: https://github.com/portabletext/types/blob/e54eb24f136d8efd51a46c6a190e7c46e79b5380/src/portableText.ts#LL154C16-L154C16
|
|
21
|
-
if (
|
|
22
|
-
typeof endPath === 'number' &&
|
|
23
|
-
sourcePath.at(-2) === 'marks' &&
|
|
24
|
-
typeof sourcePath.at(-3) === 'number' &&
|
|
25
|
-
sourcePath.at(-4) === 'children' &&
|
|
26
|
-
typeof sourcePath.at(-5) === 'number'
|
|
27
|
-
) {
|
|
28
|
-
return false
|
|
29
|
-
}
|
|
30
|
-
// Or if it's [number].markDefs[number].href it's likely a PortableTextLink: https://github.com/portabletext/types/blob/e54eb24f136d8efd51a46c6a190e7c46e79b5380/src/portableText.ts#L163
|
|
31
|
-
if (
|
|
32
|
-
endPath === 'href' &&
|
|
33
|
-
typeof sourcePath.at(-2) === 'number' &&
|
|
34
|
-
sourcePath.at(-3) === 'markDefs' &&
|
|
35
|
-
typeof sourcePath.at(-4) === 'number'
|
|
36
|
-
) {
|
|
37
|
-
return false
|
|
38
|
-
}
|
|
39
|
-
// Otherwise we have to deal with special properties of PortableTextBlock, and we can't confidently know if it's actually a `_type: 'block'` array item or not.
|
|
40
|
-
// All we know is that if it is indeed a block, and we encode the strings on these keys it'll for sure break the PortableText rendering and thus we skip encoding.
|
|
41
|
-
if (typeof endPath === 'string' && typeof sourcePath.at(-2) === 'number') {
|
|
42
|
-
// https://github.com/portabletext/types/blob/e54eb24f136d8efd51a46c6a190e7c46e79b5380/src/portableText.ts#L48-L58
|
|
43
|
-
if (endPath === 'style' || endPath === 'listItem') {
|
|
44
|
-
return false
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return true
|
|
49
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export * from '../defineCreateClient'
|
|
2
|
-
|
|
3
|
-
import defineCreateClientExports from '../defineCreateClient'
|
|
4
|
-
import envMiddleware from '../http/browserMiddleware'
|
|
5
|
-
import {SanityStegaClient} from './SanityStegaClient'
|
|
6
|
-
import type {ClientStegaConfig} from './types'
|
|
7
|
-
|
|
8
|
-
const exp = defineCreateClientExports<SanityStegaClient, ClientStegaConfig>(
|
|
9
|
-
envMiddleware,
|
|
10
|
-
SanityStegaClient,
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
export * from './shared'
|
|
14
|
-
|
|
15
|
-
/** @public */
|
|
16
|
-
export const requester = exp.requester
|
|
17
|
-
|
|
18
|
-
/** @public */
|
|
19
|
-
export const createClient = exp.createClient
|