Skip to content

Commit 3fff1aa

Browse files
committed
add NamingRule
1 parent b03c185 commit 3fff1aa

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

packages/openapi-ts/src/plugins/@orpc/contract/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IR } from '~/ir/types';
22
import { definePluginConfig } from '~/plugins/shared/utils/config';
3-
import { toCase } from '~/utils/naming';
3+
import { resolveNaming, toCase } from '~/utils/naming';
44

55
import { handler } from './plugin';
66
import type { OrpcPlugin } from './types';
@@ -65,7 +65,7 @@ export const defaultConfig: OrpcPlugin['Config'] = {
6565
exportFromIndex: false,
6666
groupKeyBuilder: defaultGroupKeyBuilder,
6767
operationKeyBuilder: defaultOperationKeyBuilder,
68-
routerName: 'router',
68+
routerName: { name: 'router' },
6969
validator: 'zod',
7070
},
7171
handler,
@@ -76,7 +76,10 @@ export const defaultConfig: OrpcPlugin['Config'] = {
7676
plugin.config.defaultTag ??= 'default';
7777
plugin.config.groupKeyBuilder ??= defaultGroupKeyBuilder;
7878
plugin.config.operationKeyBuilder ??= defaultOperationKeyBuilder;
79-
plugin.config.routerName ??= 'router';
79+
plugin.config.routerName = resolveNaming(plugin.config.routerName);
80+
if (!plugin.config.routerName.name) {
81+
plugin.config.routerName.name = 'router';
82+
}
8083

8184
plugin.config.validator ??= 'zod';
8285
plugin.dependencies.add(plugin.config.validator);

packages/openapi-ts/src/plugins/@orpc/contract/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { IR } from '~/ir/types';
22
import { createOperationComment } from '~/plugins/shared/utils/operation';
33
import { $ } from '~/ts-dsl';
4-
import { toCase } from '~/utils/naming';
4+
import { applyNaming, toCase } from '~/utils/naming';
55

66
import type { OrpcPlugin } from './types';
77

@@ -183,7 +183,8 @@ export const handler: OrpcPlugin['Handler'] = ({ plugin }) => {
183183
}
184184

185185
// Create contracts object export grouped by API path segment (in separate router file)
186-
const contractsSymbol = plugin.symbol(routerName, {
186+
const routerExportName = applyNaming('router', routerName);
187+
const contractsSymbol = plugin.symbol(routerExportName, {
187188
exported: true,
188189
meta: {
189190
category: 'contract',
@@ -225,7 +226,7 @@ export const handler: OrpcPlugin['Handler'] = ({ plugin }) => {
225226

226227
// Create type export: export type Router = typeof router (in separate router file)
227228
// Capitalize the router name for the type (e.g., 'router' → 'Router', 'contract' → 'Contract')
228-
const routerTypeName = toCase(routerName, 'PascalCase');
229+
const routerTypeName = toCase(routerExportName, 'PascalCase');
229230
const routerTypeSymbol = plugin.symbol(routerTypeName, {
230231
exported: true,
231232
meta: {

packages/openapi-ts/src/plugins/@orpc/contract/types.d.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { IR } from '~/ir/types';
22
import type { DefinePlugin, Plugin } from '~/plugins';
33
import type { PluginValidatorNames } from '~/plugins/types';
4+
import type { NamingConfig, NamingRule } from '~/utils/naming';
45

56
export type UserConfig = Plugin.Name<'@orpc/contract'> &
67
Plugin.Hooks & {
@@ -36,12 +37,24 @@ export type UserConfig = Plugin.Name<'@orpc/contract'> &
3637
*/
3738
operationKeyBuilder?: (operationId: string, groupKey: string) => string;
3839
/**
39-
* Name of the router export.
40-
* The type export will be the capitalized version (e.g., 'router' → 'Router').
40+
* Naming rule for the router export.
41+
* The type export will be the PascalCase version (e.g., 'router' → 'Router').
4142
*
4243
* @default 'router'
44+
*
45+
* @example
46+
* // Simple string
47+
* routerName: 'contract'
48+
*
49+
* @example
50+
* // Template string
51+
* routerName: '{{name}}Contract'
52+
*
53+
* @example
54+
* // With casing
55+
* routerName: { name: '{{name}}Contract', casing: 'camelCase' }
4356
*/
44-
routerName?: string;
57+
routerName?: NamingRule;
4558
/**
4659
* Validator plugin to use for input/output schemas.
4760
*
@@ -61,7 +74,7 @@ export type Config = Plugin.Name<'@orpc/contract'> &
6174
groupKeyBuilder: (operation: IR.OperationObject) => string;
6275
operationKeyBuilder: (operationId: string, groupKey: string) => string;
6376
output: string;
64-
routerName: string;
77+
routerName: NamingConfig;
6578
validator: PluginValidatorNames;
6679
};
6780

0 commit comments

Comments
 (0)