UNPKG

@kubb/plugin-ts

Version:

TypeScript code generation plugin for Kubb, transforming OpenAPI schemas into TypeScript interfaces, types, and utility functions.

388 lines (384 loc) 14 kB
const require_components = require('./components-CTopmaD3.cjs'); let node_path = require("node:path"); node_path = require_components.__toESM(node_path); let __kubb_core = require("@kubb/core"); __kubb_core = require_components.__toESM(__kubb_core); let __kubb_core_transformers = require("@kubb/core/transformers"); __kubb_core_transformers = require_components.__toESM(__kubb_core_transformers); let __kubb_plugin_oas = require("@kubb/plugin-oas"); __kubb_plugin_oas = require_components.__toESM(__kubb_plugin_oas); let __kubb_parser_ts = require("@kubb/parser-ts"); __kubb_parser_ts = require_components.__toESM(__kubb_parser_ts); let __kubb_parser_ts_factory = require("@kubb/parser-ts/factory"); __kubb_parser_ts_factory = require_components.__toESM(__kubb_parser_ts_factory); let __kubb_plugin_oas_components = require("@kubb/plugin-oas/components"); __kubb_plugin_oas_components = require_components.__toESM(__kubb_plugin_oas_components); let __kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks"); __kubb_plugin_oas_hooks = require_components.__toESM(__kubb_plugin_oas_hooks); let __kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils"); __kubb_plugin_oas_utils = require_components.__toESM(__kubb_plugin_oas_utils); let __kubb_react = require("@kubb/react"); __kubb_react = require_components.__toESM(__kubb_react); let __kubb_react_jsx_runtime = require("@kubb/react/jsx-runtime"); __kubb_react_jsx_runtime = require_components.__toESM(__kubb_react_jsx_runtime); //#region src/generators/typeGenerator.tsx function printCombinedSchema({ name, schemas, pluginManager }) { const properties = {}; if (schemas.response) properties["response"] = __kubb_parser_ts_factory.createUnionDeclaration({ nodes: schemas.responses.map((res) => { const identifier = pluginManager.resolveName({ name: res.name, pluginKey: [pluginTsName], type: "function" }); return __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); }) }); if (schemas.request) { const identifier = pluginManager.resolveName({ name: schemas.request.name, pluginKey: [pluginTsName], type: "function" }); properties["request"] = __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); } if (schemas.pathParams) { const identifier = pluginManager.resolveName({ name: schemas.pathParams.name, pluginKey: [pluginTsName], type: "function" }); properties["pathParams"] = __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); } if (schemas.queryParams) { const identifier = pluginManager.resolveName({ name: schemas.queryParams.name, pluginKey: [pluginTsName], type: "function" }); properties["queryParams"] = __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); } if (schemas.headerParams) { const identifier = pluginManager.resolveName({ name: schemas.headerParams.name, pluginKey: [pluginTsName], type: "function" }); properties["headerParams"] = __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); } if (schemas.errors) properties["errors"] = __kubb_parser_ts_factory.createUnionDeclaration({ nodes: schemas.errors.map((error) => { const identifier = pluginManager.resolveName({ name: error.name, pluginKey: [pluginTsName], type: "function" }); return __kubb_parser_ts_factory.createTypeReferenceNode(__kubb_parser_ts_factory.createIdentifier(identifier), void 0); }) }); const namespaceNode = __kubb_parser_ts_factory.createTypeAliasDeclaration({ name, type: __kubb_parser_ts_factory.createTypeLiteralNode(Object.keys(properties).map((key) => { const type = properties[key]; if (!type) return; return __kubb_parser_ts_factory.createPropertySignature({ name: __kubb_core_transformers.default.pascalCase(key), type }); }).filter(Boolean)), modifiers: [__kubb_parser_ts_factory.modifiers.export] }); return (0, __kubb_parser_ts.print)([namespaceNode]); } const typeGenerator = (0, __kubb_plugin_oas.createReactGenerator)({ name: "typescript", Operation({ operation, options }) { const { mapper, enumType, syntaxType, optionalType } = options; const { plugin, pluginManager, mode } = (0, __kubb_react.useApp)(); const oas = (0, __kubb_plugin_oas_hooks.useOas)(); const { getSchemas, getFile, getName, getGroup } = (0, __kubb_plugin_oas_hooks.useOperationManager)(); const schemaManager = (0, __kubb_plugin_oas_hooks.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 __kubb_plugin_oas.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__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_plugin_oas_components.Oas.Schema, { name, schemaObject, tree, children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, { root: file.path, path: imp.path, name: imp.name, isTypeOnly: true }, [ name, imp.name, imp.path, imp.isTypeOnly ].join("-"))), /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(require_components.Type, { name: type$1.name, typedName: type$1.typedName, description, tree, schema: schemaObject, mapper, enumType, optionalType, keysToOmit, syntaxType })] }, [name, schemaObject.$ref].join("-")); }; return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: (0, __kubb_plugin_oas_utils.getBanner)({ oas, output: plugin.options.output, config: pluginManager.config }), footer: (0, __kubb_plugin_oas_utils.getFooter)({ oas, output: plugin.options.output }), children: [operationSchemas.map(mapOperationSchema), /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.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 } = (0, __kubb_react.useApp)(); const oas = (0, __kubb_plugin_oas_hooks.useOas)(); const { getName, getImports, getFile } = (0, __kubb_plugin_oas_hooks.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__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.File, { baseName: type.file.baseName, path: type.file.path, meta: type.file.meta, banner: (0, __kubb_plugin_oas_utils.getBanner)({ oas, output, config: pluginManager.config }), footer: (0, __kubb_plugin_oas_utils.getFooter)({ oas, output }), children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Import, { root: type.file.path, path: imp.path, name: imp.name, isTypeOnly: true }, [ schema.name, imp.path, imp.isTypeOnly ].join("-"))), /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(require_components.Type, { 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 = (0, __kubb_plugin_oas.createReactGenerator)({ name: "oas", Operations() { const { pluginManager, plugin: { options: { output }, key: pluginKey } } = (0, __kubb_react.useApp)(); const oas = (0, __kubb_plugin_oas_hooks.useOas)(); const file = pluginManager.getFile({ name: "oas", extname: ".ts", pluginKey }); return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.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_jsx_runtime.jsx)(__kubb_react.File.Import, { name: ["Infer"], path: "@kubb/oas", isTypeOnly: true }), /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(require_components.OasType, { name: "oas", typeName: "Oas", api: oas.api })] }); } }); //#endregion //#region src/plugin.ts const pluginTsName = "plugin-ts"; const pluginTs = (0, __kubb_core.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: [__kubb_plugin_oas.pluginOasName], resolvePath(baseName, pathMode, options$1) { const root = node_path.default.resolve(this.config.root, this.config.output.path); if ((pathMode ?? __kubb_core.FileManager.getMode(node_path.default.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 node_path.default.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 `${(0, __kubb_core_transformers.camelCase)(ctx.group)}Controller`; }; return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options$1.group.path : options$1.group.tag }), baseName); } return node_path.default.resolve(root, output.path, baseName); }, resolveName(name, type) { const resolvedName = (0, __kubb_core_transformers.pascalCase)(name, { isFile: type === "file" }); if (type) return transformers$1?.name?.(resolvedName, type) || resolvedName; return resolvedName; }, async buildStart() { const [swaggerPlugin] = __kubb_core.PluginManager.getDependedPlugins(this.plugins, [__kubb_plugin_oas.pluginOasName]); const oas = await swaggerPlugin.context.getOas(); const root = node_path.default.resolve(this.config.root, this.config.output.path); const mode = __kubb_core.FileManager.getMode(node_path.default.resolve(root, output.path)); const schemaFiles = await new __kubb_plugin_oas.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 __kubb_plugin_oas.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 Object.defineProperty(exports, 'oasGenerator', { enumerable: true, get: function () { return oasGenerator; } }); Object.defineProperty(exports, 'pluginTs', { enumerable: true, get: function () { return pluginTs; } }); Object.defineProperty(exports, 'pluginTsName', { enumerable: true, get: function () { return pluginTsName; } }); Object.defineProperty(exports, 'typeGenerator', { enumerable: true, get: function () { return typeGenerator; } }); //# sourceMappingURL=plugin-CxMJgMjV.cjs.map