-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
71 lines (66 loc) · 2.19 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { GetNextPageParamFunction } from "@tanstack/react-query";
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
import { createGetProxy } from "../utils/misc";
import { trpcClient } from "./client-internal.client";
import type { trpcApp } from "./server";
//
// quick and dirty react-query integration (super simplified version of https://trpc.io/docs/reactjs/introduction)
//
type Inputs = inferRouterInputs<typeof trpcApp>;
type Outputs = inferRouterOutputs<typeof trpcApp>;
export { Inputs as TrpcInputs };
type ReactQueryIntegration = {
[K in keyof Inputs]: {
queryKey: K;
queryOptions: (input: Inputs[K]) => {
queryKey: unknown[];
queryFn: () => Promise<Outputs[K]>;
};
infiniteQueryOptions: (
input: Inputs[K],
options: {
getNextPageParam: GetNextPageParamFunction<Outputs[K]>;
setPageParam: (input: Inputs[K], pageParam: unknown) => Inputs[K];
}
) => {
queryKey: unknown[];
queryFn: (context: unknown) => Promise<Outputs[K]>;
getNextPageParam: any;
};
mutationKey: K;
mutationOptions: () => {
mutationKey: unknown[];
mutationFn: (input: Inputs[K]) => Promise<Outputs[K]>;
};
};
};
// prettier-ignore
export const trpc =
createGetProxy((k) =>
createGetProxy(prop => {
if (prop === "queryKey" || prop === "mutationKey") {
return k;
}
if (prop === "queryOptions") {
return (input: unknown) => ({
queryKey: [k, input],
queryFn: () => (trpcClient as any)[k].query(input),
})
}
if (prop === "infiniteQueryOptions") {
return (input: unknown, options: any) => ({
queryKey: [k, input],
queryFn: ({ pageParam }: any) => (trpcClient as any)[k].query(options.setPageParam(input, pageParam)),
getNextPageParam: options.getNextPageParam,
})
}
if (prop === "mutationOptions") {
return () => ({
mutationKey: [k],
mutationFn: (input: unknown) => (trpcClient as any)[k].mutate(input),
})
}
console.error({ k, prop });
throw new Error("invalid trpc react-query call");
})
) as ReactQueryIntegration;