Skip to content

Commit 899b157

Browse files
authored
feat(openapi-react-query): Pass abort signal to fetch (#1864)
1 parent 4fc9588 commit 899b157

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

.changeset/signal.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-react-query": patch
3+
---
4+
5+
Pass down signal to fetch function this way `useQuery` and `useSuspenseQuery` can cancel queries.

packages/openapi-react-query/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export default function createClient<Paths extends {}, Media extends MediaType =
7070
return useQuery(
7171
{
7272
queryKey: [method, path, init],
73-
queryFn: async () => {
73+
queryFn: async ({ signal }) => {
7474
const mth = method.toUpperCase() as keyof typeof client;
7575
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
76-
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
76+
const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
7777
if (error || !data) {
7878
throw error;
7979
}
@@ -88,10 +88,10 @@ export default function createClient<Paths extends {}, Media extends MediaType =
8888
return useSuspenseQuery(
8989
{
9090
queryKey: [method, path, init],
91-
queryFn: async () => {
91+
queryFn: async ({ signal }) => {
9292
const mth = method.toUpperCase() as keyof typeof client;
9393
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
94-
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
94+
const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
9595
if (error || !data) {
9696
throw error;
9797
}

packages/openapi-react-query/test/index.test.tsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.j
33
import type { paths } from "./fixtures/api.js";
44
import createClient from "../src/index.js";
55
import createFetchClient from "openapi-fetch";
6-
import { fireEvent, render, renderHook, screen, waitFor } from "@testing-library/react";
6+
import { fireEvent, render, renderHook, screen, waitFor, act } from "@testing-library/react";
77
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
88
import { Suspense, type ReactNode } from "react";
99
import { ErrorBoundary } from "react-error-boundary";
@@ -109,6 +109,26 @@ describe("client", () => {
109109
expectTypeOf(error).toEqualTypeOf<{ code: number; message: string } | null>();
110110
});
111111

112+
it("passes abort signal to fetch", async () => {
113+
let signalPassedToFetch: AbortSignal | undefined;
114+
115+
const fetchClient = createFetchClient<paths>({
116+
baseUrl,
117+
fetch: async ({ signal }) => {
118+
signalPassedToFetch = signal;
119+
await new Promise(() => {});
120+
return Response.error();
121+
},
122+
});
123+
const client = createClient(fetchClient);
124+
125+
const { unmount } = renderHook(() => client.useQuery("get", "/string-array"), { wrapper });
126+
127+
unmount();
128+
129+
expect(signalPassedToFetch?.aborted).toBeTruthy();
130+
});
131+
112132
describe("params", () => {
113133
it("should be required if OpenAPI schema requires params", async () => {
114134
const fetchClient = createFetchClient<paths>({ baseUrl });
@@ -258,6 +278,29 @@ describe("client", () => {
258278

259279
await waitFor(() => rendered.findByText("data: Hello"));
260280
});
281+
282+
it("passes abort signal to fetch", async () => {
283+
let signalPassedToFetch: AbortSignal | undefined;
284+
285+
const fetchClient = createFetchClient<paths>({
286+
baseUrl,
287+
fetch: async ({ signal }) => {
288+
signalPassedToFetch = signal;
289+
await new Promise(() => {});
290+
return Response.error();
291+
},
292+
});
293+
const client = createClient(fetchClient);
294+
const queryClient = new QueryClient({});
295+
296+
const { unmount } = renderHook(() => client.useSuspenseQuery("get", "/string-array", {}, {}, queryClient));
297+
298+
unmount();
299+
300+
await act(() => queryClient.cancelQueries());
301+
302+
expect(signalPassedToFetch?.aborted).toBeTruthy();
303+
});
261304
});
262305

263306
describe("useMutation", () => {

0 commit comments

Comments
 (0)