bravado-core
Advanced tools
Comparing version 0.9.0 to 0.11.0
@@ -11,2 +11,4 @@ var jsonRefs = require('json-refs'); | ||
var traverse = require('traverse'); | ||
var camelCase = require('lodash.camelcase'); | ||
var dir = require('node-dir'); | ||
var errors = require('./errors'); | ||
@@ -39,2 +41,16 @@ | ||
Object.defineProperty(ApiDefinition.prototype, 'resolveRefs', { | ||
enumerable: false, | ||
value: function () { | ||
var refsOptions = { | ||
relativeBase: this['x-bravado-sourceFile'], | ||
filter: [ 'local', 'relative', 'remote' ] | ||
}; | ||
return jsonRefs.resolveRefs(this, refsOptions) | ||
.then(function (results) { | ||
return results.resolved; | ||
}); | ||
} | ||
}); | ||
Object.defineProperty(ApiDefinition.prototype, 'saveJson', { | ||
@@ -94,18 +110,110 @@ enumerable: false, | ||
var doc = yaml.safeLoad(data); | ||
var refsOptions = { | ||
relativeBase: path.dirname(source), | ||
filter: [ 'relative', 'remote' ], | ||
loaderOptions: { | ||
processContent: function (res, cb) { | ||
cb(null, yaml.safeLoad(res.text)); | ||
doc['x-bravado-sourceFile'] = source; | ||
var api = new ApiDefinition(doc); | ||
return api; | ||
}); | ||
}; | ||
ApiDefinition.build = function (root, options) { | ||
var sourceDir = path.resolve(root); | ||
var index = path.resolve(root, 'api.yaml'); | ||
var resourceDir = path.resolve(root, 'resources'); | ||
var defDir = path.resolve(root, 'definitions'); | ||
var exampleDir = path.resolve(root, 'examples'); | ||
return q.nfcall(fs.readFile, index) | ||
.then(function loadApi(data) { | ||
var doc = yaml.safeLoad(data); | ||
doc.resources = doc.resources || {}; | ||
doc.definitions = doc.definitions || {}; | ||
doc.examples = doc.examples || {}; | ||
return [doc, q.nfcall(dir.files, resourceDir)]; | ||
}) | ||
.spread(function readResources(doc, files) { | ||
return q.all(files.map(function (file) { | ||
var ext = path.extname(file); | ||
if (ext === '.json' || ext === '.yaml' || ext === '.yml') { | ||
var base = path.basename(file, ext); | ||
return q.all([doc, camelCase(base), q.nfcall(fs.readFile, file)]); | ||
} else { return null; } | ||
})); | ||
}) | ||
.then(function parseResources(resources) { | ||
var doc; | ||
resources.forEach(function (item) { | ||
if (!item) { return; } | ||
if (!doc) { doc = item[0]; } | ||
doc.resources[item[1]] = yaml.safeLoad(item[2]); | ||
}); | ||
return q.all([doc, q.nfcall(dir.files, defDir)]); | ||
}) | ||
.spread(function readDefs(doc, files) { | ||
return q.all(files.map(function (file) { | ||
var ext = path.extname(file); | ||
if (ext === '.json' || ext === '.yaml' || ext === '.yml') { | ||
var base = path.basename(file, ext); | ||
var rel = path.relative(defDir, file); | ||
if (rel[0] === '_') { return null; } | ||
var jpath = path.dirname(rel).split(path.sep); | ||
jpath.push(camelCase(base)); | ||
return q.all([doc, jpath, q.nfcall(fs.readFile, file)]); | ||
} else { return null; } | ||
})); | ||
}) | ||
.then(function parseDefs(defs) { | ||
var doc; | ||
defs.forEach(function (item) { | ||
if (!item) { return; } | ||
if (!doc) { doc = item[0]; } | ||
var obj = doc.definitions; | ||
item[1].forEach(function (part, i) { | ||
if (part[0] === '_' || part[0] === '.') { return false; } | ||
if (i < item[1].length - 1) { | ||
if (!obj[part]) { | ||
obj[part] = {}; | ||
} | ||
obj = obj[part]; | ||
} else { | ||
obj[part] = yaml.safeLoad(item[2]); | ||
} | ||
} | ||
}; | ||
return jsonRefs.resolveRefs(doc, refsOptions); | ||
}); | ||
}); | ||
return q.all([doc, q.nfcall(dir.files, exampleDir)]); | ||
}) | ||
.then(function (results) { | ||
var api = results.resolved; | ||
api['x-bravado-sourceFile'] = source; | ||
var def = new ApiDefinition(api); | ||
return def; | ||
.spread(function readExamples(doc, files) { | ||
return q.all(files.map(function (file) { | ||
var ext = path.extname(file); | ||
if (ext === '.json' || ext === '.yaml' || ext === '.yml') { | ||
var base = path.basename(file, ext); | ||
var rel = path.relative(exampleDir, file); | ||
if (rel[0] === '_') { return null; } | ||
var jpath = path.dirname(rel).split(path.sep); | ||
jpath.push(camelCase(base)); | ||
return q.all([doc, jpath, q.nfcall(fs.readFile, file)]); | ||
} else { return null; } | ||
})); | ||
}) | ||
.then(function parseExamples(examples) { | ||
var doc; | ||
examples.forEach(function (item) { | ||
if (!item) { return; } | ||
if (!doc) { doc = item[0]; } | ||
var obj = doc.examples; | ||
item[1].forEach(function (part, i) { | ||
if (part[0] === '_' || part[0] === '.') { return false; } | ||
if (i < item[1].length - 1) { | ||
if (!obj[part]) { | ||
obj[part] = {}; | ||
} | ||
obj = obj[part]; | ||
} else { | ||
obj[part] = yaml.safeLoad(item[2]); | ||
} | ||
}); | ||
}); | ||
return doc; | ||
}) | ||
.then(function (doc) { | ||
doc['x-bravado-sourceFile'] = index; | ||
var api = new ApiDefinition(doc); | ||
return api; | ||
}); | ||
@@ -112,0 +220,0 @@ }; |
{ | ||
"name": "bravado-core", | ||
"version": "0.9.0", | ||
"version": "0.11.0", | ||
"description": "REST (Level 3/HATEOAS) API Framework", | ||
@@ -35,2 +35,4 @@ "directories": { | ||
"jsonwebtoken": "^5.5.4", | ||
"lodash.camelcase": "^4.1.1", | ||
"node-dir": "^0.1.12", | ||
"object-assign": "^3.0.0", | ||
@@ -37,0 +39,0 @@ "q": "^1.4.1", |
56295
1803
13
+ Addedlodash.camelcase@^4.1.1
+ Addednode-dir@^0.1.12
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcall-bind-apply-helpers@1.0.2(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addednode-dir@0.1.17(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)