|
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"; |
3 | 3 | import type { JSONSchema4 } from "json-schema"; |
4 | 4 |
|
5 | 5 | interface Serverless { |
@@ -27,45 +27,24 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin { |
27 | 27 | }; |
28 | 28 |
|
29 | 29 | 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 | + }, |
42 | 44 | }); |
43 | | - fs.writeFileSync("index.d.ts", compiledDefinitions); |
44 | | - } |
45 | | -} |
46 | 45 |
|
47 | | -const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => { |
48 | | - if ("object" !== typeof schema) { |
49 | | - return schema; |
| 46 | + writeFileSync("index.d.ts", serverlessTs.join("\n")); |
50 | 47 | } |
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 | +} |
70 | 49 |
|
71 | 50 | module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin; |
0 commit comments