|
| 1 | +/* eslint-disable @typescript-eslint/ban-types */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +import { |
| 4 | + useInfiniteQuery, |
| 5 | + useMutation, |
| 6 | + useQuery, |
| 7 | + useQueryClient, |
| 8 | + type MutateFunction, |
| 9 | + type QueryClient, |
| 10 | + type UseInfiniteQueryOptions, |
| 11 | + type UseMutationOptions, |
| 12 | + type UseQueryOptions, |
| 13 | +} from '@tanstack/vue-query'; |
| 14 | +import { inject } from 'vue'; |
| 15 | +import { DEFAULT_QUERY_ENDPOINT, FetchFn, QUERY_KEY_PREFIX, fetcher, makeUrl, marshal } from './common'; |
| 16 | +import { RequestHandlerContext } from './svelte'; |
| 17 | + |
| 18 | +export { APIContext as RequestHandlerContext } from './common'; |
| 19 | + |
| 20 | +export const VueQueryContextKey = 'zenstack-vue-query-context'; |
| 21 | + |
| 22 | +export function getContext() { |
| 23 | + return inject<RequestHandlerContext>(VueQueryContextKey, { |
| 24 | + endpoint: DEFAULT_QUERY_ENDPOINT, |
| 25 | + fetch: undefined, |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Creates a vue-query query. |
| 31 | + * |
| 32 | + * @param model The name of the model under query. |
| 33 | + * @param url The request URL. |
| 34 | + * @param args The request args object, URL-encoded and appended as "?q=" parameter |
| 35 | + * @param options The vue-query options object |
| 36 | + * @returns useQuery hook |
| 37 | + */ |
| 38 | +export function query<R>(model: string, url: string, args?: unknown, options?: UseQueryOptions<R>, fetch?: FetchFn) { |
| 39 | + const reqUrl = makeUrl(url, args); |
| 40 | + return useQuery<R>({ |
| 41 | + queryKey: [QUERY_KEY_PREFIX + model, url, args], |
| 42 | + queryFn: () => fetcher<R, false>(reqUrl, undefined, fetch, false), |
| 43 | + ...options, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates a vue-query infinite query. |
| 49 | + * |
| 50 | + * @param model The name of the model under query. |
| 51 | + * @param url The request URL. |
| 52 | + * @param args The initial request args object, URL-encoded and appended as "?q=" parameter |
| 53 | + * @param options The vue-query infinite query options object |
| 54 | + * @returns useInfiniteQuery hook |
| 55 | + */ |
| 56 | +export function infiniteQuery<R>( |
| 57 | + model: string, |
| 58 | + url: string, |
| 59 | + args?: unknown, |
| 60 | + options?: UseInfiniteQueryOptions<R>, |
| 61 | + fetch?: FetchFn |
| 62 | +) { |
| 63 | + return useInfiniteQuery<R>({ |
| 64 | + queryKey: [QUERY_KEY_PREFIX + model, url, args], |
| 65 | + queryFn: ({ pageParam }) => { |
| 66 | + return fetcher<R, false>(makeUrl(url, pageParam ?? args), undefined, fetch, false); |
| 67 | + }, |
| 68 | + ...options, |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Creates a POST mutation with vue-query. |
| 74 | + * |
| 75 | + * @param model The name of the model under mutation. |
| 76 | + * @param url The request URL. |
| 77 | + * @param options The vue-query options. |
| 78 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 79 | + * @returns useMutation hooks |
| 80 | + */ |
| 81 | +export function postMutation<T, R = any, C extends boolean = boolean, Result = C extends true ? R | undefined : R>( |
| 82 | + model: string, |
| 83 | + url: string, |
| 84 | + options?: Omit<UseMutationOptions<Result, unknown, T, unknown>, 'mutationFn'>, |
| 85 | + fetch?: FetchFn, |
| 86 | + invalidateQueries = true, |
| 87 | + checkReadBack?: C |
| 88 | +) { |
| 89 | + const queryClient = useQueryClient(); |
| 90 | + const mutationFn = (data: any) => |
| 91 | + fetcher<R, C>( |
| 92 | + url, |
| 93 | + { |
| 94 | + method: 'POST', |
| 95 | + headers: { |
| 96 | + 'content-type': 'application/json', |
| 97 | + }, |
| 98 | + body: marshal(data), |
| 99 | + }, |
| 100 | + fetch, |
| 101 | + checkReadBack |
| 102 | + ) as Promise<Result>; |
| 103 | + |
| 104 | + // TODO: figure out the typing problem |
| 105 | + const finalOptions: any = mergeOptions<T, Result>(model, options, invalidateQueries, mutationFn, queryClient); |
| 106 | + const mutation = useMutation<Result, unknown, T>(finalOptions); |
| 107 | + return mutation; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Creates a PUT mutation with vue-query. |
| 112 | + * |
| 113 | + * @param model The name of the model under mutation. |
| 114 | + * @param url The request URL. |
| 115 | + * @param options The vue-query options. |
| 116 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 117 | + * @returns useMutation hooks |
| 118 | + */ |
| 119 | +export function putMutation<T, R = any, C extends boolean = boolean, Result = C extends true ? R | undefined : R>( |
| 120 | + model: string, |
| 121 | + url: string, |
| 122 | + options?: Omit<UseMutationOptions<Result, unknown, T, unknown>, 'mutationFn'>, |
| 123 | + fetch?: FetchFn, |
| 124 | + invalidateQueries = true, |
| 125 | + checkReadBack?: C |
| 126 | +) { |
| 127 | + const queryClient = useQueryClient(); |
| 128 | + const mutationFn = (data: any) => |
| 129 | + fetcher<R, C>( |
| 130 | + url, |
| 131 | + { |
| 132 | + method: 'PUT', |
| 133 | + headers: { |
| 134 | + 'content-type': 'application/json', |
| 135 | + }, |
| 136 | + body: marshal(data), |
| 137 | + }, |
| 138 | + fetch, |
| 139 | + checkReadBack |
| 140 | + ) as Promise<Result>; |
| 141 | + |
| 142 | + // TODO: figure out the typing problem |
| 143 | + const finalOptions: any = mergeOptions<T, Result>(model, options, invalidateQueries, mutationFn, queryClient); |
| 144 | + const mutation = useMutation<Result, unknown, T>(finalOptions); |
| 145 | + return mutation; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Creates a DELETE mutation with vue-query. |
| 150 | + * |
| 151 | + * @param model The name of the model under mutation. |
| 152 | + * @param url The request URL. |
| 153 | + * @param options The vue-query options. |
| 154 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 155 | + * @returns useMutation hooks |
| 156 | + */ |
| 157 | +export function deleteMutation<T, R = any, C extends boolean = boolean, Result = C extends true ? R | undefined : R>( |
| 158 | + model: string, |
| 159 | + url: string, |
| 160 | + options?: Omit<UseMutationOptions<Result, unknown, T, unknown>, 'mutationFn'>, |
| 161 | + fetch?: FetchFn, |
| 162 | + invalidateQueries = true, |
| 163 | + checkReadBack?: C |
| 164 | +) { |
| 165 | + const queryClient = useQueryClient(); |
| 166 | + const mutationFn = (data: any) => |
| 167 | + fetcher<R, C>( |
| 168 | + makeUrl(url, data), |
| 169 | + { |
| 170 | + method: 'DELETE', |
| 171 | + }, |
| 172 | + fetch, |
| 173 | + checkReadBack |
| 174 | + ) as Promise<Result>; |
| 175 | + |
| 176 | + // TODO: figure out the typing problem |
| 177 | + const finalOptions: any = mergeOptions<T, Result>(model, options, invalidateQueries, mutationFn, queryClient); |
| 178 | + const mutation = useMutation<Result, unknown, T>(finalOptions); |
| 179 | + return mutation; |
| 180 | +} |
| 181 | + |
| 182 | +function mergeOptions<T, R = any>( |
| 183 | + model: string, |
| 184 | + options: Omit<UseMutationOptions<R, unknown, T, unknown>, 'mutationFn'> | undefined, |
| 185 | + invalidateQueries: boolean, |
| 186 | + mutationFn: MutateFunction<R, unknown, T>, |
| 187 | + queryClient: QueryClient |
| 188 | +): UseMutationOptions<R, unknown, T, unknown> { |
| 189 | + const result = { ...options, mutationFn }; |
| 190 | + if (options?.onSuccess || invalidateQueries) { |
| 191 | + result.onSuccess = (...args) => { |
| 192 | + if (invalidateQueries) { |
| 193 | + queryClient.invalidateQueries([QUERY_KEY_PREFIX + model]); |
| 194 | + } |
| 195 | + return options?.onSuccess?.(...args); |
| 196 | + }; |
| 197 | + } |
| 198 | + return result; |
| 199 | +} |
0 commit comments