1- import { compile } from "json-schema-to-typescript " ;
2- import fs from "fs" ;
1+ import { InputData , JSONSchemaInput , quicktype } from "quicktype-core " ;
2+ import { writeFileSync } from "fs" ;
33import type { JSONSchema4 } from "json-schema" ;
44
55interface Serverless {
66 configSchemaHandler : {
77 schema : JSONSchema4 ;
8- } ;
8+ }
99}
1010
1111class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
@@ -27,45 +27,24 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
2727 } ;
2828
2929 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- /**
37- * ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples
38- */
39- const compiledDefinitions = await compile ( normalizedSchema , "AWS" , {
40- ignoreMinAndMaxItems : true ,
41- unreachableDefinitions : true ,
30+ const schemaInput = new JSONSchemaInput ( undefined ) ;
31+ await schemaInput . addSource ( {
32+ name : "AWS" ,
33+ schema : JSON . stringify ( this . schema ) ,
34+ } ) ;
35+ const inputData = new InputData ( ) ;
36+ inputData . addInput ( schemaInput ) ;
37+
38+ const { lines : serverlessTs } = await quicktype ( {
39+ inputData,
40+ lang : "typescript" ,
41+ rendererOptions : {
42+ "just-types" : "true" ,
43+ } ,
4244 } ) ;
43- fs . writeFileSync ( "index.d.ts" , compiledDefinitions ) ;
44- }
45- }
4645
47- const replaceAllConstForEnum = ( schema : JSONSchema4 ) : JSONSchema4 => {
48- if ( "object" !== typeof schema ) {
49- return schema ;
46+ writeFileSync ( "index.d.ts" , serverlessTs . join ( "\n" ) ) ;
5047 }
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- } ;
48+ }
7049
7150module . exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin ;
0 commit comments