Skip to content

Commit

Permalink
fix: correct use of grouping for path and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Jan 8, 2025
1 parent 5febbe5 commit c98130b
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 47 deletions.
16 changes: 16 additions & 0 deletions .changeset/little-steaks-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@kubb/plugin-svelte-query": patch
"@kubb/plugin-react-query": patch
"@kubb/plugin-solid-query": patch
"@kubb/plugin-vue-query": patch
"@kubb/plugin-client": patch
"@kubb/plugin-faker": patch
"@kubb/plugin-msw": patch
"@kubb/plugin-oas": patch
"@kubb/plugin-swr": patch
"@kubb/plugin-zod": patch
"@kubb/plugin-ts": patch
"@kubb/core": patch
---

correct use of grouping for path and tags
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Changelog

# Changelog

## 3.4.3
- [`plugin-oas`](/plugins/plugin-oas): correct use of grouping for path and tags

## 3.4.2
- [`plugin-oas`](/plugins/plugin-oas): remove duplicated keys when set in required

Expand Down
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"clean": "npx rimraf ./dist",
"format": "prettier --write **/*.{ts,tsx}",
"generate": "kubb generate",
"generate:debug": "node --inspect ../packages/cli/bin/kubb.js",
"generate:debug": "NODE_OPTIONS='--inspect-brk' kubb generate",
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type GetFileProps<TOptions = object> = {
pluginKey: Plugin['key']
options?: TOptions
}
type GetOptionsProps<TOptions = object> = {
pluginKey: Plugin['key']
}

export class PluginManager {
readonly plugins = new Set<Plugin<GetPluginFactoryOptions<any>>>()
Expand Down Expand Up @@ -111,6 +114,14 @@ export class PluginManager {
return this
}

getOptions<TOptions = object>({ pluginKey }: GetOptionsProps<TOptions>) {
const plugins = [...this.plugins]

plugins.find((plugin) => {
return plugin.key === pluginKey
})
}

getFile<TOptions = object>({ name, mode, extname, pluginKey, options }: GetFileProps<TOptions>): KubbFile.File<{ pluginKey: Plugin['key'] }> {
const baseName = `${name}${extname}` as const
const path = this.resolvePath({ baseName, mode, pluginKey, options })
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin-client/src/generators/groupedClientGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export const groupedClientGenerator = createReactGenerator<PluginClient>({
pluginManager,
plugin: { options, key: pluginKey },
} = useApp<PluginClient>()
const { getName, getFile } = useOperationManager()
const { getName, getFile, getGroup } = useOperationManager()

const controllers = operations.reduce(
(acc, operation) => {
if (options.group?.type === 'tag') {
const tag = operation.getTags().at(0)?.name
const name = tag ? options.group?.name?.({ group: camelCase(tag) }) : undefined
const group = getGroup(operation)
const name = group?.tag ? options.group?.name?.({ group: camelCase(group.tag) }) : undefined

if (!tag || !name) {
if (!group?.tag || !name) {
return acc
}

const file = pluginManager.getFile({
name,
extname: '.ts',
pluginKey,
options: { group: tag },
options: { group },
})

const client = {
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -70,7 +70,14 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-faker/src/generators/fakerGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const fakerGenerator = createReactGenerator<PluginFaker>({
const mapOperationSchema = ({ name, schema, description, ...options }: OperationSchemaType, i: number) => {
const tree = schemaGenerator.parse({ schema, name })
const imports = schemaManager.getImports(tree)
const group = options.operation ? getGroup(options.operation, plugin.options.group) : undefined
const group = options.operation ? getGroup(options.operation) : undefined

const faker = {
name: schemaManager.getName(name, { type: 'function' }),
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-faker/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -67,7 +67,14 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-msw/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -57,7 +57,14 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
29 changes: 15 additions & 14 deletions packages/plugin-oas/src/hooks/useOperationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import type { OperationSchemas } from '../types.ts'
type FileMeta = FileMetaBase & {
pluginKey: Plugin['key']
name: string
group?: string
group?: {
tag?: string
path?: string
}
}

export type SchemaNames = {
Expand Down Expand Up @@ -42,7 +45,10 @@ type UseOperationManagerResult = {
suffix?: string
pluginKey?: Plugin['key']
extname?: KubbFile.Extname
group?: string
group?: {
tag?: string
path?: string
}
},
) => KubbFile.File<FileMeta>
groupSchemasByName: (
Expand All @@ -53,7 +59,7 @@ type UseOperationManagerResult = {
},
) => SchemaNames
getSchemas: (operation: Operation, params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] }) => OperationSchemas
getGroup: (operation: Operation, group: Group | undefined) => string | undefined
getGroup: (operation: Operation) => FileMeta['group'] | undefined
}

/**
Expand All @@ -71,13 +77,10 @@ export function useOperationManager(): UseOperationManagerResult {
})
}

const getGroup: UseOperationManagerResult['getGroup'] = (operation, group) => {
if (group?.type === 'tag') {
return operation.getTags().at(0)?.name
}

if (group?.type === 'path') {
return operation.path
const getGroup: UseOperationManagerResult['getGroup'] = (operation) => {
return {
tag: operation.getTags().at(0)?.name,
path: operation.path,
}
}

Expand All @@ -96,11 +99,9 @@ export function useOperationManager(): UseOperationManagerResult {
})
}

const getFile: UseOperationManagerResult['getFile'] = (
operation,
{ prefix, suffix, pluginKey = plugin.key, group = getGroup(operation, (plugin.options as { group?: Group })?.group), extname = '.ts' } = {},
) => {
const getFile: UseOperationManagerResult['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,
Expand Down
18 changes: 16 additions & 2 deletions packages/plugin-oas/src/hooks/useSchemaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ import { type Schema, schemaKeywords } from '../SchemaMapper'
type FileMeta = FileMetaBase & {
pluginKey: Plugin['key']
name: string
group?: string
group?: {
tag?: string
path?: string
}
}

type UseSchemaManagerResult = {
getName: (name: string, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
getFile: (name: string, params?: { pluginKey?: Plugin['key']; mode?: Mode; extname?: KubbFile.Extname; group?: string }) => KubbFile.File<FileMeta>
getFile: (
name: string,
params?: {
pluginKey?: Plugin['key']
mode?: Mode
extname?: KubbFile.Extname
group?: {
tag?: string
path?: string
}
},
) => KubbFile.File<FileMeta>
getImports: (tree: Array<Schema>) => Array<KubbFile.Import>
}

Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-oas/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import type { GetSchemasProps } from './utils/getSchemas.ts'

export type ResolvePathOptions = {
pluginKey?: Plugin['key']
group?: string
group?: {
tag?: string
path?: string
}
type?: ResolveNameParams['type']
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const queryGenerator = createReactGenerator<PluginReactQuery>({
}

const zod = {
// grouping is coming from react-query instead of zod option, we need to pass the options of zod instead
file: getFile(operation, { pluginKey: [pluginZodName] }),
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
}
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-react-query/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -99,7 +99,14 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-solid-query/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const pluginSolidQuery = createPlugin<PluginSolidQuery>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -80,7 +80,14 @@ export const pluginSolidQuery = createPlugin<PluginSolidQuery>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-svelte-query/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -88,7 +88,14 @@ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
11 changes: 9 additions & 2 deletions packages/plugin-swr/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const pluginSwr = createPlugin<PluginSwr>((options) => {
return path.resolve(root, output.path)
}

if (options?.group && group) {
if (group && (options?.group?.path || options?.group?.tag)) {
const groupName: Group['name'] = group?.name
? group.name
: (ctx) => {
Expand All @@ -88,7 +88,14 @@ export const pluginSwr = createPlugin<PluginSwr>((options) => {
return `${camelCase(ctx.group)}Controller`
}

return path.resolve(root, output.path, groupName({ group: options.group }), baseName)
return path.resolve(
root,
output.path,
groupName({
group: group.type === 'path' ? options.group.path! : options.group.tag!,
}),
baseName,
)
}

return path.resolve(root, output.path, baseName)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ts/src/generators/typeGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const typeGenerator = createReactGenerator<PluginTs>({
const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
const tree = schemaGenerator.parse({ schema, name })
const imports = schemaManager.getImports(tree)
const group = options.operation ? getGroup(options.operation, plugin.options.group) : undefined
const group = options.operation ? getGroup(options.operation) : undefined

const type = {
name: schemaManager.getName(name, { type: 'type' }),
Expand Down
Loading

0 comments on commit c98130b

Please sign in to comment.