json-schema-to-typescript
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -82,7 +82,7 @@ // Generated by dts-bundle v0.5.0 | ||
} | ||
type TsProp = { | ||
interface TsProp { | ||
name: string; | ||
required: boolean; | ||
type: TsType; | ||
}; | ||
} | ||
class Any extends TsType { | ||
@@ -89,0 +89,0 @@ _type(): string; |
@@ -20,3 +20,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsonSchemaToTypescript = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
// declareProperties: false, | ||
useInterfaceDeclaration: false | ||
useInterfaceDeclaration: true | ||
}; | ||
@@ -23,0 +23,0 @@ var TsType = (function () { |
{ | ||
"name": "json-schema-to-typescript", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "compile json schema to typescript typings", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,142 +0,143 @@ | ||
import {camelCase, upperFirst} from 'lodash' | ||
import { camelCase, upperFirst } from 'lodash' | ||
export namespace TsType { | ||
export interface TsTypeSettings { | ||
declareSimpleType?: boolean | ||
declareReferenced?: boolean | ||
useFullReferencePathAsName?: boolean | ||
// TODO declareProperties?: boolean | ||
useInterfaceDeclaration?: boolean | ||
endTypeWithSemicolon?: boolean | ||
endPropertyWithSemicolon?: boolean | ||
declarationDescription?: boolean | ||
propertyDescription?: boolean | ||
} | ||
export interface TsTypeSettings { | ||
declareSimpleType?: boolean | ||
declareReferenced?: boolean | ||
useFullReferencePathAsName?: boolean | ||
// TODO declareProperties?: boolean | ||
useInterfaceDeclaration?: boolean | ||
endTypeWithSemicolon?: boolean | ||
endPropertyWithSemicolon?: boolean | ||
declarationDescription?: boolean | ||
propertyDescription?: boolean | ||
} | ||
export var DEFAULT_SETTINGS: TsTypeSettings = { | ||
declarationDescription: true, | ||
declareReferenced: true, | ||
declareSimpleType: false, | ||
endPropertyWithSemicolon: true, | ||
endTypeWithSemicolon: true, | ||
propertyDescription: true, | ||
useFullReferencePathAsName: false, | ||
// declareProperties: false, | ||
useInterfaceDeclaration: false | ||
} | ||
export var DEFAULT_SETTINGS: TsTypeSettings = { | ||
declarationDescription: true, | ||
declareReferenced: true, | ||
declareSimpleType: false, | ||
endPropertyWithSemicolon: true, | ||
endTypeWithSemicolon: true, | ||
propertyDescription: true, | ||
useFullReferencePathAsName: false, | ||
// declareProperties: false, | ||
useInterfaceDeclaration: true | ||
} | ||
export abstract class TsType { | ||
id?: string | ||
description?: string | ||
export abstract class TsType { | ||
id?: string | ||
description?: string | ||
protected safeId() { | ||
return this.id && upperFirst(camelCase(this.id)) | ||
protected safeId() { | ||
return this.id && upperFirst(camelCase(this.id)) | ||
} | ||
protected toBlockComment(settings: TsTypeSettings) { | ||
return this.description && settings.declarationDescription ? `/** ${this.description} */\n` : '' | ||
} | ||
protected _toDeclaration(decl: string, settings: TsTypeSettings): string { | ||
return this.toBlockComment(settings) + decl + (settings.endTypeWithSemicolon ? ';' : '') | ||
} | ||
protected abstract _type(settings: TsTypeSettings): string | ||
isSimpleType() { return true } | ||
toDeclaration(settings: TsTypeSettings): string { | ||
return this._toDeclaration(`type ${this.safeId()} = ${this._type(settings)}`, settings) | ||
} | ||
toSafeType(settings: TsTypeSettings): string { | ||
return this.toType(settings) | ||
} | ||
toType(settings: TsTypeSettings): string { | ||
return this.safeId() || this._type(settings) | ||
} | ||
toString(): string { | ||
return this._type(DEFAULT_SETTINGS) | ||
} | ||
} | ||
protected toBlockComment(settings: TsTypeSettings) { | ||
return this.description && settings.declarationDescription ? `/** ${this.description} */\n` : '' | ||
export interface TsProp { | ||
name: string | ||
required: boolean | ||
type: TsType | ||
} | ||
protected _toDeclaration(decl: string, settings: TsTypeSettings): string { | ||
return this.toBlockComment(settings) + decl + (settings.endTypeWithSemicolon ? ';' : '') | ||
} | ||
protected abstract _type(settings: TsTypeSettings): string | ||
isSimpleType() { return true } | ||
toDeclaration(settings: TsTypeSettings): string { | ||
return this._toDeclaration(`type ${this.safeId()} = ${this._type(settings)}`, settings) | ||
} | ||
toSafeType(settings: TsTypeSettings): string { | ||
return this.toType(settings) | ||
} | ||
toType(settings: TsTypeSettings): string { | ||
return this.safeId() || this._type(settings) | ||
} | ||
toString(): string { | ||
return this._type(DEFAULT_SETTINGS) | ||
} | ||
} | ||
export type TsProp = { | ||
name: string | ||
required: boolean | ||
type: TsType | ||
} | ||
export class Any extends TsType { | ||
_type() { | ||
return 'any' | ||
export class Any extends TsType { | ||
_type() { | ||
return 'any' | ||
} | ||
} | ||
} | ||
export class String extends TsType { | ||
_type() { | ||
return 'string' | ||
export class String extends TsType { | ||
_type() { | ||
return 'string' | ||
} | ||
} | ||
} | ||
export class Boolean extends TsType { | ||
_type() { | ||
return 'boolean' | ||
export class Boolean extends TsType { | ||
_type() { | ||
return 'boolean' | ||
} | ||
} | ||
} | ||
export class Number extends TsType { | ||
_type() { | ||
return 'number' | ||
export class Number extends TsType { | ||
_type() { | ||
return 'number' | ||
} | ||
} | ||
} | ||
export class Object extends TsType { | ||
_type() { | ||
return 'Object' | ||
export class Object extends TsType { | ||
_type() { | ||
return 'Object' | ||
} | ||
} | ||
} | ||
export class Void extends TsType { | ||
_type() { | ||
return 'void' | ||
export class Void extends TsType { | ||
_type() { | ||
return 'void' | ||
} | ||
} | ||
} | ||
export class Literal extends TsType { | ||
constructor(private value: any) { super() } | ||
_type() { | ||
return this.value | ||
export class Literal extends TsType { | ||
constructor(private value: any) { super() } | ||
_type() { | ||
return this.value | ||
} | ||
} | ||
} | ||
export class Array extends TsType { | ||
constructor(private type?: TsType) { super() } | ||
_type(settings: TsTypeSettings) { | ||
return `${(this.type || new Any()).toSafeType(settings)}[]` | ||
export class Array extends TsType { | ||
constructor(private type?: TsType) { super() } | ||
_type(settings: TsTypeSettings) { | ||
return `${(this.type || new Any()).toSafeType(settings)}[]` | ||
} | ||
} | ||
} | ||
export class Intersection extends TsType { | ||
constructor(protected data: TsType[]) { | ||
super() | ||
export class Intersection extends TsType { | ||
constructor(protected data: TsType[]) { | ||
super() | ||
} | ||
isSimpleType() { return this.data.filter(_ => !(_ instanceof Void)).length <= 1 } | ||
_type(settings: TsTypeSettings) { | ||
return this.data | ||
.filter(_ => !(_ instanceof Void)) | ||
.map(_ => _.toSafeType(settings)) | ||
.join('&') | ||
} | ||
toSafeType(settings: TsTypeSettings) { | ||
return `${this.toType(settings)}` | ||
} | ||
} | ||
isSimpleType() { return this.data.filter(_ => !(_ instanceof Void)).length <= 1 } | ||
_type(settings: TsTypeSettings) { | ||
return this.data | ||
.filter(_ => !(_ instanceof Void)) | ||
.map(_ => _.toSafeType(settings)) | ||
.join('&') | ||
export class Union extends Intersection { | ||
isSimpleType() { return this.data.length <= 1 } | ||
_type(settings: TsTypeSettings) { | ||
return this.data | ||
.map(_ => _.toSafeType(settings)) | ||
.join('|') | ||
} | ||
} | ||
toSafeType(settings: TsTypeSettings) { | ||
return `${this.toType(settings)}` | ||
} | ||
} | ||
export class Union extends Intersection { | ||
isSimpleType() { return this.data.length <= 1 } | ||
_type(settings: TsTypeSettings) { | ||
return this.data | ||
.map(_ => _.toSafeType(settings)) | ||
.join('|') | ||
} | ||
} | ||
export class Interface extends TsType { | ||
constructor(private props: TsProp[]) { | ||
super() | ||
} | ||
static reference(id: string) { | ||
let ret = new Interface([]) | ||
ret.id = id | ||
return ret | ||
} | ||
protected _type(settings: TsTypeSettings, declaration: boolean = false) { | ||
let id = this.safeId() | ||
return declaration || !id ? `{ | ||
export class Interface extends TsType { | ||
constructor(private props: TsProp[]) { | ||
super() | ||
} | ||
static reference(id: string) { | ||
let ret = new Interface([]) | ||
ret.id = id | ||
return ret | ||
} | ||
protected _type(settings: TsTypeSettings, declaration: boolean = false) { | ||
let id = this.safeId() | ||
return declaration || !id ? `{ | ||
${this.props.map(_ => { | ||
@@ -154,13 +155,13 @@ let decl = _.name | ||
}` : id | ||
} | ||
isSimpleType() { return false } | ||
toDeclaration(settings: TsTypeSettings): string { | ||
if (settings.useInterfaceDeclaration) { | ||
return `${this.toBlockComment(settings)}interface ${this.safeId()} ${this._type(settings, true)}` | ||
} else { | ||
return this._toDeclaration(`type ${this.safeId()} = ${this._type(settings, true)}`, settings) | ||
} | ||
isSimpleType() { return false } | ||
toDeclaration(settings: TsTypeSettings): string { | ||
if (settings.useInterfaceDeclaration) { | ||
return `${this.toBlockComment(settings)}interface ${this.safeId()} ${this._type(settings, true)}` | ||
} else { | ||
return this._toDeclaration(`type ${this.safeId()} = ${this._type(settings, true)}`, settings) | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -14,5 +14,5 @@ export var schema = { | ||
export var types = `type AdditionalProperties = { | ||
export var types = `interface AdditionalProperties { | ||
foo?: string; | ||
[k: string]: number; | ||
};` | ||
}` |
@@ -34,11 +34,11 @@ export var schema = { | ||
export var types = `type Foo = { | ||
export var types = `interface Foo { | ||
a: string; | ||
b: number; | ||
}; | ||
type Bar = { | ||
} | ||
interface Bar { | ||
a: string; | ||
}; | ||
type AllOf = { | ||
} | ||
interface AllOf { | ||
foo: Foo & Bar; | ||
};` | ||
}` |
@@ -39,16 +39,16 @@ export var schema = { | ||
export var types = `type Foo = { | ||
export var types = `interface Foo { | ||
a: string; | ||
b?: number; | ||
}; | ||
type Bar = { | ||
} | ||
interface Bar { | ||
a?: "a" | "b" | "c"; | ||
[k: string]: any; | ||
}; | ||
type Baz = { | ||
} | ||
interface Baz { | ||
baz?: Bar; | ||
[k: string]: any; | ||
}; | ||
type AnyOf = { | ||
} | ||
interface AnyOf { | ||
foo: Foo | Bar | Baz; | ||
};` | ||
}` |
@@ -14,5 +14,5 @@ export var schema = { | ||
export var types = `type ArrayOfType = { | ||
export var types = `interface ArrayOfType { | ||
foo?: string[]; | ||
[k: string]: any; | ||
};` | ||
}` |
@@ -27,3 +27,3 @@ export var schema = { | ||
"required": ["firstName", "lastName"] | ||
}; | ||
} | ||
@@ -35,3 +35,3 @@ export var configurations = [ | ||
}, | ||
types: `interface ExampleSchema { | ||
types: `interface ExampleSchema { | ||
firstName: string; | ||
@@ -50,3 +50,3 @@ lastName: string; | ||
}, | ||
types: `type ExampleSchema = { | ||
types: `interface ExampleSchema { | ||
firstName: string; | ||
@@ -59,4 +59,4 @@ lastName: string; | ||
[k: string]: any; | ||
};` | ||
}` | ||
} | ||
] |
@@ -23,3 +23,3 @@ export var schema = { | ||
export var types = `type Enum = { | ||
export var types = `interface Enum { | ||
foo: "a" | "b" | "c"; | ||
@@ -30,2 +30,2 @@ bar: number; | ||
}; | ||
};` | ||
}` |
@@ -163,3 +163,3 @@ export var schema = { | ||
/** Core schema meta-schema */ | ||
type HttpJsonSchemaOrgDraft04Schema = { | ||
interface HttpJsonSchemaOrgDraft04Schema { | ||
id?: string; | ||
@@ -206,3 +206,3 @@ $schema?: string; | ||
[k: string]: any; | ||
};` | ||
}` | ||
}, | ||
@@ -215,3 +215,3 @@ { | ||
/** Core schema meta-schema */ | ||
type HttpJsonSchemaOrgDraft04Schema = { | ||
interface HttpJsonSchemaOrgDraft04Schema { | ||
id?: string; | ||
@@ -258,4 +258,4 @@ $schema?: string; | ||
[k: string]: any; | ||
};` | ||
}` | ||
} | ||
] |
@@ -21,6 +21,6 @@ export var schema = { | ||
export var types = `type ExampleSchema = { | ||
export var types = `interface ExampleSchema { | ||
firstName: string; | ||
lastName: string; | ||
age?: number; // Age in years | ||
};` | ||
}` |
@@ -30,14 +30,14 @@ export var schema = { | ||
export var types = `type Foo = { | ||
export var types = `interface Foo { | ||
a: string; | ||
b?: number; | ||
}; | ||
type Bar = { | ||
} | ||
interface Bar { | ||
a?: "a" | "b" | "c"; | ||
[k: string]: any; | ||
}; | ||
type Baz = { | ||
} | ||
interface Baz { | ||
baz?: Bar; | ||
[k: string]: any; | ||
}; | ||
} | ||
type RootAnyOf = Foo | Bar | Baz;` |
@@ -12,4 +12,4 @@ export var schema = { | ||
export var types = `type Interface1 = { | ||
export var types = `interface Interface1 { | ||
foo: string; | ||
};` | ||
}` |
@@ -22,3 +22,3 @@ export var schema = { | ||
export var types = `/** My cool schema */ | ||
type ExampleSchema = { | ||
interface ExampleSchema { | ||
firstName: string; | ||
@@ -28,2 +28,2 @@ lastName: string; | ||
[k: string]: any; | ||
};` | ||
}` |
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
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
63447