From d528e99f7f25077e9acc9909a642af352782359c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Lundg=C3=A5rd?= Date: Thu, 19 Oct 2023 13:52:57 +0200 Subject: [PATCH] feat: support `resultSourceMap=withKeyArraySelector` (#363) --- src/data/dataMethods.ts | 5 +++-- src/types.ts | 2 +- test/client.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/data/dataMethods.ts b/src/data/dataMethods.ts index 9d51e229..51408f48 100644 --- a/src/data/dataMethods.ts +++ b/src/data/dataMethods.ts @@ -323,8 +323,9 @@ export function _requestObservable( ['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') { diff --git a/src/types.ts b/src/types.ts index 3c57c7a9..ce646e6f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 */ diff --git a/test/client.test.ts b/test/client.test.ts index 037880e4..4b585ebc 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -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`)