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.

187 lines (185 loc) 6.68 kB
import { Url, Client, Operations } from './chunk-LNJCFB5O.js'; import { createReactGenerator } from '@kubb/plugin-oas'; import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'; import { getFooter, getBanner } from '@kubb/plugin-oas/utils'; import { pluginTsName } from '@kubb/plugin-ts'; import { pluginZodName } from '@kubb/plugin-zod'; import { useApp, File, Function } from '@kubb/react'; import { jsxs, jsx } from '@kubb/react/jsx-runtime'; import { camelCase } from '@kubb/core/transformers'; var clientGenerator = createReactGenerator({ name: "client", Operation({ options, operation }) { const { plugin: { options: { output, urlType } }, pluginManager } = useApp(); const oas = useOas(); const { getSchemas, getName, getFile } = useOperationManager(); 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: [pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: "type" }) }; const zod = { file: getFile(operation, { pluginKey: [pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: "function" }) }; return /* @__PURE__ */ jsxs( File, { baseName: client.file.baseName, path: client.file.path, meta: client.file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: [ /* @__PURE__ */ jsx(File.Import, { name: "fetch", path: options.importPath }), /* @__PURE__ */ jsx(File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.importPath, isTypeOnly: true }), options.parser === "zod" && /* @__PURE__ */ jsx(File.Import, { name: [zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean), root: client.file.path, path: zod.file.path }), /* @__PURE__ */ jsx( 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__ */ jsx( 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__ */ jsx( 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 } ) ] } ); } }); var operationsGenerator = createReactGenerator({ name: "client", Operations({ operations }) { const { pluginManager, plugin: { key: pluginKey, options: { output } } } = useApp(); const oas = useOas(); const name = "operations"; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey }); return /* @__PURE__ */ jsx( File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: /* @__PURE__ */ jsx(Operations, { name, operations }) } ); } }); var groupedClientGenerator = createReactGenerator({ name: "groupedClient", Operations({ operations }) { const { pluginManager, plugin: { options, key: pluginKey } } = useApp(); const oas = useOas(); const { getName, getFile, getGroup } = useOperationManager(); const controllers = operations.reduce( (acc, operation) => { if (options.group?.type === "tag") { const group = getGroup(operation); const name = group?.tag ? options.group?.name?.({ group: 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; }, [] ); return controllers.map(({ name, file, clients }) => { return /* @__PURE__ */ jsxs( File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: getBanner({ oas, output: options.output, config: pluginManager.config }), footer: getFooter({ oas, output: options.output }), children: [ clients.map((client) => /* @__PURE__ */ jsx(File.Import, { name: [client.name], root: file.path, path: client.file.path }, client.name)), /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: /* @__PURE__ */ jsx(Function, { export: true, name, children: `return { ${clients.map((client) => client.name).join(", ")} }` }) }) ] }, file.path ); }); } }); export { clientGenerator, groupedClientGenerator, operationsGenerator }; //# sourceMappingURL=chunk-QNFXNUH5.js.map //# sourceMappingURL=chunk-QNFXNUH5.js.map