Skip to content

Commit d372e71

Browse files
authored
Merge pull request #87 from stainless-api/cj/query-key-query
fix(react-query): add query params to query key
2 parents a1f5c79 + 1b58b0b commit d372e71

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

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

+1-1
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"]);
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

+1-1
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"]);
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

+19-6
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}`;
@@ -173,19 +177,23 @@ function createClientProxy(
173177

174178
if (config.extensions) {
175179
const [action, extensionMethod] = callPath.slice(-2);
180+
const method = inferHTTPMethod(action);
176181
const path = callPath.slice(0, -2);
177182
const bodyOrQuery = isQueryOrBody(pendingArgs[0])
178183
? pendingArgs[0]
179184
: undefined;
185+
const query = method === "GET" ? bodyOrQuery : undefined;
180186
const queryFn = (callTimeBody?: any) => {
181-
const method = inferHTTPMethod(action);
182187
const body = method === "GET" ? undefined : callTimeBody;
183-
const query = method === "GET" ? bodyOrQuery : undefined;
184188

185189
return makeRequest(config, action, path, body, query);
186190
};
187191
const queryKey = [
188-
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
192+
makeUrl(path, {
193+
outputCase: config.urlCase,
194+
method: "GET",
195+
}),
196+
...(query ? [`${makeQueryParams(query)}`] : []),
189197
];
190198
const handler = getExtensionHandler(
191199
config.extensions,
@@ -201,13 +209,18 @@ function createClientProxy(
201209

202210
if (isCallingHook(lastCall)) {
203211
const action = callPath.slice(-1)[0];
212+
const method = inferHTTPMethod(action);
204213
const path = callPath.slice(0, -1);
205214
const body = argumentsList[0];
206215

207216
return {
208217
queryFn: () => makeRequest(config, action, path, body),
209218
queryKey: [
210-
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
219+
makeUrl(path, {
220+
outputCase: config.urlCase,
221+
method: "GET",
222+
}),
223+
...(method === "GET" && body ? [`${makeQueryParams(body)}`] : []),
211224
],
212225
};
213226
}

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

+1-1
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"],
64+
queryKey: ["/api/cats", "color=black"],
6565
});
6666
expect(mockFetch).toBeCalledWith("/api/cats?color=black", {
6767
method: "GET",

0 commit comments

Comments
 (0)