|
| 1 | +import type { |
| 2 | + DefaultError, |
| 3 | + UseMutationOptions, |
| 4 | + UseMutationResult, |
| 5 | + UseQueryOptions, |
| 6 | + UseQueryResult, |
| 7 | +} from '@tanstack/react-query' |
| 8 | +import { useMutation, useQuery } from '@tanstack/react-query' |
| 9 | +import type { ValueOf } from 'type-fest' |
| 10 | + |
| 11 | +import type { ApiRouter } from '@kivotos/core' |
| 12 | +import { |
| 13 | + type FilterByMethod, |
| 14 | + type RestClient, |
| 15 | + type RestPayload, |
| 16 | + type RestResponse, |
| 17 | +} from '@kivotos/rest' |
| 18 | + |
| 19 | +export interface KivotosQueryClient<TApiRouter extends ApiRouter> { |
| 20 | + useQuery: < |
| 21 | + const TMethod extends keyof RestClient<TApiRouter>, |
| 22 | + const TPath extends ValueOf<FilterByMethod<TApiRouter, TMethod>>['schema']['path'], |
| 23 | + const TPayload = RestPayload<TApiRouter, TPath>, |
| 24 | + const TResponse = RestResponse<TApiRouter, TPath>, |
| 25 | + const TError = DefaultError, |
| 26 | + >( |
| 27 | + method: TMethod, |
| 28 | + path: TPath, |
| 29 | + payload: TPayload, |
| 30 | + options?: Omit<UseQueryOptions<TResponse, TError, TPayload>, 'queryKey'> |
| 31 | + ) => UseQueryResult<TResponse, TError> |
| 32 | + useMutation: < |
| 33 | + const TMethod extends keyof RestClient<TApiRouter>, |
| 34 | + const TPath extends ValueOf<FilterByMethod<TApiRouter, TMethod>>['schema']['path'], |
| 35 | + const TPayload = RestPayload<TApiRouter, TPath>, |
| 36 | + const TResponse = RestResponse<TApiRouter, TPath>, |
| 37 | + const TError = DefaultError, |
| 38 | + const TContext = unknown, |
| 39 | + >( |
| 40 | + method: TMethod, |
| 41 | + path: TPath, |
| 42 | + payload: TPayload, |
| 43 | + options?: UseMutationOptions<TResponse, TError, TPayload, TContext> |
| 44 | + ) => UseMutationResult<TResponse, TError, TPayload, TContext> |
| 45 | + queryOptions: < |
| 46 | + const TMethod extends keyof RestClient<TApiRouter>, |
| 47 | + const TPath extends ValueOf<FilterByMethod<TApiRouter, TMethod>>['schema']['path'], |
| 48 | + const TPayload = RestPayload<TApiRouter, TPath>, |
| 49 | + const TResponse = RestResponse<TApiRouter, TPath>, |
| 50 | + const TError = DefaultError, |
| 51 | + >( |
| 52 | + method: TMethod, |
| 53 | + path: TPath, |
| 54 | + payload: TPayload, |
| 55 | + options?: Omit<UseQueryOptions<TResponse, TError, TPayload>, 'queryKey'> |
| 56 | + ) => UseQueryOptions<TResponse, TError, TPayload> |
| 57 | + mutationOptions: < |
| 58 | + const TMethod extends keyof RestClient<TApiRouter>, |
| 59 | + const TPath extends ValueOf<FilterByMethod<TApiRouter, TMethod>>['schema']['path'], |
| 60 | + const TPayload = RestPayload<TApiRouter, TPath>, |
| 61 | + const TResponse = RestResponse<TApiRouter, TPath>, |
| 62 | + const TError = DefaultError, |
| 63 | + const TContext = unknown, |
| 64 | + >( |
| 65 | + method: TMethod, |
| 66 | + path: TPath, |
| 67 | + payload: TPayload, |
| 68 | + options?: UseMutationOptions<TResponse, TError, TPayload, TContext> |
| 69 | + ) => UseMutationOptions<TResponse, TError, TPayload, TContext> |
| 70 | +} |
| 71 | + |
| 72 | +export function createKivotosQueryClient<TApiRouter extends ApiRouter<any>>( |
| 73 | + restClient: RestClient<TApiRouter> |
| 74 | +): KivotosQueryClient<TApiRouter> { |
| 75 | + return { |
| 76 | + useQuery: function (method: string, path: string, payload: any, options?: any) { |
| 77 | + return useQuery({ |
| 78 | + queryKey: queryKey(method, path, payload), |
| 79 | + queryFn: () => { |
| 80 | + return (restClient as any)[method](path, payload) |
| 81 | + }, |
| 82 | + ...options, |
| 83 | + }) as UseQueryResult<any, any> |
| 84 | + }, |
| 85 | + useMutation: function (method: string, path: string, payload: any, options?: any) { |
| 86 | + return useMutation({ |
| 87 | + mutationKey: queryKey(method, path, payload), |
| 88 | + mutationFn: (data) => { |
| 89 | + return (restClient as any)[method](path, data) |
| 90 | + }, |
| 91 | + ...options, |
| 92 | + }) as UseMutationResult<any, any, any, any> |
| 93 | + }, |
| 94 | + queryOptions: function (method: string, path: string, payload: any, options?: any) { |
| 95 | + return { |
| 96 | + queryKey: queryKey(method, path, payload), |
| 97 | + queryFn: () => { |
| 98 | + return (restClient as any)[method](path, payload) |
| 99 | + }, |
| 100 | + ...options, |
| 101 | + } as UseQueryOptions<any, any, any> |
| 102 | + }, |
| 103 | + mutationOptions: function (method: string, path: string, payload: any, options?: any) { |
| 104 | + return { |
| 105 | + mutationKey: queryKey(method, path, payload), |
| 106 | + mutationFn: (data) => { |
| 107 | + return (restClient as any)[method](path, data) |
| 108 | + }, |
| 109 | + ...options, |
| 110 | + } as UseMutationOptions<any, any, any, any> |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export function queryKey(method: string, path: string | number | symbol, payload: any) { |
| 116 | + const payloadKey = { |
| 117 | + pathParams: payload?.pathParams ?? {}, |
| 118 | + query: payload?.query ?? {}, |
| 119 | + headers: payload?.headers ?? {}, |
| 120 | + } |
| 121 | + return [method, path, payloadKey] as const |
| 122 | +} |
0 commit comments