Skip to content

Commit f90722d

Browse files
authored
fix: send perspective as query param with GET request (#1008)
1 parent 29a0339 commit f90722d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/runtime/minimal-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function createClient(config: ClientConfig) {
7373
* Perform a fetch using GROQ syntax.
7474
*/
7575
async fetch<T = unknown>(query: string, params?: Record<string, unknown>) {
76-
const qs = getQuery(query, params)
76+
const qs = getQuery(query, params) + `&perspective=${perspective}`
7777
const usePostRequest = getByteSize(qs) > 9000
7878

7979
const host = useCdn && !usePostRequest ? cdnHost : apiHost

test/unit/client.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('minimal sanity client', () => {
4747
client.fetch('*[_type == "article"')
4848

4949
expect($fetch).toBeCalledWith(
50-
'https://sample-project.api.sanity.io/v1/data/query/undefined?query=*%5B_type%20%3D%3D%20%22article%22',
50+
'https://sample-project.api.sanity.io/v1/data/query/undefined?query=*%5B_type%20%3D%3D%20%22article%22&perspective=raw',
5151
expect.not.objectContaining({ method: 'post' }),
5252
)
5353
})
@@ -148,4 +148,36 @@ describe('minimal sanity client', () => {
148148
},
149149
)
150150
})
151+
152+
it('appends perspective to the query with GET request', async () => {
153+
const client = createClient({
154+
projectId: 'sample-project',
155+
apiVersion: '1',
156+
})
157+
const query = '*[_type == "article"]'
158+
await client.fetch(query)
159+
160+
expect($fetch).toBeCalledWith(
161+
`https://${project}.api.sanity.io/v1/data/query/undefined?query=${encodeURIComponent(query)}&perspective=raw`,
162+
{
163+
credentials: 'omit',
164+
headers: {
165+
Accept: 'application/json',
166+
},
167+
},
168+
)
169+
})
170+
171+
it('appends perspective to the query with POST request', () => {
172+
const client = createClient({
173+
projectId: 'sample-project',
174+
apiVersion: '1',
175+
})
176+
client.fetch(largeRequest)
177+
178+
expect($fetch).toBeCalledWith(
179+
'https://sample-project.api.sanity.io/v1/data/query/undefined',
180+
expect.objectContaining({ method: 'post', query: { perspective: 'raw' } }),
181+
)
182+
})
151183
})

0 commit comments

Comments
 (0)