Skip to content

Commit

Permalink
feat: support resultSourceMap=withKeyArraySelector (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored Oct 19, 2023
1 parent 9090490 commit d528e99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ export function _requestObservable<R>(
['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 &&
uri.indexOf('/data/query/') === 0
) {
if (options.resultSourceMap ?? config.resultSourceMap) {
options.query = {resultSourceMap: true, ...options.query}
const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap
if (resultSourceMap !== undefined && resultSourceMap !== false) {
options.query = {resultSourceMap, ...options.query}
}
const perspective = options.perspective || config.perspective
if (typeof perspective === 'string' && perspective !== 'raw') {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface ClientConfig {
/**
* Adds a `resultSourceMap` key to the API response, with the type `ContentSourceMap`
*/
resultSourceMap?: boolean
resultSourceMap?: boolean | 'withKeyArraySelector'
/**
*@deprecated set `cache` and `next` options on `client.fetch` instead
*/
Expand Down
25 changes: 25 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,31 @@ describe('client', async () => {
},
)

test.skipIf(isEdge)(
'can query for documents with resultSourceMap=withKeyArraySelector and perspective',
async () => {
nock(projectHost())
.get(
`/vX/data/query/foo?query=*&resultSourceMap=withKeyArraySelector&perspective=previewDrafts`,
)
.reply(200, {
ms: 123,
query: '*',
result,
resultSourceMap,
})

const client = getClient({
apiVersion: 'X',
resultSourceMap: 'withKeyArraySelector',
perspective: 'previewDrafts',
})
const res = await client.fetch('*', {})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
},
)

test.skipIf(isEdge)('automatically useCdn false if perspective is previewDrafts', async () => {
nock('https://abc123.api.sanity.io')
.get(`/v1/data/query/foo?query=*&perspective=previewDrafts`)
Expand Down

0 comments on commit d528e99

Please sign in to comment.