1
+ import { BrainStatementImpl } from "../port/StatementCustom/BrainStatementImpl" ;
2
+ import { DefaultStatementImpl } from "../port/StatementCustom/DefaultStatementImpl" ;
3
+ import { StatementCustom } from "../port/StatementCustom/StatementCustom" ;
4
+
1
5
type Partial < T > = {
2
6
[ P in keyof T ] ?: T [ P ] ;
3
7
} ;
@@ -7,12 +11,14 @@ interface IJson2TsConfigPrivate {
7
11
optionalFields : boolean ;
8
12
rootObjectName : string ;
9
13
addPrefix : boolean ;
14
+ brainCustom : boolean ;
10
15
}
11
16
12
17
export type IJson2TsConfig = Partial < IJson2TsConfigPrivate > ;
13
18
14
19
export class Json2Ts {
15
20
private config : IJson2TsConfigPrivate ;
21
+ private statementCustom : StatementCustom ;
16
22
17
23
private interfaces : {
18
24
[ name : string ] : {
@@ -26,8 +32,10 @@ export class Json2Ts {
26
32
optionalFields : false ,
27
33
rootObjectName : "RootObject" ,
28
34
addPrefix : false ,
35
+ brainCustom : true ,
29
36
...config ,
30
37
} ;
38
+ this . statementCustom = config . brainCustom ? new BrainStatementImpl ( ) : new DefaultStatementImpl ( ) ;
31
39
}
32
40
33
41
convert ( json : { } ) {
@@ -136,12 +144,13 @@ export class Json2Ts {
136
144
}
137
145
fields . forEach ( field => {
138
146
const type = this . interfaces [ name ] [ field ] ;
139
- const defaultValue = optionalFields ? "" : " = " + this . getDefaultValue ( type ) ;
147
+ const defaultTypeValue = this . getDefaultValue ( type ) ;
148
+ const defaultValue = optionalFields ? "" : " = " + defaultTypeValue ;
140
149
141
150
const customType = type . replace ( "[]" , "" ) ;
142
151
const customTypeCheck = Object . keys ( this . interfaces ) . includes ( customType ) ;
143
152
if ( customTypeCheck ) {
144
- interfaceStr . push ( ` @Type(() => ${ customType } )` ) ;
153
+ interfaceStr . push ( this . statementCustom . typeStatementGenerator ( customType , defaultTypeValue ) ) ;
145
154
}
146
155
const rewriteFiled = this . hasSpaceInMiddle ( field ) ? `"${ field } "` : field ;
147
156
interfaceStr . push ( ` ${ rewriteFiled } ${ optionalFields ? "?" : "" } : ${ type } ${ defaultValue } ;` ) ;
@@ -151,7 +160,7 @@ export class Json2Ts {
151
160
} )
152
161
. join ( "\n" ) ;
153
162
154
- if ( outout . includes ( "@Type" ) ) outout = 'import { Type } from "class-transformer"; \n\n' + outout ;
163
+ if ( outout . includes ( "@Type" ) ) outout = this . statementCustom . importTypeStatement + " \n\n" + outout ;
155
164
return outout ;
156
165
}
157
166
0 commit comments