|
| 1 | +import { type MaybeRef, unref } from "vue"; |
| 2 | +import { |
| 3 | + type UseMutationOptions, |
| 4 | + type UseMutationReturn, |
| 5 | + type UseQueryOptions, |
| 6 | + type UseQueryReturn, |
| 7 | + useMutation as piniaColadaUseMutation, |
| 8 | + useQuery as piniaColadaUseQuery, |
| 9 | +} from "@pinia/colada"; |
| 10 | +import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch"; |
| 11 | +import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers"; |
| 12 | + |
| 13 | +type InitWithUnknowns<Init> = Init & { [key: string]: unknown }; |
| 14 | + |
| 15 | +export const useQuery = < |
| 16 | + Method extends HttpMethod, |
| 17 | + Paths extends Record<string, Record<HttpMethod, unknown>>, |
| 18 | + Media extends MediaType, |
| 19 | + Path extends PathsWithMethod<Paths, Method>, |
| 20 | + Init extends MaybeOptionalInit<Paths[Path], Method>, |
| 21 | + Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, |
| 22 | + Options extends Omit<UseQueryOptions<Response["data"], Response["error"], Response["data"]>, "key" | "query">, |
| 23 | +>( |
| 24 | + fetchClient: MaybeRef<FetchClient>, |
| 25 | + method: MaybeRef<Method>, |
| 26 | + url: MaybeRef<PathsWithMethod<Paths, Method>>, |
| 27 | + ...[init, options]: RequiredKeysOf<Init> extends never |
| 28 | + ? [MaybeRef<InitWithUnknowns<Init>>?, MaybeRef<Options>?] |
| 29 | + : [MaybeRef<InitWithUnknowns<Init>>, MaybeRef<Options>?] |
| 30 | +): UseQueryReturn<Response["data"], Response["error"], Init> => { |
| 31 | + const fetchClientValue = unref(fetchClient); |
| 32 | + const methodValue = unref(method); |
| 33 | + const urlValue = unref(url); |
| 34 | + const initValue = unref(init); |
| 35 | + const optionsValue = unref(options); |
| 36 | + const mth = methodValue.toUpperCase() as Uppercase<typeof methodValue>; |
| 37 | + const fn = fetchClientValue[mth] as ClientMethod<Paths, typeof method, Media>; |
| 38 | + |
| 39 | + return piniaColadaUseQuery({ |
| 40 | + key: [methodValue, urlValue as string, initValue as InitWithUnknowns<typeof initValue>], |
| 41 | + query: () => fn(urlValue, optionsValue), |
| 42 | + ...optionsValue, |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +export const useMutation = < |
| 47 | + Method extends HttpMethod, |
| 48 | + Paths extends Record<string, Record<HttpMethod, unknown>>, |
| 49 | + Media extends MediaType, |
| 50 | + Path extends PathsWithMethod<Paths, Method>, |
| 51 | + Init extends MaybeOptionalInit<Paths[Path], Method>, |
| 52 | + Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types |
| 53 | + Options extends Omit<UseMutationOptions<Response["data"], Response["error"], Init>, "key" | "mutation">, |
| 54 | + Vars, |
| 55 | +>( |
| 56 | + fetchClient: MaybeRef<FetchClient>, |
| 57 | + method: MaybeRef<Method>, |
| 58 | + url: MaybeRef<PathsWithMethod<Paths, Method>>, |
| 59 | + options?: Options, |
| 60 | +): UseMutationReturn<Response["data"], Vars, Response["error"]> => { |
| 61 | + const fetchClientValue = unref(fetchClient); |
| 62 | + const methodValue = unref(method); |
| 63 | + const urlValue = unref(url); |
| 64 | + const optionsValue = unref(options); |
| 65 | + const mth = methodValue.toUpperCase() as Uppercase<typeof methodValue>; |
| 66 | + const fn = fetchClientValue[mth] as ClientMethod<Paths, typeof method, Media>; |
| 67 | + |
| 68 | + return piniaColadaUseMutation({ mutation: () => fn(urlValue, optionsValue), ...optionsValue }); |
| 69 | +}; |
0 commit comments