2
2
import type { OpenAPITSOptions } from 'openapi-typescript' ;
3
3
4
4
function 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 ) ) ?? [ ] ;
6
10
return `${ parts . map ( ( p ) => p . charAt ( 0 ) . toUpperCase ( ) + p . slice ( 1 ) ) . join ( '' ) } ` ;
7
11
}
8
12
@@ -11,7 +15,7 @@ interface ComponentWithProps {
11
15
}
12
16
13
17
export async function generate ( file : string | typeof process . stdin , options : OpenAPITSOptions ) {
14
- const enums : string [ ] = [ ] ;
18
+ const enumsByName : Record < string , string > = { } ;
15
19
const visitedEnums : Record < string , string [ ] > = { } ;
16
20
17
21
function collectEnumerations ( prefix : string , properties : ComponentWithProps [ 'properties' ] ) {
@@ -29,7 +33,10 @@ export async function generate(file: string | typeof process.stdin, options: Ope
29
33
${ values . map ( ( e ) => `${ e } = '${ e } '` ) . join ( ',\n ' ) }
30
34
}
31
35
` ;
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 ;
33
40
}
34
41
35
42
const finalOptions : OpenAPITSOptions = {
@@ -75,5 +82,5 @@ export async function generate(file: string | typeof process.stdin, options: Ope
75
82
76
83
const { default : openApiTs } = await import ( 'openapi-typescript' ) ;
77
84
const basicOutput = await openApiTs ( file , finalOptions ) ;
78
- return [ basicOutput , ...enums ] . join ( '\n' ) ;
85
+ return [ basicOutput , ...Object . values ( enumsByName ) ] . join ( '\n' ) ;
79
86
}
0 commit comments