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 { PluginSwr } from '../types' export const mutationGenerator = createReactGenerator({ name: 'swr-mutation', 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 = options.mutation !== false && !isQuery && difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method) const importPath = options.mutation ? options.mutation.importPath : 'swr' const mutation = { name: getName(operation, { type: 'function', prefix: 'use' }), typeName: getName(operation, { type: 'type' }), file: getFile(operation, { prefix: 'use' }), } 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]) // Class-based clients are not compatible with query hooks, so we generate inline clients const shouldUseClientPlugin = hasClientPlugin && options.client.clientType !== 'class' const client = { name: shouldUseClientPlugin ? 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 ? ( <> {!shouldUseClientPlugin && } {options.client.dataReturnType === 'full' && } ) : ( <> {!shouldUseClientPlugin && ( )} {options.client.dataReturnType === 'full' && ( )} )} {shouldUseClientPlugin && } {!shouldUseClientPlugin && ( )} item.name) || []), ].filter(Boolean)} root={mutation.file.path} path={type.file.path} isTypeOnly /> {!shouldUseClientPlugin && ( )} {options.mutation && ( )} ) }, })