22import type { OpenAPITSOptions } from 'openapi-typescript' ;
33
44function getEnumTitle ( path : string ) {
5- const parts = path . match ( / \/ ( [ ^ / ] + ) (? = \/ p a r a m e t e r s \/ | $ ) / g) ?. map ( ( p ) => p . slice ( 1 ) ) ?? [ ] ;
5+ const parts =
6+ path
7+ . replace ( / [ ^ A - Z a - z 0 - 9 / ] / g, '/' )
8+ . match ( / \/ ( [ ^ / ] + ) (? = \/ p a r a m e t e r s \/ | $ ) / 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
1317export 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