json-schema-typescript-generator
Version:
Generate typescript types from json schemas
108 lines • 4.04 kB
JavaScript
import * as path from 'path';
import { filtered, filteredJoin } from '../util';
import { OneOfNGenerator } from './OneOfN-generator';
import { nameGenerator } from './name-generator';
import { typeGenerator } from './type-generator';
export const fileGenerator = (locatedSchema, inputInfo) => {
const references = {
schema: new Map()
};
const gatheredInfo = {
namedSchemas: new Map(),
references,
oneOfTypes: new Set()
};
const schemaContent = schemaContentGenerator(locatedSchema, gatheredInfo, inputInfo);
const definitions = schemaMapGenerator(locatedSchema.fileLocation, locatedSchema.schema.definitions, gatheredInfo, inputInfo);
const named = namedGenerator(locatedSchema.fileLocation, gatheredInfo, inputInfo);
const imports = importsGenerator(locatedSchema.fileLocation, references);
const oneOfs = oneOfTypesGenerator(gatheredInfo.oneOfTypes);
return filteredJoin([imports, schemaContent, named, definitions, oneOfs], '\n\n') + '\n';
};
const schemaContentGenerator = (locatedSchema, gatheredInfo, inputInfo, schemaName) => {
const typeName = nameGenerator(schemaName || locatedSchema.fileLocation.fileName);
const typeContent = typeGenerator(locatedSchema, gatheredInfo, inputInfo);
return typeContent
? `export type ${typeName} = ${typeContent};`
: undefined;
};
const importsGenerator = (fileLocation, references) => {
if (references.schema.size === 0) {
return;
}
const content = [];
content.push(importMapGenerator(fileLocation, references.schema));
const defined = filtered(content);
return defined.join('\n');
};
const importMapGenerator = (fileLocation, references) => {
if (references.size === 0) {
return;
}
const imports = [];
references.forEach((names, referenceFileLocation) => {
if (names.size > 0) {
const combinedNames = Array.from(names).sort().join(', ');
const importPath = tsPathGenerator(path.normalize(path.relative(fileLocation.dir, referenceFileLocation.dir)));
const file = referenceFileLocation.fileName.length === 0 ? '' : `/${referenceFileLocation.fileName}`;
imports.push(`import { ${combinedNames} } from '${importPath}${file}';`);
}
});
return imports.join('\n');
};
const namedGenerator = (fileLocation, gatheredInfo, inputInfo) => {
if (gatheredInfo.namedSchemas.size === 0) {
return;
}
const content = [];
while (true) {
const map = gatheredInfo.namedSchemas;
gatheredInfo = {
...gatheredInfo,
namedSchemas: new Map()
};
const schemaMapContent = schemaMapGenerator(fileLocation, map, gatheredInfo, inputInfo);
if (schemaMapContent) {
content.push(schemaMapContent);
}
else {
return content.length === 0
? undefined
: content.join('\n');
}
}
};
const oneOfTypesGenerator = (typeCounts) => {
if (typeCounts.size === 0) {
return;
}
const oneOfTypeLines = [];
typeCounts.forEach((typeCount) => {
const oneOfType = OneOfNGenerator(typeCount);
if (oneOfType) {
oneOfTypeLines.push(oneOfType);
}
});
return oneOfTypeLines.join('\n');
};
const schemaMapGenerator = (fileLocation, map, gatheredInfo, inputInfo) => {
if (!map || map.size === 0) {
return;
}
const content = [];
map.forEach((namedSchema, name) => {
const namedLocatedSchema = {
fileLocation,
schema: namedSchema
};
const schemaContent = schemaContentGenerator(namedLocatedSchema, gatheredInfo, inputInfo, name);
if (schemaContent) {
content.push(schemaContent);
}
});
return content.join('\n');
};
const tsPathGenerator = (relativePath) => relativePath.startsWith('.')
? relativePath
: '.' + path.sep + relativePath;
//# sourceMappingURL=file-generator.js.map