1+ import { Enum } from './client/interfaces/Enum' ;
12import { Model } from './client/interfaces/Model' ;
3+ import { OperationResponse } from './client/interfaces/OperationResponse' ;
4+ import { Service } from './client/interfaces/Service' ;
25
36export enum Case {
47 NONE = 'none' ,
58 CAMEL = 'camel' ,
69 SNAKE = 'snake' ,
710}
8- // Convert a string from snake case or pascal case to camel case.
11+ // Convert a string from snake case to camel case.
912const toCamelCase = ( str : string ) : string => {
1013 return str . replace ( / _ ( [ a - z ] ) / g, match => match [ 1 ] . toUpperCase ( ) ) ;
1114} ;
@@ -22,17 +25,30 @@ const transforms = {
2225
2326// A recursive function that looks at the models and their properties and
2427// 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 => {
3229 return {
3330 ...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+ } ) ) ,
3753 } ;
3854} ;
0 commit comments