diff --git a/.changeset/rare-pets-lie.md b/.changeset/rare-pets-lie.md new file mode 100644 index 000000000..d38d60a28 --- /dev/null +++ b/.changeset/rare-pets-lie.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +Fixed fetch options used by NextJs to be passed to fetch. diff --git a/packages/openapi-fetch/examples/nextjs/app/page.tsx b/packages/openapi-fetch/examples/nextjs/app/page.tsx index 74509faca..b70360bc0 100644 --- a/packages/openapi-fetch/examples/nextjs/app/page.tsx +++ b/packages/openapi-fetch/examples/nextjs/app/page.tsx @@ -6,6 +6,10 @@ async function getFact() { params: { query: { max_length: 500 }, }, + next: { + revalidate: 10, + tags: ["cat"], + }, }); } diff --git a/packages/openapi-fetch/src/index.d.ts b/packages/openapi-fetch/src/index.d.ts index 5743cce33..bacc28f8e 100644 --- a/packages/openapi-fetch/src/index.d.ts +++ b/packages/openapi-fetch/src/index.d.ts @@ -108,7 +108,12 @@ export type RequestBodyOption = : { body: OperationRequestBodyContent }; export type FetchOptions = RequestOptions & - Omit; + Omit & + NextJsFetchOptions; + +export type NextJsFetchOptions = { + next?: { revalidate?: false | 0 | number; tags?: string[] }; +}; /** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ export type MaybeOptionalInit

= diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index b9a1bcc37..a16326ce1 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -98,7 +98,10 @@ export default function createClient(clientOptions) { } // fetch! - let response = await fetch(request); + let response = await fetch( + request, + init.next ? { next: init.next } : undefined, + ); // middleware (response) // execute in reverse-array order (first priority gets last transform) diff --git a/packages/openapi-fetch/test/index.test.ts b/packages/openapi-fetch/test/index.test.ts index 5fe7506b3..6a2a6038e 100644 --- a/packages/openapi-fetch/test/index.test.ts +++ b/packages/openapi-fetch/test/index.test.ts @@ -952,6 +952,31 @@ describe("client", () => { const req = fetchMocker.mock.calls[0][0]; expect(req.credentials).toBe("include"); }); + + it("passes NextJs specific options", async () => { + function createCustomFetch(data: any) { + const response = { + clone: () => ({ ...response }), + headers: new Headers(), + json: async () => data, + status: 200, + ok: true, + } as Response; + return async ( + input: RequestInfo | URL, + init?: RequestInit | undefined, + ) => { + expect(init).toEqual({ next: { tags: ["tag"], revalidate: 10 } }); + return Promise.resolve(response); + }; + } + + const customFetch = createCustomFetch({}); + const client = createClient({ fetch: customFetch }); + + mockFetchOnce({ status: 200, body: "{}" }); + client.GET("/self", { next: { tags: ["tag"], revalidate: 10 } }); + }); }); describe("responses", () => {