Skip to content

Commit 097359f

Browse files
committed
Lint
1 parent c476d99 commit 097359f

File tree

2 files changed

+112
-115
lines changed

2 files changed

+112
-115
lines changed

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

+61-63
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ import {
1717
useSuspenseQuery,
1818
useInfiniteQuery,
1919
} from "@tanstack/react-query";
20-
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient, DefaultParamsOption } from "openapi-fetch";
20+
import type {
21+
ClientMethod,
22+
FetchResponse,
23+
MaybeOptionalInit,
24+
Client as FetchClient,
25+
DefaultParamsOption,
26+
} from "openapi-fetch";
2127
import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";
2228

2329
// Helper type to dynamically infer the type from the `select` property
@@ -95,33 +101,29 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
95101
: [InitWithUnknowns<Init>, Options?, QueryClient?]
96102
) => UseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;
97103

98-
export type UseInfiniteQueryMethod<
99-
Paths extends Record<string, Record<HttpMethod, {}>>,
100-
Media extends MediaType
101-
> =
102-
(<
103-
Method extends HttpMethod,
104-
Path extends PathsWithMethod<Paths, Method>,
105-
Init extends MaybeOptionalInit<Paths[Path], Method>,
106-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
107-
Options extends Omit<
108-
UseInfiniteQueryOptions<
109-
Response["data"],
110-
Response["error"],
111-
InfiniteData<Response["data"]>,
112-
Response["data"],
113-
QueryKey<Paths, Method, Path>,
114-
unknown
115-
>,
116-
"queryKey" | "queryFn"
117-
>
118-
>(
119-
method: Method,
120-
url: Path,
121-
init: InitWithUnknowns<Init>,
122-
options: Options,
123-
queryClient?: QueryClient
124-
) => UseInfiniteQueryResult<InfiniteData<Response["data"]>, Response["error"]>);
104+
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
105+
Method extends HttpMethod,
106+
Path extends PathsWithMethod<Paths, Method>,
107+
Init extends MaybeOptionalInit<Paths[Path], Method>,
108+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>,
109+
Options extends Omit<
110+
UseInfiniteQueryOptions<
111+
Response["data"],
112+
Response["error"],
113+
InfiniteData<Response["data"]>,
114+
Response["data"],
115+
QueryKey<Paths, Method, Path>,
116+
unknown
117+
>,
118+
"queryKey" | "queryFn"
119+
>,
120+
>(
121+
method: Method,
122+
url: Path,
123+
init: InitWithUnknowns<Init>,
124+
options: Options,
125+
queryClient?: QueryClient,
126+
) => UseInfiniteQueryResult<InfiniteData<Response["data"]>, Response["error"]>;
125127

126128
export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
127129
Method extends HttpMethod,
@@ -197,41 +199,38 @@ export default function createClient<Paths extends {}, Media extends MediaType =
197199
useSuspenseQuery: (method, path, ...[init, options, queryClient]) =>
198200
useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
199201
useInfiniteQuery: (method, path, init, options, queryClient) =>
200-
useInfiniteQuery(
201-
{
202-
queryKey: [method, path, init] as const,
203-
queryFn: async <
204-
Method extends HttpMethod,
205-
Path extends PathsWithMethod<Paths, Method>,
206-
>({
207-
queryKey: [method, path, init],
208-
pageParam = 0,
209-
signal,
210-
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, unknown>) => {
211-
const mth = method.toUpperCase() as Uppercase<typeof method>;
212-
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
213-
const mergedInit = {
214-
...init,
215-
signal,
216-
params: {
217-
...(init?.params || {}),
218-
query: {
219-
...(init?.params as { query?: DefaultParamsOption })?.query,
220-
cursor: pageParam,
221-
},
222-
},
223-
};
224-
225-
const { data, error } = await fn(path, mergedInit as any);
226-
if (error) {
227-
throw error;
228-
}
229-
return data;
202+
useInfiniteQuery(
203+
{
204+
queryKey: [method, path, init] as const,
205+
queryFn: async <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>>({
206+
queryKey: [method, path, init],
207+
pageParam = 0,
208+
signal,
209+
}: QueryFunctionContext<QueryKey<Paths, Method, Path>, unknown>) => {
210+
const mth = method.toUpperCase() as Uppercase<typeof method>;
211+
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
212+
const mergedInit = {
213+
...init,
214+
signal,
215+
params: {
216+
...(init?.params || {}),
217+
query: {
218+
...(init?.params as { query?: DefaultParamsOption })?.query,
219+
cursor: pageParam,
220+
},
230221
},
231-
...options,
232-
},
233-
queryClient,
234-
),
222+
};
223+
224+
const { data, error } = await fn(path, mergedInit as any);
225+
if (error) {
226+
throw error;
227+
}
228+
return data;
229+
},
230+
...options,
231+
},
232+
queryClient,
233+
),
235234
useMutation: (method, path, options, queryClient) =>
236235
useMutation(
237236
{
@@ -252,4 +251,3 @@ export default function createClient<Paths extends {}, Media extends MediaType =
252251
),
253252
};
254253
}
255-

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

+51-52
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,6 @@ describe("client", () => {
827827
const fetchClient = createFetchClient<paths>({ baseUrl });
828828
const client = createClient(fetchClient);
829829

830-
// Track request URLs using the mock handler
831-
let firstRequestUrl: URL | undefined;
832-
let secondRequestUrl: URL | undefined;
833-
834830
// First page request handler
835831
const firstRequestHandler = useMockRequestHandler({
836832
baseUrl,
@@ -841,28 +837,32 @@ describe("client", () => {
841837
});
842838

843839
const { result, rerender } = renderHook(
844-
() => client.useInfiniteQuery("get", "/paginated-data",
845-
{
846-
params: {
847-
query: {
848-
limit: 3
849-
}
850-
}
851-
},
852-
{
853-
getNextPageParam: (lastPage) => lastPage.nextPage,
854-
initialPageParam: 0,
855-
}),
840+
() =>
841+
client.useInfiniteQuery(
842+
"get",
843+
"/paginated-data",
844+
{
845+
params: {
846+
query: {
847+
limit: 3,
848+
},
849+
},
850+
},
851+
{
852+
getNextPageParam: (lastPage) => lastPage.nextPage,
853+
initialPageParam: 0,
854+
},
855+
),
856856
{ wrapper },
857857
);
858858

859859
// Wait for initial query to complete
860860
await waitFor(() => expect(result.current.isSuccess).toBe(true));
861861

862862
// Verify first request
863-
firstRequestUrl = firstRequestHandler.getRequestUrl();
864-
expect(firstRequestUrl?.searchParams.get('limit')).toBe('3');
865-
expect(firstRequestUrl?.searchParams.get('cursor')).toBe('0');
863+
const firstRequestUrl = firstRequestHandler.getRequestUrl();
864+
expect(firstRequestUrl?.searchParams.get("limit")).toBe("3");
865+
expect(firstRequestUrl?.searchParams.get("cursor")).toBe("0");
866866

867867
// Set up mock for second page before triggering next page fetch
868868
const secondRequestHandler = useMockRequestHandler({
@@ -888,25 +888,24 @@ describe("client", () => {
888888
});
889889

890890
// Verify second request
891-
secondRequestUrl = secondRequestHandler.getRequestUrl();
892-
expect(secondRequestUrl?.searchParams.get('limit')).toBe('3');
893-
expect(secondRequestUrl?.searchParams.get('cursor')).toBe('1');
891+
const secondRequestUrl = secondRequestHandler.getRequestUrl();
892+
expect(secondRequestUrl?.searchParams.get("limit")).toBe("3");
893+
expect(secondRequestUrl?.searchParams.get("cursor")).toBe("1");
894894

895895
expect(result.current.data).toBeDefined();
896-
expect(result.current.data!.pages[0].nextPage).toBe(1);
897-
896+
expect(result.current.data?.pages[0].nextPage).toBe(1);
898897

899898
expect(result.current.data).toBeDefined();
900-
expect(result.current.data!.pages[1].nextPage).toBe(2);
899+
expect(result.current.data?.pages[1].nextPage).toBe(2);
901900

902901
// Verify the complete data structure
903902
expect(result.current.data?.pages).toEqual([
904903
{ items: [1, 2, 3], nextPage: 1 },
905-
{ items: [4, 5, 6], nextPage: 2 }
904+
{ items: [4, 5, 6], nextPage: 2 },
906905
]);
907906

908907
// Verify we can access all items through pages
909-
const allItems = result.current.data?.pages.flatMap(page => page.items);
908+
const allItems = result.current.data?.pages.flatMap((page) => page.items);
910909
expect(allItems).toEqual([1, 2, 3, 4, 5, 6]);
911910
});
912911
it("should reverse pages and pageParams when using the select option", async () => {
@@ -923,27 +922,27 @@ describe("client", () => {
923922
});
924923

925924
const { result, rerender } = renderHook(
926-
() =>
927-
client.useInfiniteQuery(
928-
"get",
929-
"/paginated-data",
930-
{
931-
params: {
932-
query: {
933-
limit: 3,
934-
},
935-
},
936-
},
937-
{
938-
getNextPageParam: (lastPage) => lastPage.nextPage,
939-
initialPageParam: 0,
940-
select: (data) => ({
941-
pages: [...data.pages].reverse(),
942-
pageParams: [...data.pageParams].reverse(),
943-
}),
944-
}
945-
),
946-
{ wrapper }
925+
() =>
926+
client.useInfiniteQuery(
927+
"get",
928+
"/paginated-data",
929+
{
930+
params: {
931+
query: {
932+
limit: 3,
933+
},
934+
},
935+
},
936+
{
937+
getNextPageParam: (lastPage) => lastPage.nextPage,
938+
initialPageParam: 0,
939+
select: (data) => ({
940+
pages: [...data.pages].reverse(),
941+
pageParams: [...data.pageParams].reverse(),
942+
}),
943+
},
944+
),
945+
{ wrapper },
947946
);
948947

949948
// Wait for initial query to complete
@@ -979,17 +978,17 @@ describe("client", () => {
979978
expect(result.current.data).toBeDefined();
980979

981980
// Since pages are reversed, the second page will now come first
982-
expect(result.current.data!.pages).toEqual([
981+
expect(result.current.data?.pages).toEqual([
983982
{ items: [4, 5, 6], nextPage: 2 },
984983
{ items: [1, 2, 3], nextPage: 1 },
985984
]);
986985

987986
// Verify reversed pageParams
988-
expect(result.current.data!.pageParams).toEqual([1, 0]);
987+
expect(result.current.data?.pageParams).toEqual([1, 0]);
989988

990989
// Verify all items from reversed pages
991-
const allItems = result.current.data!.pages.flatMap((page) => page.items);
990+
const allItems = result.current.data?.pages.flatMap((page) => page.items);
992991
expect(allItems).toEqual([4, 5, 6, 1, 2, 3]);
993992
});
994-
})
993+
});
995994
});

0 commit comments

Comments
 (0)