@kubb/plugin-react-query
Version:
React Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for React applications.
86 lines (76 loc) • 3.04 kB
text/typescript
/**
* Generated by Kubb (https://kubb.dev/).
* Do not edit manually.
*/
import fetch from 'axios'
import type { QueryKey, QueryClient, QueryObserverOptions, UseQueryResult } from '@tanstack/react-query'
import type { RequestConfig, ResponseErrorConfig } from 'axios'
import { queryOptions, useQuery } from '@tanstack/react-query'
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: '/pet/findByTags' }, ...(params ? [params] : [])] as const
export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>
/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
* {@link /pet/findByTags}
*/
export async function findPetsByTags(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
config: Partial<RequestConfig> & { client?: typeof fetch } = {},
) {
const { client: request = fetch, ...requestConfig } = config
const res = await request<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, unknown>({
method: 'GET',
url: `/pet/findByTags`,
params,
...requestConfig,
headers: { ...headers, ...requestConfig.headers },
})
return findPetsByTagsQueryResponse.parse(res.data)
}
export function findPetsByTagsQueryOptions(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
config: Partial<RequestConfig> & { client?: typeof fetch } = {},
) {
const queryKey = findPetsByTagsQueryKey(params)
return queryOptions<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, FindPetsByTagsQueryResponse, typeof queryKey>({
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return findPetsByTags(headers, params, config)
},
})
}
/**
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @summary Finds Pets by tags
* {@link /pet/findByTags}
*/
export function useFindPetsByTags<
TData = FindPetsByTagsQueryResponse,
TQueryData = FindPetsByTagsQueryResponse,
TQueryKey extends QueryKey = FindPetsByTagsQueryKey,
>(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
query?: Partial<QueryObserverOptions<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, TData, TQueryData, TQueryKey>> & {
client?: QueryClient
}
client?: Partial<RequestConfig> & { client?: typeof fetch }
} = {},
) {
const { query: { client: queryClient, ...queryOptions } = {}, client: config = {} } = options ?? {}
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params)
const query = useQuery(
{
...findPetsByTagsQueryOptions(headers, params, config),
queryKey,
...queryOptions,
} as unknown as QueryObserverOptions,
queryClient,
) as UseQueryResult<TData, ResponseErrorConfig<FindPetsByTags400>> & { queryKey: TQueryKey }
query.queryKey = queryKey as TQueryKey
return query
}