json-schema-to-typescript
Advanced tools
Comparing version 3.1.1 to 3.1.3
#!/usr/bin/env node | ||
"use strict"; | ||
var fs_1 = require('mz/fs'); | ||
var path_1 = require('path'); | ||
var index_1 = require('./index'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = require("mz/fs"); | ||
var path_1 = require("path"); | ||
var index_1 = require("./index"); | ||
// require it instead of import till typings for stdin are not released | ||
var stdin = require('stdin'); | ||
// require it instead of import till typings for minimist are kind of broken | ||
var minimist = require('minimist'); | ||
var minimist = require("minimist"); | ||
var argv = minimist(process.argv.slice(2), { | ||
@@ -42,3 +43,3 @@ alias: { | ||
} | ||
return fs_1.readFile(argIn); | ||
return fs_1.readFile(argIn, { encoding: 'utf-8' }); | ||
} | ||
@@ -60,3 +61,3 @@ function writeOutput(compiled) { | ||
process.stdout.write([ | ||
(pkg.name + " " + pkg.version), | ||
pkg.name + " " + pkg.version, | ||
'Usage: json2ts [--input, -i] [IN_FILE] [--output, -o] [OUT_FILE]', | ||
@@ -63,0 +64,0 @@ '', |
"use strict"; | ||
var TsType = require('./TsTypes'); | ||
var fs_1 = require('fs'); | ||
var lodash_1 = require('lodash'); | ||
var path_1 = require('path'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = require("fs"); | ||
var lodash_1 = require("lodash"); | ||
var path_1 = require("path"); | ||
var TsType = require("./TsTypes"); | ||
var RuleType; | ||
@@ -45,4 +46,3 @@ (function (RuleType) { | ||
var _this = this; | ||
return Array.from(this.namedEnums.values()).concat(Array.from(this.declarations.values())) | ||
.map(function (_) { return _.toDeclaration(_this.settings); }) | ||
return Array.from(this.namedEnums.values()).concat(Array.from(this.declarations.values())).map(function (_) { return _.toDeclaration(_this.settings); }) | ||
.join('\n'); | ||
@@ -115,3 +115,3 @@ }; | ||
} | ||
var file = tryFn(function () { return fs_1.readFileSync(fullPath); }, function () { throw new ReferenceError("Unable to find referenced file \"" + fullPath + "\""); }); | ||
var file = fs_1.readFileSync(fullPath); | ||
var contents = tryFn(function () { return JSON.parse(file.toString()); }, function () { throw new TypeError("Referenced local schema \"" + fullPath + "\" contains malformed JSON"); }); | ||
@@ -125,3 +125,3 @@ var targetType = this.toTsType(contents, propName, true); | ||
} | ||
return new TsType.Reference(id); | ||
return new TsType.TReference(id); | ||
}; | ||
@@ -132,3 +132,3 @@ // eg. "#/definitions/diskDevice" => ["definitions", "diskDevice"] | ||
if (refPath === '#' || refPath === '#/') { | ||
return TsType.Interface.reference(this.id); | ||
return TsType.TInterface.reference(this.id); | ||
} | ||
@@ -167,26 +167,26 @@ if (refPath[0] !== '#') { | ||
case RuleType.Enum: | ||
return new TsType.Union(rule.enum.map(function (_) { return new TsType.Literal(_); })); | ||
return new TsType.TUnion(rule.enum.map(function (_) { return new TsType.TLiteral(_); })); | ||
case RuleType.NamedEnum: | ||
Compiler.validateNamedEnum(rule); | ||
var name = this.generateEnumName(rule, propName); | ||
var tsEnum = new TsType.Enum(name, lodash_1.zip(rule.tsEnumNames, rule.enum).map(function (_) { return new TsType.EnumValue(_); })); | ||
var tsEnum = new TsType.TEnum(name, lodash_1.zip(rule.tsEnumNames, rule.enum).map(function (_) { return new TsType.TEnumValue(_); })); | ||
this.namedEnums.set(name, tsEnum); | ||
return tsEnum; | ||
case RuleType.Any: return new TsType.Any; | ||
case RuleType.Literal: return new TsType.Literal(rule); | ||
case RuleType.TypedArray: return new TsType.Array(this.toTsType(rule.items)); | ||
case RuleType.Array: return new TsType.Array; | ||
case RuleType.Boolean: return new TsType.Boolean; | ||
case RuleType.Null: return new TsType.Null; | ||
case RuleType.Number: return new TsType.Number; | ||
case RuleType.Object: return new TsType.Object; | ||
case RuleType.String: return new TsType.String; | ||
case RuleType.Any: return new TsType.TAny; | ||
case RuleType.Literal: return new TsType.TLiteral(rule); | ||
case RuleType.TypedArray: return new TsType.TArray(this.toTsType(rule.items)); | ||
case RuleType.Array: return new TsType.TArray; | ||
case RuleType.Boolean: return new TsType.TBoolean; | ||
case RuleType.Null: return new TsType.TNull; | ||
case RuleType.Number: return new TsType.TNumber; | ||
case RuleType.Object: return new TsType.TObject; | ||
case RuleType.String: return new TsType.TString; | ||
case RuleType.AllOf: | ||
return new TsType.Intersection(rule.allOf.map(function (_) { return _this.toTsType(_); })); | ||
return new TsType.TIntersection(rule.allOf.map(function (_) { return _this.toTsType(_); })); | ||
case RuleType.AnyOf: | ||
return new TsType.Union(rule.anyOf.map(function (_) { return _this.toTsType(_); })); | ||
return new TsType.TUnion(rule.anyOf.map(function (_) { return _this.toTsType(_); })); | ||
case RuleType.Reference: | ||
return this.resolveRef(rule.$ref, propName); | ||
case RuleType.Union: | ||
return new TsType.Union(rule.type.map(function (_) { return _this.toTsType({ type: _ }); })); | ||
return new TsType.TUnion(rule.type.map(function (_) { return _this.toTsType({ type: _ }); })); | ||
} | ||
@@ -227,3 +227,3 @@ throw new Error('Unknown rule:' + rule.toString()); | ||
if ('default' in schema) | ||
return new TsType.Null; | ||
return new TsType.TNull; | ||
} | ||
@@ -233,4 +233,4 @@ if (this.supportsAdditionalProperties(copy)) { | ||
if (short && props.length === 0) | ||
return new TsType.Any; | ||
var type = short ? new TsType.Any : this.toTsType(copy.additionalProperties); | ||
return new TsType.TAny; | ||
var type = short ? new TsType.TAny : this.toTsType(copy.additionalProperties); | ||
props.push({ | ||
@@ -242,19 +242,19 @@ name: '[k: string]', | ||
} | ||
return new TsType.Interface(props); | ||
return new TsType.TInterface(props); | ||
}; | ||
Compiler.DEFAULT_SETTINGS = { | ||
declareReferenced: true, | ||
endPropertyWithSemicolon: true, | ||
endTypeWithSemicolon: true, | ||
useConstEnums: true, | ||
useFullReferencePathAsName: false | ||
}; | ||
Compiler.DEFAULT_SCHEMA = { | ||
additionalProperties: true, | ||
properties: {}, | ||
required: [], | ||
type: 'object' | ||
}; | ||
return Compiler; | ||
}()); | ||
Compiler.DEFAULT_SETTINGS = { | ||
declareReferenced: true, | ||
endPropertyWithSemicolon: true, | ||
endTypeWithSemicolon: true, | ||
useConstEnums: true, | ||
useFullReferencePathAsName: false | ||
}; | ||
Compiler.DEFAULT_SCHEMA = { | ||
additionalProperties: true, | ||
properties: {}, | ||
required: [], | ||
type: 'object' | ||
}; | ||
function compile(schema, path, settings) { | ||
@@ -261,0 +261,0 @@ return new Compiler(schema, path, settings).toString(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=JSONSchema.js.map |
@@ -19,33 +19,33 @@ import { Settings } from './index'; | ||
} | ||
export declare class Any extends TsType<void> { | ||
export declare class TAny extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class String extends TsType<void> { | ||
export declare class TString extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class Boolean extends TsType<void> { | ||
export declare class TBoolean extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class Number extends TsType<void> { | ||
export declare class TNumber extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class Object extends TsType<void> { | ||
export declare class TObject extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class Null extends TsType<void> { | ||
export declare class TNull extends TsType<void> { | ||
constructor(); | ||
toString(): string; | ||
} | ||
export declare class Literal<T> extends TsType<T> { | ||
export declare class TLiteral<T> extends TsType<T> { | ||
toString(): string; | ||
} | ||
export declare class Reference extends TsType<string> { | ||
export declare class TReference extends TsType<string> { | ||
toString(): string; | ||
} | ||
export declare class EnumValue extends TsType<string> { | ||
export declare class TEnumValue extends TsType<string> { | ||
identifier: string; | ||
@@ -57,5 +57,5 @@ value: string; | ||
} | ||
export declare class Enum extends TsType<EnumValue[]> { | ||
export declare class TEnum extends TsType<TEnumValue[]> { | ||
id: string; | ||
constructor(id: string, value: EnumValue[]); | ||
constructor(id: string, value: TEnumValue[]); | ||
isSimpleType(): boolean; | ||
@@ -65,16 +65,16 @@ toString(settings: Settings): string; | ||
} | ||
export declare class Array extends TsType<TsType<any>> { | ||
export declare class TArray extends TsType<TsType<any>> { | ||
constructor(value?: TsType<any>); | ||
toString(settings: Settings): string; | ||
} | ||
export declare class Intersection<T> extends TsType<TsType<T>[]> { | ||
export declare class TIntersection<T> extends TsType<TsType<T>[]> { | ||
isSimpleType(): boolean; | ||
toString(settings: Settings): string; | ||
} | ||
export declare class Union<T> extends TsType<TsType<T>[]> { | ||
export declare class TUnion<T> extends TsType<TsType<T>[]> { | ||
isSimpleType(): boolean; | ||
toString(settings: Settings): string; | ||
} | ||
export declare class Interface extends TsType<TsProp<any>[]> { | ||
static reference(id: string): Interface; | ||
export declare class TInterface extends TsType<TsProp<any>[]> { | ||
static reference(id: string): TInterface; | ||
toString(settings: Settings): string; | ||
@@ -81,0 +81,0 @@ isSimpleType(): boolean; |
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var lodash_1 = require('lodash'); | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var lodash_1 = require("lodash"); | ||
var COMMENT_START = '/**'; | ||
@@ -36,96 +42,97 @@ var COMMENT_INDENT = ' * '; | ||
exports.TsType = TsType; | ||
var Any = (function (_super) { | ||
__extends(Any, _super); | ||
function Any() { | ||
_super.call(this, undefined); | ||
var TAny = (function (_super) { | ||
__extends(TAny, _super); | ||
function TAny() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
Any.prototype.toString = function () { | ||
TAny.prototype.toString = function () { | ||
return 'any'; | ||
}; | ||
return Any; | ||
return TAny; | ||
}(TsType)); | ||
exports.Any = Any; | ||
var String = (function (_super) { | ||
__extends(String, _super); | ||
function String() { | ||
_super.call(this, undefined); | ||
exports.TAny = TAny; | ||
var TString = (function (_super) { | ||
__extends(TString, _super); | ||
function TString() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
String.prototype.toString = function () { | ||
TString.prototype.toString = function () { | ||
return 'string'; | ||
}; | ||
return String; | ||
return TString; | ||
}(TsType)); | ||
exports.String = String; | ||
var Boolean = (function (_super) { | ||
__extends(Boolean, _super); | ||
function Boolean() { | ||
_super.call(this, undefined); | ||
exports.TString = TString; | ||
var TBoolean = (function (_super) { | ||
__extends(TBoolean, _super); | ||
function TBoolean() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
Boolean.prototype.toString = function () { | ||
TBoolean.prototype.toString = function () { | ||
return 'boolean'; | ||
}; | ||
return Boolean; | ||
return TBoolean; | ||
}(TsType)); | ||
exports.Boolean = Boolean; | ||
var Number = (function (_super) { | ||
__extends(Number, _super); | ||
function Number() { | ||
_super.call(this, undefined); | ||
exports.TBoolean = TBoolean; | ||
var TNumber = (function (_super) { | ||
__extends(TNumber, _super); | ||
function TNumber() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
Number.prototype.toString = function () { | ||
TNumber.prototype.toString = function () { | ||
return 'number'; | ||
}; | ||
return Number; | ||
return TNumber; | ||
}(TsType)); | ||
exports.Number = Number; | ||
var Object = (function (_super) { | ||
__extends(Object, _super); | ||
function Object() { | ||
_super.call(this, undefined); | ||
exports.TNumber = TNumber; | ||
var TObject = (function (_super) { | ||
__extends(TObject, _super); | ||
function TObject() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
Object.prototype.toString = function () { | ||
TObject.prototype.toString = function () { | ||
return 'Object'; | ||
}; | ||
return Object; | ||
return TObject; | ||
}(TsType)); | ||
exports.Object = Object; | ||
var Null = (function (_super) { | ||
__extends(Null, _super); | ||
function Null() { | ||
_super.call(this, undefined); | ||
exports.TObject = TObject; | ||
var TNull = (function (_super) { | ||
__extends(TNull, _super); | ||
function TNull() { | ||
return _super.call(this, undefined) || this; | ||
} | ||
Null.prototype.toString = function () { | ||
TNull.prototype.toString = function () { | ||
return 'null'; | ||
}; | ||
return Null; | ||
return TNull; | ||
}(TsType)); | ||
exports.Null = Null; | ||
var Literal = (function (_super) { | ||
__extends(Literal, _super); | ||
function Literal() { | ||
_super.apply(this, arguments); | ||
exports.TNull = TNull; | ||
var TLiteral = (function (_super) { | ||
__extends(TLiteral, _super); | ||
function TLiteral() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Literal.prototype.toString = function () { | ||
TLiteral.prototype.toString = function () { | ||
return JSON.stringify(this.value); | ||
}; | ||
return Literal; | ||
return TLiteral; | ||
}(TsType)); | ||
exports.Literal = Literal; | ||
var Reference = (function (_super) { | ||
__extends(Reference, _super); | ||
function Reference() { | ||
_super.apply(this, arguments); | ||
exports.TLiteral = TLiteral; | ||
var TReference = (function (_super) { | ||
__extends(TReference, _super); | ||
function TReference() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Reference.prototype.toString = function () { return this.value; }; | ||
return Reference; | ||
TReference.prototype.toString = function () { return this.value; }; | ||
return TReference; | ||
}(TsType)); | ||
exports.Reference = Reference; | ||
var EnumValue = (function (_super) { | ||
__extends(EnumValue, _super); | ||
function EnumValue(_a) { | ||
exports.TReference = TReference; | ||
var TEnumValue = (function (_super) { | ||
__extends(TEnumValue, _super); | ||
function TEnumValue(_a) { | ||
var identifier = _a[0], value = _a[1]; | ||
_super.call(this, value); | ||
this.identifier = identifier; | ||
var _this = _super.call(this, value) || this; | ||
_this.identifier = identifier; | ||
return _this; | ||
} | ||
EnumValue.prototype.toDeclaration = function () { | ||
TEnumValue.prototype.toDeclaration = function () { | ||
// if there is a value associated with the identifier, declare as identifier=value | ||
@@ -135,19 +142,20 @@ // else declare as identifier | ||
}; | ||
EnumValue.prototype.toString = function () { | ||
TEnumValue.prototype.toString = function () { | ||
return "Enum" + this.identifier; | ||
}; | ||
return EnumValue; | ||
return TEnumValue; | ||
}(TsType)); | ||
exports.EnumValue = EnumValue; | ||
var Enum = (function (_super) { | ||
__extends(Enum, _super); | ||
function Enum(id, value) { | ||
_super.call(this, value); | ||
this.id = id; | ||
exports.TEnumValue = TEnumValue; | ||
var TEnum = (function (_super) { | ||
__extends(TEnum, _super); | ||
function TEnum(id, value) { | ||
var _this = _super.call(this, value) || this; | ||
_this.id = id; | ||
return _this; | ||
} | ||
Enum.prototype.isSimpleType = function () { return false; }; | ||
Enum.prototype.toString = function (settings) { | ||
TEnum.prototype.isSimpleType = function () { return false; }; | ||
TEnum.prototype.toString = function (settings) { | ||
return this.safeId(); | ||
}; | ||
Enum.prototype.toDeclaration = function (settings) { | ||
TEnum.prototype.toDeclaration = function (settings) { | ||
return this.toBlockComment() | ||
@@ -161,40 +169,40 @@ + ("export " + (settings.useConstEnums ? 'const ' : '') + "enum " + this.safeId() + " {") | ||
}; | ||
return Enum; | ||
return TEnum; | ||
}(TsType)); | ||
exports.Enum = Enum; | ||
var Array = (function (_super) { | ||
__extends(Array, _super); | ||
function Array(value) { | ||
if (value === void 0) { value = new Any; } | ||
_super.call(this, value); | ||
exports.TEnum = TEnum; | ||
var TArray = (function (_super) { | ||
__extends(TArray, _super); | ||
function TArray(value) { | ||
if (value === void 0) { value = new TAny; } | ||
return _super.call(this, value) || this; | ||
} | ||
Array.prototype.toString = function (settings) { | ||
TArray.prototype.toString = function (settings) { | ||
var type = this.value.toType(settings); | ||
return (type.indexOf('|') > -1 || type.indexOf('&') > -1 ? "(" + type + ")" : type) + "[]"; // hacky | ||
}; | ||
return Array; | ||
return TArray; | ||
}(TsType)); | ||
exports.Array = Array; | ||
var Intersection = (function (_super) { | ||
__extends(Intersection, _super); | ||
function Intersection() { | ||
_super.apply(this, arguments); | ||
exports.TArray = TArray; | ||
var TIntersection = (function (_super) { | ||
__extends(TIntersection, _super); | ||
function TIntersection() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Intersection.prototype.isSimpleType = function () { return this.value.length <= 1; }; | ||
Intersection.prototype.toString = function (settings) { | ||
TIntersection.prototype.isSimpleType = function () { return this.value.length <= 1; }; | ||
TIntersection.prototype.toString = function (settings) { | ||
return this.value | ||
.filter(function (_) { return !(_ instanceof Null); }) | ||
.filter(function (_) { return !(_ instanceof TNull); }) | ||
.map(function (_) { return _.toType(settings); }) | ||
.join(' & '); | ||
}; | ||
return Intersection; | ||
return TIntersection; | ||
}(TsType)); | ||
exports.Intersection = Intersection; | ||
var Union = (function (_super) { | ||
__extends(Union, _super); | ||
function Union() { | ||
_super.apply(this, arguments); | ||
exports.TIntersection = TIntersection; | ||
var TUnion = (function (_super) { | ||
__extends(TUnion, _super); | ||
function TUnion() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Union.prototype.isSimpleType = function () { return this.value.length <= 1; }; | ||
Union.prototype.toString = function (settings) { | ||
TUnion.prototype.isSimpleType = function () { return this.value.length <= 1; }; | ||
TUnion.prototype.toString = function (settings) { | ||
return this.value | ||
@@ -204,31 +212,31 @@ .map(function (_) { return _.toType(settings); }) | ||
}; | ||
return Union; | ||
return TUnion; | ||
}(TsType)); | ||
exports.Union = Union; | ||
var Interface = (function (_super) { | ||
__extends(Interface, _super); | ||
function Interface() { | ||
_super.apply(this, arguments); | ||
exports.TUnion = TUnion; | ||
var TInterface = (function (_super) { | ||
__extends(TInterface, _super); | ||
function TInterface() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
Interface.reference = function (id) { | ||
var ret = new Interface([]); | ||
TInterface.reference = function (id) { | ||
var ret = new TInterface([]); | ||
ret.id = id; | ||
return ret; | ||
}; | ||
Interface.prototype.toString = function (settings) { | ||
TInterface.prototype.toString = function (settings) { | ||
return "{\n" | ||
+ (this.value.map(function (_) { | ||
return ("" + INDENT_STRING + (_.type.description | ||
return "" + INDENT_STRING + (_.type.description | ||
? generateComment(_.type.description).join("\n" + INDENT_STRING) + ("\n" + INDENT_STRING) | ||
: '') + _.name + (_.required ? '' : '?') + ": " + _.type.toType(settings).replace(/\n/g, '\n' + INDENT_STRING) // ghetto nested indents | ||
+ (settings.endPropertyWithSemicolon ? ';' : '')); | ||
+ (settings.endPropertyWithSemicolon ? ';' : ''); | ||
}).join('\n') + "\n}"); | ||
}; | ||
Interface.prototype.isSimpleType = function () { return false; }; | ||
Interface.prototype.toDeclaration = function (settings) { | ||
TInterface.prototype.isSimpleType = function () { return false; }; | ||
TInterface.prototype.toDeclaration = function (settings) { | ||
return this.toBlockComment() + "export interface " + this.safeId() + " " + this.toString(settings); | ||
}; | ||
return Interface; | ||
return TInterface; | ||
}(TsType)); | ||
exports.Interface = Interface; | ||
exports.TInterface = TInterface; | ||
// eg. | ||
@@ -235,0 +243,0 @@ // foo -> Foo |
{ | ||
"name": "json-schema-to-typescript", | ||
"version": "3.1.1", | ||
"version": "3.1.3", | ||
"description": "compile json schema to typescript typings", | ||
@@ -42,15 +42,15 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"lodash": "^4.6.1", | ||
"lodash": "^4.17.4", | ||
"minimist": "^1.2.0", | ||
"mz": "^2.4.0", | ||
"mz": "^2.6.0", | ||
"stdin": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/lodash": "0.0.28", | ||
"@types/minimist": "^1.1.29", | ||
"@types/lodash": "4.14.55", | ||
"@types/minimist": "^1.2.0", | ||
"@types/mz": "0.0.30", | ||
"@types/node": "^4.0.30", | ||
"ava": "^0.16.0", | ||
"tslint": "^3.15.1", | ||
"typescript": "^2.0.0" | ||
"@types/node": "^7.0.8", | ||
"ava": "^0.18.2", | ||
"tslint": "^4.5.1", | ||
"typescript": "^2.2.1" | ||
}, | ||
@@ -57,0 +57,0 @@ "ava": { |
@@ -1,6 +0,6 @@ | ||
import { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './JSONSchema' | ||
import * as TsType from './TsTypes' | ||
import { readFile, readFileSync } from 'fs' | ||
import { get, isPlainObject, last, map, merge, zip } from 'lodash' | ||
import { join, parse, ParsedPath, resolve } from 'path' | ||
import { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './JSONSchema' | ||
import * as TsType from './TsTypes' | ||
@@ -7,0 +7,0 @@ enum RuleType { |
@@ -0,3 +1,3 @@ | ||
import { camelCase, upperFirst } from 'lodash' | ||
import { Settings } from './index' | ||
import { camelCase, upperFirst } from 'lodash' | ||
@@ -4,0 +4,0 @@ const COMMENT_START = '/**' |
@@ -55,3 +55,3 @@ #!/usr/bin/env node | ||
return readFile(argIn) | ||
return readFile(argIn, { encoding: 'utf-8' }) | ||
} | ||
@@ -58,0 +58,0 @@ |
@@ -1,6 +0,6 @@ | ||
import { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './JSONSchema' | ||
import * as TsType from './TsTypes' | ||
import { readFile, readFileSync } from 'fs' | ||
import { get, isPlainObject, last, map, merge, zip } from 'lodash' | ||
import { join, parse, ParsedPath, resolve } from 'path' | ||
import { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './JSONSchema' | ||
import * as TsType from './TsTypes' | ||
@@ -49,3 +49,3 @@ enum RuleType { | ||
this.settings = Object.assign({}, Compiler.DEFAULT_SETTINGS, settings) | ||
this.declareType(this.toTsType(this.schema, '', true) as TsType.Interface, this.id, this.id) | ||
this.declareType(this.toTsType(this.schema, '', true) as TsType.TInterface, this.id, this.id) | ||
} | ||
@@ -66,3 +66,3 @@ | ||
private declarations: Map<string, TsType.TsType<any>> | ||
private namedEnums: Map<string, TsType.Enum> | ||
private namedEnums: Map<string, TsType.TEnum> | ||
private filePath: ParsedPath | ||
@@ -135,3 +135,3 @@ | ||
private resolveRefFromLocalFS<T>(refPath: string, propName: string): TsType.Reference { | ||
private resolveRefFromLocalFS<T>(refPath: string, propName: string): TsType.TReference { | ||
const fullPath = resolve(join(this.filePath.dir, refPath)) | ||
@@ -143,6 +143,3 @@ | ||
const file = tryFn( | ||
() => readFileSync(fullPath), | ||
() => { throw new ReferenceError(`Unable to find referenced file "${fullPath}"`) } | ||
) | ||
const file = readFileSync(fullPath) | ||
const contents = tryFn( | ||
@@ -158,6 +155,6 @@ () => JSON.parse(file.toString()), | ||
if (this.settings.declareReferenced) { | ||
this.declareType(targetType as TsType.Interface, id, id) | ||
this.declareType(targetType as TsType.TInterface, id, id) | ||
} | ||
return new TsType.Reference(id) | ||
return new TsType.TReference(id) | ||
} | ||
@@ -169,3 +166,3 @@ | ||
if (refPath === '#' || refPath === '#/'){ | ||
return TsType.Interface.reference(this.id) | ||
return TsType.TInterface.reference(this.id) | ||
} | ||
@@ -188,3 +185,3 @@ | ||
if (this.settings.declareReferenced || !type.isSimpleType()) { | ||
this.declareType(type as TsType.Interface, parts.join('/'), this.settings.useFullReferencePathAsName ? parts.join('/') : last(parts)) | ||
this.declareType(type as TsType.TInterface, parts.join('/'), this.settings.useFullReferencePathAsName ? parts.join('/') : last(parts)) | ||
} | ||
@@ -194,3 +191,3 @@ return type | ||
private declareType(type: TsType.Interface, refPath: string, id: string) { | ||
private declareType(type: TsType.TInterface, refPath: string, id: string) { | ||
type.id = id | ||
@@ -212,4 +209,4 @@ this.declarations.set(refPath, type) | ||
case RuleType.Enum: | ||
return new TsType.Union( | ||
(rule as EnumJSONSchema).enum.map(_ => new TsType.Literal(_)) | ||
return new TsType.TUnion( | ||
(rule as EnumJSONSchema).enum.map(_ => new TsType.TLiteral(_)) | ||
) | ||
@@ -221,7 +218,7 @@ | ||
const name = this.generateEnumName(rule, propName) | ||
const tsEnum = new TsType.Enum(name, | ||
const tsEnum = new TsType.TEnum(name, | ||
zip( | ||
(rule as NamedEnumJSONSchema).tsEnumNames, | ||
(rule as NamedEnumJSONSchema).enum | ||
).map(_ => new TsType.EnumValue(_)) | ||
).map(_ => new TsType.TEnumValue(_)) | ||
) | ||
@@ -231,19 +228,19 @@ this.namedEnums.set(name, tsEnum) | ||
case RuleType.Any: return new TsType.Any | ||
case RuleType.Literal: return new TsType.Literal(rule) | ||
case RuleType.TypedArray: return new TsType.Array(this.toTsType(rule.items!)) | ||
case RuleType.Array: return new TsType.Array | ||
case RuleType.Boolean: return new TsType.Boolean | ||
case RuleType.Null: return new TsType.Null | ||
case RuleType.Number: return new TsType.Number | ||
case RuleType.Object: return new TsType.Object | ||
case RuleType.String: return new TsType.String | ||
case RuleType.Any: return new TsType.TAny | ||
case RuleType.Literal: return new TsType.TLiteral(rule) | ||
case RuleType.TypedArray: return new TsType.TArray(this.toTsType(rule.items!)) | ||
case RuleType.Array: return new TsType.TArray | ||
case RuleType.Boolean: return new TsType.TBoolean | ||
case RuleType.Null: return new TsType.TNull | ||
case RuleType.Number: return new TsType.TNumber | ||
case RuleType.Object: return new TsType.TObject | ||
case RuleType.String: return new TsType.TString | ||
case RuleType.AllOf: | ||
return new TsType.Intersection(rule.allOf!.map(_ => this.toTsType(_))) | ||
return new TsType.TIntersection(rule.allOf!.map(_ => this.toTsType(_))) | ||
case RuleType.AnyOf: | ||
return new TsType.Union(rule.anyOf!.map(_ => this.toTsType(_))) | ||
return new TsType.TUnion(rule.anyOf!.map(_ => this.toTsType(_))) | ||
case RuleType.Reference: | ||
return this.resolveRef(rule.$ref!, propName!) | ||
case RuleType.Union: | ||
return new TsType.Union((rule.type as any[]).map(_ => this.toTsType({ type: _ }))) | ||
return new TsType.TUnion((rule.type as any[]).map(_ => this.toTsType({ type: _ }))) | ||
} | ||
@@ -277,3 +274,3 @@ throw new Error('Unknown rule:' + rule.toString()) | ||
} | ||
private toTsDeclaration(schema: JSONSchema): TsType.Interface | TsType.Null { | ||
private toTsDeclaration(schema: JSONSchema): TsType.TInterface | TsType.TNull { | ||
const copy = merge({}, Compiler.DEFAULT_SCHEMA, schema) | ||
@@ -291,3 +288,3 @@ const props = map( | ||
if ('default' in schema) | ||
return new TsType.Null | ||
return new TsType.TNull | ||
} | ||
@@ -297,4 +294,4 @@ if (this.supportsAdditionalProperties(copy)) { | ||
if (short && props.length === 0) | ||
return new TsType.Any | ||
const type = short ? new TsType.Any : this.toTsType(copy.additionalProperties as JSONSchema) | ||
return new TsType.TAny | ||
const type = short ? new TsType.TAny : this.toTsType(copy.additionalProperties as JSONSchema) | ||
props.push({ | ||
@@ -306,3 +303,3 @@ name: '[k: string]', | ||
} | ||
return new TsType.Interface(props) | ||
return new TsType.TInterface(props) | ||
} | ||
@@ -309,0 +306,0 @@ } |
@@ -0,3 +1,3 @@ | ||
import { camelCase, upperFirst } from 'lodash' | ||
import { Settings } from './index' | ||
import { camelCase, upperFirst } from 'lodash' | ||
@@ -41,3 +41,3 @@ const COMMENT_START = '/**' | ||
export class Any extends TsType<void> { | ||
export class TAny extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -48,3 +48,3 @@ toString() { | ||
} | ||
export class String extends TsType<void> { | ||
export class TString extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -55,3 +55,3 @@ toString() { | ||
} | ||
export class Boolean extends TsType<void> { | ||
export class TBoolean extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -62,3 +62,3 @@ toString() { | ||
} | ||
export class Number extends TsType<void> { | ||
export class TNumber extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -69,3 +69,3 @@ toString() { | ||
} | ||
export class Object extends TsType<void> { | ||
export class TObject extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -76,3 +76,3 @@ toString() { | ||
} | ||
export class Null extends TsType<void> { | ||
export class TNull extends TsType<void> { | ||
constructor() { super(undefined) } | ||
@@ -83,3 +83,3 @@ toString() { | ||
} | ||
export class Literal<T> extends TsType<T> { | ||
export class TLiteral<T> extends TsType<T> { | ||
toString() { | ||
@@ -90,7 +90,7 @@ return JSON.stringify(this.value) | ||
export class Reference extends TsType<string> { | ||
export class TReference extends TsType<string> { | ||
toString() { return this.value } | ||
} | ||
export class EnumValue extends TsType<string> { | ||
export class TEnumValue extends TsType<string> { | ||
identifier: string | ||
@@ -115,4 +115,4 @@ value: string | ||
export class Enum extends TsType<EnumValue[]> { | ||
constructor(public id: string, value: EnumValue[]) { | ||
export class TEnum extends TsType<TEnumValue[]> { | ||
constructor(public id: string, value: TEnumValue[]) { | ||
super(value) | ||
@@ -135,4 +135,4 @@ } | ||
export class Array extends TsType<TsType<any>> { | ||
constructor(value: TsType<any> = new Any) { super(value) } | ||
export class TArray extends TsType<TsType<any>> { | ||
constructor(value: TsType<any> = new TAny) { super(value) } | ||
toString(settings: Settings) { | ||
@@ -144,7 +144,7 @@ const type = this.value.toType(settings) | ||
export class Intersection<T> extends TsType<TsType<T>[]> { | ||
export class TIntersection<T> extends TsType<TsType<T>[]> { | ||
isSimpleType() { return this.value.length <= 1 } | ||
toString(settings: Settings) { | ||
return this.value | ||
.filter(_ => !(_ instanceof Null)) | ||
.filter(_ => !(_ instanceof TNull)) | ||
.map(_ => _.toType(settings)) | ||
@@ -155,3 +155,3 @@ .join(' & ') | ||
export class Union<T> extends TsType<TsType<T>[]> { | ||
export class TUnion<T> extends TsType<TsType<T>[]> { | ||
isSimpleType() { return this.value.length <= 1 } | ||
@@ -165,5 +165,5 @@ toString(settings: Settings) { | ||
export class Interface extends TsType<TsProp<any>[]> { | ||
export class TInterface extends TsType<TsProp<any>[]> { | ||
static reference(id: string) { | ||
let ret = new Interface([]) | ||
let ret = new TInterface([]) | ||
ret.id = id | ||
@@ -170,0 +170,0 @@ return ret |
@@ -21,3 +21,3 @@ import { compile, Settings } from '../src/index' | ||
.filter(_ => !_[1].exclude) | ||
.forEach(_ => run(_[1], _[0])) | ||
.forEach(_ => run(_[1], path.resolve(__dirname, _[0]))) | ||
} | ||
@@ -24,0 +24,0 @@ |
@@ -7,10 +7,9 @@ { | ||
"indent": [true, "spaces"], | ||
"linebreak-style": [true, "CR"], | ||
"linebreak-style": [true, "LF"], | ||
"member-access": false, | ||
"no-angle-bracket-type-assertion": true, | ||
"no-bitwise": true, | ||
"no-consecutive-blank-lines": true, | ||
"no-consecutive-blank-lines": [true], | ||
"no-debugger": true, | ||
"no-default-export": true, | ||
"no-duplicate-key": true, | ||
"no-duplicate-variable": true, | ||
@@ -31,3 +30,3 @@ "no-eval": true, | ||
"one-variable-per-declaration": [true, "ignore-for-loop"], | ||
"ordered-imports": true, | ||
"ordered-imports": [true], | ||
"quotemark": [true, "single"], | ||
@@ -34,0 +33,0 @@ "semicolon": [true, "never"], |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
210087
7024
Updatedlodash@^4.17.4
Updatedmz@^2.6.0