UNPKG

@kubb/plugin-oas

Version:
208 lines (206 loc) • 5.47 kB
import { SchemaGenerator, schemaKeywords } from './chunk-VJ6RX6RF.js'; import './chunk-3Y4QGY6D.js'; import { Oas, Operation, Schema } from './chunk-BAU7PO7T.js'; import { useContext, useApp } from '@kubb/react'; function useOas() { const { oas } = useContext(Oas.Context); if (!oas) { throw new Error("Oas is not defined"); } return oas; } function useOperation() { const { operation } = useContext(Operation.Context); if (!operation) { throw new Error("Operation is not defined"); } return operation; } function useOperationManager() { const { plugin, pluginManager } = useApp(); const { generator } = useContext(Oas.Context); 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 }; } function useOperations({ method, path } = {}) { const { operations } = useContext(Oas.Context); if (!operations) { throw new Error("Operations is not defined"); } let items = operations; if (path) { items = items.filter((item) => item.path === path); } if (method) { items = items.filter((item) => item.method === method); } return items; } function useSchema() { const props = useContext(Schema.Context); return props; } function useSchemaManager() { const { plugin, pluginManager } = useApp(); 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) => { const refs = SchemaGenerator.deepSearch(tree, schemaKeywords.ref); return refs?.map((item) => { if (!item.args.path || !item.args.isImportable) { return void 0; } return { name: [item.args.name], path: item.args.path }; }).filter(Boolean); }; return { getName, getFile, getImports }; } export { useOas, useOperation, useOperationManager, useOperations, useSchema, useSchemaManager }; //# sourceMappingURL=hooks.js.map //# sourceMappingURL=hooks.js.map