import { isOptional } from '@kubb/oas' import { Client } from '@kubb/plugin-client/components' import type { OperationSchemas } from '@kubb/plugin-oas' import { 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' type Props = { name: string clientName: string queryKeyName: string typeSchemas: OperationSchemas 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'] typeSchemas: OperationSchemas } function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) { 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, }, }, config: { type: 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, config: { type: typeSchemas.request?.name ? `Partial> & { client?: typeof fetch }` : 'Partial & { client?: typeof fetch }', default: '{}', }, }) } export function QueryOptions({ name, clientName, typeSchemas, paramsCasing, paramsType, dataReturnType, pathParamsType, queryKeyName }: 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 params = getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }) const clientParams = Client.getParams({ paramsType, paramsCasing, typeSchemas, pathParamsType, isConfigurable: true, }) const queryKeyParams = QueryKey.getParams({ pathParamsType, paramsCasing, typeSchemas, }) const enabled = Object.entries(queryKeyParams.flatParams) .map(([key, item]) => (item && !item.optional ? key : undefined)) .filter(Boolean) .join('&& ') const enabledText = enabled ? `enabled: !!(${enabled}),` : '' return ( {` const queryKey = ${queryKeyName}(${queryKeyParams.toCall()}) return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({ ${enabledText} queryKey, queryFn: async ({ signal }) => { config.signal = signal return ${clientName}(${clientParams.toCall()}) }, }) `} ) } QueryOptions.getParams = getParams