UNPKG

@kubb/plugin-client

Version:

API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.

449 lines (444 loc) 17.1 kB
const require_chunk = require('./chunk-CbDLau6x.cjs'); const require_Operations = require('./Operations-DmQa1_7x.cjs'); let node_path = require("node:path"); node_path = require_chunk.__toESM(node_path); let _kubb_core_transformers = require("@kubb/core/transformers"); let _kubb_plugin_zod = require("@kubb/plugin-zod"); 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 _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils"); let _kubb_plugin_ts = require("@kubb/plugin-ts"); let _kubb_react_fabric = require("@kubb/react-fabric"); let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime"); //#region src/generators/classClientGenerator.tsx const classClientGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "classClient", Operations({ operations, generator, plugin, config }) { const { options, key: pluginKey } = plugin; const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const { getName, getFile, getGroup, getSchemas } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator); function buildOperationData(operation) { 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" }) }; return { operation, name: getName(operation, { type: "function" }), typeSchemas: type.schemas, zodSchemas: zod.schemas, typeFile: type.file, zodFile: zod.file }; } const controllers = operations.reduce((acc, operation) => { const group = getGroup(operation); const groupName = group?.tag ? options.group?.name?.({ group: (0, _kubb_core_transformers.camelCase)(group.tag) }) ?? (0, _kubb_core_transformers.pascalCase)(group.tag) : "Client"; if (!group?.tag && !options.group) { const name = "ApiClient"; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey }); const operationData = buildOperationData(operation); const previousFile = acc.find((item) => item.file.path === file.path); if (previousFile) previousFile.operations.push(operationData); else acc.push({ name, file, operations: [operationData] }); } else if (group?.tag) { const name = groupName; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey, options: { group } }); const operationData = buildOperationData(operation); const previousFile = acc.find((item) => item.file.path === file.path); if (previousFile) previousFile.operations.push(operationData); else acc.push({ name, file, operations: [operationData] }); } return acc; }, []); function collectTypeImports(ops) { const typeImportsByFile = /* @__PURE__ */ new Map(); const typeFilesByPath = /* @__PURE__ */ new Map(); ops.forEach((op) => { const { typeSchemas, typeFile } = op; if (!typeImportsByFile.has(typeFile.path)) typeImportsByFile.set(typeFile.path, /* @__PURE__ */ new Set()); const typeImports = typeImportsByFile.get(typeFile.path); if (typeSchemas.request?.name) typeImports.add(typeSchemas.request.name); if (typeSchemas.response?.name) typeImports.add(typeSchemas.response.name); if (typeSchemas.pathParams?.name) typeImports.add(typeSchemas.pathParams.name); if (typeSchemas.queryParams?.name) typeImports.add(typeSchemas.queryParams.name); if (typeSchemas.headerParams?.name) typeImports.add(typeSchemas.headerParams.name); typeSchemas.statusCodes?.forEach((item) => { if (item?.name) typeImports.add(item.name); }); typeFilesByPath.set(typeFile.path, typeFile); }); return { typeImportsByFile, typeFilesByPath }; } function collectZodImports(ops) { const zodImportsByFile = /* @__PURE__ */ new Map(); const zodFilesByPath = /* @__PURE__ */ new Map(); ops.forEach((op) => { const { zodSchemas, zodFile } = op; if (!zodImportsByFile.has(zodFile.path)) zodImportsByFile.set(zodFile.path, /* @__PURE__ */ new Set()); const zodImports = zodImportsByFile.get(zodFile.path); if (zodSchemas?.response?.name) zodImports.add(zodSchemas.response.name); if (zodSchemas?.request?.name) zodImports.add(zodSchemas.request.name); zodFilesByPath.set(zodFile.path, zodFile); }); return { zodImportsByFile, zodFilesByPath }; } return controllers.map(({ name, file, operations: ops }) => { const { typeImportsByFile, typeFilesByPath } = collectTypeImports(ops); const { zodImportsByFile, zodFilesByPath } = options.parser === "zod" ? collectZodImports(ops) : { zodImportsByFile: /* @__PURE__ */ new Map(), zodFilesByPath: /* @__PURE__ */ new Map() }; const hasFormData = ops.some((op) => op.operation.getContentType() === "multipart/form-data"); return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: (0, _kubb_plugin_oas_utils.getBanner)({ oas, output: options.output, config: pluginManager.config }), footer: (0, _kubb_plugin_oas_utils.getFooter)({ oas, output: options.output }), children: [ options.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.importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.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: 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: ["RequestConfig", "ResponseErrorConfig"], root: file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true })] }), hasFormData && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["buildFormData"], root: file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/config.ts") }), Array.from(typeImportsByFile.entries()).map(([filePath, imports]) => { const typeFile = typeFilesByPath.get(filePath); if (!typeFile) return null; const importNames = Array.from(imports).filter(Boolean); if (importNames.length === 0) return null; return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: importNames, root: file.path, path: typeFile.path, isTypeOnly: true }, filePath); }), options.parser === "zod" && Array.from(zodImportsByFile.entries()).map(([filePath, imports]) => { const zodFile = zodFilesByPath.get(filePath); if (!zodFile) return null; const importNames = Array.from(imports).filter(Boolean); if (importNames.length === 0) return null; return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: importNames, root: file.path, path: zodFile.path }, filePath); }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Operations.ClassClient, { name, operations: ops, baseURL: options.baseURL, dataReturnType: options.dataReturnType, pathParamsType: options.pathParamsType, paramsCasing: options.paramsCasing, paramsType: options.paramsType, parser: options.parser }) ] }, file.path); }); } }); //#endregion //#region src/generators/clientGenerator.tsx const clientGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "client", Operation({ config, plugin, operation, generator }) { const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const { options, options: { output, urlType } } = plugin; const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const { getSchemas, getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator); const client = { name: getName(operation, { type: "function" }), file: getFile(operation) }; const url = { name: getName(operation, { type: "function", suffix: "url", prefix: "get" }), file: getFile(operation) }; 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 isFormData = operation.getContentType() === "multipart/form-data"; return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, { baseName: client.file.baseName, path: client.file.path, meta: client.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.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.importPath }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.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: client.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: ["RequestConfig", "ResponseErrorConfig"], root: client.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true })] }), isFormData && type.schemas.request?.name && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: ["buildFormData"], root: client.file.path, path: node_path.default.resolve(config.root, config.output.path, ".kubb/config.ts") }), 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: client.file.path, path: zod.file.path }), /* @__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: client.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Operations.Url, { name: url.name, baseURL: options.baseURL, pathParamsType: options.pathParamsType, paramsCasing: options.paramsCasing, paramsType: options.paramsType, typeSchemas: type.schemas, operation, isIndexable: urlType === "export", isExportable: urlType === "export" }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Operations.Client, { name: client.name, urlName: url.name, baseURL: options.baseURL, dataReturnType: options.dataReturnType, pathParamsType: options.pathParamsType, paramsCasing: options.paramsCasing, paramsType: options.paramsType, typeSchemas: type.schemas, operation, parser: options.parser, zodSchemas: zod.schemas }) ] }); } }); //#endregion //#region src/generators/groupedClientGenerator.tsx const groupedClientGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "groupedClient", Operations({ operations, generator, plugin }) { const { options, key: pluginKey } = plugin; const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const { getName, getFile, getGroup } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator); return operations.reduce((acc, operation) => { if (options.group?.type === "tag") { const group = getGroup(operation); const name = group?.tag ? options.group?.name?.({ group: (0, _kubb_core_transformers.camelCase)(group.tag) }) : void 0; if (!group?.tag || !name) return acc; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey, options: { group } }); const client = { name: getName(operation, { type: "function" }), file: getFile(operation) }; const previousFile = acc.find((item) => item.file.path === file.path); if (previousFile) previousFile.clients.push(client); else acc.push({ name, file, clients: [client] }); } return acc; }, []).map(({ name, file, clients }) => { return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: (0, _kubb_plugin_oas_utils.getBanner)({ oas, output: options.output, config: pluginManager.config }), footer: (0, _kubb_plugin_oas_utils.getFooter)({ oas, output: options.output }), children: [clients.map((client) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, { name: [client.name], root: file.path, path: client.file.path }, client.name)), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, { export: true, name, children: `return { ${clients.map((client) => client.name).join(", ")} }` }) })] }, file.path); }); } }); //#endregion //#region src/generators/operationsGenerator.tsx const operationsGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({ name: "client", Operations({ operations, plugin }) { const { key: pluginKey, options: { output } } = plugin; const pluginManager = (0, _kubb_core_hooks.usePluginManager)(); const oas = (0, _kubb_plugin_oas_hooks.useOas)(); const name = "operations"; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey }); return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: (0, _kubb_plugin_oas_utils.getBanner)({ oas, output, config: pluginManager.config }), footer: (0, _kubb_plugin_oas_utils.getFooter)({ oas, output }), children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Operations.Operations, { name, operations }) }); } }); //#endregion Object.defineProperty(exports, 'classClientGenerator', { enumerable: true, get: function () { return classClientGenerator; } }); Object.defineProperty(exports, 'clientGenerator', { enumerable: true, get: function () { return clientGenerator; } }); Object.defineProperty(exports, 'groupedClientGenerator', { enumerable: true, get: function () { return groupedClientGenerator; } }); Object.defineProperty(exports, 'operationsGenerator', { enumerable: true, get: function () { return operationsGenerator; } }); //# sourceMappingURL=generators-CfbdSA-k.cjs.map