|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
2 |
| -import { MutationKey, useMutation, UseMutationOptions, UseMutationResult, useQueryClient } from "react-query"; |
| 2 | +import { |
| 3 | + MutationKey, |
| 4 | + useMutation, |
| 5 | + UseMutationOptions, |
| 6 | + UseMutationResult, |
| 7 | + useQueryClient, |
| 8 | +} from "react-query"; |
3 | 9 | import { DeepPartial } from "src/helpers/typeUtils";
|
4 | 10 | import { AppQueryOptions } from ".";
|
5 |
| -import { AppRoutes, ApiPayloadType, ApiResponseType, ApiRoute } from ".."; |
| 11 | +import { ApiPayloadType, ApiResponseType, ApiRoute, AppRoutes } from ".."; |
6 | 12 | import { httpPost } from "../imperative";
|
7 |
| -import { appQueryKeyBuilder } from "./appQueryKeyBuilder"; |
| 13 | +import { useQueryKeyBuilder } from "./useQueryKeyBuilder"; |
8 | 14 | /**
|
9 |
| - * |
| 15 | + * |
10 | 16 | * @param routeOrRouteObj a route from AppRoutes or an object with a scope and a route
|
11 |
| -* @param appQueryOptions http options for the query. Path params is needed to replace path variables |
| 17 | + * @param appQueryOptions http options for the query. Path params is needed to replace path variables |
12 | 18 | * @param mutationOptions react-query's mutation options
|
13 |
| - * @returns |
| 19 | + * @returns |
14 | 20 | */
|
15 |
| -export function useAppMutation<Scope extends keyof AppRoutes = "main", Route extends ApiRoute<Scope> = ApiRoute<Scope>> |
16 |
| - (routeOrRouteObj: Route | { scope: Scope, route: Route }, appQueryOptions: Partial<Omit<AppQueryOptions<ApiPayloadType<Scope, Route>>, "apiScope">> = {}, |
17 |
| - mutationOptions: UseMutationOptions = {} |
18 |
| - ): UseMutationResult<ApiResponseType<Scope, Route, "mutation">, any, DeepPartial<(ApiPayloadType<Scope, Route> & { _pathParams?: { [key: string]: any } })>, any> { |
19 |
| - |
20 |
| - const queryClient = useQueryClient(); |
21 |
| - const keyForUseQuery = appQueryKeyBuilder(routeOrRouteObj, appQueryOptions);// any = [route, typeof queryOptions.query === "string" ? queryOptions.query : { ...queryOptions.query }]; |
22 |
| - |
23 |
| - |
24 |
| - /** |
25 |
| - * _pathParams is used to replace the url path variables. |
26 |
| - * It has been put here because by default react query allows you to add extra data to mutateFn only when you call useMutation |
27 |
| - * and not when you call mutate or mutateAsync. |
28 |
| - * Sometimes may be tricky to have pathParams at the moment you call useMutation, so this has been added here for conveniece. |
29 |
| - * It has been prefixed with underscore here (and no in useAppMutation) to allow the case in which the final user needs to send a property called pathParams |
30 |
| - * in the request payload. |
31 |
| - * If you need to send a post request with a _pathParams property, it will not work |
32 |
| - */ |
33 |
| - return useMutation<ApiResponseType<Scope, Route, "mutation">, any, DeepPartial<(ApiPayloadType<Scope, Route> & { _pathParams?: { [key: string]: any } })>, any>(keyForUseQuery as MutationKey, ({ _pathParams, ...params }: any) => { |
34 |
| - console.log("options", appQueryOptions) |
35 |
| - console.log("params", params, _pathParams) |
36 |
| - // const finalRoute = (routeOrRouteObj as string).split("/") |
37 |
| - // .map((part) => { |
38 |
| - // if (part.startsWith(":")) { |
39 |
| - // const finalPart = part.substring(1); |
40 |
| - // const pathParam = _pathParams?.[finalPart] ?? appQueryOptions.pathParams?.[finalPart]; |
41 |
| - // if (!pathParam) { |
42 |
| - // console.warn("you are missing a path param for route", routeOrRouteObj) |
43 |
| - // return undefined; |
44 |
| - // } |
45 |
| - // return pathParam |
46 |
| - // } |
47 |
| - // return part; |
48 |
| - // }) |
49 |
| - // .join("/"); |
50 |
| - // const { pathParams, ...restOfAppQueryOptions } = appQueryOptions; |
51 |
| - return httpPost(routeOrRouteObj as any, { payload: params, ...appQueryOptions, pathParams: _pathParams ?? appQueryOptions.pathParams }) as Promise<ApiResponseType<Scope, Route, "mutation">> |
52 |
| - |
53 |
| - }, { |
54 |
| - // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
55 |
| - //@ts-ignore |
56 |
| - onSuccess: (data: any, variables: any, context: any) => { |
57 |
| - queryClient.invalidateQueries({ queryKey: routeOrRouteObj as any }); |
58 |
| - mutationOptions.onSuccess?.(data, variables, context) |
59 |
| - // return true; |
60 |
| - }, |
61 |
| - ...mutationOptions, |
62 |
| - }); |
63 |
| - |
| 21 | +export function useAppMutation< |
| 22 | + Scope extends keyof AppRoutes = "main", |
| 23 | + Route extends ApiRoute<Scope> = ApiRoute<Scope> |
| 24 | +>( |
| 25 | + routeOrRouteObj: Route | { scope: Scope; route: Route }, |
| 26 | + appQueryOptions: Partial< |
| 27 | + Omit<AppQueryOptions<ApiPayloadType<Scope, Route>>, "apiScope"> |
| 28 | + > = {}, |
| 29 | + mutationOptions: UseMutationOptions = {} |
| 30 | +): UseMutationResult< |
| 31 | + ApiResponseType<Scope, Route, "mutation">, |
| 32 | + any, |
| 33 | + DeepPartial< |
| 34 | + ApiPayloadType<Scope, Route> & { _pathParams?: { [key: string]: any } } |
| 35 | + >, |
| 36 | + any |
| 37 | +> { |
| 38 | + const queryClient = useQueryClient(); |
| 39 | + const keyForUseQuery = useQueryKeyBuilder(routeOrRouteObj, appQueryOptions); // any = [route, typeof queryOptions.query === "string" ? queryOptions.query : { ...queryOptions.query }]; |
64 | 40 |
|
| 41 | + /** |
| 42 | + * _pathParams is used to replace the url path variables. |
| 43 | + * It has been put here because by default react query allows you to add extra data to mutateFn only when you call useMutation |
| 44 | + * and not when you call mutate or mutateAsync. |
| 45 | + * Sometimes may be tricky to have pathParams at the moment you call useMutation, so this has been added here for conveniece. |
| 46 | + * It has been prefixed with underscore here (and no in useAppMutation) to allow the case in which the final user needs to send a property called pathParams |
| 47 | + * in the request payload. |
| 48 | + * If you need to send a post request with a _pathParams property, it will not work |
| 49 | + */ |
| 50 | + return useMutation< |
| 51 | + ApiResponseType<Scope, Route, "mutation">, |
| 52 | + any, |
| 53 | + DeepPartial< |
| 54 | + ApiPayloadType<Scope, Route> & { _pathParams?: { [key: string]: any } } |
| 55 | + >, |
| 56 | + any |
| 57 | + >( |
| 58 | + keyForUseQuery as MutationKey, |
| 59 | + ({ _pathParams, ...params }: any) => { |
| 60 | + console.log("options", appQueryOptions); |
| 61 | + console.log("params", params, _pathParams); |
| 62 | + // const finalRoute = (routeOrRouteObj as string).split("/") |
| 63 | + // .map((part) => { |
| 64 | + // if (part.startsWith(":")) { |
| 65 | + // const finalPart = part.substring(1); |
| 66 | + // const pathParam = _pathParams?.[finalPart] ?? appQueryOptions.pathParams?.[finalPart]; |
| 67 | + // if (!pathParam) { |
| 68 | + // console.warn("you are missing a path param for route", routeOrRouteObj) |
| 69 | + // return undefined; |
| 70 | + // } |
| 71 | + // return pathParam |
| 72 | + // } |
| 73 | + // return part; |
| 74 | + // }) |
| 75 | + // .join("/"); |
| 76 | + // const { pathParams, ...restOfAppQueryOptions } = appQueryOptions; |
| 77 | + return httpPost(routeOrRouteObj as any, { |
| 78 | + payload: params, |
| 79 | + ...appQueryOptions, |
| 80 | + pathParams: _pathParams ?? appQueryOptions.pathParams, |
| 81 | + }) as Promise<ApiResponseType<Scope, Route, "mutation">>; |
| 82 | + }, |
| 83 | + { |
| 84 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 85 | + //@ts-ignore |
| 86 | + onSuccess: (data: any, variables: any, context: any) => { |
| 87 | + queryClient.invalidateQueries({ queryKey: routeOrRouteObj as any }); |
| 88 | + mutationOptions.onSuccess?.(data, variables, context); |
| 89 | + // return true; |
| 90 | + }, |
| 91 | + ...mutationOptions, |
| 92 | + } |
| 93 | + ); |
65 | 94 | }
|
66 |
| - |
|
0 commit comments