Skip to content

Commit 23651ce

Browse files
committed
fix: issue in the query client, forgot to support input parameter
1 parent 9512de7 commit 23651ce

5 files changed

Lines changed: 94 additions & 15 deletions

File tree

example/app/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export default async function Home() {
77
const user = await serverTrpc.getUser.fetch();
88
console.log({ user });
99

10+
const mockOrganizationSlug = 'my-mock-org-slug';
11+
const org = await serverTrpc.getOrganization.fetch({
12+
'orgSlug':mockOrganizationSlug
13+
});
14+
console.log({ org });
15+
1016
return (
1117
<div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">
1218
<main className="flex min-h-screen w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">

example/components/client-comp.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ export const ClientComp: React.FC = () => {
77
const user = clientTrpc.getUser.useQuery();
88
console.log({ clientUser: user.data });
99

10+
const mockOrganizationSlug = "my-mock-org-slug";
11+
const organization = clientTrpc.getOrganization.useQuery({
12+
input: {
13+
orgSlug: mockOrganizationSlug,
14+
},
15+
});
16+
console.log({ clientOrganization: organization });
17+
1018
if (user.isLoading) {
1119
return <>Loading...</>;
1220
}

example/trpc/router.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ interface MockUser {
1616
email:string,
1717
avatar:string
1818
}
19+
20+
interface MockOrganization {
21+
id: string,
22+
name: string,
23+
slug: string,
24+
description: string,
25+
memberCount: number,
26+
createdAt: string
27+
}
1928
export const appRouter = ctx.router({
2029
getUser: ctx.endpoint.action(async ({ hello, howAreYou, request }) => {
2130
console.log({
@@ -61,6 +70,35 @@ export const appRouter = ctx.router({
6170
}, 1000);
6271
});
6372
}),
73+
getOrganization: ctx.endpoint
74+
.input(
75+
z.object({
76+
orgSlug: z.string(),
77+
})
78+
)
79+
.action(async ({ orgSlug }, { hello, howAreYou, request }) => {
80+
console.log({
81+
myCustomContext: {
82+
hello,
83+
howAreYou,
84+
},
85+
nextjsRequest: request,
86+
orgSlug,
87+
});
88+
89+
return new Promise<MockOrganization>((resolve) => {
90+
setTimeout(() => {
91+
resolve({
92+
id: "org-123",
93+
name: `Organization ${orgSlug}`,
94+
slug: orgSlug,
95+
description: `This is a mock organization for ${orgSlug}`,
96+
memberCount: 25,
97+
createdAt: "2023-01-01T00:00:00.000Z",
98+
});
99+
}, 1000);
100+
});
101+
}),
64102
});
65103

66104
export type AppRouter = typeof appRouter;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@creatorem/next-trpc",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/creatorem/next-trpc"

src/create-trpc-query-client.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,48 @@ import {
77
getTrpcFetch,
88
} from "./create-trpc-client";
99
import type { Router, Endpoint, router } from "./core";
10+
import z from "zod";
1011

1112
type TrpcClientWithQuery<R extends Router<any>> = {
1213
[K in keyof R]: R[K] extends Endpoint<infer Output, infer Input, any>
1314
? EndpointClient<Input, Output> & {
14-
useQuery: (
15-
queryOptions?: Omit<
16-
Parameters<typeof useQueryType>[0],
17-
"queryKey" | "queryFn"
18-
>
19-
) => ReturnType<
20-
typeof useQueryType<Awaited<Output>, Error, Awaited<Output>, string[]>
21-
>;
15+
useQuery: Input extends import("zod").Schema
16+
? (
17+
queryOptions: Omit<
18+
Parameters<typeof useQueryType>[0],
19+
"queryKey" | "queryFn"
20+
> & {
21+
input: z.infer<Input>;
22+
}
23+
) => ReturnType<
24+
typeof useQueryType<
25+
Awaited<Output>,
26+
Error,
27+
Awaited<Output>,
28+
string[]
29+
>
30+
>
31+
: (
32+
queryOptions?: Omit<
33+
Parameters<typeof useQueryType>[0],
34+
"queryKey" | "queryFn"
35+
>
36+
) => ReturnType<
37+
typeof useQueryType<
38+
Awaited<Output>,
39+
Error,
40+
Awaited<Output>,
41+
string[]
42+
>
43+
>;
2244
}
2345
: never;
2446
};
2547

2648
export const createTrpcQueryClient = <
2749
R extends ReturnType<typeof router<any, Router<any>>>
2850
>(
29-
opts: createTrpcClientOptions & {useQuery: typeof useQueryType}
51+
opts: createTrpcClientOptions & { useQuery: typeof useQueryType }
3052
): TrpcClientWithQuery<R> => {
3153
return new Proxy({} as TrpcClientWithQuery<R>, {
3254
get(target, prop) {
@@ -40,16 +62,21 @@ export const createTrpcQueryClient = <
4062
queryOptions?: Omit<
4163
Parameters<typeof useQueryType>[0],
4264
"queryKey" | "queryFn"
43-
>
65+
> & {
66+
input?: any;
67+
}
4468
) => {
4569
const endpointName = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
4670
return opts.useQuery({
4771
...queryOptions,
4872
queryKey: [endpointName],
49-
queryFn: getTrpcFetch({
50-
endpointSlug: prop,
51-
...opts,
52-
}),
73+
queryFn: async () => {
74+
const fetcher = getTrpcFetch({
75+
endpointSlug: prop,
76+
...opts,
77+
});
78+
return await fetcher(queryOptions?.input);
79+
},
5380
});
5481
},
5582
};

0 commit comments

Comments
 (0)