Skip to content

Commit 1b58b0b

Browse files
committed
split query key
1 parent 9e82374 commit 1b58b0b

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

packages/client/src/codegen/generated-api-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe("Generated API Client", () => {
122122
it("can pass query params", async () => {
123123
const { queryFn, queryKey } = client.cats.useList({ color: "black" });
124124

125-
expect(queryKey).toEqual(["/api/cats?color=black"]);
125+
expect(queryKey).toEqual(["/api/cats", "color=black"]);
126126
expect(queryFn).toBeTypeOf("function");
127127

128128
const cats = await queryFn();

packages/client/src/core/api-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe("API Client", () => {
174174
it("can pass query params", async () => {
175175
const { queryFn, queryKey } = client.cats.useList({ color: "black" });
176176

177-
expect(queryKey).toEqual(["/api/cats?color=black"]);
177+
expect(queryKey).toEqual(["/api/cats", "color=black"]);
178178
expect(queryFn).toBeTypeOf("function");
179179

180180
const cats = await queryFn();

packages/client/src/core/api-client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ function isQueryOrBody(arg: unknown): arg is Record<string, string> {
4848
return typeof arg === "object";
4949
}
5050

51+
function makeQueryParams(query: Record<string, string>) {
52+
return new URLSearchParams(Object.entries(query));
53+
}
54+
5155
function makeUrl(
5256
[basePath, ...callPath]: string[],
5357
{
@@ -72,9 +76,9 @@ function makeUrl(
7276
: callPath.map((str) => str.replace(":", "")).join("/");
7377

7478
if (query) {
75-
url = `${url}?${new URLSearchParams(Object.entries(query))}`;
79+
url = `${url}?${makeQueryParams(query)}`;
7680
} else if (method === "GET" && body !== undefined && body !== null) {
77-
url = `${url}?${new URLSearchParams(Object.entries(body))}`;
81+
url = `${url}?${makeQueryParams(body as Record<string, string>)}`;
7882
}
7983

8084
return `${basePath}/${url}`;
@@ -188,8 +192,8 @@ function createClientProxy(
188192
makeUrl(path, {
189193
outputCase: config.urlCase,
190194
method: "GET",
191-
query,
192195
}),
196+
...(query ? [`${makeQueryParams(query)}`] : []),
193197
];
194198
const handler = getExtensionHandler(
195199
config.extensions,
@@ -215,8 +219,8 @@ function createClientProxy(
215219
makeUrl(path, {
216220
outputCase: config.urlCase,
217221
method: "GET",
218-
query: method === "GET" ? body : undefined,
219222
}),
223+
...(method === "GET" && body ? [`${makeQueryParams(body)}`] : []),
220224
],
221225
};
222226
}

packages/client/src/extensions/extensions.generated.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("react-query extension runtime", () => {
6161
client.cats.list({ color: "black" }).useQuery();
6262
expect(mockUseQuery).toBeCalledWith({
6363
queryFn: expect.any(Function),
64-
queryKey: ["/api/cats?color=black"],
64+
queryKey: ["/api/cats", "color=black"],
6565
});
6666
expect(mockFetch).toBeCalledWith("/api/cats?color=black", {
6767
method: "GET",

0 commit comments

Comments
 (0)