@omer-x/typesculptor
Version:
generates interpretable json objects to generate TypeScript types from JSON Schemas
155 lines (147 loc) • 5.03 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
generateTypeDefinition: () => generateTypeDefinition
});
module.exports = __toCommonJS(index_exports);
// src/core/handleArrayType.ts
function handleArrayType(schema) {
if (schema.type !== "array") throw new Error("Schema type must be 'array'");
const dependencies = [];
const itemBodies = [];
for (const item of [schema.items].flat()) {
const itemDefinition = generateTypeDefinition(item);
dependencies.push(...itemDefinition.dependencies);
itemBodies.push(itemDefinition.body);
}
if (schema.minItems && schema.maxItems && schema.minItems === schema.maxItems) {
return { dependencies, body: `[${itemBodies.join(", ")}]` };
}
if (itemBodies.length > 1) {
return { dependencies, body: `(${itemBodies.join(", ")})[]` };
}
return { dependencies, body: `${itemBodies.join(", ")}[]` };
}
// src/utils/generateIndentation.ts
function generateIndentation(count, size = 2) {
if (count < 1 || size < 1) return "";
return Array(count * size).fill(" ").join("");
}
// src/core/handleObjectType.ts
function handleObjectType(schema, indentation) {
if (schema.type !== "object") throw new Error("Schema type must be 'object'");
const dependencies = [];
const propertyBodies = {};
const propertyDescriptions = {};
for (const [propertyName, property] of Object.entries(schema.properties ?? {})) {
const propertyDefinition = generateTypeDefinition(property, indentation + 1);
dependencies.push(...propertyDefinition.dependencies);
propertyBodies[propertyName] = propertyDefinition.body;
if (property.description) {
propertyDescriptions[propertyName] = property.description;
}
}
const isRequired = (propertyName) => (schema.required ?? []).includes(propertyName);
const properties = Object.keys(schema.properties ?? {}).map((propertyName) => [
"/**",
` * ${propertyDescriptions[propertyName] ?? "missing-description"}`,
" */",
`${propertyName}${isRequired(propertyName) ? "" : "?"}: ${propertyBodies[propertyName]},`
].map((line) => generateIndentation(indentation + 1) + line).join("\n"));
return {
dependencies,
body: ["{", properties.join("\n"), "}"].join("\n")
};
}
// src/core/handleStringType.ts
function handleStringType(schema) {
if (schema.type !== "string") throw new Error("Schema type must be 'string'");
if (schema.enum) {
return {
dependencies: [],
body: schema.enum.map((item) => `"${item}"`).join(" | ")
};
}
return {
dependencies: [],
body: "string"
};
}
// src/core/handleUnionType.ts
function handleUnionType(schemas) {
const dependencies = [];
const itemBodies = [];
for (const item of schemas) {
const itemDefinition = generateTypeDefinition(item);
dependencies.push(...itemDefinition.dependencies);
itemBodies.push(itemDefinition.body);
}
if (itemBodies.length > 1) {
return { dependencies, body: `(${itemBodies.join(" | ")})` };
}
return { dependencies, body: itemBodies.join(" | ") };
}
// src/core/generateTypeDefinition.ts
function generateTypeDefinition(schema, indentation = 0) {
if ("$ref" in schema) {
const [_, __, _category, componentName] = schema.$ref.split("/");
if (!componentName) throw new Error("Invalid $ref in schema");
return {
dependencies: [componentName],
body: componentName
};
}
if (schema.anyOf) return handleUnionType(schema.anyOf);
if (schema.oneOf) return handleUnionType(schema.oneOf);
switch (schema.type) {
case "null":
return {
dependencies: [],
body: "null"
};
case "boolean":
return {
dependencies: [],
body: "boolean"
};
case "integer":
case "number":
return {
dependencies: [],
body: "number"
};
case "string":
return handleStringType(schema);
case "object":
return handleObjectType(schema, indentation);
case "array":
return handleArrayType(schema);
default:
return {
dependencies: [],
body: "unknown"
};
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
generateTypeDefinition
});