Comparing version 4.0.0-beta14 to 4.0.0-beta15
@@ -0,1 +1,5 @@ | ||
4.0.0-beta15 - November 21, 2016 | ||
- Downgraded datatype-expansion library | ||
- Making all the types consistent ourselves now, always an array | ||
4.0.0-beta14 - November 21, 2016 | ||
@@ -2,0 +6,0 @@ - Updated datatype-expansion library |
170
index.js
@@ -8,2 +8,4 @@ #!/usr/bin/env node | ||
const fs = require('fs'); | ||
const makeExamplesAndTypesConsistent = require('./consistency-helpers'); | ||
const { arraysToObjects, recursiveObjectToArray } = require('./arrays-objects-helpers'); | ||
@@ -16,33 +18,11 @@ function _makeUniqueId(string) { | ||
function _isObject(obj) { | ||
return obj === Object(obj); | ||
} | ||
// EXAMPLE INPUT: | ||
// { | ||
// foo: { | ||
// name: "foo!" | ||
// }, | ||
// bar: { | ||
// name: "bar" | ||
// } | ||
// } | ||
// | ||
// EXAMPLE OUTPUT: | ||
// [ { name: "foo!", key: "foo" }, { name: "bar", key: "bar" } ] | ||
function _objectToArray(obj) { | ||
if (Array.isArray(obj)) { | ||
return obj; | ||
// Add unique id's and parent URL's plus parent URI parameters to resources | ||
function _addRaml2htmlProperties(ramlObj, parentUrl, allUriParameters) { | ||
// Add unique id's to top level documentation chapters | ||
if (ramlObj.documentation) { | ||
ramlObj.documentation.forEach((docSection) => { | ||
docSection.uniqueId = _makeUniqueId(docSection.title); | ||
}); | ||
} | ||
return Object.keys(obj).map((key) => { | ||
if (_isObject(obj[key])) { | ||
obj[key].key = key; | ||
} | ||
return obj[key]; | ||
}); | ||
} | ||
function _traverse(ramlObj, parentUrl, allUriParameters) { | ||
// Add unique id's and parent URL's plus parent URI parameters to resources | ||
if (!ramlObj.resources) { | ||
@@ -57,7 +37,2 @@ return ramlObj; | ||
// Since types are an array everywhere else, make sure they're also an array on the resource level | ||
if (resource.type && !Array.isArray(resource.type)) { | ||
resource.type = [resource.type]; | ||
} | ||
if (allUriParameters) { | ||
@@ -68,4 +43,2 @@ resource.allUriParameters.push.apply(resource.allUriParameters, allUriParameters); | ||
if (resource.uriParameters) { | ||
resource.uriParameters = _objectToArray(resource.uriParameters); | ||
resource.uriParameters.forEach((uriParameter) => { | ||
@@ -76,2 +49,3 @@ resource.allUriParameters.push(uriParameter); | ||
// Copy the RESOURCE uri parameters to the METHOD, because that's where they will be rendered. | ||
if (resource.methods) { | ||
@@ -83,3 +57,3 @@ resource.methods.forEach((method) => { | ||
_traverse(resource, resource.parentUrl + resource.relativeUri, resource.allUriParameters); | ||
_addRaml2htmlProperties(resource, resource.parentUrl + resource.relativeUri, resource.allUriParameters); | ||
}); | ||
@@ -90,96 +64,2 @@ | ||
function _expandTypes(arr, types) { | ||
return arr.map((obj) => { | ||
if (obj.type && Array.isArray(obj.type)) { | ||
obj.type.forEach((type) => { | ||
if (types[type]) { | ||
Object.assign(obj, types[type]); | ||
} | ||
}); | ||
if (obj.type.length === 1) { | ||
obj.type = obj.type[0]; | ||
} | ||
} | ||
if (obj.items && types[obj.items]) { | ||
obj.items = types[obj.items]; | ||
} | ||
return obj; | ||
}); | ||
} | ||
function _makeExamplesConsistent(arr) { | ||
return arr.map((obj) => { | ||
if (obj.examples && obj.examples.length) { | ||
obj.examples = obj.examples.map(example => (example.value ? example.value : example)); | ||
} | ||
if (obj.structuredExample) { | ||
if (typeof obj.examples === 'undefined') { | ||
obj.examples = []; | ||
} | ||
obj.examples.push(obj.structuredExample.value); | ||
delete obj.example; | ||
delete obj.structuredExample; | ||
} | ||
return obj; | ||
}); | ||
} | ||
function _recursiveObjectToArray(obj, types) { | ||
if (_isObject(obj)) { | ||
Object.keys(obj).forEach((key) => { | ||
const value = obj[key]; | ||
if (_isObject(obj) && ['responses', 'body', 'queryParameters', 'headers', 'properties', 'baseUriParameters', 'annotations', 'uriParameters'].indexOf(key) !== -1) { | ||
obj[key] = _objectToArray(value); | ||
if (types) { | ||
obj[key] = _expandTypes(obj[key], types); | ||
} | ||
obj[key] = _makeExamplesConsistent(obj[key]); | ||
} | ||
_recursiveObjectToArray(value, types); | ||
}); | ||
} else if (Array.isArray(obj)) { | ||
obj.forEach((value) => { | ||
_recursiveObjectToArray(value, types); | ||
}); | ||
} | ||
return obj; | ||
} | ||
// EXAMPLE INPUT: | ||
// [ | ||
// { foo: { ... } }, | ||
// { bar: { ... } }, | ||
// ] | ||
// | ||
// EXAMPLE OUTPUT: | ||
// { foo: { ... }, bar: { ... } } | ||
function _arrayToObject(arr) { | ||
return arr.reduce((acc, cur) => { | ||
Object.keys(cur).forEach((key) => { | ||
acc[key] = cur[key]; | ||
}); | ||
return acc; | ||
}, {}); | ||
} | ||
function _arraysToObjects(ramlObj) { | ||
['types', 'traits', 'resourceTypes', 'annotationTypes', 'securitySchemes'].forEach((key) => { | ||
if (ramlObj[key]) { | ||
ramlObj[key] = _arrayToObject(ramlObj[key]); | ||
} | ||
}); | ||
return ramlObj; | ||
} | ||
// This uses the datatype-expansion library to expand all the root type to their canonical expanded form | ||
@@ -194,3 +74,3 @@ function _expandRootTypes(types) { | ||
if (expanded) { | ||
tools.canonicalForm(expanded, (err, canonical) => { | ||
tools.canonicalForm(expanded, (err2, canonical) => { | ||
if (canonical) { | ||
@@ -208,4 +88,2 @@ types[key] = canonical; | ||
function _enhanceRamlObj(ramlObj) { | ||
ramlObj = _traverse(ramlObj); | ||
// Some of the structures (like `types`) are an array that hold key/value pairs, which is very annoying to work with. | ||
@@ -222,3 +100,3 @@ // Let's make them into a simple object, this makes it easy to use them for direct lookups. | ||
// { foo: { ... }, bar: { ... } } | ||
ramlObj = _arraysToObjects(ramlObj); | ||
ramlObj = arraysToObjects(ramlObj); | ||
@@ -228,5 +106,8 @@ // We want to expand inherited root types, so that later on when we copy type properties into an object, | ||
// Delete the types from the ramlObj so it's not processed again later on. | ||
const types = _expandRootTypes(ramlObj.types); | ||
const types = makeExamplesAndTypesConsistent(_expandRootTypes(ramlObj.types)); | ||
delete ramlObj.types; | ||
// Recursibely go over the entire object and make all examples and types consistent. | ||
ramlObj = makeExamplesAndTypesConsistent(ramlObj, types); | ||
// Other structures (like `responses`) are an object that hold other wrapped objects. | ||
@@ -248,15 +129,12 @@ // Flatten this to simple (non-wrapped) objects in an array instead, | ||
// [ { name: "foo!", key: "foo" }, { name: "bar", key: "bar" } ] | ||
ramlObj = _recursiveObjectToArray(ramlObj, types); | ||
ramlObj = recursiveObjectToArray(ramlObj); | ||
// Add unique id's to top level documentation chapters | ||
if (ramlObj.documentation) { | ||
ramlObj.documentation.forEach((docSection) => { | ||
docSection.uniqueId = _makeUniqueId(docSection.title); | ||
}); | ||
// Now add all the properties and things that we need for raml2html, stuff like the uniqueId, parentUrl, | ||
// and allUriParameters. | ||
ramlObj = _addRaml2htmlProperties(ramlObj); | ||
if (types) { | ||
ramlObj.types = types; | ||
} | ||
// if (types) { | ||
// ramlObj.types = types; | ||
// } | ||
return ramlObj; | ||
@@ -263,0 +141,0 @@ } |
{ | ||
"name": "raml2obj", | ||
"description": "RAML to object", | ||
"version": "4.0.0-beta14", | ||
"version": "4.0.0-beta15", | ||
"author": { | ||
@@ -13,3 +13,3 @@ "name": "Kevin Renskers", | ||
"dependencies": { | ||
"datatype-expansion": "0.1.0", | ||
"datatype-expansion": "0.0.15", | ||
"raml-1-parser": "1.1.8" | ||
@@ -20,2 +20,3 @@ }, | ||
"eslint-config-loopwerk": "6.0.x", | ||
"glob": "7.1.1", | ||
"mocha": "3.1.x" | ||
@@ -35,2 +36,3 @@ }, | ||
"scripts": { | ||
"json": "node test/output-json.js", | ||
"lint": "eslint ./", | ||
@@ -41,3 +43,5 @@ "lintfix": "eslint --fix ./", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"arrays-objects-helpers.js", | ||
"consistency-helpers.js" | ||
], | ||
@@ -44,0 +48,0 @@ "engines": { |
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
14466
7
246
4
1
+ Addeddatatype-expansion@0.0.15(transitive)
- Removeddatatype-expansion@0.1.0(transitive)
Updateddatatype-expansion@0.0.15