@kubb/plugin-react-query
Version:
React Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for React applications.
174 lines (154 loc) • 5.27 kB
text/typescript
import path from 'node:path'
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
import { camelCase, pascalCase } from '@kubb/core/transformers'
import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
import { pluginTsName } from '@kubb/plugin-ts'
import { pluginZodName } from '@kubb/plugin-zod'
import type { Plugin } from '@kubb/core'
import type { PluginOas } from '@kubb/plugin-oas'
import { MutationKey } from './components'
import { QueryKey } from './components/QueryKey.tsx'
import { infiniteQueryGenerator, mutationGenerator, queryGenerator, suspenseQueryGenerator } from './generators'
import type { PluginReactQuery } from './types.ts'
export const pluginReactQueryName = 'plugin-react-query' satisfies PluginReactQuery['name']
export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
const {
output = { path: 'hooks', barrelType: 'named' },
group,
exclude = [],
include,
override = [],
parser = 'client',
suspense = {},
infinite = false,
transformers = {},
paramsType = 'inline',
pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
generators = [queryGenerator, suspenseQueryGenerator, infiniteQueryGenerator, mutationGenerator].filter(Boolean),
mutation = {},
query = {},
mutationKey = MutationKey.getTransformer,
queryKey = QueryKey.getTransformer,
paramsCasing,
contentType,
} = options
return {
name: pluginReactQueryName,
options: {
output,
client: {
importPath: '@kubb/plugin-client/clients/axios',
dataReturnType: 'data',
pathParamsType,
...options.client,
},
infinite: infinite
? {
queryParam: 'id',
initialPageParam: 0,
cursorParam: undefined,
...infinite,
}
: false,
suspense,
queryKey,
query:
query === false
? false
: {
methods: ['get'],
importPath: '@tanstack/react-query',
...query,
},
mutationKey,
mutation: {
methods: ['post', 'put', 'patch', 'delete'],
importPath: '@tanstack/react-query',
...mutation,
},
paramsType,
pathParamsType,
parser,
paramsCasing,
group,
},
pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
resolvePath(baseName, pathMode, options) {
const root = path.resolve(this.config.root, this.config.output.path)
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
if (mode === 'single') {
/**
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
*/
return path.resolve(root, output.path)
}
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
if (group?.type === 'path') {
return `${ctx.group.split('/')[1]}`
}
return `${camelCase(ctx.group)}Controller`
}
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}
return path.resolve(root, output.path, baseName)
},
resolveName(name, type) {
let resolvedName = camelCase(name)
if (type === 'file' || type === 'function') {
resolvedName = camelCase(name, {
isFile: type === 'file',
})
}
if (type === 'type') {
resolvedName = pascalCase(name)
}
if (type) {
return transformers?.name?.(resolvedName, type) || resolvedName
}
return resolvedName
},
async buildStart() {
const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
const oas = await swaggerPlugin.context.getOas()
const root = path.resolve(this.config.root, this.config.output.path)
const mode = FileManager.getMode(path.resolve(root, output.path))
const baseURL = await swaggerPlugin.context.getBaseURL()
if (baseURL) {
this.plugin.options.client.baseURL = baseURL
}
const operationGenerator = new OperationGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
exclude,
include,
override,
mode,
})
const files = await operationGenerator.build(...generators)
await this.addFile(...files)
const barrelFiles = await this.fileManager.getBarrelFiles({
type: output.barrelType ?? 'named',
root,
output,
meta: {
pluginKey: this.plugin.key,
},
logger: this.logger,
})
await this.addFile(...barrelFiles)
},
}
})