@kubb/plugin-ts
Version:
TypeScript code generation plugin for Kubb, transforming OpenAPI schemas into TypeScript interfaces, types, and utility functions.
354 lines (350 loc) • 11.2 kB
JavaScript
import { OasType, Type as Type$1 } from "./components-BKIt6l3E.js";
import path from "node:path";
import { FileManager, PluginManager, createPlugin } from "@kubb/core";
import transformers, { camelCase, pascalCase } from "@kubb/core/transformers";
import { OperationGenerator, SchemaGenerator, createReactGenerator, pluginOasName } from "@kubb/plugin-oas";
import { print } from "@kubb/parser-ts";
import * as factory from "@kubb/parser-ts/factory";
import { Oas } from "@kubb/plugin-oas/components";
import { useOas, useOperationManager, useSchemaManager } from "@kubb/plugin-oas/hooks";
import { getBanner, getFooter } from "@kubb/plugin-oas/utils";
import { File, useApp } from "@kubb/react";
import { jsx, jsxs } from "@kubb/react/jsx-runtime";
//#region src/generators/typeGenerator.tsx
function printCombinedSchema({ name, schemas, pluginManager }) {
const properties = {};
if (schemas.response) properties["response"] = factory.createUnionDeclaration({ nodes: schemas.responses.map((res) => {
const identifier = pluginManager.resolveName({
name: res.name,
pluginKey: [pluginTsName],
type: "function"
});
return factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}) });
if (schemas.request) {
const identifier = pluginManager.resolveName({
name: schemas.request.name,
pluginKey: [pluginTsName],
type: "function"
});
properties["request"] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}
if (schemas.pathParams) {
const identifier = pluginManager.resolveName({
name: schemas.pathParams.name,
pluginKey: [pluginTsName],
type: "function"
});
properties["pathParams"] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}
if (schemas.queryParams) {
const identifier = pluginManager.resolveName({
name: schemas.queryParams.name,
pluginKey: [pluginTsName],
type: "function"
});
properties["queryParams"] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}
if (schemas.headerParams) {
const identifier = pluginManager.resolveName({
name: schemas.headerParams.name,
pluginKey: [pluginTsName],
type: "function"
});
properties["headerParams"] = factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}
if (schemas.errors) properties["errors"] = factory.createUnionDeclaration({ nodes: schemas.errors.map((error) => {
const identifier = pluginManager.resolveName({
name: error.name,
pluginKey: [pluginTsName],
type: "function"
});
return factory.createTypeReferenceNode(factory.createIdentifier(identifier), void 0);
}) });
const namespaceNode = factory.createTypeAliasDeclaration({
name,
type: factory.createTypeLiteralNode(Object.keys(properties).map((key) => {
const type = properties[key];
if (!type) return;
return factory.createPropertySignature({
name: transformers.pascalCase(key),
type
});
}).filter(Boolean)),
modifiers: [factory.modifiers.export]
});
return print([namespaceNode]);
}
const typeGenerator = createReactGenerator({
name: "typescript",
Operation({ operation, options }) {
const { mapper, enumType, syntaxType, optionalType } = options;
const { plugin, pluginManager, mode } = useApp();
const oas = useOas();
const { getSchemas, getFile, getName, getGroup } = useOperationManager();
const schemaManager = useSchemaManager();
const file = getFile(operation);
const schemas = getSchemas(operation);
const type = getName(operation, {
type: "function",
pluginKey: [pluginTsName]
});
const combinedSchemaName = operation.method === "get" ? `${type}Query` : `${type}Mutation`;
const schemaGenerator = new SchemaGenerator(options, {
oas,
plugin,
pluginManager,
mode,
override: options.override
});
const operationSchemas = [
schemas.pathParams,
schemas.queryParams,
schemas.headerParams,
schemas.statusCodes,
schemas.request,
schemas.response
].flat().filter(Boolean);
const mapOperationSchema = ({ name, schema: schemaObject, description, keysToOmit,...options$1 }) => {
const tree = schemaGenerator.parse({
schemaObject,
name
});
const imports = schemaManager.getImports(tree);
const group = options$1.operation ? getGroup(options$1.operation) : void 0;
const type$1 = {
name: schemaManager.getName(name, { type: "type" }),
typedName: schemaManager.getName(name, { type: "type" }),
file: schemaManager.getFile(options$1.operationName || name, { group })
};
return /* @__PURE__ */ jsxs(Oas.Schema, {
name,
schemaObject,
tree,
children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
root: file.path,
path: imp.path,
name: imp.name,
isTypeOnly: true
}, [
name,
imp.name,
imp.path,
imp.isTypeOnly
].join("-"))), /* @__PURE__ */ jsx(Type$1, {
name: type$1.name,
typedName: type$1.typedName,
description,
tree,
schema: schemaObject,
mapper,
enumType,
optionalType,
keysToOmit,
syntaxType
})]
}, [name, schemaObject.$ref].join("-"));
};
return /* @__PURE__ */ jsxs(File, {
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: getBanner({
oas,
output: plugin.options.output,
config: pluginManager.config
}),
footer: getFooter({
oas,
output: plugin.options.output
}),
children: [operationSchemas.map(mapOperationSchema), /* @__PURE__ */ jsx(File.Source, {
name: combinedSchemaName,
isExportable: true,
isIndexable: true,
isTypeOnly: true,
children: printCombinedSchema({
name: combinedSchemaName,
schemas,
pluginManager
})
})]
});
},
Schema({ schema, options }) {
const { mapper, enumType, syntaxType, optionalType } = options;
const { mode, plugin: { options: { output } }, pluginManager } = useApp();
const oas = useOas();
const { getName, getImports, getFile } = useSchemaManager();
const imports = getImports(schema.tree);
if (enumType === "asPascalConst") console.warn(`enumType '${enumType}' is deprecated`);
const type = {
name: getName(schema.name, { type: "function" }),
typedName: getName(schema.name, { type: "type" }),
file: getFile(schema.name)
};
return /* @__PURE__ */ jsxs(File, {
baseName: type.file.baseName,
path: type.file.path,
meta: type.file.meta,
banner: getBanner({
oas,
output,
config: pluginManager.config
}),
footer: getFooter({
oas,
output
}),
children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ jsx(File.Import, {
root: type.file.path,
path: imp.path,
name: imp.name,
isTypeOnly: true
}, [
schema.name,
imp.path,
imp.isTypeOnly
].join("-"))), /* @__PURE__ */ jsx(Type$1, {
name: type.name,
typedName: type.typedName,
description: schema.value.description,
tree: schema.tree,
schema: schema.value,
mapper,
enumType,
optionalType,
syntaxType
})]
});
}
});
//#endregion
//#region src/generators/oasGenerator.tsx
const oasGenerator = createReactGenerator({
name: "oas",
Operations() {
const { pluginManager, plugin: { options: { output }, key: pluginKey } } = useApp();
const oas = useOas();
const file = pluginManager.getFile({
name: "oas",
extname: ".ts",
pluginKey
});
return /* @__PURE__ */ jsxs(File, {
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: getBanner({
oas,
output,
config: pluginManager.config
}),
footer: getFooter({
oas,
output
}),
children: [/* @__PURE__ */ jsx(File.Import, {
name: ["Infer"],
path: "@kubb/oas",
isTypeOnly: true
}), /* @__PURE__ */ jsx(OasType, {
name: "oas",
typeName: "Oas",
api: oas.api
})]
});
}
});
//#endregion
//#region src/plugin.ts
const pluginTsName = "plugin-ts";
const pluginTs = createPlugin((options) => {
const { output = {
path: "types",
barrelType: "named"
}, group, exclude = [], include, override = [], enumType = "asConst", enumSuffix = "enum", dateType = "string", unknownType = "any", optionalType = "questionToken", emptySchemaType = unknownType, syntaxType = "type", transformers: transformers$1 = {}, oasType = false, mapper = {}, generators = [typeGenerator, oasType === "infer" ? oasGenerator : void 0].filter(Boolean), contentType } = options;
return {
name: pluginTsName,
options: {
output,
transformers: transformers$1,
dateType,
optionalType,
oasType,
enumType,
enumSuffix,
unknownType,
emptySchemaType,
syntaxType,
group,
override,
mapper
},
context() {
return { usedEnumNames: {} };
},
pre: [pluginOasName],
resolvePath(baseName, pathMode, options$1) {
const root = path.resolve(this.config.root, this.config.output.path);
if ((pathMode ?? FileManager.getMode(path.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 path.resolve(root, output.path);
if (group && (options$1?.group?.path || options$1?.group?.tag)) {
const groupName = group?.name ? group.name : (ctx) => {
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
return `${camelCase(ctx.group)}Controller`;
};
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options$1.group.path : options$1.group.tag }), baseName);
}
return path.resolve(root, output.path, baseName);
},
resolveName(name, type) {
const resolvedName = pascalCase(name, { isFile: type === "file" });
if (type) return transformers$1?.name?.(resolvedName, type) || resolvedName;
return resolvedName;
},
async buildStart() {
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
const oas = await swaggerPlugin.context.getOas();
const root = path.resolve(this.config.root, this.config.output.path);
const mode = FileManager.getMode(path.resolve(root, output.path));
const schemaFiles = await new SchemaGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
include: void 0,
override,
mode,
output: output.path
}).build(...generators);
await this.addFile(...schemaFiles);
const operationFiles = await new OperationGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
exclude,
include,
override,
mode
}).build(...generators);
await this.addFile(...operationFiles);
const barrelFiles = await this.fileManager.getBarrelFiles({
type: output.barrelType ?? "named",
root,
output,
meta: { pluginKey: this.plugin.key },
logger: this.logger
});
await this.addFile(...barrelFiles);
}
};
});
//#endregion
export { oasGenerator, pluginTs, pluginTsName, typeGenerator };
//# sourceMappingURL=plugin-nmU_mIiP.js.map