|
1 | 1 | import safeStringify from "fast-safe-stringify";
|
2 | 2 | import {
|
3 | 3 | DeepAsyncFnRecord,
|
| 4 | + TypedGetSDKQueryKey, |
4 | 5 | Opts,
|
5 |
| - TypedQuery, |
6 |
| - TypedUseInfiniteQuery, |
7 |
| - TypedUseQuery, |
| 6 | + TypedSDK as TypedSDK, |
| 7 | + TypedUseInfiniteQuery as TypedUseInfiniteSDK, |
| 8 | + TypedUseQuery as TypedUseSDK, |
| 9 | + TypedUseSDKMutation, |
8 | 10 | } from "./types";
|
9 | 11 | import {
|
10 | 12 | useInfiniteQuery,
|
11 | 13 | UseInfiniteQueryOptions,
|
| 14 | + useMutation, |
| 15 | + UseMutationOptions, |
12 | 16 | useQuery,
|
13 | 17 | UseQueryOptions,
|
14 | 18 | } from "react-query";
|
15 |
| -import { createBifrostQuery } from "./server"; |
16 |
| - |
17 |
| -export { createBifrostQuery } from "./server"; |
| 19 | +import { createBifrostSDK } from "./server"; |
18 | 20 |
|
19 | 21 | export function createBifrost<Endpoints extends DeepAsyncFnRecord<Endpoints>>(
|
20 | 22 | opts: Opts
|
21 | 23 | ): {
|
22 |
| - query: TypedQuery<Endpoints>; |
23 |
| - useQuery: () => TypedUseQuery<Endpoints>; |
24 |
| - useInfiniteQuery: () => TypedUseInfiniteQuery<Endpoints>; |
25 |
| - // useQuerys: () => |
| 24 | + sdk: TypedSDK<Endpoints>; |
| 25 | + sdkPrefetch: TypedSDK<Endpoints>; |
| 26 | + getSDKQueryKey: TypedGetSDKQueryKey<Endpoints>; |
| 27 | + useSDK: () => TypedUseSDK<Endpoints>; |
| 28 | + useInfiniteSDK: () => TypedUseInfiniteSDK<Endpoints>; |
| 29 | + useSDKMutation: () => TypedUseSDKMutation<Endpoints>; |
26 | 30 | } {
|
27 | 31 | return {
|
28 |
| - query: createBifrostQuery<Endpoints>(opts), |
29 |
| - useQuery: createBifrostUseQuery<Endpoints>(opts), |
30 |
| - useInfiniteQuery: createBifrostUseInfiniteQuery<Endpoints>(opts), |
| 32 | + sdk: createBifrostSDK<Endpoints>(opts), |
| 33 | + sdkPrefetch: createBifrostSDKPrefetch<Endpoints>(opts), |
| 34 | + getSDKQueryKey: createGetSDKQueryKey<Endpoints>(opts), |
| 35 | + useSDK: createBifrostUseSDK<Endpoints>(opts), |
| 36 | + useInfiniteSDK: createBifrostUseInfiniteSDK<Endpoints>(opts), |
| 37 | + useSDKMutation: createBifrostUseSDKMutation<Endpoints>(opts), |
31 | 38 | };
|
32 | 39 | }
|
33 | 40 |
|
34 |
| -export function createBifrostUseQuery< |
| 41 | +export function createBifrostUseSDK< |
35 | 42 | Endpoints extends DeepAsyncFnRecord<Endpoints>
|
36 |
| ->(opts: Opts): () => TypedUseQuery<Endpoints> { |
37 |
| - const getNextUseQuery = (p: { path: string[] }): any => { |
38 |
| - return new Proxy( |
39 |
| - () => {}, //use function as base, so that it can be called... |
40 |
| - { |
41 |
| - apply(__, ___, args) { |
42 |
| - const argument = args[0] as any; |
43 |
| - |
44 |
| - const extraQueryOpts = (args[1] || {}) as UseQueryOptions; |
45 |
| - const queryKey = [...p.path, safeStringify.stableStringify(argument)]; |
46 |
| - |
47 |
| - const queryOpts: UseQueryOptions = { |
48 |
| - queryKey, |
49 |
| - queryFn: ({ signal, queryKey, meta, pageParam }) => |
50 |
| - opts.doFetch({ |
51 |
| - argument, |
52 |
| - path: p.path, |
53 |
| - signal, |
54 |
| - queryKey: queryKey as any, |
55 |
| - meta, |
56 |
| - pageParam, |
57 |
| - }), |
58 |
| - ...extraQueryOpts, |
59 |
| - }; |
60 |
| - return useQuery(queryOpts); |
61 |
| - }, |
62 |
| - get(__, prop) { |
63 |
| - return getNextUseQuery({ |
64 |
| - path: p.path.concat(prop.toString()), |
65 |
| - }); |
66 |
| - }, |
67 |
| - } |
68 |
| - ); |
| 43 | +>(opts: Opts): () => TypedUseSDK<Endpoints> { |
| 44 | + const getNextUseSDK = (p: { path: string[] }): any => { |
| 45 | + return new Proxy(() => {}, { |
| 46 | + apply(__, ___, args) { |
| 47 | + const argument = args[0] as any; |
| 48 | + |
| 49 | + const extraQueryOpts = (args[1] || {}) as UseQueryOptions; |
| 50 | + |
| 51 | + const queryOpts: UseQueryOptions = { |
| 52 | + queryKey: getQueryKey(p.path, argument), |
| 53 | + queryFn: ({ signal, queryKey, meta, pageParam }) => |
| 54 | + opts.doFetch({ |
| 55 | + argument, |
| 56 | + path: p.path, |
| 57 | + signal, |
| 58 | + queryKey: queryKey as any, |
| 59 | + meta, |
| 60 | + pageParam, |
| 61 | + }), |
| 62 | + ...extraQueryOpts, |
| 63 | + }; |
| 64 | + |
| 65 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 66 | + return useQuery(queryOpts); |
| 67 | + }, |
| 68 | + get(__, prop) { |
| 69 | + return getNextUseSDK({ |
| 70 | + path: p.path.concat(prop.toString()), |
| 71 | + }); |
| 72 | + }, |
| 73 | + }); |
69 | 74 | };
|
70 | 75 |
|
71 |
| - const useQueryRet = () => getNextUseQuery({ path: [] }); |
| 76 | + const useQueryRet = () => getNextUseSDK({ path: [] }); |
72 | 77 |
|
73 | 78 | return useQueryRet;
|
74 | 79 | }
|
75 | 80 |
|
76 |
| -export function createBifrostUseInfiniteQuery< |
| 81 | +export function createBifrostUseInfiniteSDK< |
77 | 82 | Endpoints extends DeepAsyncFnRecord<Endpoints>
|
78 |
| ->(opts: Opts): () => TypedUseInfiniteQuery<Endpoints> { |
79 |
| - const getNextUseQuery = (p: { path: string[] }): any => { |
80 |
| - return new Proxy( |
81 |
| - () => {}, //use function as base, so that it can be called... |
82 |
| - { |
83 |
| - apply(__, ___, args) { |
84 |
| - const argument = args[0] as any; |
85 |
| - |
86 |
| - const extraQueryOpts = (args[1] || {}) as UseInfiniteQueryOptions; |
87 |
| - const queryKey = [...p.path, safeStringify.stableStringify(argument)]; |
88 |
| - |
89 |
| - const queryOpts: UseInfiniteQueryOptions = { |
90 |
| - queryKey, |
91 |
| - queryFn: ({ signal, queryKey, meta, pageParam }) => |
92 |
| - opts.doFetch({ |
93 |
| - argument, |
94 |
| - path: p.path, |
95 |
| - signal, |
96 |
| - queryKey: queryKey as any, |
97 |
| - meta, |
98 |
| - pageParam, |
99 |
| - }), |
100 |
| - ...extraQueryOpts, |
101 |
| - }; |
102 |
| - return useInfiniteQuery(queryOpts); |
103 |
| - }, |
104 |
| - get(__, prop) { |
105 |
| - return getNextUseQuery({ |
106 |
| - path: p.path.concat(prop.toString()), |
107 |
| - }); |
108 |
| - }, |
109 |
| - } |
110 |
| - ); |
| 83 | +>(opts: Opts): () => TypedUseInfiniteSDK<Endpoints> { |
| 84 | + const getNextUseInfiniteSDK = (p: { path: string[] }): any => { |
| 85 | + return new Proxy(() => {}, { |
| 86 | + apply(__, ___, args) { |
| 87 | + const argument = args[0] as any; |
| 88 | + |
| 89 | + const extraQueryOpts = (args[1] || {}) as UseInfiniteQueryOptions; |
| 90 | + |
| 91 | + const queryOpts: UseInfiniteQueryOptions = { |
| 92 | + queryKey: getQueryKey(p.path, argument), |
| 93 | + queryFn: ({ signal, queryKey, meta, pageParam }) => |
| 94 | + opts.doFetch({ |
| 95 | + argument, |
| 96 | + path: p.path, |
| 97 | + signal, |
| 98 | + queryKey: queryKey as any, |
| 99 | + meta, |
| 100 | + pageParam, |
| 101 | + }), |
| 102 | + ...extraQueryOpts, |
| 103 | + }; |
| 104 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 105 | + return useInfiniteQuery(queryOpts); |
| 106 | + }, |
| 107 | + get(__, prop) { |
| 108 | + return getNextUseInfiniteSDK({ |
| 109 | + path: p.path.concat(prop.toString()), |
| 110 | + }); |
| 111 | + }, |
| 112 | + }); |
111 | 113 | };
|
112 | 114 |
|
113 |
| - const useQueryRet = () => getNextUseQuery({ path: [] }); |
| 115 | + const useQueryRet = () => getNextUseInfiniteSDK({ path: [] }); |
114 | 116 |
|
115 | 117 | return useQueryRet;
|
116 | 118 | }
|
| 119 | + |
| 120 | +export function createBifrostUseSDKMutation< |
| 121 | + Endpoints extends DeepAsyncFnRecord<Endpoints> |
| 122 | +>(opts: Opts): () => TypedUseSDKMutation<Endpoints> { |
| 123 | + const getNextUseMutation = (p: { path: string[] }): any => { |
| 124 | + return new Proxy(() => {}, { |
| 125 | + apply(__, ___, args) { |
| 126 | + const argument = args[0] as any; |
| 127 | + |
| 128 | + const extraQueryOpts = (args[1] || {}) as UseMutationOptions; |
| 129 | + const mutationKey = getQueryKey(p.path, argument); |
| 130 | + |
| 131 | + const queryOpts: UseMutationOptions = { |
| 132 | + mutationKey, |
| 133 | + mutationFn: () => |
| 134 | + opts.doFetch({ |
| 135 | + argument, |
| 136 | + path: p.path, |
| 137 | + queryKey: mutationKey as any, |
| 138 | + }), |
| 139 | + ...extraQueryOpts, |
| 140 | + }; |
| 141 | + |
| 142 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 143 | + return useMutation(queryOpts); |
| 144 | + }, |
| 145 | + get(__, prop) { |
| 146 | + return getNextUseMutation({ |
| 147 | + path: p.path.concat(prop.toString()), |
| 148 | + }); |
| 149 | + }, |
| 150 | + }); |
| 151 | + }; |
| 152 | + |
| 153 | + const useMutationRet = () => getNextUseMutation({ path: [] }); |
| 154 | + |
| 155 | + return useMutationRet; |
| 156 | +} |
| 157 | + |
| 158 | +export function createBifrostSDKPrefetch< |
| 159 | + Endpoints extends DeepAsyncFnRecord<Endpoints> |
| 160 | +>(opts: Opts): TypedSDK<Endpoints> { |
| 161 | + const getNextSDK = (path: string[]): any => { |
| 162 | + return new Proxy(() => {}, { |
| 163 | + apply(__, ___, args) { |
| 164 | + const argument = args[0]; |
| 165 | + const prom = opts.doFetch({ argument, path }); |
| 166 | + prom.then((resp) => { |
| 167 | + opts.queryClient.setQueryData(getQueryKey(path, argument), resp); |
| 168 | + }); |
| 169 | + |
| 170 | + return prom; |
| 171 | + }, |
| 172 | + get(__, prop) { |
| 173 | + return getNextSDK(path.concat(prop.toString())); |
| 174 | + }, |
| 175 | + }); |
| 176 | + }; |
| 177 | + |
| 178 | + return getNextSDK([]); |
| 179 | +} |
| 180 | + |
| 181 | +export function createGetSDKQueryKey< |
| 182 | + Endpoints extends DeepAsyncFnRecord<Endpoints> |
| 183 | +>(opts: Opts): TypedGetSDKQueryKey<Endpoints> { |
| 184 | + const getNextGetSDKQueryKey = (path: string[]): any => { |
| 185 | + return new Proxy(() => {}, { |
| 186 | + apply(__, ___, args) { |
| 187 | + return getQueryKey(path, args[0]); |
| 188 | + }, |
| 189 | + get(__, prop) { |
| 190 | + return getNextGetSDKQueryKey(path.concat(prop.toString())); |
| 191 | + }, |
| 192 | + }); |
| 193 | + }; |
| 194 | + |
| 195 | + return getNextGetSDKQueryKey([]); |
| 196 | +} |
| 197 | + |
| 198 | +function getQueryKey(path: string[], argument: unknown) { |
| 199 | + const queryKey = [...path]; |
| 200 | + if (argument !== "undefined") { |
| 201 | + queryKey.push(safeStringify.stableStringify(argument)); |
| 202 | + } |
| 203 | + return queryKey; |
| 204 | +} |
0 commit comments