Skip to content

Commit

Permalink
feat: allow setting useCdn: boolean on client.fetch (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Dec 12, 2023
1 parent 26ce483 commit 936ec9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export function _dataRequest(
signal: options.signal,
fetch: options.fetch,
useAbortSignal: options.useAbortSignal,
useCdn: options.useCdn,
}

return _requestObservable(client, httpRequest, reqOptions).pipe(
Expand Down Expand Up @@ -312,7 +313,7 @@ export function _requestObservable<R>(
? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && uri.indexOf('/data/') === 0
: options.canUseCdn

let useCdn = config.useCdn && canUseCdn
let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn

const tag =
options.tag && config.requestTagPrefix
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
url?: string
uri?: string
canUseCdn?: boolean
useCdn?: boolean
tag?: string
resultSourceMap?: boolean | 'withKeyArraySelector'
perspective?: ClientPerspective
Expand Down Expand Up @@ -668,6 +669,7 @@ export interface ResponseQueryOptions<T = 'next'> extends RequestOptions {
resultSourceMap?: boolean | 'withKeyArraySelector'
cache?: RequestInit['cache']
next?: T extends keyof RequestInit ? RequestInit[T] : never
useCdn?: boolean
}

/** @public */
Expand Down
26 changes: 26 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,32 @@ describe('client', async () => {
},
)

test.skipIf(isEdge)('allow overriding useCdn to false on client.fetch', async () => {
nock('https://abc123.api.sanity.io').get(`/v1/data/query/foo?query=*`).reply(200, {
ms: 123,
query: '*',
result,
})

const client = createClient({projectId: 'abc123', dataset: 'foo', useCdn: true})
const res = await client.fetch('*', {}, {useCdn: false})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
})

test.skipIf(isEdge)('allow overriding useCdn to true on client.fetch', async () => {
nock('https://abc123.apicdn.sanity.io').get(`/v1/data/query/foo?query=*`).reply(200, {
ms: 123,
query: '*',
result,
})

const client = createClient({projectId: 'abc123', dataset: 'foo', useCdn: false})
const res = await client.fetch('*', {}, {useCdn: true})
expect(res.length, 'length should match').toBe(1)
expect(res[0].rating, 'data should match').toBe(5)
})

test.skipIf(isEdge)('throws on invalid request tag on request', () => {
nock(projectHost()).get(`/v1/data/query/foo?query=*&tag=mycompany.syncjob`).reply(200, {
ms: 123,
Expand Down

0 comments on commit 936ec9e

Please sign in to comment.