UNPKG

typescript-scaffolder

Version:

![npm version](https://img.shields.io/npm/v/typescript-scaffolder) ### Unit Test Coverage: 97.53%

57 lines (56 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToJsonSchema = convertToJsonSchema; exports.mapType = mapType; function convertToJsonSchema(parsed, knownInterfaces) { const schema = { $schema: "http://json-schema.org/draft-07/schema#", title: parsed.name, type: "object", properties: {}, required: [], definitions: {}, }; parsed.properties.forEach((prop) => { schema.properties[prop.name] = mapType(prop.type, knownInterfaces); if (!prop.optional) { schema.required.push(prop.name); } }); if (schema.required.length === 0) { delete schema.required; } return schema; } function mapType(tsType, knownInterfaces) { if (tsType === "string") { return { type: "string" }; } if (tsType === "number") { return { type: "number" }; } if (tsType === "boolean") { return { type: "boolean" }; } if (tsType.endsWith("[]")) { const itemType = tsType.slice(0, -2); return { type: "array", items: mapType(itemType, knownInterfaces), }; } if (tsType.includes("|")) { const literals = tsType.split("|").map(v => v.trim()); const isLiteralUnion = literals.every(v => /^"(.*)"$/.test(v)); if (isLiteralUnion) { return { enum: literals.map(v => v.slice(1, -1)) }; } } // If type matches a known interface, return a $ref const match = knownInterfaces.find(i => i.name === tsType); if (match) { return { $ref: `#/definitions/${tsType}` }; } // fallback to generic object return { type: "object" }; }