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

View File

@ -203,7 +203,7 @@ export const handler: OrpcPlugin['Handler'] = ({ plugin }) => {
// Group operations by group key
const operationsByGroup = new Map<string, IR.OperationObject[]>()
for (const op of operations) {
const groupKey = groupKeyBuilder(op.path as string)
const groupKey = groupKeyBuilder(op)
if (!operationsByGroup.has(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'>
& Plugin.Hooks & {
@ -23,10 +23,11 @@ export type UserConfig = Plugin.Name<'orpc'>
*/
defaultTag?: string
/**
* Custom function to extract group key from path for router grouping.
* @default (path) => path.split('/').filter(Boolean)[0] || 'common'
* Custom function to extract group key for router grouping.
* 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.
* @default (operationId, groupKey) => simplified operationId
@ -40,7 +41,7 @@ export type Config = Plugin.Name<'orpc'>
exportFromIndex: boolean
contractNameBuilder: (operationId: string) => string
defaultTag: string
groupKeyBuilder: (path: string) => string
groupKeyBuilder: (operation: IR.OperationObject) => string
operationKeyBuilder: (operationId: string, groupKey: string) => string
}