|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +import { |
| 4 | + useMutation, |
| 5 | + useQuery, |
| 6 | + useQueryClient, |
| 7 | + type MutateFunction, |
| 8 | + type QueryClient, |
| 9 | + type UseMutationOptions, |
| 10 | + type UseQueryOptions, |
| 11 | +} from '@tanstack/react-query'; |
| 12 | +import { createContext } from 'react'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Context for configuring react hooks. |
| 16 | + */ |
| 17 | +export const RequestHandlerContext = createContext<RequestHandlerContext>({ |
| 18 | + endpoint: DEFAULT_QUERY_ENDPOINT, |
| 19 | +}); |
| 20 | + |
| 21 | +/** |
| 22 | + * Context provider. |
| 23 | + */ |
| 24 | +export const Provider = RequestHandlerContext.Provider; |
| 25 | + |
| 26 | +/** |
| 27 | + * Creates a react-query query. |
| 28 | + * |
| 29 | + * @param model The name of the model under query. |
| 30 | + * @param url The request URL. |
| 31 | + * @param args The request args object, which will be superjson-stringified and appended as "?q=" parameter |
| 32 | + * @param options The react-query options object |
| 33 | + * @returns useQuery hook |
| 34 | + */ |
| 35 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 36 | +export function query<R>(model: string, url: string, args?: unknown, options?: UseQueryOptions<R>) { |
| 37 | + const reqUrl = makeUrl(url, args); |
| 38 | + return useQuery<R>({ |
| 39 | + queryKey: [QUERY_KEY_PREFIX + model, url, args], |
| 40 | + queryFn: () => fetcher<R>(reqUrl), |
| 41 | + ...options, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Creates a POST mutation with react-query. |
| 47 | + * |
| 48 | + * @param model The name of the model under mutation. |
| 49 | + * @param url The request URL. |
| 50 | + * @param options The react-query options. |
| 51 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 52 | + * @returns useMutation hooks |
| 53 | + */ |
| 54 | +export function postMutation<T, R = any>( |
| 55 | + model: string, |
| 56 | + url: string, |
| 57 | + options?: Omit<UseMutationOptions<R, unknown, T>, 'mutationFn'>, |
| 58 | + invalidateQueries = true |
| 59 | +) { |
| 60 | + const queryClient = useQueryClient(); |
| 61 | + const mutationFn = (data: any) => |
| 62 | + fetcher<R>(url, { |
| 63 | + method: 'POST', |
| 64 | + headers: { |
| 65 | + 'content-type': 'application/json', |
| 66 | + }, |
| 67 | + body: marshal(data), |
| 68 | + }); |
| 69 | + |
| 70 | + const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient); |
| 71 | + const mutation = useMutation<R, unknown, T>(finalOptions); |
| 72 | + return mutation; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Creates a PUT mutation with react-query. |
| 77 | + * |
| 78 | + * @param model The name of the model under mutation. |
| 79 | + * @param url The request URL. |
| 80 | + * @param options The react-query options. |
| 81 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 82 | + * @returns useMutation hooks |
| 83 | + */ |
| 84 | +export function putMutation<T, R = any>( |
| 85 | + model: string, |
| 86 | + url: string, |
| 87 | + options?: Omit<UseMutationOptions<R, unknown, T>, 'mutationFn'>, |
| 88 | + invalidateQueries = true |
| 89 | +) { |
| 90 | + const queryClient = useQueryClient(); |
| 91 | + const mutationFn = (data: any) => |
| 92 | + fetcher<R>(url, { |
| 93 | + method: 'PUT', |
| 94 | + headers: { |
| 95 | + 'content-type': 'application/json', |
| 96 | + }, |
| 97 | + body: marshal(data), |
| 98 | + }); |
| 99 | + |
| 100 | + const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient); |
| 101 | + const mutation = useMutation<R, unknown, T>(finalOptions); |
| 102 | + return mutation; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Creates a DELETE mutation with react-query. |
| 107 | + * |
| 108 | + * @param model The name of the model under mutation. |
| 109 | + * @param url The request URL. |
| 110 | + * @param options The react-query options. |
| 111 | + * @param invalidateQueries Whether to invalidate queries after mutation. |
| 112 | + * @returns useMutation hooks |
| 113 | + */ |
| 114 | +export function deleteMutation<T, R = any>( |
| 115 | + model: string, |
| 116 | + url: string, |
| 117 | + options?: Omit<UseMutationOptions<R, unknown, T>, 'mutationFn'>, |
| 118 | + invalidateQueries = true |
| 119 | +) { |
| 120 | + const queryClient = useQueryClient(); |
| 121 | + const mutationFn = (data: any) => |
| 122 | + fetcher<R>(makeUrl(url, data), { |
| 123 | + method: 'DELETE', |
| 124 | + }); |
| 125 | + |
| 126 | + const finalOptions = mergeOptions<T, R>(model, options, invalidateQueries, mutationFn, queryClient); |
| 127 | + const mutation = useMutation<R, unknown, T>(finalOptions); |
| 128 | + return mutation; |
| 129 | +} |
| 130 | + |
| 131 | +function mergeOptions<T, R = any>( |
| 132 | + model: string, |
| 133 | + options: Omit<UseMutationOptions<R, unknown, T, unknown>, 'mutationFn'> | undefined, |
| 134 | + invalidateQueries: boolean, |
| 135 | + mutationFn: MutateFunction<R, unknown, T>, |
| 136 | + queryClient: QueryClient |
| 137 | +): UseMutationOptions<R, unknown, T, unknown> { |
| 138 | + const result = { ...options, mutationFn }; |
| 139 | + if (options?.onSuccess || invalidateQueries) { |
| 140 | + result.onSuccess = (...args) => { |
| 141 | + if (invalidateQueries) { |
| 142 | + queryClient.invalidateQueries([QUERY_KEY_PREFIX + model]); |
| 143 | + } |
| 144 | + return options?.onSuccess?.(...args); |
| 145 | + }; |
| 146 | + } |
| 147 | + return result; |
| 148 | +} |
0 commit comments