@@ -2,10 +2,16 @@ import glob from "glob";
2
2
import path from "path" ;
3
3
import fs from "fs" ;
4
4
import { compile } from "json-schema-to-typescript" ;
5
+ import { promisify } from "util" ;
5
6
6
- const opts = { bannerComment : "" } ;
7
+ const compileOptions = { bannerComment : "" } ;
7
8
const defaultSchema = { type : "object" , additionalProperties : false } ;
8
9
10
+ export interface Options {
11
+ glob : string ;
12
+ prefix : string ;
13
+ }
14
+
9
15
export async function generateReplyInterfaces (
10
16
prefix : string ,
11
17
replies : Record < any , any > = { }
@@ -18,7 +24,7 @@ export async function generateReplyInterfaces(
18
24
await compile (
19
25
replySchema || defaultSchema ,
20
26
prefix + "Reply" + replyCode . toUpperCase ( ) ,
21
- opts
27
+ compileOptions
22
28
)
23
29
) ;
24
30
}
@@ -29,12 +35,13 @@ type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"}
29
35
` . trim ( ) ;
30
36
}
31
37
32
- async function writeFile (
38
+ async function generateInterfaces (
33
39
parsedPath : path . ParsedPath ,
34
- prefix : string ,
35
- schema : any
40
+ schema : any ,
41
+ options : Options
36
42
) {
37
- const template = `/* tslint:disable */
43
+ return `\
44
+ /* tslint:disable */
38
45
/* eslint-disable */
39
46
/**
40
47
* This file was automatically generated. DO NOT MODIFY IT BY HAND.
@@ -45,38 +52,51 @@ import { RouteHandler } from "fastify"
45
52
46
53
import schema from './${ parsedPath . base } '
47
54
48
- ${ await compile ( schema . params || defaultSchema , prefix + "Params" , opts ) }
55
+ ${ await compile (
56
+ schema . params || defaultSchema ,
57
+ options . prefix + "Params" ,
58
+ compileOptions
59
+ ) }
49
60
${ await compile (
50
61
schema . querystring || schema . query || defaultSchema ,
51
- prefix + "Query" ,
52
- opts
62
+ options . prefix + "Query" ,
63
+ compileOptions
53
64
) }
54
- ${ await compile ( schema . body || defaultSchema , prefix + "Body" , opts ) }
55
- ${ await compile ( schema . headers || defaultSchema , prefix + "Headers" , opts ) }
56
- ${ await generateReplyInterfaces ( prefix , schema . response ) }
65
+ ${ await compile (
66
+ schema . body || defaultSchema ,
67
+ options . prefix + "Body" ,
68
+ compileOptions
69
+ ) }
70
+ ${ await compile (
71
+ schema . headers || defaultSchema ,
72
+ options . prefix + "Headers" ,
73
+ compileOptions
74
+ ) }
75
+ ${ await generateReplyInterfaces ( options . prefix , schema . response ) }
57
76
58
- type ${ prefix } Handler = RouteHandler<{
59
- Query: ${ prefix } Query;
60
- Body: ${ prefix } Body;
61
- Params: ${ prefix } Params;
62
- Headers: ${ prefix } Headers;
63
- Reply: ${ prefix } Reply;
77
+ type ${ options . prefix } Handler = RouteHandler<{
78
+ Query: ${ options . prefix } Query;
79
+ Body: ${ options . prefix } Body;
80
+ Params: ${ options . prefix } Params;
81
+ Headers: ${ options . prefix } Headers;
82
+ Reply: ${ options . prefix } Reply;
64
83
}>;
65
84
66
- export { ${ prefix } Handler, schema }
67
- ` ;
85
+ export { ${ options . prefix } Handler, schema }\
86
+ ` ;
87
+ }
68
88
69
- fs . writeFileSync (
70
- path . join ( parsedPath . dir , parsedPath . name + ".ts" ) ,
71
- template
72
- ) ;
89
+ async function writeFile ( parsedPath : path . ParsedPath , template : string ) {
90
+ const write = promisify ( fs . writeFile ) ;
91
+ return write ( path . join ( parsedPath . dir , parsedPath . name + ".ts" ) , template ) ;
73
92
}
74
93
75
- export function convert ( globString : string , prefix : string ) {
76
- const filePaths = glob . sync ( globString ) ;
77
- filePaths . forEach ( ( filePath ) => {
94
+ export async function convert ( options : Options ) {
95
+ const filePaths = glob . sync ( options . glob ) ;
96
+ for ( const filePath of filePaths ) {
78
97
const parsedPath = path . parse ( filePath ) ;
79
98
const schema = JSON . parse ( fs . readFileSync ( filePath , "utf-8" ) ) ;
80
- writeFile ( parsedPath , prefix , schema ) ;
81
- } ) ;
99
+ const template = await generateInterfaces ( parsedPath , schema , options ) ;
100
+ await writeFile ( parsedPath , template ) ;
101
+ }
82
102
}
0 commit comments