Skip to content

Commit b6afac9

Browse files
committed
chore: expose name conflict resolver
1 parent 3fc1239 commit b6afac9

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

dev/openapi-ts.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export default defineConfig(() => {
4747
// 'full.yaml',
4848
// 'sdk-method-class-conflict.yaml',
4949
// 'object-property-names.yaml',
50-
// 'openai.yaml',
51-
'opencode.yaml',
50+
'openai.yaml',
51+
// 'opencode.yaml',
5252
// 'pagination-ref.yaml',
5353
// 'sdk-instance.yaml',
5454
// 'sdk-nested-classes.yaml',
@@ -106,6 +106,10 @@ export default defineConfig(() => {
106106
// importFileExtension: '.js',
107107
// indexFile: false,
108108
// lint: 'eslint',
109+
nameConflictResolver({ attempt, baseName }) {
110+
console.log('resolving conflict for:', { attempt, baseName });
111+
return attempt === 0 ? baseName : `${baseName}_N${attempt + 1}`;
112+
},
109113
path: path.resolve(__dirname, '.gen'),
110114
preferExportAll: true,
111115
resolveModuleName: (moduleName) => {

packages/codegen-core/src/__tests__/exports.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type _TypeExports = [
3939
index.ImportMember,
4040
index.ImportModule,
4141
index.Language,
42+
index.NameConflictResolver,
4243
index.NameConflictResolvers,
4344
index.Node,
4445
index.Output,

packages/codegen-core/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export {
2626
simpleNameConflictResolver,
2727
underscoreNameConflictResolver,
2828
} from './planner/resolvers';
29-
export type { IAnalysisContext as AnalysisContext } from './planner/types';
29+
export type {
30+
IAnalysisContext as AnalysisContext,
31+
NameConflictResolver,
32+
} from './planner/types';
3033
export { Project } from './project/project';
3134
export type { IProject } from './project/types';
3235
export { fromRef, fromRefs, isRef, ref, refs } from './refs/refs';

packages/openapi-ts/src/ir/context.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Project, simpleNameConflictResolver } from '@hey-api/codegen-core';
1+
import { Project } from '@hey-api/codegen-core';
22

33
import type { Package } from '~/config/utils/package';
44
import { packageFactory } from '~/config/utils/package';
@@ -68,11 +68,9 @@ export class Context<Spec extends Record<string, any> = any> {
6868
spec: Spec;
6969
}) {
7070
this.config = config;
71+
// TODO: allow overriding via config
7172
this.gen = new Project({
7273
defaultFileName: 'index',
73-
defaultNameConflictResolver(args) {
74-
return simpleNameConflictResolver(args);
75-
},
7674
fileName: (base) => {
7775
const name = buildName({
7876
config: config.output.fileName,
@@ -86,10 +84,11 @@ export class Context<Spec extends Record<string, any> = any> {
8684
? name
8785
: `${name}${suffix}`;
8886
},
89-
nameConflictResolvers: {
90-
// TODO: allow overriding via config
91-
},
92-
// TODO: allow overriding via config
87+
nameConflictResolvers: config.output.nameConflictResolver
88+
? {
89+
typescript: config.output.nameConflictResolver,
90+
}
91+
: undefined,
9392
renderers: [
9493
new TypeScriptRenderer({
9594
preferExportAll: config.output.preferExportAll,

packages/openapi-ts/src/types/output.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NameConflictResolver } from '@hey-api/codegen-core';
12
import type ts from 'typescript';
23

34
import type { StringCase, StringName } from './case';
@@ -89,6 +90,11 @@ export type UserOutput = {
8990
* @default null
9091
*/
9192
lint?: Linters | null;
93+
/**
94+
* Optional name conflict resolver to customize how naming conflicts
95+
* are handled.
96+
*/
97+
nameConflictResolver?: NameConflictResolver;
9298
/**
9399
* The absolute path to the output folder.
94100
*/
@@ -178,6 +184,11 @@ export type Output = {
178184
* Which linter to use to process output folder?
179185
*/
180186
lint: Linters | null;
187+
/**
188+
* Optional name conflict resolver to customize how naming conflicts
189+
* are handled.
190+
*/
191+
nameConflictResolver: NameConflictResolver | undefined;
181192
/**
182193
* The absolute path to the output folder.
183194
*/

0 commit comments

Comments
 (0)