1
1
import fs from "fs" ;
2
2
import glob from "glob" ;
3
3
import yaml from "js-yaml" ;
4
- import { compile } from "json-schema-to-typescript" ;
4
+ import { compile , Options as CompilerOptions } from "json-schema-to-typescript" ;
5
5
import path from "path" ;
6
6
import { promisify } from "util" ;
7
7
8
- const compileOptions = { bannerComment : "" } ;
8
+ const compileOptions : Partial < CompilerOptions > = { bannerComment : "" } ;
9
9
const defaultSchema = { type : "object" , additionalProperties : false } ;
10
10
11
11
export interface Options {
@@ -15,6 +15,13 @@ export interface Options {
15
15
module : string ;
16
16
}
17
17
18
+ function addDefaultValueToSchema ( schema : any ) {
19
+ return {
20
+ ...schema ,
21
+ additionalProperties : schema . additionalProperties || false ,
22
+ } ;
23
+ }
24
+
18
25
export async function generateReplyInterfaces (
19
26
prefix : string ,
20
27
replies : Record < any , any > = { }
@@ -25,7 +32,7 @@ export async function generateReplyInterfaces(
25
32
generatedReplyNames . push ( prefix + "Reply" + replyCode . toUpperCase ( ) ) ;
26
33
generatedInterfaces . push (
27
34
await compile (
28
- replySchema || defaultSchema ,
35
+ addDefaultValueToSchema ( replySchema || defaultSchema ) ,
29
36
prefix + "Reply" + replyCode . toUpperCase ( ) ,
30
37
compileOptions
31
38
)
@@ -72,17 +79,17 @@ import { RouteHandler } from "${options.module}"
72
79
${ importOrWriteSchema ( parsedPath , schema , options , isYaml ) }
73
80
74
81
${ await compile (
75
- schema . params || defaultSchema ,
82
+ addDefaultValueToSchema ( schema . params || defaultSchema ) ,
76
83
options . prefix + "Params" ,
77
84
compileOptions
78
85
) }
79
86
${ await compile (
80
- schema . querystring || schema . query || defaultSchema ,
87
+ addDefaultValueToSchema ( schema . querystring || schema . query || defaultSchema ) ,
81
88
options . prefix + "Query" ,
82
89
compileOptions
83
90
) }
84
91
${ await compile (
85
- schema . body || defaultSchema ,
92
+ addDefaultValueToSchema ( schema . body || defaultSchema ) ,
86
93
options . prefix + "Body" ,
87
94
compileOptions
88
95
) }
0 commit comments