diff --git a/.changeset/orange-dots-sort.md b/.changeset/orange-dots-sort.md new file mode 100644 index 000000000..91dc8bfb6 --- /dev/null +++ b/.changeset/orange-dots-sort.md @@ -0,0 +1,5 @@ +--- +"@kubb/swagger-zod": patch +--- + +use of `useOperationHelpers` to abstract operation logic diff --git a/docs/plugins/swagger/hooks/index.md b/docs/plugins/swagger/hooks/index.md index e4e605736..d53fca1cc 100644 --- a/docs/plugins/swagger/hooks/index.md +++ b/docs/plugins/swagger/hooks/index.md @@ -20,7 +20,7 @@ See [Oas](https://github.com/readmeio/oas) to understand how to use the `Oas` in import { useOas } from '@kubb/react' function Component() { - const oas = useOas() + const { oas } = useOas() return null } diff --git a/packages/core/src/PluginManager.ts b/packages/core/src/PluginManager.ts index 75ecc674d..c7b485837 100644 --- a/packages/core/src/PluginManager.ts +++ b/packages/core/src/PluginManager.ts @@ -2,6 +2,7 @@ import PQueue from 'p-queue' +import { readSync } from './fs/read.ts' import { transformReservedWord } from './transformers/transformReservedWord.ts' import { EventEmitter } from './utils/EventEmitter.ts' import { setUniqueName } from './utils/uniqueName.ts' @@ -73,6 +74,14 @@ type Events = { error: [error: Error] } +type GetFileProps = { + name: string + mode?: KubbFile.Mode + extName: KubbFile.Extname + pluginKey: Plugin['key'] + options?: TOptions +} + export class PluginManager { readonly plugins: PluginWithLifeCycle[] readonly fileManager: FileManager @@ -118,6 +127,31 @@ export class PluginManager { return this } + getFile({ name, mode, extName, pluginKey, options }: GetFileProps): KubbFile.File<{ pluginKey: Plugin['key'] }> { + let source = '' + const baseName = `${name}${extName}` as const + const path = this.resolvePath({ baseName, mode, pluginKey, options }) + + if (!path) { + throw new Error(`Filepath should be defined for resolvedName "${name}" and pluginKey [${JSON.stringify(pluginKey)}]`) + } + + try { + source = readSync(path) + } catch (_e) { + // + } + + return { + path, + baseName, + meta: { + pluginKey, + }, + source, + } + } + resolvePath = (params: ResolvePathParams): KubbFile.OptionalPath => { if (params.pluginKey) { const paths = this.hookForPluginSync({ diff --git a/packages/react/src/client/createRoot.ts b/packages/react/src/client/createRoot.ts index 49adac7d8..d6cc9a9f7 100644 --- a/packages/react/src/client/createRoot.ts +++ b/packages/react/src/client/createRoot.ts @@ -2,7 +2,7 @@ import { createNode } from '../shared/dom.ts' import { ReactTemplate } from '../shared/ReactTemplate.tsx' import type { Logger } from '@kubb/core/logger' -import type { AppContextProps } from '../components/App.ts' +import type { AppContextProps } from '../components/App.tsx' import type { DOMElement } from '../types.ts' import type { RootType } from './types.ts' diff --git a/packages/react/src/components/index.ts b/packages/react/src/components/index.ts index 5f23e3bbe..8eadcb212 100644 --- a/packages/react/src/components/index.ts +++ b/packages/react/src/components/index.ts @@ -1,4 +1,4 @@ -export type { AppContextProps } from './App.ts' +export type { AppContextProps } from './App.tsx' export type { EditorLanguage } from './Editor.tsx' export { Editor, TypeScript } from './Editor.tsx' export { File } from './File.tsx' diff --git a/packages/react/src/hooks/useApp.ts b/packages/react/src/hooks/useApp.ts index 254c977d7..b01fe7856 100644 --- a/packages/react/src/hooks/useApp.ts +++ b/packages/react/src/hooks/useApp.ts @@ -2,7 +2,7 @@ import { useContext } from 'react' import { App } from '../components/App.tsx' -import type { AppContextProps } from '../components/App.ts' +import type { AppContextProps } from '../components/App.tsx' export function useApp = Record>(): AppContextProps { return useContext(App.Context) as AppContextProps diff --git a/packages/react/src/hooks/useFile.ts b/packages/react/src/hooks/useFile.ts index 91e0e086b..50b07dead 100644 --- a/packages/react/src/hooks/useFile.ts +++ b/packages/react/src/hooks/useFile.ts @@ -1,4 +1,3 @@ -import { readSync } from '@kubb/core/fs' import { usePluginManager } from '@kubb/react' import type { KubbFile, Plugin } from '@kubb/core' @@ -14,26 +13,5 @@ type Props = { export function useFile({ name, mode, extName, pluginKey, options }: Props): KubbFile.File<{ pluginKey: Plugin['key'] }> { const pluginManager = usePluginManager() - let source = '' - const baseName = `${name}${extName}` as const - const path = pluginManager.resolvePath({ baseName, mode, pluginKey, options }) - - if (!path) { - throw new Error(`Filepath should be defined for resolvedName "${name}" and pluginKey [${JSON.stringify(pluginKey)}]`) - } - - try { - source = readSync(path) - } catch (_e) { - // - } - - return { - path, - baseName, - meta: { - pluginKey, - }, - source, - } + return pluginManager.getFile({ name, mode, extName, pluginKey, options }) } diff --git a/packages/react/src/hooks/useMeta.ts b/packages/react/src/hooks/useMeta.ts index f2b87fba3..3cf0f64d8 100644 --- a/packages/react/src/hooks/useMeta.ts +++ b/packages/react/src/hooks/useMeta.ts @@ -1,6 +1,6 @@ import { useApp } from './useApp.ts' -import type { AppContextProps } from '../components/App.ts' +import type { AppContextProps } from '../components/App.tsx' export function useMeta = Record>(): AppContextProps['meta'] { const app = useApp() diff --git a/packages/swagger-client/src/components/Operations.tsx b/packages/swagger-client/src/components/Operations.tsx index fa2853517..36b9e307c 100644 --- a/packages/swagger-client/src/components/Operations.tsx +++ b/packages/swagger-client/src/components/Operations.tsx @@ -86,7 +86,7 @@ export function Operations({ operationsByMethod, Template = defaultTemplates.default, }: Props): KubbNode { - const oas = useOas() + const { oas } = useOas() const operations = getOperations(oas, operationsByMethod) return ( diff --git a/packages/swagger-faker/src/components/Mutation.tsx b/packages/swagger-faker/src/components/Mutation.tsx index 16e68c573..a83580921 100644 --- a/packages/swagger-faker/src/components/Mutation.tsx +++ b/packages/swagger-faker/src/components/Mutation.tsx @@ -30,7 +30,7 @@ Mutation.File = function({}: FileProps): ReactNode { const schemas = useSchemas() const pluginManager = usePluginManager() - const oas = useOas() + const { oas } = useOas() const file = useOperationFile() const builder = new FakerBuilder(options, { oas, pluginManager }) diff --git a/packages/swagger-faker/src/components/Query.tsx b/packages/swagger-faker/src/components/Query.tsx index d5d56eb54..f55f64cc6 100644 --- a/packages/swagger-faker/src/components/Query.tsx +++ b/packages/swagger-faker/src/components/Query.tsx @@ -30,7 +30,7 @@ Query.File = function({}: FileProps): ReactNode { const schemas = useSchemas() const pluginManager = usePluginManager() - const oas = useOas() + const { oas } = useOas() const file = useOperationFile() const builder = new FakerBuilder(options, { oas, pluginManager }) diff --git a/packages/swagger-ts/src/components/Mutation.tsx b/packages/swagger-ts/src/components/Mutation.tsx index 9b5ba72c1..c663b69bd 100644 --- a/packages/swagger-ts/src/components/Mutation.tsx +++ b/packages/swagger-ts/src/components/Mutation.tsx @@ -107,7 +107,7 @@ Mutation.File = function({ mode }: FileProps): ReactNode { const schemas = useSchemas() const pluginManager = usePluginManager() - const oas = useOas() + const { oas } = useOas() const file = useOperationFile() const factoryName = useOperationName({ type: 'type' }) const operation = useOperation() diff --git a/packages/swagger-ts/src/components/OasType.tsx b/packages/swagger-ts/src/components/OasType.tsx index f36ed763f..441a6bbca 100644 --- a/packages/swagger-ts/src/components/OasType.tsx +++ b/packages/swagger-ts/src/components/OasType.tsx @@ -47,7 +47,7 @@ export function OasType({ typeName, Template = defaultTemplates.default, }: Props): ReactNode { - const oas = useOas() + const { oas } = useOas() return