UNPKG

query-with-axios

Version:

A utility package for making API requests with Axios and managing queries and mutations using TanStack Query.

31 lines (26 loc) 1.51 kB
import { Ref } from 'vue'; import { UseQueryOptions, UseQueryReturnType, UseMutationOptions } from '@tanstack/vue-query'; import { AxiosError } from 'axios'; type IAxiosRoute<T extends Record<string, any>> = { [K in keyof T]: T[K]; }; declare namespace RouteDefinitions { interface Routes { } } type RoutesType = RouteDefinitions.Routes; declare class InitAxiosRoute { private static axiosRoute; static createAxiosRoute<T extends Record<string, any>>(axiosRouteInit: IAxiosRoute<T>): void; static getApiRoute: () => RoutesType; } type IPayload<T> = T extends (payload: infer P) => Promise<unknown> ? P : never; type IReturn<T> = T extends (payload: never) => Promise<infer R> ? R : never; declare const useQueryWithAxios: <T extends keyof RoutesType, R extends keyof RoutesType[T]>(route: T, routeMethod: R, payload?: Ref<IPayload<RoutesType[T][R]>>, options?: Omit<UseQueryOptions<IReturn<RoutesType[T][R]>, AxiosError>, "queryKey">) => UseQueryReturnType<IReturn<RoutesType[T][R]>, AxiosError>; declare const useMutationWithAxios: <T extends keyof RoutesType, R extends keyof RoutesType[T]>(route: T, routeMethod: R, options?: UseMutationOptions<IReturn<RoutesType[T][R]>, AxiosError, IPayload<RoutesType[T][R]>>) => any; interface IAxiosData<T> { data: T; message: string; } type IResponse<T> = Promise<IAxiosData<T>>; export { type IAxiosData, type IAxiosRoute, type IResponse, InitAxiosRoute, RouteDefinitions, type RoutesType, useMutationWithAxios, useQueryWithAxios };