From 0aa4c6dd9e7ec5cebc9847f7a5e0101a9c4a316d Mon Sep 17 00:00:00 2001 From: Cody Olsen <81981+stipsan@users.noreply.github.com> Date: Wed, 26 Jul 2023 03:19:56 +0200 Subject: [PATCH] feat: support `fetch` for Next.js `app-router` (#249) --- package-lock.json | 4 ++-- package.json | 5 +++-- src/data/dataMethods.ts | 8 +++++++- src/http/requestOptions.ts | 4 ++++ src/types.ts | 23 +++++++++++++++++++++-- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index fdc34eff..98341136 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sanity/client", - "version": "6.1.7", + "version": "6.2.0-fetch.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sanity/client", - "version": "6.1.7", + "version": "6.2.0-fetch.6", "license": "MIT", "dependencies": { "@sanity/eventsource": "^5.0.0", diff --git a/package.json b/package.json index e08c6599..e279ba7b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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": { diff --git a/src/data/dataMethods.ts b/src/data/dataMethods.ts index eccfcda5..465e7be0 100644 --- a/src/data/dataMethods.ts +++ b/src/data/dataMethods.ts @@ -69,8 +69,13 @@ export function _fetch( ): Observable | 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 */ @@ -226,6 +231,7 @@ export function _dataRequest( tag, canUseCdn: isQuery, signal: options.signal, + fetch: options.fetch, } return _requestObservable(client, httpRequest, reqOptions).pipe( diff --git a/src/http/requestOptions.ts b/src/http/requestOptions.ts index b35eab35..fe31c1f5 100644 --- a/src/http/requestOptions.ts +++ b/src/http/requestOptions.ts @@ -29,5 +29,9 @@ export function requestOptions(config: Any, overrides: Any = {}): Omit { + cache?: RequestInit['cache'] + next?: T extends keyof RequestInit ? RequestInit[T] : never +} + /** @public */ export type ClientPerspective = 'previewDrafts' | 'published' | 'raw' @@ -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 */ @@ -486,12 +499,18 @@ export interface ListenOptions { } /** @public */ -export type FilteredResponseQueryOptions = RequestOptions & { +export type ResponseQueryOptions = 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 }