UNPKG

json-schema-typescript-generator

Version:
52 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectGenerator = void 0; const options_1 = require("../options"); const TypeGenerator_1 = require("./TypeGenerator"); const type_generator_1 = require("./type-generator"); const objectGenerator = (locatedSchema, gatheredInfo, inputInfo) => { const schema = locatedSchema.schema; if (!schema.type || !schema.type.has('object') || !(schema.object)) { return; } const { properties, required, additionalProperties } = schema.object; const lines = []; lines.push('{'); if (properties) { Array.from(properties.entries()).forEach(([propertyName, propertySchema]) => { const propertyLocatedSchema = (0, TypeGenerator_1.located)(propertySchema, locatedSchema); const type = (0, type_generator_1.typeGenerator)(propertyLocatedSchema, gatheredInfo, inputInfo); if (type) { const isRequired = (required && required.has(propertyName)); const isQuestion = (!isRequired && inputInfo.options.ts.optionalFields == options_1.OptionalFieldPattern.QUESTION); const isPipeUndefined = (!isRequired && inputInfo.options.ts.optionalFields == options_1.OptionalFieldPattern.PIPE_UNDEFINED); const lineParts = []; lineParts.push(propertyName); if (isQuestion) { lineParts.push('?'); } lineParts.push(`: ${type}`); if (isPipeUndefined) { lineParts.push(' | undefined'); } lineParts.push(';'); lines.push(lineParts.join('')); } }); } if (!additionalProperties) { lines.push('}'); } else { const lastLineParts = []; lastLineParts.push('} & Record<string, '); const valueLocatedSchema = (0, TypeGenerator_1.located)(additionalProperties, locatedSchema); const valueType = (0, type_generator_1.typeGenerator)(valueLocatedSchema, gatheredInfo, inputInfo) || inputInfo.options.ts.untyped; lastLineParts.push(valueType); lastLineParts.push('>'); lines.push(lastLineParts.join('')); } return lines.join('\n'); }; exports.objectGenerator = objectGenerator; //# sourceMappingURL=object-generator.js.map