Socket
Socket
Sign inDemoInstall

json-schema-to-flow-type

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-flow-type - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

54

lib/index.js

@@ -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>"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc