Skip to content

Commit

Permalink
fix: use of useOperationHelpers to abstract operation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 9, 2024
1 parent 607ccbc commit fb25317
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 70 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-dots-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-zod": patch
---

use of `useOperationHelpers` to abstract operation logic
2 changes: 1 addition & 1 deletion docs/plugins/swagger/hooks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 34 additions & 0 deletions packages/core/src/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -73,6 +74,14 @@ type Events = {
error: [error: Error]
}

type GetFileProps<TOptions = object> = {
name: string
mode?: KubbFile.Mode
extName: KubbFile.Extname
pluginKey: Plugin['key']
options?: TOptions
}

export class PluginManager {
readonly plugins: PluginWithLifeCycle[]
readonly fileManager: FileManager
Expand Down Expand Up @@ -118,6 +127,31 @@ export class PluginManager {
return this
}

getFile<TOptions = object>({ name, mode, extName, pluginKey, options }: GetFileProps<TOptions>): 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 = <TOptions = object>(params: ResolvePathParams<TOptions>): KubbFile.OptionalPath => {
if (params.pluginKey) {
const paths = this.hookForPluginSync({
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/client/createRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Meta extends Record<string, unknown> = Record<string, unknown>>(): AppContextProps<Meta> {
return useContext(App.Context) as AppContextProps<Meta>
Expand Down
24 changes: 1 addition & 23 deletions packages/react/src/hooks/useFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readSync } from '@kubb/core/fs'
import { usePluginManager } from '@kubb/react'

import type { KubbFile, Plugin } from '@kubb/core'
Expand All @@ -14,26 +13,5 @@ type Props<TOptions = object> = {
export function useFile<TOptions = object>({ name, mode, extName, pluginKey, options }: Props<TOptions>): 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<TOptions>({ name, mode, extName, pluginKey, options })
}
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useMeta.ts
Original file line number Diff line number Diff line change
@@ -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<Meta extends Record<string, unknown> = Record<string, unknown>>(): AppContextProps<Meta>['meta'] {
const app = useApp<Meta>()
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-client/src/components/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-faker/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-faker/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/OasType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function OasType({
typeName,
Template = defaultTemplates.default,
}: Props): ReactNode {
const oas = useOas()
const { oas } = useOas()

return <Template name={name} typeName={typeName} api={oas.api} />
}
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-ts/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Query.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()
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-zod/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Mutation.File = function({ mode = 'directory' }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const oas = useOas()
const { oas } = useOas()
const file = useOperationFile()

const builder = new ZodBuilder(options, { oas, pluginManager })
Expand Down
2 changes: 1 addition & 1 deletion packages/swagger-zod/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Query.File = function({ mode = 'directory' }: FileProps): ReactNode {

const schemas = useSchemas()
const pluginManager = usePluginManager()
const oas = useOas()
const { oas } = useOas()
const file = useOperationFile()

const builder = new ZodBuilder(options, { oas, pluginManager })
Expand Down
3 changes: 2 additions & 1 deletion packages/swagger/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { useOas } from './useOas.ts'
export { useOperation, useOperationFile, useOperationName } from './useOperation.ts'
export { useOperationHelpers } from './useOperationHelpers.ts'
export { useOperations } from './useOperations.ts'
export { useGetSchemas, useSchemas } from './useSchemas.ts'
export { useSchemas } from './useSchemas.ts'
14 changes: 10 additions & 4 deletions packages/swagger/src/hooks/useOas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { useContext } from '@kubb/react'

import { Oas } from '../components/Oas.tsx'

import type { GetSchemas } from '../components/Oas.tsx'
import type { Oas as OasType } from '../oas/index.ts'

export function useOas(): OasType {
const { oas } = useContext(Oas.Context)
type Result = {
oas: OasType
getSchemas: GetSchemas
}

export function useOas(): Result {
const { oas, getSchemas } = useContext(Oas.Context)

if (!oas) {
if (!oas || !getSchemas) {
throw new Error('Oas is not defined')
}

return oas
return { oas, getSchemas }
}
30 changes: 13 additions & 17 deletions packages/swagger/src/hooks/useOperation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useContext, useFile, usePlugin, usePluginManager } from '@kubb/react'
import { useContext, usePlugin } from '@kubb/react'

import { Operation } from '../components/Operation.tsx'
import { useOperationHelpers } from './useOperationHelpers.ts'

import type { KubbFile, Plugin, ResolveNameParams } from '@kubb/core'
import type { Operation as OperationType } from '../oas/index.ts'
import type { ResolvePathOptions } from '../types.ts'

export function useOperation(): OperationType {
const { operation } = useContext(Operation.Context)
Expand All @@ -23,12 +23,15 @@ type UseOperationNameProps = {

export function useOperationName({ type, ...rest }: UseOperationNameProps): string {
const plugin = usePlugin()
const pluginManager = usePluginManager()
const operation = useOperation()
const { getOperationName } = useOperationHelpers({ operation })

const pluginKey = rest.pluginKey || plugin.key

return pluginManager.resolveName({ name: operation.getOperationId(), pluginKey, type })
return getOperationName({
pluginKey,
type,
})
}

type FileMeta = KubbFile.FileMetaBase & {
Expand All @@ -46,20 +49,13 @@ export function useOperationFile(props: UseOperationFileProps = {}): KubbFile.Fi
const plugin = usePlugin()
const operation = useOperation()

const { getOperationFile } = useOperationHelpers({ operation })

const pluginKey = props.pluginKey || plugin.key
// needed for the `output.group`
const tag = operation?.getTags().at(0)?.name
const name = useOperationName({ type: 'file', pluginKey })
const extName = props.extName || '.ts'
const file = useFile<ResolvePathOptions>({ name, extName, pluginKey, options: { type: 'file', pluginKey, tag } })

return {
...file,
meta: {
...file.meta,
name,
pluginKey,
tag,
},
}
return getOperationFile({
pluginKey,
extName,
})
}
50 changes: 50 additions & 0 deletions packages/swagger/src/hooks/useOperationHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { usePluginManager } from '@kubb/react'

import type { KubbFile, Plugin, ResolveNameParams } from '@kubb/core'
import type { Operation as OperationType } from '../oas/index.ts'

type FileMeta = KubbFile.FileMetaBase & {
pluginKey: Plugin['key']
name: string
tag?: string
}

type UseOperationHelpersProps = {
operation: OperationType
}

type UseOperationHelpersResult = {
getOperationName: (params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
getOperationFile: (params: { pluginKey: Plugin['key']; extName?: KubbFile.Extname }) => KubbFile.File<FileMeta>
}

export function useOperationHelpers({ operation }: UseOperationHelpersProps): UseOperationHelpersResult {
const pluginManager = usePluginManager()

const getOperationName: UseOperationHelpersResult['getOperationName'] = ({ pluginKey, type }) => {
return pluginManager.resolveName({ name: operation.getOperationId(), pluginKey, type })
}

const getOperationFile: UseOperationHelpersResult['getOperationFile'] = ({ pluginKey, extName = '.ts' }) => {
// needed for the `output.group`
const tag = operation?.getTags().at(0)?.name
const name = getOperationName({ type: 'file', pluginKey })

const file = pluginManager.getFile({ name, extName, pluginKey, options: { type: 'file', pluginKey, tag } })

return {
...file,
meta: {
...file.meta,
name,
pluginKey,
tag,
},
}
}

return {
getOperationName,
getOperationFile,
}
}
2 changes: 1 addition & 1 deletion packages/swagger/src/hooks/useOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext } from '@kubb/react'

import { Oas } from '../components/Oas.tsx'

import type { Operation, HttpMethod } from '../oas/index.ts'
import type { HttpMethod, Operation } from '../oas/index.ts'

type UseOperationsProps = {
/**
Expand Down
12 changes: 1 addition & 11 deletions packages/swagger/src/hooks/useSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from '@kubb/react'

import { Oas, type GetSchemas } from '../components/Oas.tsx'
import { Oas } from '../components/Oas.tsx'
import { Operation } from '../components/Operation.tsx'

import type { OperationSchemas } from '../types.ts'
Expand All @@ -22,13 +22,3 @@ export function useSchemas(): OperationSchemas {

return schemas
}

export function useGetSchemas(): GetSchemas {
const { getSchemas } = useContext(Oas.Context)

if (!getSchemas) {
throw new Error(`getSchemas is not defined`)
}

return getSchemas
}

0 comments on commit fb25317

Please sign in to comment.