UNPKG

@kubb/plugin-react-query

Version:

React Query hooks generator plugin for Kubb, creating type-safe API client hooks from OpenAPI specifications for React applications.

168 lines (167 loc) 6.69 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_components = require("./components-BHQT9ZLc.cjs"); const require_generators = require("./generators-W_darhpm.cjs"); let node_path = require("node:path"); node_path = require_components.__toESM(node_path); let _kubb_core = require("@kubb/core"); let _kubb_plugin_client = require("@kubb/plugin-client"); let _kubb_plugin_client_templates_clients_axios_source = require("@kubb/plugin-client/templates/clients/axios.source"); let _kubb_plugin_client_templates_clients_fetch_source = require("@kubb/plugin-client/templates/clients/fetch.source"); let _kubb_plugin_client_templates_config_source = require("@kubb/plugin-client/templates/config.source"); let _kubb_plugin_oas = require("@kubb/plugin-oas"); let _kubb_plugin_ts = require("@kubb/plugin-ts"); let _kubb_plugin_zod = require("@kubb/plugin-zod"); //#region src/plugin.ts const pluginReactQueryName = "plugin-react-query"; const pluginReactQuery = (0, _kubb_core.definePlugin)((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 = [ require_generators.queryGenerator, require_generators.suspenseQueryGenerator, require_generators.infiniteQueryGenerator, require_generators.suspenseInfiniteQueryGenerator, require_generators.mutationGenerator, require_generators.hookOptionsGenerator, require_generators.customHookOptionsFileGenerator ].filter(Boolean), mutation = {}, query = {}, mutationKey = require_components.MutationKey.getTransformer, queryKey = require_components.QueryKey.getTransformer, customOptions, paramsCasing, contentType, client } = options; const clientName = client?.client ?? "axios"; const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : void 0); return { name: pluginReactQueryName, options: { output, client: { bundle: client?.bundle, baseURL: client?.baseURL, client: clientName, clientType: client?.clientType ?? "function", dataReturnType: client?.dataReturnType ?? "data", pathParamsType, importPath: clientImportPath, paramsCasing }, infinite: infinite ? { queryParam: "id", initialPageParam: 0, cursorParam: void 0, nextParam: void 0, previousParam: void 0, ...infinite } : false, suspense, queryKey, query: query === false ? false : { methods: ["get"], importPath: "@tanstack/react-query", ...query }, mutationKey, mutation: mutation === false ? false : { methods: [ "post", "put", "patch", "delete" ], importPath: "@tanstack/react-query", ...mutation }, customOptions: customOptions ? { name: "useCustomHookOptions", ...customOptions } : void 0, paramsType, pathParamsType, parser, paramsCasing, group }, pre: [ _kubb_plugin_oas.pluginOasName, _kubb_plugin_ts.pluginTsName, parser === "zod" ? _kubb_plugin_zod.pluginZodName : void 0 ].filter(Boolean), resolvePath(baseName, pathMode, options) { const root = node_path.default.resolve(this.config.root, this.config.output.path); if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path))) === "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 node_path.default.resolve(root, output.path); if (group && (options?.group?.path || options?.group?.tag)) { const groupName = group?.name ? group.name : (ctx) => { if (group?.type === "path") return `${ctx.group.split("/")[1]}`; return `${require_components.camelCase(ctx.group)}Controller`; }; return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName); } return node_path.default.resolve(root, output.path, baseName); }, resolveName(name, type) { let resolvedName = require_components.camelCase(name); if (type === "file" || type === "function") resolvedName = require_components.camelCase(name, { isFile: type === "file" }); if (type === "type") resolvedName = require_components.pascalCase(name); if (type) return transformers?.name?.(resolvedName, type) || resolvedName; return resolvedName; }, async install() { const root = node_path.default.resolve(this.config.root, this.config.output.path); const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path)); const oas = await this.getOas(); const baseURL = await this.getBaseURL(); if (baseURL) this.plugin.options.client.baseURL = baseURL; const hasClientPlugin = !!this.pluginManager.getPluginByKey([_kubb_plugin_client.pluginClientName]); if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) await this.upsertFile({ baseName: "fetch.ts", path: node_path.default.resolve(root, ".kubb/fetch.ts"), sources: [{ name: "fetch", value: this.plugin.options.client.client === "fetch" ? _kubb_plugin_client_templates_clients_fetch_source.source : _kubb_plugin_client_templates_clients_axios_source.source, isExportable: true, isIndexable: true }], imports: [], exports: [] }); if (!hasClientPlugin) await this.upsertFile({ baseName: "config.ts", path: node_path.default.resolve(root, ".kubb/config.ts"), sources: [{ name: "config", value: _kubb_plugin_client_templates_config_source.source, isExportable: false, isIndexable: false }], imports: [], exports: [] }); const files = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, { fabric: this.fabric, oas, pluginManager: this.pluginManager, events: this.events, plugin: this.plugin, contentType, exclude, include, override, mode }).build(...generators); await this.upsertFile(...files); const barrelFiles = await (0, _kubb_core.getBarrelFiles)(this.fabric.files, { type: output.barrelType ?? "named", root, output, meta: { pluginKey: this.plugin.key } }); await this.upsertFile(...barrelFiles); } }; }); //#endregion exports.pluginReactQuery = pluginReactQuery; exports.pluginReactQueryName = pluginReactQueryName; //# sourceMappingURL=index.cjs.map