@kubb/plugin-vue-query
Version:
Vue Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for Vue.js applications.
89 lines (79 loc) • 3.24 kB
text/typescript
/**
* Generated by Kubb (https://kubb.dev/).
* Do not edit manually.
*/
import fetch from 'axios'
import type { QueryKey, QueryClient, QueryObserverOptions, UseQueryReturnType } from '@tanstack/react-query'
import type { RequestConfig, ResponseErrorConfig } from 'axios'
import type { MaybeRefOrGetter } from 'vue'
import { queryOptions, useQuery } from '@tanstack/react-query'
import { toValue } from 'vue'
export const findPetsByTagsQueryKey = (params?: MaybeRefOrGetter<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: MaybeRefOrGetter<FindPetsByTagsQueryParams>,
params?: MaybeRefOrGetter<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(toValue(headers), toValue(params), toValue(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: MaybeRefOrGetter<FindPetsByTagsHeaderParams>,
params?: MaybeRefOrGetter<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 UseQueryReturnType<TData, ResponseErrorConfig<FindPetsByTags400>> & { queryKey: TQueryKey }
query.queryKey = queryKey as TQueryKey
return query
}