UNPKG

@kubb/plugin-swr

Version:

SWR hooks generator plugin for Kubb, creating type-safe data fetching hooks from OpenAPI specifications for React and Next.js applications.

135 lines (133 loc) 4.31 kB
import { queryGenerator, mutationGenerator } from './chunk-GWLPXHRE.js'; import { MutationKey, QueryKey } from './chunk-RZTUH4QW.js'; import path from 'path'; import { createPlugin, PluginManager, FileManager } from '@kubb/core'; import { camelCase, pascalCase } from '@kubb/core/transformers'; import { pluginOasName, OperationGenerator } from '@kubb/plugin-oas'; import { pluginTsName } from '@kubb/plugin-ts'; import { pluginZodName } from '@kubb/plugin-zod'; var pluginSwrName = "plugin-swr"; var pluginSwr = createPlugin((options) => { const { output = { path: "hooks", barrelType: "named" }, group, exclude = [], include, override = [], parser = "client", transformers = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = MutationKey.getTransformer, queryKey = QueryKey.getTransformer, generators = [queryGenerator, mutationGenerator].filter(Boolean), paramsCasing, contentType } = options; return { name: pluginSwrName, options: { output, client: { importPath: "@kubb/plugin-client/clients/axios", dataReturnType: "data", ...client }, queryKey, query: query === false ? false : { importPath: "swr", methods: ["get"], ...query }, mutationKey, mutation: { importPath: "swr/mutation", methods: ["post", "put", "delete", "patch"], ...mutation }, parser, paramsType, pathParamsType, paramsCasing, group }, pre: [pluginOasName, pluginTsName, parser === "zod" ? pluginZodName : void 0].filter(Boolean), resolvePath(baseName, pathMode, options2) { const root = path.resolve(this.config.root, this.config.output.path); const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path)); if (mode === "single") { return path.resolve(root, output.path); } if (group && (options2?.group?.path || options2?.group?.tag)) { const groupName = 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" ? options2.group.path : options2.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] = PluginManager.getDependedPlugins(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); } }; }); export { pluginSwr, pluginSwrName }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map