Skip to content

Drop init argument when not needed in query key #2061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-steaks-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": patch
---

Drop init argument when not needed in query key
9 changes: 7 additions & 2 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type QueryKey<
Paths extends Record<string, Record<HttpMethod, {}>>,
Method extends HttpMethod,
Path extends PathsWithMethod<Paths, Method>,
> = readonly [Method, Path, MaybeOptionalInit<Paths[Path], Method>];
Init = MaybeOptionalInit<Paths[Path], Method>,
> = Init extends undefined ? readonly [Method, Path] : readonly [Method, Path, Init];

export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
Method extends HttpMethod,
Expand Down Expand Up @@ -123,7 +124,11 @@ export default function createClient<Paths extends {}, Media extends MediaType =
};

const queryOptions: QueryOptionsFunction<Paths, Media> = (method, path, ...[init, options]) => ({
queryKey: [method, path, init as InitWithUnknowns<typeof init>] as const,
queryKey: (init === undefined ? ([method, path] as const) : ([method, path, init] as const)) as QueryKey<
Paths,
typeof method,
typeof path
>,
queryFn,
...options,
});
Expand Down
10 changes: 10 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ describe("client", () => {
expectTypeOf(result.current.data).toEqualTypeOf<"select(true)">();
expectTypeOf(result.current.error).toEqualTypeOf<false | null>();
});

it("returns query options without an init", async () => {
const fetchClient = createFetchClient<minimalGetPaths>({
baseUrl,
fetch: () => Promise.resolve(Response.json(true)),
});
const client = createClient(fetchClient);

expect(client.queryOptions("get", "/foo").queryKey.length).toBe(2);
});
});

describe("useQuery", () => {
Expand Down
Loading