@kubb/plugin-client
Version:
API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.
219 lines (215 loc) • 6.2 kB
JavaScript
import { n as Client, r as Url, t as Operations } from "./Operations-CmHRmAdn.js";
import { camelCase } from "@kubb/core/transformers";
import { createReactGenerator } from "@kubb/plugin-oas";
import { pluginZodName } from "@kubb/plugin-zod";
import { usePlugin, usePluginManager } from "@kubb/core/hooks";
import { useOas, useOperationManager } from "@kubb/plugin-oas/hooks";
import { getBanner, getFooter } from "@kubb/plugin-oas/utils";
import { pluginTsName } from "@kubb/plugin-ts";
import { File, Function } from "@kubb/react-fabric";
import { jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
//#region src/generators/clientGenerator.tsx
const clientGenerator = createReactGenerator({
name: "client",
Operation({ options, operation }) {
const pluginManager = usePluginManager();
const { options: { output, urlType } } = usePlugin();
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
})
]
});
}
});
//#endregion
//#region src/generators/operationsGenerator.tsx
const operationsGenerator = createReactGenerator({
name: "client",
Operations({ operations }) {
const { key: pluginKey, options: { output } } = usePlugin();
const pluginManager = usePluginManager();
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
})
});
}
});
//#endregion
//#region src/generators/groupedClientGenerator.tsx
const groupedClientGenerator = createReactGenerator({
name: "groupedClient",
Operations({ operations }) {
const { options, key: pluginKey } = usePlugin();
const pluginManager = usePluginManager();
const oas = useOas();
const { getName, getFile, getGroup } = useOperationManager();
return 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;
}, []).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);
});
}
});
//#endregion
export { operationsGenerator as n, clientGenerator as r, groupedClientGenerator as t };
//# sourceMappingURL=generators-0Sc5zPfx.js.map