UNPKG

@kubb/plugin-oas

Version:

OpenAPI Specification (OAS) plugin for Kubb, providing core functionality for parsing and processing OpenAPI/Swagger schemas for code generation.

192 lines (188 loc) 4.63 kB
import { t as SchemaGenerator } from "./SchemaGenerator-CUJJ1Cb-.js"; import { n as schemaKeywords } from "./SchemaMapper-Ck7sFVaF.js"; import "./getSchemas-B0dk_Cbz.js"; import { useApp } from "@kubb/react-fabric"; import { usePlugin, usePluginManager } from "@kubb/core/hooks"; //#region src/hooks/useOas.ts function useOas() { const { meta } = useApp(); return meta.oas; } //#endregion //#region src/hooks/useOperationManager.ts /** * `useOperationManager` will return some helper functions that can be used to get the operation file, get the operation name. */ function useOperationManager(generator) { const plugin = usePlugin(); const pluginManager = usePluginManager(); const getName = (operation, { prefix = "", suffix = "", pluginKey = plugin.key, type }) => { return pluginManager.resolveName({ name: `${prefix} ${operation.getOperationId()} ${suffix}`, pluginKey, type }); }; const getGroup = (operation) => { return { tag: operation.getTags().at(0)?.name, path: operation.path }; }; const getSchemas = (operation, params) => { if (!generator) throw new Error(`'generator' is not defined`); return generator.getSchemas(operation, { resolveName: (name) => pluginManager.resolveName({ name, pluginKey: params?.pluginKey, type: params?.type }) }); }; const getFile = (operation, { prefix, suffix, pluginKey = plugin.key, extname = ".ts" } = {}) => { const name = getName(operation, { type: "file", pluginKey, prefix, suffix }); const group = getGroup(operation); const file = pluginManager.getFile({ name, extname, pluginKey, options: { type: "file", pluginKey, group } }); return { ...file, meta: { ...file.meta, name, pluginKey, group } }; }; const groupSchemasByName = (operation, { pluginKey = plugin.key, type }) => { if (!generator) throw new Error(`'generator' is not defined`); const schemas = generator.getSchemas(operation); const errors = (schemas.errors || []).reduce((prev, acc) => { if (!acc.statusCode) return prev; prev[acc.statusCode] = pluginManager.resolveName({ name: acc.name, pluginKey, type }); return prev; }, {}); const responses = (schemas.responses || []).reduce((prev, acc) => { if (!acc.statusCode) return prev; prev[acc.statusCode] = pluginManager.resolveName({ name: acc.name, pluginKey, type }); return prev; }, {}); return { request: schemas.request?.name ? pluginManager.resolveName({ name: schemas.request.name, pluginKey, type }) : void 0, parameters: { path: schemas.pathParams?.name ? pluginManager.resolveName({ name: schemas.pathParams.name, pluginKey, type }) : void 0, query: schemas.queryParams?.name ? pluginManager.resolveName({ name: schemas.queryParams.name, pluginKey, type }) : void 0, header: schemas.headerParams?.name ? pluginManager.resolveName({ name: schemas.headerParams.name, pluginKey, type }) : void 0 }, responses: { ...responses, ["default"]: pluginManager.resolveName({ name: schemas.response.name, pluginKey, type }), ...errors }, errors }; }; return { getName, getFile, getSchemas, groupSchemasByName, getGroup }; } //#endregion //#region src/hooks/useSchemaManager.ts /** * `useSchemaManager` will return some helper functions that can be used to get the schema file, get the schema name. */ function useSchemaManager() { const plugin = usePlugin(); const pluginManager = usePluginManager(); const getName = (name, { pluginKey = plugin.key, type }) => { return pluginManager.resolveName({ name, pluginKey, type }); }; const getFile = (name, { mode = "split", pluginKey = plugin.key, extname = ".ts", group } = {}) => { const resolvedName = mode === "single" ? "" : getName(name, { type: "file", pluginKey }); const file = pluginManager.getFile({ name: resolvedName, extname, pluginKey, options: { type: "file", pluginKey, group } }); return { ...file, meta: { ...file.meta, name: resolvedName, pluginKey } }; }; const getImports = (tree) => { return SchemaGenerator.deepSearch(tree, schemaKeywords.ref)?.map((item) => { if (!item.args.path || !item.args.isImportable) return; return { name: [item.args.name], path: item.args.path }; }).filter(Boolean); }; return { getName, getFile, getImports }; } //#endregion export { useOas, useOperationManager, useSchemaManager }; //# sourceMappingURL=hooks.js.map