Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support fetch for Next.js app-router #249

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanity/client",
"version": "6.1.7",
"version": "6.2.0-fetch.6",
"description": "Client for retrieving, creating and patching data from Sanity.io",
"keywords": [
"sanity",
Expand Down Expand Up @@ -71,6 +71,7 @@
"prepublishOnly": "npm run build",
"rollup": "NODE_ENV=production rollup -c rollup.config.cjs",
"test": "vitest",
"type-check": "tsc --noEmit",
"test:browser": "npm test -- --config ./vitest.browser.config.ts",
"test:bun": "(cd runtimes/bun && bun wiptest)",
"test:deno": "deno test --allow-read --allow-net --allow-env --fail-fast --import-map=runtimes/deno/import_map.json runtimes/deno",
Expand All @@ -91,7 +92,7 @@
},
"dependencies": {
"@sanity/eventsource": "^5.0.0",
"get-it": "^8.1.0",
"get-it": "^8.2.0",
"rxjs": "^7.0.0"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/data/dataMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ export function _fetch<R, Q extends QueryParams>(
): Observable<RawQueryResponse<R> | R> {
const mapResponse =
options.filterResponse === false ? (res: Any) => res : (res: Any) => res.result
const {cache, next, ...opts} = options
const reqOpts =
typeof cache !== 'undefined' || typeof next !== 'undefined'
? {...opts, fetch: {cache, next}}
: opts

return _dataRequest(client, httpRequest, 'query', {query, params}, options).pipe(map(mapResponse))
return _dataRequest(client, httpRequest, 'query', {query, params}, reqOpts).pipe(map(mapResponse))
}

/** @internal */
Expand Down Expand Up @@ -226,6 +231,7 @@ export function _dataRequest(
tag,
canUseCdn: isQuery,
signal: options.signal,
fetch: options.fetch,
}

return _requestObservable(client, httpRequest, reqOptions).pipe(
Expand Down
4 changes: 4 additions & 0 deletions src/http/requestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOp
proxy: overrides.proxy || config.proxy,
json: true,
withCredentials,
fetch:
typeof overrides.fetch === 'object' && typeof config.fetch === 'object'
? {...config.fetch, ...overrides.fetch}
: overrides.fetch || config.fetch,
})
}
23 changes: 21 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export interface RequestOptions {
signal?: AbortSignal
}

/**
* Options for the native `fetch` feature, used by the Next.js app-router
* @public
*/
export interface RequestFetchOptions<T = 'next'> {
cache?: RequestInit['cache']
next?: T extends keyof RequestInit ? RequestInit[T] : never
}

/** @public */
export type ClientPerspective = 'previewDrafts' | 'published' | 'raw'

Expand Down Expand Up @@ -76,6 +85,10 @@ export interface ClientConfig {
* Adds a `resultSourceMap` key to the API response, with the type `ContentSourceMap`
*/
resultSourceMap?: boolean
/**
* Experimental, opts-in to using native `fetch` as transport in order to make full use of React Server Components
*/
fetch?: RequestFetchOptions | boolean
}

/** @public */
Expand Down Expand Up @@ -486,12 +499,18 @@ export interface ListenOptions {
}

/** @public */
export type FilteredResponseQueryOptions = RequestOptions & {
export type ResponseQueryOptions<T = 'next'> = RequestOptions & {
cache?: RequestInit['cache']
next?: T extends keyof RequestInit ? RequestInit[T] : never
}

/** @public */
export type FilteredResponseQueryOptions = ResponseQueryOptions & {
filterResponse?: true
}

/** @public */
export type UnfilteredResponseQueryOptions = RequestOptions & {
export type UnfilteredResponseQueryOptions = ResponseQueryOptions & {
filterResponse: false
}

Expand Down