File tree 2 files changed +78
-5
lines changed
2 files changed +78
-5
lines changed Original file line number Diff line number Diff line change
1
+ import { generateReplyInterfaces } from "." ;
2
+
3
+ describe ( "#generateReplyInterfaces" , ( ) => {
4
+ it ( "should generate the reply interface with an empty schema" , async ( ) => {
5
+ expect ( await generateReplyInterfaces ( "" ) ) . toEqual ( "type Reply = {}" ) ;
6
+ expect ( await generateReplyInterfaces ( "XXX" ) ) . toEqual ( "type XXXReply = {}" ) ;
7
+ } ) ;
8
+
9
+ it ( "should generate the reply interface with a single schema" , async ( ) => {
10
+ const schema = {
11
+ "2xx" : {
12
+ additionalProperties : false ,
13
+ properties : {
14
+ guid : {
15
+ type : "string" ,
16
+ } ,
17
+ } ,
18
+ type : "object" ,
19
+ } ,
20
+ } ;
21
+ const genertatedInterface = `\
22
+ export interface Reply2XX {
23
+ guid?: string;
24
+ }
25
+
26
+ type Reply = Reply2XX\
27
+ ` ;
28
+
29
+ expect ( await generateReplyInterfaces ( "" , schema ) ) . toEqual (
30
+ genertatedInterface
31
+ ) ;
32
+ } ) ;
33
+
34
+ it ( "should generate the reply interface with a multiple schema" , async ( ) => {
35
+ const schema = {
36
+ "2xx" : {
37
+ additionalProperties : false ,
38
+ properties : {
39
+ guid : {
40
+ type : "string" ,
41
+ } ,
42
+ } ,
43
+ type : "object" ,
44
+ } ,
45
+ "4xx" : {
46
+ additionalProperties : false ,
47
+ properties : {
48
+ status : {
49
+ type : "number" ,
50
+ } ,
51
+ } ,
52
+ type : "object" ,
53
+ } ,
54
+ } ;
55
+ const genertatedInterface = `\
56
+ export interface Reply2XX {
57
+ guid?: string;
58
+ }
59
+
60
+ export interface Reply4XX {
61
+ status?: number;
62
+ }
63
+
64
+ type Reply = Reply2XX | Reply4XX\
65
+ ` ;
66
+
67
+ expect ( await generateReplyInterfaces ( "" , schema ) ) . toEqual (
68
+ genertatedInterface
69
+ ) ;
70
+ } ) ;
71
+ } ) ;
Original file line number Diff line number Diff line change @@ -6,10 +6,13 @@ import { compile } from "json-schema-to-typescript";
6
6
const opts = { bannerComment : "" } ;
7
7
const defaultSchema = { type : "object" , additionalProperties : false } ;
8
8
9
- async function generateReplyInterfaces ( prefix : string , replies = { } ) {
9
+ export async function generateReplyInterfaces (
10
+ prefix : string ,
11
+ replies : Record < any , any > = { }
12
+ ) {
10
13
const generatedInterfaces = [ ] ;
11
14
const generatedReplyNames = [ ] ;
12
- for ( const [ replyCode , replySchema ] of Object . entries < any > ( replies ) ) {
15
+ for ( const [ replyCode , replySchema ] of Object . entries ( replies ) ) {
13
16
generatedReplyNames . push ( prefix + "Reply" + replyCode . toUpperCase ( ) ) ;
14
17
generatedInterfaces . push (
15
18
await compile (
@@ -22,9 +25,8 @@ async function generateReplyInterfaces(prefix: string, replies = {}) {
22
25
23
26
return `
24
27
${ generatedInterfaces . join ( "\n" ) }
25
-
26
- type ${ prefix } Reply = ${ generatedReplyNames . join ( " | " ) }
27
- ` ;
28
+ type ${ prefix } Reply = ${ generatedReplyNames . join ( " | " ) || "{}" }
29
+ ` . trim ( ) ;
28
30
}
29
31
30
32
async function writeFile (
You can’t perform that action at this time.
0 commit comments