1
+ import { Enum } from './client/interfaces/Enum' ;
1
2
import { Model } from './client/interfaces/Model' ;
3
+ import { OperationResponse } from './client/interfaces/OperationResponse' ;
4
+ import { Service } from './client/interfaces/Service' ;
2
5
3
6
export enum Case {
4
7
NONE = 'none' ,
5
8
CAMEL = 'camel' ,
6
9
SNAKE = 'snake' ,
7
10
}
8
- // Convert a string from snake case or pascal case to camel case.
11
+ // Convert a string from snake case to camel case.
9
12
const toCamelCase = ( str : string ) : string => {
10
13
return str . replace ( / _ ( [ a - z ] ) / g, match => match [ 1 ] . toUpperCase ( ) ) ;
11
14
} ;
@@ -22,17 +25,30 @@ const transforms = {
22
25
23
26
// A recursive function that looks at the models and their properties and
24
27
// converts each property name using the provided transform function.
25
- export const convertModelNames = ( model : Model , type : Exclude < Case , Case . NONE > ) : Model => {
26
- if ( ! model . properties . length ) {
27
- return {
28
- ...model ,
29
- name : transforms [ type ] ( model . name ) ,
30
- } ;
31
- }
28
+ export const convertModelNames = < T extends Model | OperationResponse > ( model : T , type : Exclude < Case , Case . NONE > ) : T => {
32
29
return {
33
30
...model ,
34
- properties : model . properties . map ( property => {
35
- return convertModelNames ( property , type ) ;
36
- } ) ,
31
+ name : transforms [ type ] ( model . name ) ,
32
+ link : model . link ? convertModelNames ( model . link , type ) : null ,
33
+ enum : model . enum . map ( modelEnum => convertEnumName ( modelEnum , type ) ) ,
34
+ enums : model . enums . map ( property => convertModelNames ( property , type ) ) ,
35
+ properties : model . properties . map ( property => convertModelNames ( property , type ) ) ,
36
+ } ;
37
+ } ;
38
+
39
+ const convertEnumName = ( modelEnum : Enum , type : Exclude < Case , Case . NONE > ) : Enum => {
40
+ return {
41
+ ...modelEnum ,
42
+ name : transforms [ type ] ( modelEnum . name ) ,
43
+ } ;
44
+ } ;
45
+
46
+ export const convertServiceCase = ( service : Service , type : Exclude < Case , Case . NONE > ) : Service => {
47
+ return {
48
+ ...service ,
49
+ operations : service . operations . map ( op => ( {
50
+ ...op ,
51
+ results : op . results . map ( results => convertModelNames ( results , type ) ) ,
52
+ } ) ) ,
37
53
} ;
38
54
} ;
0 commit comments