From a08cd674170515f66187c9dbb3be2daa6f21bfc2 Mon Sep 17 00:00:00 2001 From: Stijn Van Hulle Date: Wed, 18 Oct 2023 18:31:29 +0200 Subject: [PATCH] chore: cleanup OperationGenerator --- .../src/gen/hooks/useFindPetsByStatusHook.ts | 2 +- .../src/builders/ClientBuilder.tsx | 6 +-- .../src/generators/OperationGenerator.ts | 36 ++++--------- packages/swagger-client/src/plugin.ts | 4 +- .../src/generators/OperationGenerator.ts | 35 ++++++------ packages/swagger-faker/src/plugin.ts | 5 +- .../src/generators/OperationGenerator.ts | 41 +++++++------- packages/swagger-msw/src/plugin.ts | 4 +- .../src/generators/OperationGenerator.ts | 38 ++++++------- packages/swagger-swr/src/plugin.ts | 4 +- .../src/generators/OperationGenerator.ts | 54 ++++++++----------- packages/swagger-tanstack-query/src/plugin.ts | 4 +- .../src/generators/OperationGenerator.test.ts | 12 +++-- .../src/generators/OperationGenerator.ts | 53 ++++++++++-------- packages/swagger-ts/src/plugin.ts | 5 +- .../src/generators/OperationGenerator.ts | 31 +++++------ packages/swagger-zod/src/plugin.ts | 5 +- .../src/generators/OperationGenerator.ts | 53 +++++++++--------- packages/swagger-zodios/src/plugin.ts | 7 ++- .../src/generators/OperationGenerator.test.ts | 14 ++++- .../src/generators/OperationGenerator.ts | 16 +++--- 21 files changed, 192 insertions(+), 237 deletions(-) diff --git a/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts b/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts index bec6f0121..8db08d3da 100644 --- a/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts +++ b/examples/react-query-v5/src/gen/hooks/useFindPetsByStatusHook.ts @@ -45,7 +45,7 @@ export function useFindPetsByStatusHook & { queryKey: QueryKey } - query.queryKey = queryKey + query.queryKey = queryKey as QueryKey return query } diff --git a/packages/swagger-client/src/builders/ClientBuilder.tsx b/packages/swagger-client/src/builders/ClientBuilder.tsx index c80c4992b..ba0dcf3e1 100644 --- a/packages/swagger-client/src/builders/ClientBuilder.tsx +++ b/packages/swagger-client/src/builders/ClientBuilder.tsx @@ -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' @@ -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 } diff --git a/packages/swagger-client/src/generators/OperationGenerator.ts b/packages/swagger-client/src/generators/OperationGenerator.ts index 27dbd9654..6b6b6f9fb 100644 --- a/packages/swagger-client/src/generators/OperationGenerator.ts +++ b/packages/swagger-client/src/generators/OperationGenerator.ts @@ -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['resolvePath'] - resolveName: PluginContext['resolveName'] + clientPath?: PluginOptions['client'] + clientImportPath?: PluginOptions['clientImportPath'] + dataReturnType: NonNullable } export class OperationGenerator extends Generator { - resolve(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options - - return resolve({ - operation, - resolveName, - resolvePath, - pluginName, - }) - } - async all(paths: Record>): Promise | 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) { diff --git a/packages/swagger-client/src/plugin.ts b/packages/swagger-client/src/plugin.ts index 3970387b2..c7bc41a92 100644 --- a/packages/swagger-client/src/plugin.ts +++ b/packages/swagger-client/src/plugin.ts @@ -60,15 +60,13 @@ export const definePlugin = createPlugin((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() diff --git a/packages/swagger-faker/src/generators/OperationGenerator.ts b/packages/swagger-faker/src/generators/OperationGenerator.ts index aa7745602..b27f074a2 100644 --- a/packages/swagger-faker/src/generators/OperationGenerator.ts +++ b/packages/swagger-faker/src/generators/OperationGenerator.ts @@ -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 } export class OperationGenerator extends Generator { 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, }) } @@ -35,15 +30,15 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | 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, @@ -58,7 +53,7 @@ export class OperationGenerator extends Generator { .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 { @@ -79,15 +74,15 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | 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, @@ -103,7 +98,7 @@ export class OperationGenerator extends Generator { .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 { diff --git a/packages/swagger-faker/src/plugin.ts b/packages/swagger-faker/src/plugin.ts index 55e37d2a6..99b890186 100644 --- a/packages/swagger-faker/src/plugin.ts +++ b/packages/swagger-faker/src/plugin.ts @@ -160,12 +160,11 @@ export const definePlugin = createPlugin((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, }) diff --git a/packages/swagger-msw/src/generators/OperationGenerator.ts b/packages/swagger-msw/src/generators/OperationGenerator.ts index 3f2f7f225..7679f21f0 100644 --- a/packages/swagger-msw/src/generators/OperationGenerator.ts +++ b/packages/swagger-msw/src/generators/OperationGenerator.ts @@ -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 { 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>): Promise | 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) { @@ -59,7 +54,7 @@ export class OperationGenerator extends Generator { 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) @@ -106,9 +101,9 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | 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 }) @@ -131,9 +126,9 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | 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, diff --git a/packages/swagger-msw/src/plugin.ts b/packages/swagger-msw/src/plugin.ts index c11ac774a..76a922ac6 100644 --- a/packages/swagger-msw/src/plugin.ts +++ b/packages/swagger-msw/src/plugin.ts @@ -67,12 +67,10 @@ export const definePlugin = createPlugin((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() diff --git a/packages/swagger-swr/src/generators/OperationGenerator.ts b/packages/swagger-swr/src/generators/OperationGenerator.ts index 60aef7f68..bfa491fd8 100644 --- a/packages/swagger-swr/src/generators/OperationGenerator.ts +++ b/packages/swagger-swr/src/generators/OperationGenerator.ts @@ -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['resolvePath'] - resolveName: PluginContext['resolveName'] + clientPath?: NonNullable + clientImportPath?: NonNullable + dataReturnType: NonNullable } export class OperationGenerator extends Generator { 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, }) } diff --git a/packages/swagger-swr/src/plugin.ts b/packages/swagger-swr/src/plugin.ts index 07f912818..94542e079 100644 --- a/packages/swagger-swr/src/plugin.ts +++ b/packages/swagger-swr/src/plugin.ts @@ -60,15 +60,13 @@ export const definePlugin = createPlugin((options) => { const clientPath: KubbFile.OptionalPath = options.client ? pathParser.resolve(this.config.root, options.client) : undefined const operationGenerator = new OperationGenerator({ + oas, pluginManager: this.pluginManager, contentType: swaggerPlugin.api.contentType, dataReturnType, clientImportPath: options.clientImportPath, clientPath, - oas, skipBy, - resolvePath: (params) => this.resolvePath({ pluginName, ...params }), - resolveName: (params) => this.resolveName({ pluginName, ...params }), }) const files = await operationGenerator.build() diff --git a/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts b/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts index d555553e7..f236793f2 100644 --- a/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts +++ b/packages/swagger-tanstack-query/src/generators/OperationGenerator.ts @@ -5,32 +5,24 @@ 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 { FileMeta, Framework, FrameworkImports, Options as PluginOptions } from '../types.ts' +import type { KubbFile } from '@kubb/core' +import type { Operation, OperationSchema, OperationSchemas, Resolver } from '@kubb/swagger' +import type { FileMeta, FrameworkImports, Options as PluginOptions } from '../types.ts' type Options = { - pluginManager: PluginManager - framework: Framework - clientPath?: KubbFile.OptionalPath - clientImportPath?: KubbFile.OptionalPath - dataReturnType: PluginOptions['dataReturnType'] - oas: Oas - contentType?: ContentType - skipBy: SkipBy[] - resolvePath: PluginContext['resolvePath'] - resolveName: PluginContext['resolveName'] + framework: NonNullable + clientPath?: PluginOptions['client'] + clientImportPath?: PluginOptions['clientImportPath'] + dataReturnType: NonNullable /** * Only used of infinite */ - infinite?: { - queryParam?: string - } + infinite?: PluginOptions['infinite'] } export class OperationGenerator extends Generator { resolve(operation: Operation): Resolver { - const { resolvePath, resolveName, framework } = this.options + const { pluginManager, framework } = this.options const imports = this.getFrameworkSpecificImports(framework) const name = imports.getName(operation) @@ -38,32 +30,32 @@ export class OperationGenerator extends Generator { 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, }) } @@ -79,13 +71,13 @@ export class OperationGenerator extends Generator { } getFrameworkSpecificImports(framework: Options['framework']): FrameworkImports { - const { resolveName } = this.options + const { pluginManager } = this.options const isV5 = new PackageManager().isValidSync('@tanstack/react-query', '>=5') if (framework === 'svelte') { return { - getName: (operation) => resolveName({ name: `${operation.getOperationId()} query` }), + getName: (operation) => pluginManager.resolveName({ name: `${operation.getOperationId()} query`, pluginName }), query: { useQuery: 'createQuery', QueryKey: 'QueryKey', @@ -107,7 +99,7 @@ export class OperationGenerator extends Generator { if (framework === 'solid') { return { - getName: (operation) => resolveName({ name: `${operation.getOperationId()} query` }), + getName: (operation) => pluginManager.resolveName({ name: `${operation.getOperationId()} query`, pluginName }), query: { useQuery: 'createQuery', QueryKey: 'QueryKey', @@ -129,7 +121,7 @@ export class OperationGenerator extends Generator { if (framework === 'vue') { return { - getName: (operation) => resolveName({ name: `use ${operation.getOperationId()}` }), + getName: (operation) => pluginManager.resolveName({ name: `use ${operation.getOperationId()}`, pluginName }), query: { useQuery: 'useQuery', QueryKey: 'QueryKey', @@ -150,7 +142,7 @@ export class OperationGenerator extends Generator { } return { - getName: (operation) => resolveName({ name: `use ${operation.getOperationId()}` }), + getName: (operation) => pluginManager.resolveName({ name: `use ${operation.getOperationId()}`, pluginName }), query: { useQuery: 'useQuery', QueryKey: 'QueryKey', diff --git a/packages/swagger-tanstack-query/src/plugin.ts b/packages/swagger-tanstack-query/src/plugin.ts index 5ce9fad43..fce03db3b 100644 --- a/packages/swagger-tanstack-query/src/plugin.ts +++ b/packages/swagger-tanstack-query/src/plugin.ts @@ -59,6 +59,7 @@ export const definePlugin = createPlugin((options) => { const clientPath: KubbFile.OptionalPath = options.client ? pathParser.resolve(this.config.root, options.client) : undefined const operationGenerator = new OperationGenerator({ + oas, pluginManager: this.pluginManager, contentType: swaggerPlugin.api.contentType, dataReturnType, @@ -67,9 +68,6 @@ export const definePlugin = createPlugin((options) => { skipBy, clientPath, clientImportPath: options.clientImportPath, - oas, - resolvePath: (params) => this.resolvePath({ pluginName, ...params }), - resolveName: (params) => this.resolveName({ pluginName, ...params }), }) const files = await operationGenerator.build() diff --git a/packages/swagger-ts/src/generators/OperationGenerator.test.ts b/packages/swagger-ts/src/generators/OperationGenerator.test.ts index ae5f8dc49..2ca3ffc7e 100644 --- a/packages/swagger-ts/src/generators/OperationGenerator.test.ts +++ b/packages/swagger-ts/src/generators/OperationGenerator.test.ts @@ -3,7 +3,7 @@ import { oasParser } from '@kubb/swagger' import { format } from '../../mocks/format.ts' import { OperationGenerator } from './OperationGenerator.ts' -import type { PluginContext } from '@kubb/core' +import type { PluginContext, PluginManager } from '@kubb/core' describe('OperationGenerator', () => { const resolvePath = () => './pets.ts' @@ -13,8 +13,9 @@ describe('OperationGenerator', () => { const oas = await oasParser({ root: './', output: { path: 'test', clean: true }, input: { path: 'packages/swagger-ts/mocks/petStore.yaml' } }) const og = await new OperationGenerator({ oas, - resolvePath, - resolveName, + skipBy: [], + contentType: undefined, + pluginManager: { resolvePath, resolveName } as unknown as PluginManager, enumType: 'asConst', mode: 'directory', dateType: 'string', @@ -80,8 +81,9 @@ describe('OperationGenerator', () => { const oas = await oasParser({ root: './', output: { path: 'test', clean: true }, input: { path: 'packages/swagger-ts/mocks/petStore.yaml' } }) const og = await new OperationGenerator({ oas, - resolvePath, - resolveName, + skipBy: [], + contentType: undefined, + pluginManager: { resolvePath, resolveName } as unknown as PluginManager, enumType: 'asConst', mode: 'directory', dateType: 'string', diff --git a/packages/swagger-ts/src/generators/OperationGenerator.ts b/packages/swagger-ts/src/generators/OperationGenerator.ts index c149a4e26..14d7bb9d0 100644 --- a/packages/swagger-ts/src/generators/OperationGenerator.ts +++ b/packages/swagger-ts/src/generators/OperationGenerator.ts @@ -4,30 +4,25 @@ import { OperationGenerator as Generator, resolve } from '@kubb/swagger' import { TypeBuilder } 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 - contentType?: ContentType - skipBy?: SkipBy[] - resolvePath: PluginContext['resolvePath'] - resolveName: PluginContext['resolveName'] mode: KubbFile.Mode - enumType: 'enum' | 'asConst' | 'asPascalConst' - dateType: 'string' | 'date' - optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined' + enumType: NonNullable + dateType: NonNullable + optionalType: NonNullable } export class OperationGenerator extends Generator { 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, }) } @@ -37,15 +32,15 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { resolvePath, mode, resolveName, oas, enumType, dateType, optionalType } = this.options + const { pluginManager, mode, oas, enumType, dateType, optionalType } = this.options const type = this.resolve(operation) const fileResolver: FileResolver = (name) => { // Used when a react-query type(request, response, params) has an import of a global type - const root = resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } }) + const root = pluginManager.resolvePath({ baseName: type.baseName, 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, }) @@ -59,7 +54,14 @@ export class OperationGenerator extends Generator { .add(schemas.headerParams) .add(schemas.response) .add(schemas.errors) - .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName, enumType, optionalType, dateType }) + .configure({ + fileResolver: mode === 'file' ? undefined : fileResolver, + withJSDocs: true, + resolveName: pluginManager.resolveName, + enumType, + optionalType, + dateType, + }) .print() return { @@ -74,15 +76,15 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { resolvePath, mode, resolveName, oas, enumType, dateType, optionalType } = this.options + const { pluginManager, mode, oas, enumType, dateType, optionalType } = this.options const type = this.resolve(operation) const fileResolver: FileResolver = (name) => { // Used when a react-query type(request, response, params) has an import of a global type - const root = resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } }) + const root = pluginManager.resolvePath({ baseName: type.baseName, 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, }) @@ -97,7 +99,14 @@ export class OperationGenerator extends Generator { .add(schemas.request) .add(schemas.response) .add(schemas.errors) - .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName, enumType, optionalType, dateType }) + .configure({ + fileResolver: mode === 'file' ? undefined : fileResolver, + withJSDocs: true, + resolveName: pluginManager.resolveName, + enumType, + optionalType, + dateType, + }) .print() return { diff --git a/packages/swagger-ts/src/plugin.ts b/packages/swagger-ts/src/plugin.ts index 917a4baea..523d28d2d 100644 --- a/packages/swagger-ts/src/plugin.ts +++ b/packages/swagger-ts/src/plugin.ts @@ -149,12 +149,11 @@ export const definePlugin = createPlugin((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 }), enumType, dateType, optionalType, diff --git a/packages/swagger-zod/src/generators/OperationGenerator.ts b/packages/swagger-zod/src/generators/OperationGenerator.ts index 6391c80db..fbe8ffcbd 100644 --- a/packages/swagger-zod/src/generators/OperationGenerator.ts +++ b/packages/swagger-zod/src/generators/OperationGenerator.ts @@ -4,27 +4,22 @@ import { OperationGenerator as Generator, resolve } from '@kubb/swagger' import { ZodBuilder } 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 { KubbFile } from '@kubb/core' +import type { FileResolver, Operation, OperationSchemas, Resolver } from '@kubb/swagger' import type { FileMeta } from '../types.ts' type Options = { - oas: Oas - contentType?: ContentType - skipBy?: SkipBy[] - resolvePath: PluginContext['resolvePath'] - resolveName: PluginContext['resolveName'] mode: KubbFile.Mode } export class OperationGenerator extends Generator { 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, }) } @@ -34,15 +29,15 @@ export class OperationGenerator extends Generator { } async get(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { resolvePath, mode, resolveName, oas } = this.options + const { pluginManager, mode, oas } = this.options const zod = this.resolve(operation) const fileResolver: FileResolver = (name) => { // Used when a react-query type(request, response, params) has an import of a global type - const root = resolvePath({ baseName: zod.name, pluginName, options: { tag: operation.getTags()[0]?.name } }) + const root = pluginManager.resolvePath({ baseName: zod.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, }) @@ -56,7 +51,7 @@ export class OperationGenerator extends Generator { .add(schemas.headerParams) .add(schemas.response) .add(schemas.errors) - .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName }) + .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName: pluginManager.resolveName }) .print() return { @@ -77,15 +72,15 @@ export class OperationGenerator extends Generator { } async post(operation: Operation, schemas: OperationSchemas): Promise | null> { - const { resolvePath, mode, resolveName, oas } = this.options + const { mode, pluginManager, oas } = this.options const zod = this.resolve(operation) const fileResolver: FileResolver = (name) => { // Used when a react-query type(request, response, params) has an import of a global type - const root = resolvePath({ baseName: zod.name, pluginName, options: { tag: operation.getTags()[0]?.name } }) + const root = pluginManager.resolvePath({ baseName: zod.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, }) @@ -100,7 +95,7 @@ export class OperationGenerator extends Generator { .add(schemas.request) .add(schemas.response) .add(schemas.errors) - .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName }) + .configure({ fileResolver: mode === 'file' ? undefined : fileResolver, withJSDocs: true, resolveName: pluginManager.resolveName }) .print() return { diff --git a/packages/swagger-zod/src/plugin.ts b/packages/swagger-zod/src/plugin.ts index af4533e65..43f985a96 100644 --- a/packages/swagger-zod/src/plugin.ts +++ b/packages/swagger-zod/src/plugin.ts @@ -157,12 +157,11 @@ export const definePlugin = createPlugin((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 }), }) const files = await operationGenerator.build() diff --git a/packages/swagger-zodios/src/generators/OperationGenerator.ts b/packages/swagger-zodios/src/generators/OperationGenerator.ts index e32fa9b77..4c869f780 100644 --- a/packages/swagger-zodios/src/generators/OperationGenerator.ts +++ b/packages/swagger-zodios/src/generators/OperationGenerator.ts @@ -6,15 +6,10 @@ import { camelCase, camelCaseTransformMerge } from 'change-case' import { pluginName } from '../plugin.ts' -import type { KubbFile, PluginContext } from '@kubb/core' -import type { ContentType, HttpMethod, Oas, OpenAPIV3, Operation, Resolver, SkipBy } from '@kubb/swagger' +import type { KubbFile } from '@kubb/core' +import type { HttpMethod, OpenAPIV3, Operation, Resolver } from '@kubb/swagger' type Options = { - oas: Oas - skipBy?: SkipBy[] - contentType?: ContentType - resolvePath: PluginContext['resolvePath'] - resolveName: PluginContext['resolveName'] output: string } @@ -22,26 +17,26 @@ const methods: HttpMethod[] = ['get', 'post', 'patch', 'put', 'delete'] export class OperationGenerator extends Generator { resolve(): Resolver { - const { resolvePath, output, resolveName } = this.options + const { pluginManager, output } = this.options - const name = resolveName({ name: output.replace('.ts', ''), pluginName }) + const name = pluginManager.resolveName({ name: output.replace('.ts', ''), pluginName }) return resolve({ name, - resolveName, - resolvePath, + resolveName: pluginManager.resolveName, + resolvePath: pluginManager.resolvePath, pluginName, }) } resolveResponse(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options const schemas = this.getSchemas(operation) - const name = resolveName({ name: schemas.response.name, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: schemas.response.name, pluginName: swaggerZodPluginName }) const baseName: KubbFile.BaseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, @@ -59,7 +54,7 @@ export class OperationGenerator extends Generator { } resolveRequest(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options const schemas = this.getSchemas(operation) @@ -67,9 +62,9 @@ export class OperationGenerator extends Generator { throw new Error('schemas.request should be defined') } - const name = resolveName({ name: schemas.request.name, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: schemas.request.name, pluginName: swaggerZodPluginName }) const baseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` as const - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, @@ -87,16 +82,16 @@ export class OperationGenerator extends Generator { } resolveHeaderParams(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options const schemas = this.getSchemas(operation) if (!schemas.headerParams?.name) { throw new Error('schemas.pathParams should be defined') } - const name = resolveName({ name: schemas.headerParams.name, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: schemas.headerParams.name, pluginName: swaggerZodPluginName }) const baseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` as const - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, @@ -114,16 +109,16 @@ export class OperationGenerator extends Generator { } resolvePathParams(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options const schemas = this.getSchemas(operation) if (!schemas.pathParams?.name) { throw new Error('schemas.pathParams should be defined') } - const name = resolveName({ name: schemas.pathParams.name, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: schemas.pathParams.name, pluginName: swaggerZodPluginName }) const baseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` as const - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, @@ -141,7 +136,7 @@ export class OperationGenerator extends Generator { } resolveQueryParams(operation: Operation): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options const schemas = this.getSchemas(operation) @@ -149,9 +144,9 @@ export class OperationGenerator extends Generator { throw new Error('schemas.queryParams should be defined') } - const name = resolveName({ name: schemas.queryParams.name, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: schemas.queryParams.name, pluginName: swaggerZodPluginName }) const baseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` as const - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, @@ -169,11 +164,11 @@ export class OperationGenerator extends Generator { } resolveError(operation: Operation, statusCode: number): Resolver { - const { resolvePath, resolveName } = this.options + const { pluginManager } = this.options - const name = resolveName({ name: `${operation.getOperationId()} ${statusCode}`, pluginName: swaggerZodPluginName }) + const name = pluginManager.resolveName({ name: `${operation.getOperationId()} ${statusCode}`, pluginName: swaggerZodPluginName }) const baseName = `${camelCase(`${operation.getOperationId()}Schema`, { delimiter: '', transform: camelCaseTransformMerge })}.ts` as const - const filePath = resolvePath({ + const filePath = pluginManager.resolvePath({ baseName: baseName, options: { tag: operation.getTags()[0]?.name }, pluginName: swaggerZodPluginName, diff --git a/packages/swagger-zodios/src/plugin.ts b/packages/swagger-zodios/src/plugin.ts index f6be30215..bcece0d81 100644 --- a/packages/swagger-zodios/src/plugin.ts +++ b/packages/swagger-zodios/src/plugin.ts @@ -40,12 +40,11 @@ export const definePlugin = createPlugin((options) => { const oas = await swaggerPlugin.api.getOas() const operationGenerator = new OperationGenerator({ - contentType: swaggerPlugin.api.contentType, oas, - output, + pluginManager: this.pluginManager, + contentType: swaggerPlugin.api.contentType, skipBy: swaggerZodPlugin.options.skipBy, - resolvePath: (params) => this.resolvePath({ pluginName, ...params }), - resolveName: (params) => this.resolveName({ pluginName, ...params }), + output, }) const files = await operationGenerator.build() diff --git a/packages/swagger/src/generators/OperationGenerator.test.ts b/packages/swagger/src/generators/OperationGenerator.test.ts index 07a10d352..38de6162e 100644 --- a/packages/swagger/src/generators/OperationGenerator.test.ts +++ b/packages/swagger/src/generators/OperationGenerator.test.ts @@ -1,7 +1,7 @@ import { oasParser } from '../parsers/oasParser.ts' import { OperationGenerator } from './OperationGenerator.ts' -import type { KubbFile } from '@kubb/core' +import type { KubbFile, PluginManager } from '@kubb/core' import type { Operation, Resolver } from '../types.ts' class DummyOperationGenerator extends OperationGenerator { @@ -56,7 +56,7 @@ describe('abstract class OperationGenerator', () => { input: { path: 'packages/swagger/mocks/petStore.yaml' }, }) - const og = new DummyOperationGenerator({ oas }) + const og = new DummyOperationGenerator({ oas, contentType: undefined, pluginManager: undefined as unknown as PluginManager, skipBy: [] }) expect(og.getSchemas(oas.operation('/pets', 'get')).pathParams).toBeUndefined() expect(og.getSchemas(oas.operation('/pets', 'get')).queryParams).toBeDefined() @@ -71,6 +71,8 @@ describe('abstract class OperationGenerator', () => { const og = new DummyOperationGenerator({ oas, + contentType: undefined, + pluginManager: undefined as unknown as PluginManager, skipBy: [ { type: 'tag', @@ -93,6 +95,8 @@ describe('abstract class OperationGenerator', () => { const og = new DummyOperationGenerator({ oas, + contentType: undefined, + pluginManager: undefined as unknown as PluginManager, skipBy: [ { type: 'operationId', @@ -121,6 +125,8 @@ describe('abstract class OperationGenerator', () => { const og = new DummyOperationGenerator({ oas, + contentType: undefined, + pluginManager: undefined as unknown as PluginManager, skipBy: [ { type: 'path', @@ -149,6 +155,8 @@ describe('abstract class OperationGenerator', () => { const og = new DummyOperationGenerator({ oas, + contentType: undefined, + pluginManager: undefined as unknown as PluginManager, skipBy: [ { type: 'method', @@ -171,6 +179,8 @@ describe('abstract class OperationGenerator', () => { const og = new DummyOperationGenerator({ oas, + contentType: undefined, + pluginManager: undefined as unknown as PluginManager, skipBy: [ { type: 'path', diff --git a/packages/swagger/src/generators/OperationGenerator.ts b/packages/swagger/src/generators/OperationGenerator.ts index 9f5f3311c..b6276868e 100644 --- a/packages/swagger/src/generators/OperationGenerator.ts +++ b/packages/swagger/src/generators/OperationGenerator.ts @@ -6,19 +6,20 @@ import { findSchemaDefinition } from 'oas/utils' import { isReference } from '../utils/isReference.ts' -import type { KubbFile } from '@kubb/core' +import type { KubbFile, PluginManager } from '@kubb/core' import type Operation from 'oas/operation' import type { HttpMethods as HttpMethod, MediaTypeObject, RequestBodyObject } from 'oas/rmoas.types' import type { OpenAPIV3 } from 'openapi-types' -import type { ContentType, Oas, OperationSchemas, Resolver, SkipBy } from '../types.ts' +import type { ContentType, Oas, OperationSchemas, SkipBy } from '../types.ts' type Options = { oas: Oas - skipBy?: SkipBy[] - contentType?: ContentType + skipBy: Array | undefined + contentType: ContentType | undefined + pluginManager: PluginManager } -export abstract class OperationGenerator extends Generator { +export abstract class OperationGenerator extends Generator { /** * * Validate an operation to see if used with camelCase we don't overwrite other files @@ -347,9 +348,4 @@ export abstract class OperationGenerator ext * Combination of GET, POST, PATCH, PUT, DELETE */ abstract all(paths: Record>): Promise - - /** - * Call resolveType and get back the name, filePath and baseName - */ - abstract resolve(operation: Operation): Resolver }