pass IR.OperationObject as group key builder

This commit is contained in:
Stephen Zhou 2026-01-25 20:27:45 +08:00
parent 9a02cbe0d3
commit b41aee2278
No known key found for this signature in database
3 changed files with 10 additions and 8 deletions

View File

@ -1,3 +1,4 @@
import type { IR } from '@hey-api/openapi-ts'
import type { OrpcPlugin } from './types' import type { OrpcPlugin } from './types'
import { definePluginConfig } from '@hey-api/openapi-ts' import { definePluginConfig } from '@hey-api/openapi-ts'
@ -15,8 +16,8 @@ function toCamelCase(str: string): string {
// Default: extract first path segment and convert to camelCase // Default: extract first path segment and convert to camelCase
// "/chat-messages/{id}" → "chatMessages" // "/chat-messages/{id}" → "chatMessages"
function defaultGroupKeyBuilder(path: string): string { function defaultGroupKeyBuilder(operation: IR.OperationObject): string {
const segment = path.split('/').filter(Boolean)[0] || 'common' const segment = operation.path.split('/').filter(Boolean)[0] || 'common'
return toCamelCase(segment) return toCamelCase(segment)
} }

View File

@ -203,7 +203,7 @@ export const handler: OrpcPlugin['Handler'] = ({ plugin }) => {
// Group operations by group key // Group operations by group key
const operationsByGroup = new Map<string, IR.OperationObject[]>() const operationsByGroup = new Map<string, IR.OperationObject[]>()
for (const op of operations) { for (const op of operations) {
const groupKey = groupKeyBuilder(op.path as string) const groupKey = groupKeyBuilder(op)
if (!operationsByGroup.has(groupKey)) { if (!operationsByGroup.has(groupKey)) {
operationsByGroup.set(groupKey, []) operationsByGroup.set(groupKey, [])
} }

View File

@ -1,4 +1,4 @@
import type { DefinePlugin, Plugin } from '@hey-api/openapi-ts' import type { DefinePlugin, IR, Plugin } from '@hey-api/openapi-ts'
export type UserConfig = Plugin.Name<'orpc'> export type UserConfig = Plugin.Name<'orpc'>
& Plugin.Hooks & { & Plugin.Hooks & {
@ -23,10 +23,11 @@ export type UserConfig = Plugin.Name<'orpc'>
*/ */
defaultTag?: string defaultTag?: string
/** /**
* Custom function to extract group key from path for router grouping. * Custom function to extract group key for router grouping.
* @default (path) => path.split('/').filter(Boolean)[0] || 'common' * Receives the full IR.OperationObject.
* @default extracts first path segment as camelCase
*/ */
groupKeyBuilder?: (path: string) => string groupKeyBuilder?: (operation: IR.OperationObject) => string
/** /**
* Custom function to generate operation key within a group. * Custom function to generate operation key within a group.
* @default (operationId, groupKey) => simplified operationId * @default (operationId, groupKey) => simplified operationId
@ -40,7 +41,7 @@ export type Config = Plugin.Name<'orpc'>
exportFromIndex: boolean exportFromIndex: boolean
contractNameBuilder: (operationId: string) => string contractNameBuilder: (operationId: string) => string
defaultTag: string defaultTag: string
groupKeyBuilder: (path: string) => string groupKeyBuilder: (operation: IR.OperationObject) => string
operationKeyBuilder: (operationId: string, groupKey: string) => string operationKeyBuilder: (operationId: string, groupKey: string) => string
} }