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.

412 lines (409 loc) 16.4 kB
const require_Query = require('./Query-CbVZ99IO.cjs'); let node_path = require("node:path"); node_path = require_Query.__toESM(node_path); let _kubb_plugin_client = require("@kubb/plugin-client"); let _kubb_plugin_ts = require("@kubb/plugin-ts"); let _kubb_plugin_zod = require("@kubb/plugin-zod"); let _kubb_plugin_client_components = require("@kubb/plugin-client/components"); let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils"); let _kubb_react_fabric = require("@kubb/react-fabric"); let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime"); let _kubb_core_hooks = require("@kubb/core/hooks"); let _kubb_plugin_oas_generators = require("@kubb/plugin-oas/generators"); let _kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks"); let remeda = require("remeda"); //#region src/generators/mutationGenerator.tsx const mutationGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "swr-mutation", Operation({ config, operation, generator, plugin }) { const { options, options: { output } } = plugin; const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const { getSchemas, getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator); const isQuery = !!options.query && options.query?.methods.some((method) => operation.method === method); const isMutation = options.mutation !== false && !isQuery && (0, remeda.difference)(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method); const importPath = options.mutation ? options.mutation.importPath : "swr"; const mutation = { name: getName(operation, { type: "function", prefix: "use" }), typeName: getName(operation, { type: "type" }), file: getFile(operation, { prefix: "use" }) }; const type = { file: getFile(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName], type: "type" }) }; const zod = { file: getFile(operation, { pluginKey: [_kubb_plugin_zod.pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [_kubb_plugin_zod.pluginZodName], type: "function" }) }; const shouldUseClientPlugin = !!pluginManager.getPluginByKey([_kubb_plugin_client.pluginClientName]) && options.client.clientType !== "class"; const client = { name: shouldUseClientPlugin ? getName(operation, { type: "function", pluginKey: [_kubb_plugin_client.pluginClientName] }) : getName(operation, { type: "function" }), file: getFile(operation, { pluginKey: [_kubb_plugin_client.pluginClientName] }) }; const mutationKey = { name: getName(operation, { type: "const", suffix: "MutationKey" }), typeName: getName(operation, { type: "type", suffix: "MutationKey" }) }; if (!isMutation) return null; return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, { baseName: mutation.file.baseName, path: mutation.file.path, meta: mutation.file.meta, banner: (0, _kubb_plugin_oas_utils.getBanner)({ oas, output, config: pluginManager.config }), footer: (0, _kubb_plugin_oas_utils.getFooter)({ oas, output }), children: [ options.parser === "zod" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean), root: mutation.file.path, path: zod.file.path }), options.client.importPath ? /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: "fetch", path: options.client.importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ "Client", "RequestConfig", "ResponseErrorConfig" ], path: options.client.importPath, isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["ResponseConfig"], path: options.client.importPath, isTypeOnly: true }) ] }) : /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["fetch"], root: mutation.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts") }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ "Client", "RequestConfig", "ResponseErrorConfig" ], root: mutation.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["ResponseConfig"], root: mutation.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }) ] }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: "useSWRMutation", path: importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["SWRMutationConfiguration", "SWRMutationResponse"], path: importPath, isTypeOnly: true }), shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [client.name], root: mutation.file.path, path: client.file.path }), !shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["buildFormData"], root: mutation.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/config.ts") }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ type.schemas.request?.name, type.schemas.response.name, type.schemas.pathParams?.name, type.schemas.queryParams?.name, type.schemas.headerParams?.name, ...type.schemas.statusCodes?.map((item) => item.name) || [] ].filter(Boolean), root: mutation.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Query.MutationKey, { name: mutationKey.name, typeName: mutationKey.typeName, operation, pathParamsType: options.pathParamsType, typeSchemas: type.schemas, paramsCasing: options.paramsCasing, transformer: options.mutationKey }), !shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_plugin_client_components.Client, { name: client.name, baseURL: options.client.baseURL, operation, typeSchemas: type.schemas, zodSchemas: zod.schemas, dataReturnType: options.client.dataReturnType || "data", paramsCasing: options.client?.paramsCasing || options.paramsCasing, paramsType: options.paramsType, pathParamsType: options.pathParamsType, parser: options.parser }), options.mutation && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Query.Mutation, { name: mutation.name, clientName: client.name, typeName: mutation.typeName, typeSchemas: type.schemas, operation, dataReturnType: options.client.dataReturnType || "data", paramsType: options.paramsType, paramsCasing: options.paramsCasing, pathParamsType: options.pathParamsType, mutationKeyName: mutationKey.name, mutationKeyTypeName: mutationKey.typeName, paramsToTrigger: options.mutation.paramsToTrigger }) ] }); } }); //#endregion //#region src/generators/queryGenerator.tsx const queryGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "swr-query", Operation({ config, operation, generator, plugin }) { const { options, options: { output } } = plugin; const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const { getSchemas, getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator); const isQuery = typeof options.query === "boolean" ? true : options.query?.methods.some((method) => operation.method === method); const isMutation = (0, remeda.difference)(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method); const importPath = options.query ? options.query.importPath : "swr/mutation"; const query = { name: getName(operation, { type: "function", prefix: "use" }), typeName: getName(operation, { type: "type" }), file: getFile(operation, { prefix: "use" }) }; const shouldUseClientPlugin = !!pluginManager.getPluginByKey([_kubb_plugin_client.pluginClientName]) && options.client.clientType !== "class"; const client = { name: shouldUseClientPlugin ? getName(operation, { type: "function", pluginKey: [_kubb_plugin_client.pluginClientName] }) : getName(operation, { type: "function" }), file: getFile(operation, { pluginKey: [_kubb_plugin_client.pluginClientName] }) }; const queryOptions = { name: getName(operation, { type: "function", suffix: "QueryOptions" }) }; const queryKey = { name: getName(operation, { type: "const", suffix: "QueryKey" }), typeName: getName(operation, { type: "type", suffix: "QueryKey" }) }; const type = { file: getFile(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName], type: "type" }) }; const zod = { file: getFile(operation, { pluginKey: [_kubb_plugin_zod.pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [_kubb_plugin_zod.pluginZodName], type: "function" }) }; if (!isQuery || isMutation) return null; return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, { baseName: query.file.baseName, path: query.file.path, meta: query.file.meta, banner: (0, _kubb_plugin_oas_utils.getBanner)({ oas, output, config: pluginManager.config }), footer: (0, _kubb_plugin_oas_utils.getFooter)({ oas, output }), children: [ options.parser === "zod" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean), root: query.file.path, path: zod.file.path }), options.client.importPath ? /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: "fetch", path: options.client.importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ "Client", "RequestConfig", "ResponseErrorConfig" ], path: options.client.importPath, isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["ResponseConfig"], path: options.client.importPath, isTypeOnly: true }) ] }) : /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["fetch"], root: query.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts") }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ "Client", "RequestConfig", "ResponseErrorConfig" ], root: query.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["ResponseConfig"], root: query.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }) ] }), shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [client.name], root: query.file.path, path: client.file.path }), !shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["buildFormData"], root: query.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/config.ts") }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [ type.schemas.request?.name, type.schemas.response.name, type.schemas.pathParams?.name, type.schemas.queryParams?.name, type.schemas.headerParams?.name, ...type.schemas.statusCodes?.map((item) => item.name) || [] ].filter(Boolean), root: query.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Query.QueryKey, { name: queryKey.name, typeName: queryKey.typeName, operation, pathParamsType: options.pathParamsType, typeSchemas: type.schemas, paramsCasing: options.paramsCasing, transformer: options.queryKey }), !shouldUseClientPlugin && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_plugin_client_components.Client, { name: client.name, baseURL: options.client.baseURL, operation, typeSchemas: type.schemas, zodSchemas: zod.schemas, dataReturnType: options.client.dataReturnType || "data", paramsCasing: options.client.paramsCasing || options.paramsCasing, paramsType: options.paramsType, pathParamsType: options.pathParamsType, parser: options.parser }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Query.QueryOptions, { name: queryOptions.name, clientName: client.name, typeSchemas: type.schemas, paramsType: options.paramsType, paramsCasing: options.paramsCasing, pathParamsType: options.pathParamsType }), options.query && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: "useSWR", path: importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["SWRResponse"], path: importPath, isTypeOnly: true }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Query.Query, { name: query.name, queryOptionsName: queryOptions.name, typeSchemas: type.schemas, paramsType: options.paramsType, pathParamsType: options.pathParamsType, operation, dataReturnType: options.client.dataReturnType || "data", queryKeyName: queryKey.name, paramsCasing: options.paramsCasing, queryKeyTypeName: queryKey.typeName }) ] }) ] }); } }); //#endregion Object.defineProperty(exports, 'mutationGenerator', { enumerable: true, get: function () { return mutationGenerator; } }); Object.defineProperty(exports, 'queryGenerator', { enumerable: true, get: function () { return queryGenerator; } }); //# sourceMappingURL=queryGenerator-CfKDvnGS.cjs.map