Skip to content

Commit

Permalink
chore: cleanup OperationGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 18, 2023
1 parent abab7bf commit a08cd67
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function useFindPetsByStatusHook<TData = FindPetsByStatusQueryResponse, T
...queryOptions,
}) as UseQueryResult<TData, TError> & { queryKey: QueryKey }

query.queryKey = queryKey
query.queryKey = queryKey as QueryKey

return query
}
6 changes: 3 additions & 3 deletions packages/swagger-client/src/builders/ClientBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useResolve as useResolveType } from '@kubb/swagger-ts'
import { ClientFunction } from '../components/index.ts'
import { pluginName } from '../plugin.ts'

import type { KubbFile, PluginManager } from '@kubb/core'
import type { PluginManager } from '@kubb/core'
import type { AppContextProps, RootType } from '@kubb/react'
import type { Operation, OperationSchemas } from '@kubb/swagger'
import type { AppMeta, Options as PluginOptions } from '../types.ts'
Expand All @@ -17,8 +17,8 @@ type Config = {
dataReturnType: PluginOptions['dataReturnType']
operation: Operation
schemas: OperationSchemas
clientPath?: KubbFile.OptionalPath
clientImportPath?: KubbFile.OptionalPath
clientPath?: PluginOptions['client']
clientImportPath?: PluginOptions['clientImportPath']
}

type ClientResult = { Component: React.ElementType }
Expand Down
36 changes: 10 additions & 26 deletions packages/swagger-client/src/generators/OperationGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,33 @@
import { URLPath } from '@kubb/core'
import { OperationGenerator as Generator, resolve } from '@kubb/swagger'
import { OperationGenerator as Generator } from '@kubb/swagger'

import { ClientBuilder } from '../builders/ClientBuilder.tsx'
import { pluginName } from '../plugin.ts'

import type { KubbFile, PluginContext, PluginManager } from '@kubb/core'
import type { ContentType, HttpMethod, Oas, Operation, OperationSchemas, ResolvePathOptions, Resolver, SkipBy } from '@kubb/swagger'
import type { KubbFile } from '@kubb/core'
import type { HttpMethod, Operation, OperationSchemas } from '@kubb/swagger'
import type { FileMeta, Options as PluginOptions } from '../types.ts'

type Options = {
pluginManager: PluginManager
clientPath?: KubbFile.OptionalPath
clientImportPath?: KubbFile.OptionalPath
dataReturnType: PluginOptions['dataReturnType']
oas: Oas
contentType?: ContentType
skipBy: SkipBy[]
resolvePath: PluginContext<ResolvePathOptions>['resolvePath']
resolveName: PluginContext['resolveName']
clientPath?: PluginOptions['client']
clientImportPath?: PluginOptions['clientImportPath']
dataReturnType: NonNullable<PluginOptions['dataReturnType']>
}

export class OperationGenerator extends Generator<Options> {
resolve(operation: Operation): Resolver {
const { resolvePath, resolveName } = this.options

return resolve({
operation,
resolveName,
resolvePath,
pluginName,
})
}

async all(paths: Record<string, Record<HttpMethod, Operation>>): Promise<KubbFile.File<FileMeta> | null> {
const { resolvePath, resolveName, oas } = this.options
const { pluginManager, oas } = this.options

const controllerName = resolveName({ name: 'operations' })
const controllerName = pluginManager.resolveName({ name: 'operations', pluginName })

if (!controllerName) {
throw new Error('controllerName should be defined')
}

const controllerId = `${controllerName}.ts` as const
const controllerFilePath = resolvePath({
const controllerFilePath = pluginManager.resolvePath({
baseName: controllerId,
pluginName,
})

if (!controllerFilePath) {
Expand Down
4 changes: 1 addition & 3 deletions packages/swagger-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
const clientPath = pathParser.resolve(root, 'client.ts')

const operationGenerator = new OperationGenerator({
oas,
pluginManager: this.pluginManager,
contentType: swaggerPlugin.api.contentType,
dataReturnType,
clientPath,
clientImportPath: options.clientImportPath,
oas,
skipBy,
resolvePath: (params) => this.resolvePath({ pluginName, ...params }),
resolveName: (params) => this.resolveName({ pluginName, ...params }),
})

const files = await operationGenerator.build()
Expand Down
35 changes: 15 additions & 20 deletions packages/swagger-faker/src/generators/OperationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@ import { OperationGenerator as Generator, resolve } from '@kubb/swagger'
import { FakerBuilder } from '../builders/index.ts'
import { pluginName } from '../plugin.ts'

import type { KubbFile, PluginContext } from '@kubb/core'
import type { ContentType, FileResolver, Oas, Operation, OperationSchemas, Resolver, SkipBy } from '@kubb/swagger'
import type { FileMeta } from '../types.ts'
import type { KubbFile } from '@kubb/core'
import type { FileResolver, Operation, OperationSchemas, Resolver } from '@kubb/swagger'
import type { FileMeta, Options as PluginOptions } from '../types.ts'

type Options = {
oas: Oas
skipBy?: SkipBy[]
contentType?: ContentType
resolvePath: PluginContext['resolvePath']
resolveName: PluginContext['resolveName']
mode: KubbFile.Mode
dateType: 'string' | 'date'
dateType: NonNullable<PluginOptions['dateType']>
}

export class OperationGenerator extends Generator<Options> {
resolve(operation: Operation): Resolver {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

return resolve({
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
pluginName,
})
}
Expand All @@ -35,15 +30,15 @@ export class OperationGenerator extends Generator<Options> {
}

async get(operation: Operation, schemas: OperationSchemas): Promise<KubbFile.File<FileMeta> | null> {
const { resolvePath, mode, resolveName, oas, dateType } = this.options
const { mode, oas, dateType, pluginManager } = this.options

const faker = this.resolve(operation)

const fileResolver: FileResolver = (name, ref) => {
// Used when a react-query type(request, response, params) has an import of a global type
const root = resolvePath({ baseName: faker.name, pluginName, options: { tag: operation.getTags()[0]?.name } })
const root = pluginManager.resolvePath({ baseName: faker.name, pluginName, options: { tag: operation.getTags()[0]?.name } })
// refs import, will always been created with the SwaggerTS plugin, our global type
const resolvedTypeId = resolvePath({
const resolvedTypeId = pluginManager.resolvePath({
baseName: `${name}.ts`,
pluginName: ref.pluginName || pluginName,
options: ref.pluginName ? { tag: operation.getTags()[0]?.name } : undefined,
Expand All @@ -58,7 +53,7 @@ export class OperationGenerator extends Generator<Options> {
.add(schemas.headerParams)
.add(schemas.response)
.add(schemas.errors)
.configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, dateType, resolveName })
.configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, dateType, resolveName: pluginManager.resolveName })
.print()

return {
Expand All @@ -79,15 +74,15 @@ export class OperationGenerator extends Generator<Options> {
}

async post(operation: Operation, schemas: OperationSchemas): Promise<KubbFile.File<FileMeta> | null> {
const { resolvePath, mode, resolveName, oas, dateType } = this.options
const { pluginManager, mode, oas, dateType } = this.options

const faker = this.resolve(operation)

const fileResolver: FileResolver = (name, ref) => {
// Used when a react-query type(request, response, params) has an import of a global type
const root = resolvePath({ baseName: faker.name, pluginName, options: { tag: operation.getTags()[0]?.name } })
const root = pluginManager.resolvePath({ baseName: faker.name, pluginName, options: { tag: operation.getTags()[0]?.name } })
// refs import, will always been created with the SwaggerTS plugin, our global type
const resolvedTypeId = resolvePath({
const resolvedTypeId = pluginManager.resolvePath({
baseName: `${name}.ts`,
pluginName: ref.pluginName || pluginName,
options: ref.pluginName ? { tag: operation.getTags()[0]?.name } : undefined,
Expand All @@ -103,7 +98,7 @@ export class OperationGenerator extends Generator<Options> {
.add(schemas.request)
.add(schemas.response)
.add(schemas.errors)
.configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName, dateType })
.configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName: pluginManager.resolveName, dateType })
.print()

return {
Expand Down
5 changes: 2 additions & 3 deletions packages/swagger-faker/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
}

const operationGenerator = new OperationGenerator({
contentType: swaggerPlugin.api.contentType,
oas,
pluginManager: this.pluginManager,
contentType: swaggerPlugin.api.contentType,
skipBy,
mode,
resolvePath: (params) => this.resolvePath({ pluginName, ...params }),
resolveName: (params) => this.resolveName({ pluginName, ...params }),
dateType,
})

Expand Down
41 changes: 18 additions & 23 deletions packages/swagger-msw/src/generators/OperationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,42 @@ import { resolve as resolveSwaggerFaker, pluginName as swaggerFakerPluginName }
import { MSWBuilder } from '../builders/index.ts'
import { pluginName } from '../plugin.ts'

import type { KubbFile, PluginContext, PluginManager } from '@kubb/core'
import type { ContentType, HttpMethod, Oas, Operation, OperationSchemas, Resolver, SkipBy } from '@kubb/swagger'
import type { KubbFile } from '@kubb/core'
import type { HttpMethod, Operation, OperationSchemas, Resolver } from '@kubb/swagger'
import type { FileMeta } from '../types.ts'

type Options = {
pluginManager: PluginManager
oas: Oas
skipBy?: SkipBy[]
contentType?: ContentType
resolvePath: PluginContext['resolvePath']
resolveName: PluginContext['resolveName']
}
// eslint-disable-next-line @typescript-eslint/ban-types
type Options = {}

export class OperationGenerator extends Generator<Options> {
resolve(operation: Operation): Resolver {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

return resolve({
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
pluginName,
})
}

resolveFaker(operation: Operation): Resolver | null {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

return resolveSwaggerFaker({
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
})
}

async all(paths: Record<string, Record<HttpMethod, Operation>>): Promise<KubbFile.File<FileMeta> | null> {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

const controllerFileName = `handlers.ts`
const controllerFilePath = resolvePath({
const controllerFilePath = pluginManager.resolvePath({
baseName: controllerFileName,
pluginName,
})

if (!controllerFilePath) {
Expand All @@ -59,7 +54,7 @@ export class OperationGenerator extends Generator<Options> {

const addOperationToHandler = (operation: Operation) => {
if (operation) {
const name = resolveName({ name: `${operation.getOperationId()}` })
const name = pluginManager.resolveName({ name: `${operation.getOperationId()}`, pluginName })

const msw = this.resolve(operation)

Expand Down Expand Up @@ -106,9 +101,9 @@ export class OperationGenerator extends Generator<Options> {
}

async get(operation: Operation, schemas: OperationSchemas): Promise<KubbFile.File<FileMeta> | null> {
const { pluginManager, resolveName, oas } = this.options
const { pluginManager, oas } = this.options

const responseName = resolveName({ name: schemas.response.name, pluginName: swaggerFakerPluginName })
const responseName = pluginManager.resolveName({ name: schemas.response.name, pluginName: swaggerFakerPluginName })

const mswBuilder = new MSWBuilder(oas).configure({ pluginManager, schemas, responseName, operation })

Expand All @@ -131,9 +126,9 @@ export class OperationGenerator extends Generator<Options> {
}

async post(operation: Operation, schemas: OperationSchemas): Promise<KubbFile.File<FileMeta> | null> {
const { pluginManager, resolveName, oas } = this.options
const { pluginManager, oas } = this.options

const responseName = resolveName({ name: schemas.response.name, pluginName: swaggerFakerPluginName })
const responseName = pluginManager.resolveName({ name: schemas.response.name, pluginName: swaggerFakerPluginName })

const mswBuilder = new MSWBuilder(oas).configure({
pluginManager,
Expand Down
4 changes: 1 addition & 3 deletions packages/swagger-msw/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
const oas = await swaggerPlugin.api.getOas()

const operationGenerator = new OperationGenerator({
oas,
pluginManager: this.pluginManager,
contentType: swaggerPlugin.api.contentType,
oas,
skipBy,
resolvePath: (params) => this.resolvePath({ pluginName, ...params }),
resolveName: (params) => this.resolveName({ pluginName, ...params }),
})

const files = await operationGenerator.build()
Expand Down
38 changes: 16 additions & 22 deletions packages/swagger-swr/src/generators/OperationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,51 @@ import { resolve as resolveSwaggerTypescript, pluginName as swaggerTypescriptPlu
import { QueryBuilder } from '../builders/QueryBuilder.tsx'
import { pluginName } from '../plugin.ts'

import type { KubbFile, PluginContext, PluginManager } from '@kubb/core'
import type { ContentType, Oas, Operation, OperationSchema, OperationSchemas, ResolvePathOptions, Resolver, SkipBy } from '@kubb/swagger'
import type { KubbFile } from '@kubb/core'
import type { Operation, OperationSchema, OperationSchemas, Resolver } from '@kubb/swagger'
import type { FileMeta, Options as PluginOptions } from '../types.ts'

type Options = {
pluginManager: PluginManager
clientPath?: KubbFile.OptionalPath
clientImportPath?: KubbFile.OptionalPath
dataReturnType: PluginOptions['dataReturnType']
oas: Oas
contentType?: ContentType
skipBy?: SkipBy[]
resolvePath: PluginContext<ResolvePathOptions>['resolvePath']
resolveName: PluginContext['resolveName']
clientPath?: NonNullable<PluginOptions['client']>
clientImportPath?: NonNullable<PluginOptions['clientImportPath']>
dataReturnType: NonNullable<PluginOptions['dataReturnType']>
}

export class OperationGenerator extends Generator<Options> {
resolve(operation: Operation): Resolver {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

const name = resolveName({ name: `use ${operation.getOperationId()}`, pluginName })
const name = pluginManager.resolveName({ name: `use ${operation.getOperationId()}`, pluginName })

return resolve({
name,
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
pluginName,
})
}

resolveType(operation: Operation): Resolver {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

return resolveSwaggerTypescript({
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
})
}

resolveError(operation: Operation, statusCode: number): Resolver {
const { resolvePath, resolveName } = this.options
const { pluginManager } = this.options

const name = resolveName({ name: `${operation.getOperationId()} ${statusCode}`, pluginName: swaggerTypescriptPluginName })
const name = pluginManager.resolveName({ name: `${operation.getOperationId()} ${statusCode}`, pluginName: swaggerTypescriptPluginName })

return resolveSwaggerTypescript({
name,
operation,
resolveName,
resolvePath,
resolveName: pluginManager.resolveName,
resolvePath: pluginManager.resolvePath,
})
}

Expand Down
Loading

1 comment on commit a08cd67

@vercel
Copy link

@vercel vercel bot commented on a08cd67 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kubb – ./

www.kubb.dev
kubb-git-main-kubb.vercel.app
kubb-kubb.vercel.app
kubb.dev

Please sign in to comment.