import path from 'node:path' import { usePluginManager } from '@kubb/core/hooks' import { pluginClientName } from '@kubb/plugin-client' import { Client } from '@kubb/plugin-client/components' import { createReactGenerator } from '@kubb/plugin-oas/generators' import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks' import { getBanner, getFooter } from '@kubb/plugin-oas/utils' import { pluginTsName } from '@kubb/plugin-ts' import { pluginZodName } from '@kubb/plugin-zod' import { File } from '@kubb/react-fabric' import { difference } from 'remeda' import { Mutation, MutationKey } from '../components' import type { PluginSvelteQuery } from '../types' export const mutationGenerator = createReactGenerator({ name: 'svelte-query', Operation({ config, operation, generator, plugin }) { const { options, options: { output }, } = plugin const pluginManager = usePluginManager() const oas = useOas() const { getSchemas, getName, getFile } = useOperationManager(generator) const isQuery = !!options.query && options.query?.methods.some((method) => operation.method === method) const isMutation = !isQuery && difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method) const importPath = options.mutation ? options.mutation.importPath : '@tanstack/svelte-query' const mutation = { name: getName(operation, { type: 'function', prefix: 'create' }), typeName: getName(operation, { type: 'type' }), file: getFile(operation, { prefix: 'create' }), } const type = { file: getFile(operation, { pluginKey: [pluginTsName] }), //todo remove type? schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }), } const zod = { file: getFile(operation, { pluginKey: [pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }), } const hasClientPlugin = !!pluginManager.getPluginByKey([pluginClientName]) const client = { name: hasClientPlugin ? getName(operation, { type: 'function', pluginKey: [pluginClientName], }) : getName(operation, { type: 'function', }), file: getFile(operation, { pluginKey: [pluginClientName] }), } const mutationKey = { name: getName(operation, { type: 'const', suffix: 'MutationKey' }), typeName: getName(operation, { type: 'type', suffix: 'MutationKey' }), } if (!isMutation) { return null } return ( {options.parser === 'zod' && ( )} {options.client.importPath ? ( <> {options.client.dataReturnType === 'full' && } ) : ( <> {options.client.dataReturnType === 'full' && ( )} )} {!!hasClientPlugin && } item.name) || []), ].filter(Boolean)} root={mutation.file.path} path={type.file.path} isTypeOnly /> {!hasClientPlugin && ( )} {options.mutation && ( <> )} ) }, })