json-schema-to-flow-type
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -8,4 +8,10 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
var _babelGenerator = require('babel-generator'); | ||
var _babelGenerator2 = _interopRequireDefault(_babelGenerator); | ||
var _lodash = require('lodash'); | ||
@@ -21,24 +27,22 @@ | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
const upperCamelCase = str => _lodash2.default.upperFirst(_lodash2.default.camelCase(str)); | ||
const unionTypes = types => _lodash2.default.join(_lodash2.default.uniq(types), ' | '); | ||
const optional = astNode => _lodash2.default.assign(astNode, { optional: true }); | ||
const intersectionTypes = types => _lodash2.default.join(_lodash2.default.uniq(types), ' & '); | ||
const processArraySchema = (flowSchema, processor) => t.genericTypeAnnotation(t.identifier('Array'), t.typeParameterInstantiation([processor(flowSchema.flowType('any'))])); | ||
const fieldsToFlowObjectDef = fields => { | ||
const flowDef = _lodash2.default.map(fields, (typeDefine, field) => `${ field }: ${ typeDefine }`).join(', '); | ||
const processObjectSchema = (flowSchema, processor) => { | ||
const properties = _lodash2.default.map(flowSchema.$properties || {}, (fieldFlowSchema, field) => { | ||
const ast = t.objectTypeProperty(t.identifier(field), processor(fieldFlowSchema)); | ||
return `{ ${ flowDef } }`; | ||
}; | ||
if (flowSchema.$required) { | ||
return ast; | ||
} | ||
const processArraySchema = (flowSchema, processor) => `Array<${ processor(flowSchema.flowType('any')) }>`; | ||
return optional(ast); | ||
}); | ||
const processObjectSchema = (flowSchema, processor) => { | ||
const fields = _lodash2.default.reduce(flowSchema.$properties || {}, (nextFields, fieldFlowSchema, field) => _extends({}, nextFields, { | ||
[`${ field }${ _lodash2.default.includes(flowSchema.$required, field) ? '' : '?' }`]: processor(fieldFlowSchema) | ||
}), {}); | ||
return fieldsToFlowObjectDef(_extends({}, fields, flowSchema.$union ? { | ||
'[key: any]': processor(flowSchema.flowType('any')) | ||
} : {})); | ||
return t.objectTypeAnnotation(properties, flowSchema.$union ? [t.objectTypeIndexer(t.identifier('key'), t.anyTypeAnnotation(), processor(flowSchema.flowType('any')))] : null); | ||
}; | ||
@@ -48,7 +52,7 @@ | ||
if (flowSchema.$flowRef) { | ||
return upperCamelCase(flowSchema.$flowRef); | ||
return t.identifier(upperCamelCase(flowSchema.$flowRef)); | ||
} | ||
if (flowSchema.$enum) { | ||
return unionTypes(_lodash2.default.map(flowSchema.$enum, value => JSON.stringify(value))); | ||
return t.createUnionTypeAnnotation(_lodash2.default.map(flowSchema.$enum, value => t.identifier(JSON.stringify(value)))); | ||
} | ||
@@ -65,15 +69,19 @@ | ||
if (flowSchema.$union) { | ||
return unionTypes(_lodash2.default.map(flowSchema.$union, toFlowType)); | ||
return t.unionTypeAnnotation(_lodash2.default.map(flowSchema.$union, toFlowType)); | ||
} | ||
if (flowSchema.$intersection) { | ||
return intersectionTypes(_lodash2.default.map(flowSchema.$intersection, toFlowType)); | ||
return t.intersectionTypeAnnotation(_lodash2.default.map(flowSchema.$intersection, toFlowType)); | ||
} | ||
return flowSchema.$flowType; | ||
if (flowSchema.$flowType === 'any') { | ||
return t.anyTypeAnnotation(); | ||
} | ||
return t.createTypeAnnotationBasedOnTypeof(flowSchema.$flowType); | ||
}; | ||
const toFlow = exports.toFlow = flowSchema => `export type ${ upperCamelCase(flowSchema.$id) } = ${ toFlowType(flowSchema) };`; | ||
const toFlow = exports.toFlow = flowSchema => t.exportNamedDeclaration(t.typeAlias(t.identifier(upperCamelCase(flowSchema.$id)), null, toFlowType(flowSchema)), []); | ||
const schemaToFlow = exports.schemaToFlow = flowSchema => [..._lodash2.default.map(flowSchema.$definitions, toFlow), toFlow(flowSchema)].join('\n\n'); | ||
const schemaToFlow = exports.schemaToFlow = flowSchema => (0, _babelGenerator2.default)(t.program([..._lodash2.default.map(flowSchema.$definitions, toFlow), toFlow(flowSchema)])).code; | ||
@@ -80,0 +88,0 @@ exports.simplifySchema = _Schema.simplifySchema; |
@@ -23,10 +23,9 @@ 'use strict'; | ||
if (_lodash2.default.isString(ref)) { | ||
var _$split = _lodash2.default.split(ref, '#'); | ||
var _$split = _lodash2.default.split(ref, '#'), | ||
_$split2 = _slicedToArray(_$split, 2); | ||
var _$split2 = _slicedToArray(_$split, 2); | ||
const importRef = _$split2[0], | ||
keyPath = _$split2[1]; | ||
const importRef = _$split2[0]; | ||
const keyPath = _$split2[1]; | ||
const keyPathArr = _lodash2.default.drop(_lodash2.default.split(keyPath, '/')); | ||
@@ -33,0 +32,0 @@ |
{ | ||
"name": "json-schema-to-flow-type", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"ava": { | ||
@@ -15,3 +15,3 @@ "files": [ | ||
"lint": "eslint ./src --fix", | ||
"test": "npm run lint && ava", | ||
"test": "ava", | ||
"build": "rm -rf lib && babel src --ignore __**__ -d lib", | ||
@@ -21,10 +21,15 @@ "prepublish": "flow-typed update && npm test && npm run build" | ||
"devDependencies": { | ||
"@morlay/babel-preset": "2.1.x", | ||
"@morlay/eslint-config": "2.1.x", | ||
"@morlay/babel-preset": "3.0.x", | ||
"@morlay/eslint-config": "3.0.x", | ||
"ava": "0.16.x", | ||
"flow-bin": "0.33.x", | ||
"babel-cli": "6.18.x", | ||
"babel-core": "6.18.x", | ||
"babel-generator": "6.19.0", | ||
"eslint": "3.10.x", | ||
"flow-bin": "0.35.x", | ||
"flow-typed": "2.0.x" | ||
}, | ||
"dependencies": { | ||
"lodash": "4.16.x" | ||
"babel-types": "6.19.0", | ||
"lodash": "4.17.x" | ||
}, | ||
@@ -41,2 +46,2 @@ "main": "lib/index.js", | ||
"author": "Morlay <morlay@qq.com>" | ||
} | ||
} |
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
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
13267
216
0
2
9
+ Addedbabel-types@6.19.0
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedbabel-types@6.19.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)
+ Addedto-fast-properties@1.0.3(transitive)
- Removedlodash@4.16.6(transitive)
Updatedlodash@4.17.x