{"version":3,"file":"index.cjs","names":["MutationKey","QueryKey","queryGenerator","mutationGenerator","pluginOasName","pluginTsName","pluginZodName","path","camelCase","pascalCase","pluginClientName","fetchClientSource","axiosClientSource","configSource","OperationGenerator"],"sources":["../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { camelCase, pascalCase } from '@internals/utils'\nimport { definePlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { pluginClientName } from '@kubb/plugin-client'\nimport { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'\nimport { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'\nimport { source as configSource } from '@kubb/plugin-client/templates/config.source'\nimport { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { MutationKey, QueryKey } from './components'\nimport { mutationGenerator, queryGenerator } from './generators'\nimport type { PluginSwr } from './types.ts'\n\nexport const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']\n\nexport const pluginSwr = definePlugin<PluginSwr>((options) => {\n  const {\n    output = { path: 'hooks', barrelType: 'named' },\n    group,\n    exclude = [],\n    include,\n    override = [],\n    parser = 'client',\n    transformers = {},\n    query,\n    mutation,\n    client,\n    paramsType = 'inline',\n    pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n    mutationKey = MutationKey.getTransformer,\n    queryKey = QueryKey.getTransformer,\n    generators = [queryGenerator, mutationGenerator].filter(Boolean),\n    paramsCasing,\n    contentType,\n  } = options\n\n  const clientName = client?.client ?? 'axios'\n  const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)\n\n  return {\n    name: pluginSwrName,\n    options: {\n      output,\n      client: {\n        bundle: client?.bundle,\n        baseURL: client?.baseURL,\n        client: clientName,\n        clientType: client?.clientType ?? 'function',\n        importPath: clientImportPath,\n        dataReturnType: client?.dataReturnType ?? 'data',\n        paramsCasing,\n      },\n      queryKey,\n      query:\n        query === false\n          ? false\n          : {\n              importPath: 'swr',\n              methods: ['get'],\n              ...query,\n            },\n      mutationKey,\n      mutation:\n        mutation === false\n          ? false\n          : {\n              importPath: 'swr/mutation',\n              methods: ['post', 'put', 'delete', 'patch'],\n              ...mutation,\n            },\n      parser,\n      paramsType,\n      pathParamsType,\n      paramsCasing,\n      group,\n    },\n    pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),\n    resolvePath(baseName, pathMode, options) {\n      const root = path.resolve(this.config.root, this.config.output.path)\n      const mode = pathMode ?? getMode(path.resolve(root, output.path))\n\n      if (mode === 'single') {\n        /**\n         * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n         * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n         */\n        return path.resolve(root, output.path)\n      }\n\n      if (group && (options?.group?.path || options?.group?.tag)) {\n        const groupName: Group['name'] = group?.name\n          ? group.name\n          : (ctx) => {\n              if (group?.type === 'path') {\n                return `${ctx.group.split('/')[1]}`\n              }\n              return `${camelCase(ctx.group)}Controller`\n            }\n\n        return path.resolve(\n          root,\n          output.path,\n          groupName({\n            group: group.type === 'path' ? options.group.path! : options.group.tag!,\n          }),\n          baseName,\n        )\n      }\n\n      return path.resolve(root, output.path, baseName)\n    },\n    resolveName(name, type) {\n      let resolvedName = camelCase(name)\n\n      if (type === 'file' || type === 'function') {\n        resolvedName = camelCase(name, {\n          isFile: type === 'file',\n        })\n      }\n\n      if (type === 'type') {\n        resolvedName = pascalCase(name)\n      }\n\n      if (type) {\n        return transformers?.name?.(resolvedName, type) || resolvedName\n      }\n\n      return resolvedName\n    },\n    async install() {\n      const root = path.resolve(this.config.root, this.config.output.path)\n      const mode = getMode(path.resolve(root, output.path))\n      const oas = await this.getOas()\n      const baseURL = await this.getBaseURL()\n\n      if (baseURL) {\n        this.plugin.options.client.baseURL = baseURL\n      }\n\n      const hasClientPlugin = !!this.pluginManager.getPluginByKey([pluginClientName])\n\n      if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) {\n        // pre add bundled fetch\n        await this.upsertFile({\n          baseName: 'fetch.ts',\n          path: path.resolve(root, '.kubb/fetch.ts'),\n          sources: [\n            {\n              name: 'fetch',\n              value: this.plugin.options.client.client === 'fetch' ? fetchClientSource : axiosClientSource,\n              isExportable: true,\n              isIndexable: true,\n            },\n          ],\n          imports: [],\n          exports: [],\n        })\n      }\n\n      if (!hasClientPlugin) {\n        await this.addFile({\n          baseName: 'config.ts',\n          path: path.resolve(root, '.kubb/config.ts'),\n          sources: [\n            {\n              name: 'config',\n              value: configSource,\n              isExportable: false,\n              isIndexable: false,\n            },\n          ],\n          imports: [],\n          exports: [],\n        })\n      }\n\n      const operationGenerator = new OperationGenerator(this.plugin.options, {\n        fabric: this.fabric,\n        oas,\n        pluginManager: this.pluginManager,\n        events: this.events,\n        plugin: this.plugin,\n        contentType,\n        exclude,\n        include,\n        override,\n        mode,\n      })\n\n      const files = await operationGenerator.build(...generators)\n      await this.upsertFile(...files)\n\n      const barrelFiles = await getBarrelFiles(this.fabric.files, {\n        root,\n        output,\n        meta: {\n          pluginKey: this.plugin.key,\n        },\n      })\n\n      await this.upsertFile(...barrelFiles)\n    },\n  }\n})\n"],"mappings":";;;;;;;;;;;;;;AAcA,MAAa,gBAAgB;AAE7B,MAAa,aAAA,GAAA,WAAA,eAAqC,YAAY;CAC5D,MAAM,EACJ,SAAS;EAAE,MAAM;EAAS,YAAY;EAAS,EAC/C,OACA,UAAU,EAAE,EACZ,SACA,WAAW,EAAE,EACb,SAAS,UACT,eAAe,EAAE,EACjB,OACA,UACA,QACA,aAAa,UACb,iBAAiB,eAAe,WAAW,WAAW,QAAQ,kBAAkB,UAChF,cAAcA,mBAAAA,YAAY,gBAC1B,WAAWC,mBAAAA,SAAS,gBACpB,aAAa,CAACC,mBAAAA,gBAAgBC,mBAAAA,kBAAkB,CAAC,OAAO,QAAQ,EAChE,cACA,gBACE;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