UNPKG

@kubb/plugin-react-query

Version:

React Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for React applications.

72 lines (62 loc) 2.71 kB
/** * Generated by Kubb (https://kubb.dev/). * Do not edit manually. */ import fetch from '@kubb/plugin-client/clients/axios' import type { RequestConfig, ResponseErrorConfig } from '@kubb/plugin-client/clients/axios' import type { QueryKey, QueryClient, QueryObserverOptions, UseQueryResult } from '@tanstack/react-query' import { queryOptions, useQuery } from '@tanstack/react-query' export const getPetByIdQueryKey = (petId: GetPetByIdPathParams['pet_id']) => [{ url: '/pet/:pet_id', params: { petId: petId } }] as const export type GetPetByIdQueryKey = ReturnType<typeof getPetByIdQueryKey> /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:pet_id} */ export async function getPetById(petId: GetPetByIdPathParams['pet_id'], config: Partial<RequestConfig> & { client?: typeof fetch } = {}) { const { client: request = fetch, ...requestConfig } = config const res = await request<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, unknown>({ method: 'GET', url: `/pet/${petId}`, ...requestConfig, }) return getPetByIdQueryResponse.parse(res.data) } export function getPetByIdQueryOptions(petId: GetPetByIdPathParams['pet_id'], config: Partial<RequestConfig> & { client?: typeof fetch } = {}) { const queryKey = getPetByIdQueryKey(petId) return queryOptions<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, GetPetByIdQueryResponse, typeof queryKey>({ enabled: !!petId, queryKey, queryFn: async ({ signal }) => { config.signal = signal return getPetById(petId, config) }, }) } /** * @description Returns a single pet * @summary Find pet by ID * {@link /pet/:pet_id} */ export function useGetPetById<TData = GetPetByIdQueryResponse, TQueryData = GetPetByIdQueryResponse, TQueryKey extends QueryKey = GetPetByIdQueryKey>( petId: GetPetByIdPathParams['pet_id'], options: { query?: Partial<QueryObserverOptions<GetPetByIdQueryResponse, ResponseErrorConfig<GetPetById400 | GetPetById404>, TData, TQueryData, TQueryKey>> & { client?: QueryClient } client?: Partial<RequestConfig> & { client?: typeof fetch } } = {}, ) { const { query: { client: queryClient, ...queryOptions } = {}, client: config = {} } = options ?? {} const queryKey = queryOptions?.queryKey ?? getPetByIdQueryKey(petId) const query = useQuery( { ...getPetByIdQueryOptions(petId, config), queryKey, ...queryOptions, } as unknown as QueryObserverOptions, queryClient, ) as UseQueryResult<TData, ResponseErrorConfig<GetPetById400 | GetPetById404>> & { queryKey: TQueryKey } query.queryKey = queryKey as TQueryKey return query }