Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"url": "https://github.com/serverless/typescript/issues"
},
"homepage": "https://github.com/serverless/typescript#readme",
"dependencies": {},
"devDependencies": {
"@types/json-schema": "^7.0.6",
"@typescript-eslint/eslint-plugin": "^4.10.0",
Expand All @@ -35,7 +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",
"json-schema-to-typescript": "^10.1.3",
"lodash": "^4.17.21",
"prettier": "^2.2.1",
"serverless": "*",
"typescript": "^4.0.5"
Expand Down
36 changes: 3 additions & 33 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { compile } from "json-schema-to-typescript";
import fs from "fs";
import type { JSONSchema4 } from "json-schema";
import * as _ from "lodash";

interface Serverless {
configSchemaHandler: {
Expand All @@ -12,7 +13,7 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
private schema: JSONSchema4;

constructor(serverless: Serverless) {
this.schema = serverless.configSchemaHandler.schema;
this.schema = _.cloneDeep(serverless.configSchemaHandler.schema);
}

commands = {
Expand All @@ -27,45 +28,14 @@ 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", {
const compiledDefinitions = await compile(this.schema, "AWS", {
ignoreMinAndMaxItems: true,
unreachableDefinitions: true,
});
fs.writeFileSync("index.d.ts", compiledDefinitions);
}
}

const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => {
if ("object" !== typeof schema) {
return schema;
}

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;