import { isOptional, type Operation } from '@kubb/oas' import type { OperationSchemas } from '@kubb/plugin-oas' import { getComments, getPathParams } from '@kubb/plugin-oas/utils' import { File, Function, FunctionParams } from '@kubb/react-fabric' import type { KubbNode } from '@kubb/react-fabric/types' import type { PluginSvelteQuery } from '../types.ts' import { QueryKey } from './QueryKey.tsx' import { QueryOptions } from './QueryOptions.tsx' type Props = { /** * Name of the function */ name: string queryOptionsName: string queryKeyName: string queryKeyTypeName: string typeSchemas: OperationSchemas operation: Operation paramsCasing: PluginSvelteQuery['resolvedOptions']['paramsCasing'] paramsType: PluginSvelteQuery['resolvedOptions']['paramsType'] pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType'] dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType'] } type GetParamsProps = { paramsCasing: PluginSvelteQuery['resolvedOptions']['paramsCasing'] paramsType: PluginSvelteQuery['resolvedOptions']['paramsType'] pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType'] dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType'] typeSchemas: OperationSchemas } function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) { const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>` if (paramsType === 'object') { return FunctionParams.factory({ data: { mode: 'object', children: { ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema), } : undefined, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema), } : undefined, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema), } : undefined, }, }, options: { type: ` { query?: Partial> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial> & { client?: typeof fetch }` : 'Partial & { client?: typeof fetch }'} } `, default: '{}', }, }) } return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? { mode: pathParamsType === 'object' ? 'object' : 'inlineSpread', children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }), optional: isOptional(typeSchemas.pathParams?.schema), } : undefined, data: typeSchemas.request?.name ? { type: typeSchemas.request?.name, optional: isOptional(typeSchemas.request?.schema), } : undefined, params: typeSchemas.queryParams?.name ? { type: typeSchemas.queryParams?.name, optional: isOptional(typeSchemas.queryParams?.schema), } : undefined, headers: typeSchemas.headerParams?.name ? { type: typeSchemas.headerParams?.name, optional: isOptional(typeSchemas.headerParams?.schema), } : undefined, options: { type: ` { query?: Partial> & { client?: QueryClient }, client?: ${typeSchemas.request?.name ? `Partial> & { client?: typeof fetch }` : 'Partial & { client?: typeof fetch }'} } `, default: '{}', }, }) } export function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, operation, }: Props): KubbNode { const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>` const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>` const returnType = `CreateQueryResult<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }` const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`] const queryKeyParams = QueryKey.getParams({ pathParamsType, paramsCasing, typeSchemas, }) const queryOptionsParams = QueryOptions.getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas, }) const params = getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas, }) const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as CreateBaseQueryOptions` return ( {` const { query: queryConfig = {}, client: config = {} } = options ?? {} const { client: queryClient, ...queryOptions } = queryConfig const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()}) const query = createQuery(() => ({ ...${queryOptions}, queryKey, ...queryOptions as unknown as Omit }), queryClient? () => queryClient: undefined) as ${returnType} query.queryKey = queryKey as TQueryKey return query `} ) } Query.getParams = getParams