diff --git a/package.json b/package.json index 1587fb5..6def41a 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "eslint-config-prettier": "^7.1.0", "eslint-plugin-import": "^2.22.1", "eslint-plugin-prettier": "^3.3.0", - "json-schema-to-typescript": "^10.0.2", "prettier": "^2.2.1", + "quicktype-core": "^6.0.69", "serverless": "*", "typescript": "^4.0.5" } diff --git a/src/plugin.ts b/src/plugin.ts index dc3a5f9..2191ea0 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,5 +1,5 @@ -import { compile } from "json-schema-to-typescript"; -import fs from "fs"; +import { InputData, JSONSchemaInput, quicktype } from "quicktype-core"; +import { writeFileSync } from "fs"; import type { JSONSchema4 } from "json-schema"; interface Serverless { @@ -27,45 +27,24 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin { }; async generateSchema() { - /** - * https://github.com/serverless/typescript/issues/4 - * JSON Schema v6 `const` keyword converted to `enum` - */ - const normalizedSchema = replaceAllConstForEnum(this.schema); - - /** - * ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples - */ - const compiledDefinitions = await compile(normalizedSchema, "AWS", { - ignoreMinAndMaxItems: true, - unreachableDefinitions: true, + const schemaInput = new JSONSchemaInput(undefined); + await schemaInput.addSource({ + name: "AWS", + schema: JSON.stringify(this.schema), + }); + const inputData = new InputData(); + inputData.addInput(schemaInput); + + const { lines: serverlessTs } = await quicktype({ + inputData, + lang: "typescript", + rendererOptions: { + "just-types": "true", + }, }); - fs.writeFileSync("index.d.ts", compiledDefinitions); - } -} -const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => { - if ("object" !== typeof schema) { - return schema; + writeFileSync("index.d.ts", serverlessTs.join("\n")); } - - return Object.fromEntries( - Object.entries(schema).map(([key, value]) => { - if (key === "const") { - return ["enum", [value]]; - } - - if (Array.isArray(value)) { - return [key, value.map(replaceAllConstForEnum)]; - } - - if ("object" === typeof value && value !== null) { - return [key, replaceAllConstForEnum(value)]; - } - - return [key, value]; - }) - ); -}; +} module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;