Skip to content

Commit 5fced12

Browse files
Replace json-schema-to-typescript by quicktype
1 parent 3d43d14 commit 5fced12

File tree

2 files changed

+19
-40
lines changed

2 files changed

+19
-40
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"eslint-config-prettier": "^7.1.0",
3535
"eslint-plugin-import": "^2.22.1",
3636
"eslint-plugin-prettier": "^3.3.0",
37-
"json-schema-to-typescript": "^10.0.2",
3837
"prettier": "^2.2.1",
38+
"quicktype-core": "^6.0.69",
3939
"serverless": "*",
4040
"typescript": "^4.0.5"
4141
}

src/plugin.ts

+18-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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";
33
import type { JSONSchema4 } from "json-schema";
44

55
interface Serverless {
@@ -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

7150
module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;

0 commit comments

Comments
 (0)