Skip to content

Commit 5e2195f

Browse files
committed
fix(openapi-react-query): reject query options as request init
1 parent 0cc7ee7 commit 5e2195f

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-react-query": patch
3+
---
4+
5+
Reject React Query options passed as request init.

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "ope
3030
type InferSelectReturnType<TData, TSelect> = TSelect extends (data: TData) => infer R ? R : TData;
3131

3232
type InitWithUnknowns<Init> = Init & { [key: string]: unknown };
33+
// Keep arbitrary fetch init fields while reserving options for React Query itself.
34+
type QueryInit<Init> = InitWithUnknowns<Init> & { [Key in keyof UseQueryOptions]?: never };
35+
type InfiniteQueryInit<Init> = InitWithUnknowns<Init> & { [Key in keyof UseInfiniteQueryOptions]?: never };
3336

3437
export type QueryKey<
3538
Paths extends Record<string, Record<HttpMethod, {}>>,
@@ -55,9 +58,7 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
5558
>(
5659
method: Method,
5760
path: Path,
58-
...[init, options]: RequiredKeysOf<Init> extends never
59-
? [InitWithUnknowns<Init>?, Options?]
60-
: [InitWithUnknowns<Init>, Options?]
61+
...[init, options]: RequiredKeysOf<Init> extends never ? [QueryInit<Init>?, Options?] : [QueryInit<Init>, Options?]
6162
) => NoInfer<
6263
Omit<
6364
UseQueryOptions<
@@ -99,8 +100,8 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
99100
method: Method,
100101
url: Path,
101102
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
102-
? [InitWithUnknowns<Init>?, Options?, QueryClient?]
103-
: [InitWithUnknowns<Init>, Options?, QueryClient?]
103+
? [QueryInit<Init>?, Options?, QueryClient?]
104+
: [QueryInit<Init>, Options?, QueryClient?]
104105
) => UseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;
105106

106107
export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
@@ -123,7 +124,7 @@ export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMetho
123124
>(
124125
method: Method,
125126
url: Path,
126-
init: InitWithUnknowns<Init>,
127+
init: InfiniteQueryInit<Init>,
127128
options: Options,
128129
queryClient?: QueryClient,
129130
) => UseInfiniteQueryResult<
@@ -149,8 +150,8 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
149150
method: Method,
150151
url: Path,
151152
...[init, options, queryClient]: RequiredKeysOf<Init> extends never
152-
? [InitWithUnknowns<Init>?, Options?, QueryClient?]
153-
: [InitWithUnknowns<Init>, Options?, QueryClient?]
153+
? [QueryInit<Init>?, Options?, QueryClient?]
154+
: [QueryInit<Init>, Options?, QueryClient?]
154155
) => UseSuspenseQueryResult<InferSelectReturnType<Response["data"], Options["select"]>, Response["error"]>;
155156

156157
export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,18 @@ describe("client", () => {
478478
>();
479479
});
480480

481+
it("should reject query options passed as request init", () => {
482+
const fetchClient = createFetchClient<minimalGetPaths>({ baseUrl });
483+
const client = createClient(fetchClient);
484+
485+
// @ts-expect-error Query options belong in the fourth argument.
486+
const invalidQuery = () => client.useQuery("get", "/foo", { retry: false });
487+
const customInit = () => client.useQuery("get", "/foo", { customRequestOption: true });
488+
expectTypeOf(customInit).toBeFunction();
489+
490+
expectTypeOf(invalidQuery).toBeFunction();
491+
});
492+
481493
it("passes abort signal to fetch", async () => {
482494
let signalPassedToFetch: AbortSignal | undefined;
483495

0 commit comments

Comments
 (0)