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.

142 lines (137 loc) 4.73 kB
'use strict'; var chunk2N4Q32XH_cjs = require('./chunk-2N4Q32XH.cjs'); var chunkMNOTKHSE_cjs = require('./chunk-MNOTKHSE.cjs'); var path = require('path'); var core = require('@kubb/core'); var transformers = require('@kubb/core/transformers'); var pluginOas = require('@kubb/plugin-oas'); var pluginTs = require('@kubb/plugin-ts'); var pluginZod = require('@kubb/plugin-zod'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var path__default = /*#__PURE__*/_interopDefault(path); var pluginSwrName = "plugin-swr"; var pluginSwr = core.createPlugin((options) => { const { output = { path: "hooks", barrelType: "named" }, group, exclude = [], include, override = [], parser = "client", transformers: transformers$1 = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = chunkMNOTKHSE_cjs.MutationKey.getTransformer, queryKey = chunkMNOTKHSE_cjs.QueryKey.getTransformer, generators = [chunk2N4Q32XH_cjs.queryGenerator, chunk2N4Q32XH_cjs.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: [pluginOas.pluginOasName, pluginTs.pluginTsName, parser === "zod" ? pluginZod.pluginZodName : void 0].filter(Boolean), resolvePath(baseName, pathMode, options2) { const root = path__default.default.resolve(this.config.root, this.config.output.path); const mode = pathMode ?? core.FileManager.getMode(path__default.default.resolve(root, output.path)); if (mode === "single") { return path__default.default.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 `${transformers.camelCase(ctx.group)}Controller`; }; return path__default.default.resolve( root, output.path, groupName({ group: group.type === "path" ? options2.group.path : options2.group.tag }), baseName ); } return path__default.default.resolve(root, output.path, baseName); }, resolveName(name, type) { let resolvedName = transformers.camelCase(name); if (type === "file" || type === "function") { resolvedName = transformers.camelCase(name, { isFile: type === "file" }); } if (type === "type") { resolvedName = transformers.pascalCase(name); } if (type) { return transformers$1?.name?.(resolvedName, type) || resolvedName; } return resolvedName; }, async buildStart() { const [swaggerPlugin] = core.PluginManager.getDependedPlugins(this.plugins, [pluginOas.pluginOasName]); const oas = await swaggerPlugin.context.getOas(); const root = path__default.default.resolve(this.config.root, this.config.output.path); const mode = core.FileManager.getMode(path__default.default.resolve(root, output.path)); const baseURL = await swaggerPlugin.context.getBaseURL(); if (baseURL) { this.plugin.options.client.baseURL = baseURL; } const operationGenerator = new pluginOas.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); } }; }); exports.pluginSwr = pluginSwr; exports.pluginSwrName = pluginSwrName; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map