|
1 |
| -export * from '../runtime/vue'; |
| 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 InfiniteData, |
| 9 | + type QueryKey, |
| 10 | + type UseInfiniteQueryOptions, |
| 11 | + type UseMutationOptions, |
| 12 | + type UseQueryOptions, |
| 13 | +} from '@tanstack/vue-query'; |
| 14 | +import type { ModelMeta } from '@zenstackhq/runtime/cross'; |
| 15 | +import { computed, inject, provide, toValue, type ComputedRef, type MaybeRefOrGetter } from 'vue'; |
| 16 | +import { |
| 17 | + APIContext, |
| 18 | + DEFAULT_QUERY_ENDPOINT, |
| 19 | + fetcher, |
| 20 | + getQueryKey, |
| 21 | + makeUrl, |
| 22 | + marshal, |
| 23 | + setupInvalidation, |
| 24 | + setupOptimisticUpdate, |
| 25 | + type ExtraMutationOptions, |
| 26 | + type ExtraQueryOptions, |
| 27 | + type FetchFn, |
| 28 | +} from '../runtime/common'; |
| 29 | + |
| 30 | +export { APIContext as RequestHandlerContext } from '../runtime/common'; |
| 31 | + |
| 32 | +export const VueQueryContextKey = 'zenstack-vue-query-context'; |
| 33 | + |
| 34 | +/** |
| 35 | + * Provide context for the generated TanStack Query hooks. |
| 36 | + */ |
| 37 | +export function provideHooksContext(context: APIContext) { |
| 38 | + provide<APIContext>(VueQueryContextKey, context); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Hooks context. |
| 43 | + */ |
| 44 | +export function getHooksContext() { |
| 45 | + const { endpoint, ...rest } = inject<APIContext>(VueQueryContextKey, { |
| 46 | + endpoint: DEFAULT_QUERY_ENDPOINT, |
| 47 | + fetch: undefined, |
| 48 | + logging: false, |
| 49 | + }); |
| 50 | + return { endpoint: endpoint ?? DEFAULT_QUERY_ENDPOINT, ...rest }; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a vue-query query. |
| 55 | + * |
| 56 | + * @param model The name of the model under query. |
| 57 | + * @param url The request URL. |
| 58 | + * @param args The request args object, URL-encoded and appended as "?q=" parameter |
| 59 | + * @param options The vue-query options object |
| 60 | + * @param fetch The fetch function to use for sending the HTTP request |
| 61 | + * @returns useQuery hook |
| 62 | + */ |
| 63 | +export function useModelQuery<TQueryFnData, TData, TError>( |
| 64 | + model: string, |
| 65 | + url: string, |
| 66 | + args?: MaybeRefOrGetter<unknown> | ComputedRef<unknown>, |
| 67 | + options?: |
| 68 | + | MaybeRefOrGetter<Omit<UseQueryOptions<TQueryFnData, TError, TData>, 'queryKey'> & ExtraQueryOptions> |
| 69 | + | ComputedRef<Omit<UseQueryOptions<TQueryFnData, TError, TData>, 'queryKey'> & ExtraQueryOptions>, |
| 70 | + fetch?: FetchFn |
| 71 | +) { |
| 72 | + const queryOptions = computed(() => { |
| 73 | + const optionsValue = toValue< |
| 74 | + (Omit<UseQueryOptions<TQueryFnData, TError, TData>, 'queryKey'> & ExtraQueryOptions) | undefined |
| 75 | + >(options); |
| 76 | + return { |
| 77 | + queryKey: getQueryKey(model, url, args, { |
| 78 | + infinite: false, |
| 79 | + optimisticUpdate: optionsValue?.optimisticUpdate !== false, |
| 80 | + }), |
| 81 | + queryFn: ({ queryKey }: { queryKey: QueryKey }) => { |
| 82 | + const [_prefix, _model, _op, args] = queryKey; |
| 83 | + const reqUrl = makeUrl(url, toValue(args)); |
| 84 | + return fetcher<TQueryFnData, false>(reqUrl, undefined, fetch, false); |
| 85 | + }, |
| 86 | + ...optionsValue, |
| 87 | + }; |
| 88 | + }); |
| 89 | + return useQuery<TQueryFnData, TError, TData>(queryOptions); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Creates a vue-query infinite query. |
| 94 | + * |
| 95 | + * @param model The name of the model under query. |
| 96 | + * @param url The request URL. |
| 97 | + * @param args The initial request args object, URL-encoded and appended as "?q=" parameter |
| 98 | + * @param options The vue-query infinite query options object |
| 99 | + * @param fetch The fetch function to use for sending the HTTP request |
| 100 | + * @returns useInfiniteQuery hook |
| 101 | + */ |
| 102 | +export function useInfiniteModelQuery<TQueryFnData, TData, TError>( |
| 103 | + model: string, |
| 104 | + url: string, |
| 105 | + args?: MaybeRefOrGetter<unknown> | ComputedRef<unknown>, |
| 106 | + options?: |
| 107 | + | MaybeRefOrGetter< |
| 108 | + Omit<UseInfiniteQueryOptions<TQueryFnData, TError, InfiniteData<TData>>, 'queryKey' | 'initialPageParam'> |
| 109 | + > |
| 110 | + | ComputedRef< |
| 111 | + Omit<UseInfiniteQueryOptions<TQueryFnData, TError, InfiniteData<TData>>, 'queryKey' | 'initialPageParam'> |
| 112 | + >, |
| 113 | + fetch?: FetchFn |
| 114 | +) { |
| 115 | + // CHECKME: vue-query's `useInfiniteQuery`'s input typing seems wrong |
| 116 | + const queryOptions: any = computed(() => ({ |
| 117 | + queryKey: getQueryKey(model, url, args, { infinite: true, optimisticUpdate: false }), |
| 118 | + queryFn: ({ queryKey, pageParam }: { queryKey: QueryKey; pageParam?: unknown }) => { |
| 119 | + const [_prefix, _model, _op, args] = queryKey; |
| 120 | + const reqUrl = makeUrl(url, pageParam ?? toValue(args)); |
| 121 | + return fetcher<TQueryFnData, false>(reqUrl, undefined, fetch, false); |
| 122 | + }, |
| 123 | + initialPageParam: toValue(args), |
| 124 | + ...toValue(options), |
| 125 | + })); |
| 126 | + |
| 127 | + return useInfiniteQuery<TQueryFnData, TError, InfiniteData<TData>>(queryOptions); |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Creates a mutation with vue-query. |
| 132 | + * |
| 133 | + * @param model The name of the model under mutation. |
| 134 | + * @param method The HTTP method. |
| 135 | + * @param modelMeta The model metadata. |
| 136 | + * @param url The request URL. |
| 137 | + * @param options The vue-query options. |
| 138 | + * @param fetch The fetch function to use for sending the HTTP request |
| 139 | + * @param checkReadBack Whether to check for read back errors and return undefined if found. |
| 140 | + * @returns useMutation hooks |
| 141 | + */ |
| 142 | +export function useModelMutation< |
| 143 | + TArgs, |
| 144 | + TError, |
| 145 | + R = any, |
| 146 | + C extends boolean = boolean, |
| 147 | + Result = C extends true ? R | undefined : R |
| 148 | +>( |
| 149 | + model: string, |
| 150 | + method: 'POST' | 'PUT' | 'DELETE', |
| 151 | + url: string, |
| 152 | + modelMeta: ModelMeta, |
| 153 | + options?: |
| 154 | + | MaybeRefOrGetter< |
| 155 | + Omit<UseMutationOptions<Result, TError, TArgs, unknown>, 'mutationFn'> & ExtraMutationOptions |
| 156 | + > |
| 157 | + | ComputedRef<Omit<UseMutationOptions<Result, TError, TArgs, unknown>, 'mutationFn'> & ExtraMutationOptions>, |
| 158 | + fetch?: FetchFn, |
| 159 | + checkReadBack?: C |
| 160 | +) { |
| 161 | + const queryClient = useQueryClient(); |
| 162 | + const mutationFn = (data: any) => { |
| 163 | + const reqUrl = method === 'DELETE' ? makeUrl(url, data) : url; |
| 164 | + const fetchInit: RequestInit = { |
| 165 | + method, |
| 166 | + ...(method !== 'DELETE' && { |
| 167 | + headers: { |
| 168 | + 'content-type': 'application/json', |
| 169 | + }, |
| 170 | + body: marshal(data), |
| 171 | + }), |
| 172 | + }; |
| 173 | + return fetcher<R, C>(reqUrl, fetchInit, fetch, checkReadBack) as Promise<Result>; |
| 174 | + }; |
| 175 | + |
| 176 | + const optionsValue = toValue< |
| 177 | + (Omit<UseMutationOptions<Result, TError, TArgs, unknown>, 'mutationFn'> & ExtraMutationOptions) | undefined |
| 178 | + >(options); |
| 179 | + // TODO: figure out the typing problem |
| 180 | + const finalOptions: any = computed(() => ({ ...optionsValue, mutationFn })); |
| 181 | + const operation = url.split('/').pop(); |
| 182 | + const invalidateQueries = optionsValue?.invalidateQueries !== false; |
| 183 | + const optimisticUpdate = !!optionsValue?.optimisticUpdate; |
| 184 | + |
| 185 | + if (operation) { |
| 186 | + const { logging } = getHooksContext(); |
| 187 | + if (invalidateQueries) { |
| 188 | + setupInvalidation( |
| 189 | + model, |
| 190 | + operation, |
| 191 | + modelMeta, |
| 192 | + toValue(finalOptions), |
| 193 | + (predicate) => queryClient.invalidateQueries({ predicate }), |
| 194 | + logging |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + if (optimisticUpdate) { |
| 199 | + setupOptimisticUpdate( |
| 200 | + model, |
| 201 | + operation, |
| 202 | + modelMeta, |
| 203 | + toValue(finalOptions), |
| 204 | + queryClient.getQueryCache().getAll(), |
| 205 | + (queryKey, data) => queryClient.setQueryData<unknown>(queryKey, data), |
| 206 | + invalidateQueries ? (predicate) => queryClient.invalidateQueries({ predicate }) : undefined, |
| 207 | + logging |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + return useMutation<Result, TError, TArgs>(finalOptions); |
| 212 | +} |
0 commit comments