json-schema-to-typescript
Advanced tools
Comparing version 4.6.5 to 5.0.0
@@ -39,2 +39,3 @@ #!/usr/bin/env node | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var minimist = require("minimist"); | ||
var fs_1 = require("mz/fs"); | ||
@@ -44,3 +45,2 @@ var path_1 = require("path"); | ||
var index_1 = require("./index"); | ||
var minimist = require("minimist"); | ||
main(minimist(process.argv.slice(2), { | ||
@@ -47,0 +47,0 @@ alias: { |
@@ -104,3 +104,3 @@ "use strict"; | ||
} | ||
function generateType(ast, options, indentDepth) { | ||
function generateType(ast, options) { | ||
utils_1.log(cli_color_1.whiteBright.bgMagenta('generator'), ast); | ||
@@ -113,8 +113,8 @@ if (AST_1.hasStandaloneName(ast)) { | ||
case 'ARRAY': return (function () { | ||
var type = generateType(ast.params, options, indentDepth + 1); | ||
var type = generateType(ast.params, options); | ||
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'; | ||
})(); | ||
case 'BOOLEAN': return 'boolean'; | ||
case 'INTERFACE': return generateInterface(ast, options, indentDepth + 1); | ||
case 'INTERSECTION': return generateSetOperation(ast, options, indentDepth); | ||
case 'INTERFACE': return generateInterface(ast, options); | ||
case 'INTERSECTION': return generateSetOperation(ast, options); | ||
case 'LITERAL': return JSON.stringify(ast.params); | ||
@@ -127,5 +127,5 @@ case 'NUMBER': return 'number'; | ||
case 'TUPLE': return '[' | ||
+ ast.params.map(function (_) { return generateType(_, options, indentDepth + 1); }).join(', ') | ||
+ ast.params.map(function (_) { return generateType(_, options); }).join(', ') | ||
+ ']'; | ||
case 'UNION': return generateSetOperation(ast, options, indentDepth); | ||
case 'UNION': return generateSetOperation(ast, options); | ||
} | ||
@@ -136,40 +136,36 @@ } | ||
*/ | ||
function generateSetOperation(ast, options, indentDepth) { | ||
var members = ast.params.map(function (_) { return generateType(_, options, indentDepth); }); | ||
function generateSetOperation(ast, options) { | ||
var members = ast.params.map(function (_) { return generateType(_, options); }); | ||
var separator = ast.type === 'UNION' ? '|' : '&'; | ||
return members.length === 1 ? members[0] : '(' + members.join(' ' + separator + ' ') + ')'; | ||
} | ||
function generateInterface(ast, options, indentDepth) { | ||
function generateInterface(ast, options) { | ||
return "{" | ||
+ '\n' | ||
+ options.indentWith.repeat(indentDepth) | ||
+ ast.params | ||
.map(function (_a) { | ||
var isRequired = _a.isRequired, keyName = _a.keyName, ast = _a.ast; | ||
return [isRequired, keyName, ast, generateType(ast, options, indentDepth)]; | ||
return [isRequired, keyName, ast, generateType(ast, options)]; | ||
}) | ||
.map(function (_a) { | ||
var isRequired = _a[0], keyName = _a[1], ast = _a[2], type = _a[3]; | ||
return (AST_1.hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment, options, indentDepth + 1) + '\n' : '') | ||
+ options.indentWith | ||
return (AST_1.hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment) + '\n' : '') | ||
+ escapeKeyName(keyName) | ||
+ (isRequired ? '' : '?') | ||
+ ': ' | ||
+ (AST_1.hasStandaloneName(ast) ? utils_1.toSafeString(type) : type) | ||
+ (options.enableTrailingSemicolonForInterfaceProperties ? ';' : ''); | ||
+ (AST_1.hasStandaloneName(ast) ? utils_1.toSafeString(type) : type); | ||
}) | ||
.join('\n' + options.indentWith.repeat(indentDepth)) | ||
.join('\n') | ||
+ '\n' | ||
+ options.indentWith.repeat(indentDepth) + '}'; | ||
+ '}'; | ||
} | ||
function generateComment(comment, options, indentDepth) { | ||
return options.indentWith.repeat(indentDepth) | ||
+ [ | ||
'/**' | ||
].concat(comment.split('\n').map(function (_) { return ' * ' + _; }), [ | ||
' */' | ||
]).join('\n' + options.indentWith.repeat(indentDepth)); | ||
function generateComment(comment) { | ||
return [ | ||
'/**' | ||
].concat(comment.split('\n').map(function (_) { return ' * ' + _; }), [ | ||
' */' | ||
]).join('\n'); | ||
} | ||
function generateStandaloneEnum(ast, options) { | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ 'export ' + (options.enableConstEnums ? 'const ' : '') + ("enum " + utils_1.toSafeString(ast.standaloneName) + " {") | ||
@@ -179,6 +175,3 @@ + '\n' | ||
var ast = _a.ast, keyName = _a.keyName; | ||
return options.indentWith | ||
+ keyName | ||
+ ' = ' | ||
+ generateType(ast, options, 0); | ||
return keyName + ' = ' + generateType(ast, options); | ||
}) | ||
@@ -190,12 +183,10 @@ .join(',\n') | ||
function generateStandaloneInterface(ast, options) { | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ ("export interface " + utils_1.toSafeString(ast.standaloneName) + " ") | ||
+ (ast.superTypes.length > 0 ? "extends " + ast.superTypes.map(function (superType) { return utils_1.toSafeString(superType.standaloneName); }).join(', ') + " " : '') | ||
+ generateInterface(ast, options, 0) | ||
+ (options.enableTrailingSemicolonForInterfaces ? ';' : ''); | ||
+ generateInterface(ast, options); | ||
} | ||
function generateStandaloneType(ast, options) { | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
+ ("export type " + utils_1.toSafeString(ast.standaloneName) + " = " + generateType(lodash_1.omit(ast, 'standaloneName'), options, 0)) | ||
+ (options.enableTrailingSemicolonForTypes ? ';' : ''); | ||
return (AST_1.hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ ("export type " + utils_1.toSafeString(ast.standaloneName) + " = " + generateType(lodash_1.omit(ast, 'standaloneName') /* TODO */, options)); | ||
} | ||
@@ -202,0 +193,0 @@ function escapeKeyName(keyName) { |
import { JSONSchema4 } from 'json-schema'; | ||
import { Options as PrettierOptions } from 'prettier'; | ||
export { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './types/JSONSchema'; | ||
@@ -8,7 +9,3 @@ export interface Options { | ||
enableConstEnums: boolean; | ||
enableTrailingSemicolonForTypes: boolean; | ||
enableTrailingSemicolonForEnums: boolean; | ||
enableTrailingSemicolonForInterfaceProperties: boolean; | ||
enableTrailingSemicolonForInterfaces: boolean; | ||
indentWith: string; | ||
style: PrettierOptions; | ||
} | ||
@@ -15,0 +12,0 @@ export declare const DEFAULT_OPTIONS: Options; |
@@ -59,2 +59,3 @@ "use strict"; | ||
var path_1 = require("path"); | ||
var formatter_1 = require("./formatter"); | ||
var generator_1 = require("./generator"); | ||
@@ -72,7 +73,11 @@ var normalizer_1 = require("./normalizer"); | ||
enableConstEnums: true, | ||
enableTrailingSemicolonForEnums: false, | ||
enableTrailingSemicolonForInterfaceProperties: true, | ||
enableTrailingSemicolonForInterfaces: false, | ||
enableTrailingSemicolonForTypes: true, | ||
indentWith: ' ' | ||
style: { | ||
bracketSpacing: false, | ||
printWidth: 120, | ||
semi: true, | ||
singleQuote: false, | ||
tabWidth: 2, | ||
trailingComma: 'none', | ||
useTabs: false | ||
} | ||
}; | ||
@@ -89,7 +94,7 @@ function compileFromFile(filename, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _options, errors, _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var _options, errors, _a, _b, _c, _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
_options = __assign({}, exports.DEFAULT_OPTIONS, options); | ||
_options = lodash_1.merge({}, exports.DEFAULT_OPTIONS, options); | ||
errors = validator_1.validate(schema, name); | ||
@@ -104,8 +109,9 @@ if (errors.length) { | ||
} | ||
_a = generator_1.generate; | ||
_b = optimizer_1.optimize; | ||
_c = parser_1.parse; | ||
_a = formatter_1.format; | ||
_b = generator_1.generate; | ||
_c = optimizer_1.optimize; | ||
_d = parser_1.parse; | ||
return [4 /*yield*/, resolver_1.dereference(normalizer_1.normalize(schema, name), _options.cwd)]; | ||
case 1: return [2 /*return*/, _a.apply(void 0, [_b.apply(void 0, [_c.apply(void 0, [_d.sent()])]), | ||
_options])]; | ||
case 1: return [2 /*return*/, _a.apply(void 0, [_b.apply(void 0, [_c.apply(void 0, [_d.apply(void 0, [_e.sent()])]), | ||
_options])])]; | ||
} | ||
@@ -112,0 +118,0 @@ }); |
{ | ||
"name": "json-schema-to-typescript", | ||
"version": "4.6.5", | ||
"version": "5.0.0", | ||
"description": "compile json schema to typescript typings", | ||
@@ -49,3 +49,3 @@ "main": "dist/src/index.js", | ||
"cli-color": "^1.2.0", | ||
"json-schema-ref-parser": "^3.3.1", | ||
"json-schema-ref-parser": "^4.0.4", | ||
"json-stringify-safe": "^5.0.1", | ||
@@ -55,2 +55,3 @@ "lodash": "^4.17.4", | ||
"mz": "^2.7.0", | ||
"prettier": "^1.8.2", | ||
"stdin": "0.0.1" | ||
@@ -61,15 +62,14 @@ }, | ||
"@types/json-schema": "^4.0.0", | ||
"@types/lodash": "4.14.74", | ||
"@types/lodash": "4.14.83", | ||
"@types/minimist": "^1.2.0", | ||
"@types/mz": "0.0.31", | ||
"@types/node": "^8.0.28", | ||
"ava": "^0.22.0", | ||
"browserify": "^14.4.0", | ||
"@types/mz": "0.0.32", | ||
"@types/node": "^8.0.50", | ||
"@types/prettier": "^1.7.2", | ||
"ava": "^0.23.0", | ||
"browserify": "^14.5.0", | ||
"concurrently": "^3.5.0", | ||
"fast-diff": "^1.1.1", | ||
"shx": "^0.2.2", | ||
"table-layout": "^0.4.2", | ||
"tsify": "^3.0.3", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.5.2" | ||
"tslint": "^5.8.0", | ||
"typescript": "^2.6.1" | ||
}, | ||
@@ -80,2 +80,3 @@ "ava": { | ||
], | ||
"snapshotDir": "./test/__snapshots__", | ||
"source": [ | ||
@@ -82,0 +83,0 @@ "./dist/test/e2e/*.js", |
@@ -47,3 +47,3 @@ # json-schema-to-typescript [![Build Status][build]](https://circleci.com/gh/bcherny/json-schema-to-typescript) [![npm]](https://www.npmjs.com/package/json-schema-to-typescript) [![mit]](https://opensource.org/licenses/MIT) | ||
age?: number; | ||
hairColor?: ("black" | "brown" | "blue"); | ||
hairColor?: "black" | "brown" | "blue"; | ||
} | ||
@@ -90,7 +90,3 @@ ``` | ||
| enableConstEnums | boolean | Prepend enums with `const`? | | ||
| enableTrailingSemicolonForTypes | boolean | | | ||
| enableTrailingSemicolonForEnums | boolean | | | ||
| enableTrailingSemicolonForInterfaceProperties | boolean | | | ||
| enableTrailingSemicolonForInterfaces | boolean | | | ||
| indentWith | string | Tabs or spaces? | | ||
| style | object | A [Prettier](https://prettier.io/docs/en/options.html) configuration | | ||
@@ -97,0 +93,0 @@ ## CLI |
#!/usr/bin/env node | ||
import { JSONSchema4 } from 'json-schema' | ||
import minimist = require('minimist') | ||
import { readFile, writeFile } from 'mz/fs' | ||
import { resolve } from 'path' | ||
import stdin = require('stdin') | ||
import { JSONSchema4 } from 'json-schema' | ||
import { compile } from './index' | ||
import minimist = require('minimist') | ||
@@ -10,0 +10,0 @@ main(minimist(process.argv.slice(2), { |
@@ -130,3 +130,3 @@ import { whiteBright } from 'cli-color' | ||
function generateType(ast: AST, options: Options, indentDepth: number): string { | ||
function generateType(ast: AST, options: Options): string { | ||
log(whiteBright.bgMagenta('generator'), ast) | ||
@@ -141,8 +141,8 @@ | ||
case 'ARRAY': return (() => { | ||
let type = generateType(ast.params, options, indentDepth + 1) | ||
let type = generateType(ast.params, options) | ||
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]' | ||
})() | ||
case 'BOOLEAN': return 'boolean' | ||
case 'INTERFACE': return generateInterface(ast, options, indentDepth + 1) | ||
case 'INTERSECTION': return generateSetOperation(ast, options, indentDepth) | ||
case 'INTERFACE': return generateInterface(ast, options) | ||
case 'INTERSECTION': return generateSetOperation(ast, options) | ||
case 'LITERAL': return JSON.stringify(ast.params) | ||
@@ -155,5 +155,5 @@ case 'NUMBER': return 'number' | ||
case 'TUPLE': return '[' | ||
+ ast.params.map(_ => generateType(_, options, indentDepth + 1)).join(', ') | ||
+ ast.params.map(_ => generateType(_, options)).join(', ') | ||
+ ']' | ||
case 'UNION': return generateSetOperation(ast, options, indentDepth) | ||
case 'UNION': return generateSetOperation(ast, options) | ||
} | ||
@@ -165,4 +165,4 @@ } | ||
*/ | ||
function generateSetOperation(ast: TIntersection | TUnion, options: Options, indentDepth: number): string { | ||
const members = (ast as TUnion).params.map(_ => generateType(_, options, indentDepth)) | ||
function generateSetOperation(ast: TIntersection | TUnion, options: Options): string { | ||
const members = (ast as TUnion).params.map(_ => generateType(_, options)) | ||
const separator = ast.type === 'UNION' ? '|' : '&' | ||
@@ -174,13 +174,10 @@ return members.length === 1 ? members[0] : '(' + members.join(' ' + separator + ' ') + ')' | ||
ast: TInterface, | ||
options: Options, | ||
indentDepth: number | ||
options: Options | ||
): string { | ||
return `{` | ||
+ '\n' | ||
+ options.indentWith.repeat(indentDepth) | ||
+ ast.params | ||
.map(({ isRequired, keyName, ast }) => [isRequired, keyName, ast, generateType(ast, options, indentDepth)] as [boolean, string, AST, string]) | ||
.map(({ isRequired, keyName, ast }) => [isRequired, keyName, ast, generateType(ast, options)] as [boolean, string, AST, string]) | ||
.map(([isRequired, keyName, ast, type]) => | ||
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment, options, indentDepth + 1) + '\n' : '') | ||
+ options.indentWith | ||
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment) + '\n' : '') | ||
+ escapeKeyName(keyName) | ||
@@ -190,27 +187,22 @@ + (isRequired ? '' : '?') | ||
+ (hasStandaloneName(ast) ? toSafeString(type) : type) | ||
+ (options.enableTrailingSemicolonForInterfaceProperties ? ';' : '') | ||
) | ||
.join('\n' + options.indentWith.repeat(indentDepth)) | ||
.join('\n') | ||
+ '\n' | ||
+ options.indentWith.repeat(indentDepth) + '}' | ||
+ '}' | ||
} | ||
function generateComment(comment: string, options: Options, indentDepth: number): string { | ||
return options.indentWith.repeat(indentDepth) | ||
+ [ | ||
'/**', | ||
...comment.split('\n').map(_ => ' * ' + _), | ||
' */' | ||
].join('\n' + options.indentWith.repeat(indentDepth)) | ||
function generateComment(comment: string): string { | ||
return [ | ||
'/**', | ||
...comment.split('\n').map(_ => ' * ' + _), | ||
' */' | ||
].join('\n') | ||
} | ||
function generateStandaloneEnum(ast: TEnum, options: Options): string { | ||
return (hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
return (hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ 'export ' + (options.enableConstEnums ? 'const ' : '') + `enum ${toSafeString(ast.standaloneName)} {` | ||
+ '\n' | ||
+ ast.params.map(({ ast, keyName }) => | ||
options.indentWith | ||
+ keyName | ||
+ ' = ' | ||
+ generateType(ast, options, 0) | ||
keyName + ' = ' + generateType(ast, options) | ||
) | ||
@@ -223,13 +215,11 @@ .join(',\n') | ||
function generateStandaloneInterface(ast: TNamedInterface, options: Options): string { | ||
return (hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
return (hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ `export interface ${toSafeString(ast.standaloneName)} ` | ||
+ (ast.superTypes.length > 0 ? `extends ${ast.superTypes.map(superType => toSafeString(superType.standaloneName)).join(', ')} ` : '') | ||
+ generateInterface(ast, options, 0) | ||
+ (options.enableTrailingSemicolonForInterfaces ? ';' : '') | ||
+ generateInterface(ast, options) | ||
} | ||
function generateStandaloneType(ast: ASTWithStandaloneName, options: Options): string { | ||
return (hasComment(ast) ? generateComment(ast.comment, options, 0) + '\n' : '') | ||
+ `export type ${toSafeString(ast.standaloneName)} = ${generateType(omit<ASTWithStandaloneName, AST>(ast, 'standaloneName'), options, 0)}` | ||
+ (options.enableTrailingSemicolonForTypes ? ';' : '') | ||
return (hasComment(ast) ? generateComment(ast.comment) + '\n' : '') | ||
+ `export type ${toSafeString(ast.standaloneName)} = ${generateType(omit<AST>(ast, 'standaloneName') as AST /* TODO */, options)}` | ||
} | ||
@@ -236,0 +226,0 @@ |
import { readFileSync } from 'fs' | ||
import { JSONSchema4 } from 'json-schema' | ||
import { endsWith } from 'lodash' | ||
import { endsWith, merge } from 'lodash' | ||
import { dirname } from 'path' | ||
import { Options as PrettierOptions } from 'prettier' | ||
import { format } from './formatter' | ||
import { generate } from './generator' | ||
@@ -20,7 +22,3 @@ import { normalize } from './normalizer' | ||
enableConstEnums: boolean | ||
enableTrailingSemicolonForTypes: boolean | ||
enableTrailingSemicolonForEnums: boolean | ||
enableTrailingSemicolonForInterfaceProperties: boolean | ||
enableTrailingSemicolonForInterfaces: boolean | ||
indentWith: string | ||
style: PrettierOptions | ||
} | ||
@@ -37,7 +35,11 @@ | ||
enableConstEnums: true, // by default, avoid generating code | ||
enableTrailingSemicolonForEnums: false, | ||
enableTrailingSemicolonForInterfaceProperties: true, | ||
enableTrailingSemicolonForInterfaces: false, | ||
enableTrailingSemicolonForTypes: true, | ||
indentWith: ' ' | ||
style: { | ||
bracketSpacing: false, | ||
printWidth: 120, | ||
semi: true, | ||
singleQuote: false, | ||
tabWidth: 2, | ||
trailingComma: 'none', | ||
useTabs: false | ||
} | ||
} | ||
@@ -70,3 +72,3 @@ | ||
const _options = { ...DEFAULT_OPTIONS, ...options } | ||
const _options = merge({}, DEFAULT_OPTIONS, options) | ||
@@ -84,8 +86,8 @@ const errors = validate(schema, name) | ||
return generate( | ||
return format(generate( | ||
optimize(parse(await dereference(normalize(schema, name), _options.cwd))), | ||
_options | ||
) | ||
)) | ||
} | ||
export class ValidationError extends Error {} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6134700
14
54
8
3786
168
1
+ Addedprettier@^1.8.2
+ Addedjson-schema-ref-parser@4.1.1(transitive)
+ Addedprettier@1.19.1(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedes6-promise@4.2.8(transitive)
- Removedjson-schema-ref-parser@3.3.1(transitive)
- Removedlodash.get@4.4.2(transitive)
- Removedlodash.isequal@4.5.0(transitive)
- Removedvalidator@10.11.0(transitive)
- Removedz-schema@3.25.1(transitive)