1
1
import { compile } from "json-schema-to-typescript" ;
2
2
import fs from "fs" ;
3
3
import type { JSONSchema4 } from "json-schema" ;
4
+ import * as _ from "lodash" ;
4
5
5
6
interface Serverless {
6
7
configSchemaHandler : {
@@ -12,7 +13,7 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
12
13
private schema : JSONSchema4 ;
13
14
14
15
constructor ( serverless : Serverless ) {
15
- this . schema = serverless . configSchemaHandler . schema ;
16
+ this . schema = _ . cloneDeep ( serverless . configSchemaHandler . schema ) ;
16
17
}
17
18
18
19
commands = {
@@ -27,45 +28,14 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
27
28
} ;
28
29
29
30
async generateSchema ( ) {
30
- /**
31
- * https://github.com/serverless/typescript/issues/4
32
- * JSON Schema v6 `const` keyword converted to `enum`
33
- */
34
- const normalizedSchema = replaceAllConstForEnum ( this . schema ) ;
35
-
36
31
/**
37
32
* ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples
38
33
*/
39
- const compiledDefinitions = await compile ( normalizedSchema , "AWS" , {
34
+ const compiledDefinitions = await compile ( this . schema , "AWS" , {
40
35
ignoreMinAndMaxItems : true ,
41
- unreachableDefinitions : true ,
42
36
} ) ;
43
37
fs . writeFileSync ( "index.d.ts" , compiledDefinitions ) ;
44
38
}
45
39
}
46
40
47
- const replaceAllConstForEnum = ( schema : JSONSchema4 ) : JSONSchema4 => {
48
- if ( "object" !== typeof schema ) {
49
- return schema ;
50
- }
51
-
52
- return Object . fromEntries (
53
- Object . entries ( schema ) . map ( ( [ key , value ] ) => {
54
- if ( key === "const" ) {
55
- return [ "enum" , [ value ] ] ;
56
- }
57
-
58
- if ( Array . isArray ( value ) ) {
59
- return [ key , value . map ( replaceAllConstForEnum ) ] ;
60
- }
61
-
62
- if ( "object" === typeof value && value !== null ) {
63
- return [ key , replaceAllConstForEnum ( value ) ] ;
64
- }
65
-
66
- return [ key , value ] ;
67
- } )
68
- ) ;
69
- } ;
70
-
71
41
module . exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin ;
0 commit comments