Skip to content

Commit 9d3eba5

Browse files
committedJul 28, 2023
feat(naming): Handle special characters in name and dedupe
1 parent e63494e commit 9d3eba5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed
 

‎src/index.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import type { OpenAPITSOptions } from 'openapi-typescript';
33

44
function getEnumTitle(path: string) {
5-
const parts = path.match(/\/([^/]+)(?=\/parameters\/|$)/g)?.map((p) => p.slice(1)) ?? [];
5+
const parts =
6+
path
7+
.replace(/[^A-Za-z0-9/]/g, '/')
8+
.match(/\/([^/]+)(?=\/parameters\/|$)/g)
9+
?.map((p) => p.slice(1)) ?? [];
610
return `${parts.map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join('')}`;
711
}
812

@@ -11,7 +15,7 @@ interface ComponentWithProps {
1115
}
1216

1317
export async function generate(file: string | typeof process.stdin, options: OpenAPITSOptions) {
14-
const enums: string[] = [];
18+
const enumsByName: Record<string, string> = {};
1519
const visitedEnums: Record<string, string[]> = {};
1620

1721
function collectEnumerations(prefix: string, properties: ComponentWithProps['properties']) {
@@ -29,7 +33,10 @@ export async function generate(file: string | typeof process.stdin, options: Ope
2933
${values.map((e) => `${e} = '${e}'`).join(',\n ')}
3034
}
3135
`;
32-
enums.push(definition);
36+
if (enumsByName[title] && enumsByName[title] !== definition) {
37+
throw new Error(`Enum ${title} already exists with different values`);
38+
}
39+
enumsByName[title] = definition;
3340
}
3441

3542
const finalOptions: OpenAPITSOptions = {
@@ -75,5 +82,5 @@ export async function generate(file: string | typeof process.stdin, options: Ope
7582

7683
const { default: openApiTs } = await import('openapi-typescript');
7784
const basicOutput = await openApiTs(file, finalOptions);
78-
return [basicOutput, ...enums].join('\n');
85+
return [basicOutput, ...Object.values(enumsByName)].join('\n');
7986
}

0 commit comments

Comments
 (0)
Please sign in to comment.