Skip to content

Commit

Permalink
Merge pull request #87 from stainless-api/cj/query-key-query
Browse files Browse the repository at this point in the history
fix(react-query): add query params to query key
  • Loading branch information
cjquines authored Jan 21, 2025
2 parents a1f5c79 + 1b58b0b commit d372e71
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/codegen/generated-api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe("Generated API Client", () => {
it("can pass query params", async () => {
const { queryFn, queryKey } = client.cats.useList({ color: "black" });

expect(queryKey).toEqual(["/api/cats"]);
expect(queryKey).toEqual(["/api/cats", "color=black"]);
expect(queryFn).toBeTypeOf("function");

const cats = await queryFn();
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/core/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe("API Client", () => {
it("can pass query params", async () => {
const { queryFn, queryKey } = client.cats.useList({ color: "black" });

expect(queryKey).toEqual(["/api/cats"]);
expect(queryKey).toEqual(["/api/cats", "color=black"]);
expect(queryFn).toBeTypeOf("function");

const cats = await queryFn();
Expand Down
25 changes: 19 additions & 6 deletions packages/client/src/core/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function isQueryOrBody(arg: unknown): arg is Record<string, string> {
return typeof arg === "object";
}

function makeQueryParams(query: Record<string, string>) {
return new URLSearchParams(Object.entries(query));
}

function makeUrl(
[basePath, ...callPath]: string[],
{
Expand All @@ -72,9 +76,9 @@ function makeUrl(
: callPath.map((str) => str.replace(":", "")).join("/");

if (query) {
url = `${url}?${new URLSearchParams(Object.entries(query))}`;
url = `${url}?${makeQueryParams(query)}`;
} else if (method === "GET" && body !== undefined && body !== null) {
url = `${url}?${new URLSearchParams(Object.entries(body))}`;
url = `${url}?${makeQueryParams(body as Record<string, string>)}`;
}

return `${basePath}/${url}`;
Expand Down Expand Up @@ -173,19 +177,23 @@ function createClientProxy(

if (config.extensions) {
const [action, extensionMethod] = callPath.slice(-2);
const method = inferHTTPMethod(action);
const path = callPath.slice(0, -2);
const bodyOrQuery = isQueryOrBody(pendingArgs[0])
? pendingArgs[0]
: undefined;
const query = method === "GET" ? bodyOrQuery : undefined;
const queryFn = (callTimeBody?: any) => {
const method = inferHTTPMethod(action);
const body = method === "GET" ? undefined : callTimeBody;
const query = method === "GET" ? bodyOrQuery : undefined;

return makeRequest(config, action, path, body, query);
};
const queryKey = [
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
makeUrl(path, {
outputCase: config.urlCase,
method: "GET",
}),
...(query ? [`${makeQueryParams(query)}`] : []),
];
const handler = getExtensionHandler(
config.extensions,
Expand All @@ -201,13 +209,18 @@ function createClientProxy(

if (isCallingHook(lastCall)) {
const action = callPath.slice(-1)[0];
const method = inferHTTPMethod(action);
const path = callPath.slice(0, -1);
const body = argumentsList[0];

return {
queryFn: () => makeRequest(config, action, path, body),
queryKey: [
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
makeUrl(path, {
outputCase: config.urlCase,
method: "GET",
}),
...(method === "GET" && body ? [`${makeQueryParams(body)}`] : []),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("react-query extension runtime", () => {
client.cats.list({ color: "black" }).useQuery();
expect(mockUseQuery).toBeCalledWith({
queryFn: expect.any(Function),
queryKey: ["/api/cats"],
queryKey: ["/api/cats", "color=black"],
});
expect(mockFetch).toBeCalledWith("/api/cats?color=black", {
method: "GET",
Expand Down

0 comments on commit d372e71

Please sign in to comment.