create-raml
Advanced tools
Comparing version 1.1.1 to 2.0.1
# create-raml changelog | ||
## [2.0.1](http://github.com/ivanoff/create-raml/tree/2.0.1) (2017-03-01) | ||
[Full Changelog](http://github.com/ivanoff/create-raml/compare/1.1.1...2.0.1) | ||
**What Was Done:** | ||
- add express support | ||
- add raml2obj validator test | ||
- add RAML v0.8 | ||
## [1.1.1](http://github.com/ivanoff/create-raml/tree/1.1.1) (2017-02-28) | ||
@@ -4,0 +13,0 @@ [Full Changelog](http://github.com/ivanoff/create-raml/compare/1.0.1...1.1.1) |
204
index.js
@@ -8,88 +8,158 @@ /*! | ||
var fs = require('fs'); | ||
var path = require("path"); | ||
var path = require('path'); | ||
var nunjucks = require('nunjucks'); | ||
exports = module.exports = function (options) { | ||
if (typeof options === 'undefined') var options = {}; | ||
if (typeof options.templateFileName === 'undefined') | ||
options.templateFileName = path.resolve(__dirname, 'template/raml_1_0.nunjucks'); | ||
var typesData = {}; | ||
var methodsData = {}; | ||
return { | ||
type: function (name, obj) { | ||
return (typeof name === 'undefined') ? typesData | ||
: (typeof obj === 'undefined') ? typesData[name] | ||
: typesData[name] = obj; | ||
}, | ||
if (undef(options)) options = {}; | ||
var ramlFile = (options.version === 0.8) ? 'raml_0_8.nunjucks' : 'raml_1_0.nunjucks'; | ||
if (undef(options.templateFileName)) | ||
options.templateFileName = path.resolve(__dirname, 'templates', ramlFile); | ||
methods: function (name, method, obj) { | ||
if (typeof name === 'undefined') { | ||
return methodsData; | ||
} else if (typeof method === 'undefined') { | ||
return methodsData[name]; | ||
} else if (typeof obj === 'undefined') { | ||
methodsData[name] = method; | ||
} else { | ||
/* istanbul ignore else */ | ||
if (typeof methodsData[name] === 'undefined') methodsData[name] = {}; | ||
methodsData[name][method] = obj; | ||
} | ||
}, | ||
var exp = {}; | ||
generate: function (cb) { | ||
var _this = this; | ||
exp.type = function (name, obj) { | ||
return (undef(name)) ? typesData | ||
: (undef(obj)) ? typesData[name] | ||
: typesData[name] = obj; | ||
} | ||
var url = Object.keys(methodsData).sort(); | ||
var methodsTree = {}; | ||
for (var i = url.length - 1; i >= 0; i--) { | ||
for (var j = i - 1; j >= 0; j--) { | ||
/* istanbul ignore else */ | ||
if (typeof methodsTree[url[i]] === 'undefined' && url[i].indexOf(url[j] + '/') == 0) { | ||
methodsTree[url[i]] = { | ||
shortLink: url[i].replace(url[j] + '/', ''), | ||
deep: url[j].split('/').length, | ||
}; | ||
exp.methods = function (name, method, obj) { | ||
if (undef(name)) { | ||
return methodsData; | ||
} else if (undef(method)) { | ||
return methodsData[name]; | ||
} else if (undef(obj)) { | ||
methodsData[name] = method; | ||
} else { | ||
if (undef(methodsData[name])) methodsData[name] = {}; | ||
methodsData[name][method] = obj; | ||
} | ||
} | ||
exp.express = function (req, res) { | ||
if (options.express && Object.keys(methodsData).length === 0) | ||
methodsData = parseExpressData(options.express); | ||
this.generate(function (err, data) { | ||
res.send(data); | ||
}); | ||
} | ||
exp.storeResponses = function (req, res, next) { | ||
var resEnd = res.end; | ||
res.end = function (chunk) { | ||
resEnd.apply(res, arguments); | ||
if(req.route) { | ||
var r = req.route; | ||
if(undef(methodsData[r.path])) methodsData[r.path] = {}; | ||
if(undef(methodsData[r.path][r.stack[0].method])) | ||
methodsData[r.path][r.stack[0].method] = { | ||
description: r.stack[0].method + ' STORED ' + r.path, | ||
} | ||
} | ||
if (typeof methodsTree[url[i]] === 'undefined') { | ||
methodsTree[url[i]] = { shortLink: url[i], deep: 0 }; | ||
} | ||
var m = methodsData[r.path][r.stack[0].method]; | ||
if(undef(m.responses)) m.responses = {}; | ||
if(undef(m.responses[res.statusCode])) | ||
m.responses[res.statusCode] = { | ||
'application/json': {aaa:123}, | ||
'application/text': 'hi!' | ||
}; | ||
} | ||
}; | ||
fs.readFile(options.templateFileName, function (err, template) { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
var view = { | ||
options: options, | ||
typesData: typesData, | ||
methods: url, | ||
methodsTree: methodsTree, | ||
methodsData: methodsData, | ||
stringify: function (value, replacer, space, indent) { | ||
return JSON.stringify(value, replacer, space).replace(/^/gm, Array(indent).join(' ')); | ||
}, | ||
next(); | ||
} | ||
updatePath: function (path) { | ||
return path.replace(/\/:([^/:-]+)/g, '/{$1}'); | ||
}, | ||
exp.generate = function (cb) { | ||
fs.readFile(options.templateFileName, function (err, template) { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
var view = { | ||
options: options, | ||
types: typesData, | ||
url: Object.keys(methodsData).sort(), | ||
methods: getMethodsTree(methodsData), | ||
f: additional(), | ||
}; | ||
cb(null, nunjucks.renderString(template.toString(), view)); | ||
} | ||
}); | ||
} | ||
regReplace: function (what, from, to, flags) { | ||
return what.replace(new RegExp(from, flags), to); | ||
}, | ||
if(options.express) options.express.use(exp.storeResponses.bind(exp)) | ||
repeat: function (what, count) { | ||
return Array(count + 1).join(what); | ||
}, | ||
}; | ||
cb(null, nunjucks.renderString(template.toString(), view)); | ||
} | ||
}); | ||
return exp; | ||
}; | ||
function undef(v) { | ||
return typeof v === 'undefined'; | ||
} | ||
function additional() { | ||
return { | ||
stringify: function (value, replacer, space, indent) { | ||
return JSON.stringify(value, replacer, space).replace(/^/gm, Array(indent).join(' ')); | ||
}, | ||
updatePath: function (path) { | ||
return path.replace(/^([^\/])/, '/$1').replace(/:([^/:-]+)/g, '{$1}'); | ||
}, | ||
regReplace: function (what, from, to, flags) { | ||
return what.replace(new RegExp(from, flags), to); | ||
}, | ||
repeat: function (what, count) { | ||
return Array(count + 1).join(what); | ||
}, | ||
}; | ||
} | ||
}; | ||
function getMethodsTree(methodsData) { | ||
var result = {}; | ||
var url = Object.keys(methodsData).sort(); | ||
for (var i = url.length - 1; i >= 0; i--) { | ||
for (var j = i - 1; j >= 0; j--) { | ||
if (undef(result[url[i]]) && url[i].indexOf(url[j] + '/') == 0) { | ||
result[url[i]] = { | ||
shortLink: url[i].replace(url[j] + '/', ''), | ||
deep: url[j].split('/').length, | ||
data: methodsData[url[i]], | ||
}; | ||
} | ||
} | ||
if (undef(result[url[i]])) { | ||
result[url[i]] = { shortLink: url[i], deep: 0, data: methodsData[url[i]] }; | ||
} | ||
} | ||
return result; | ||
} | ||
function parseExpressData(app) { | ||
if (undef(app) || undef(app._router)) return {}; | ||
var s = app._router.stack; | ||
if (!Array.isArray(s)) return {}; | ||
var result = {}; | ||
for (var i = 0; i < s.length; i++) { | ||
if (undef(s[i])) continue; | ||
var r = s[i].route; | ||
if (undef(r) || undef(r.path) || undef(r.methods)) continue; | ||
if (undef(result[r.path])) result[r.path] = {}; | ||
Object.keys(r.methods).forEach(function (method) { | ||
result[r.path][method] = { | ||
description: method + ' ' + r.path, | ||
}; | ||
}); | ||
} | ||
return result; | ||
} | ||
{ | ||
"name": "create-raml", | ||
"version": "1.1.1", | ||
"version": "2.0.1", | ||
"description": "Create RAML", | ||
@@ -28,6 +28,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "^3.3.0", | ||
"coveralls": "^2.11.15", | ||
"express": "^4.15.0", | ||
"istanbul": "^0.4.5", | ||
"chai": "^3.3.0", | ||
"mocha": "^2.3.3", | ||
"node-mocks-http": "^1.6.1", | ||
"raml2html": "^5.0.0", | ||
@@ -34,0 +36,0 @@ "raml2obj": "^5.0.0" |
@@ -15,3 +15,3 @@ | ||
v1.1.1 | ||
v2.0.1 | ||
@@ -26,3 +26,3 @@ | ||
```javascript | ||
var Raml = require('../index'); | ||
var Raml = require('create-raml'); | ||
var raml = new Raml({ | ||
@@ -59,3 +59,3 @@ title: 'Testing', | ||
baseUri: http://localhost:3000 | ||
version: | ||
version: v1 | ||
@@ -105,2 +105,7 @@ types: | ||
## Change Log | ||
[all changes](CHANGELOG.md) | ||
## Created by | ||
@@ -107,0 +112,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var Raml = require('../index'); | ||
var Raml = require('../'); | ||
var testFileName = 'test-raml2html.raml'; | ||
@@ -25,3 +25,3 @@ | ||
describe('testing with raml2html', function () { | ||
describe.skip('testing with raml2html', function () { | ||
@@ -34,3 +34,3 @@ describe('Simple RAML', function () { | ||
baseUri: 'http://localhost:3000', | ||
version: 'v1', | ||
versionAPI: 'v1', | ||
}); | ||
@@ -41,3 +41,3 @@ }); | ||
fs.unlinkSync(testFileName); | ||
this.vm = null; | ||
this.raml = null; | ||
}); | ||
@@ -128,73 +128,2 @@ | ||
describe('type and methods getters', function () { | ||
beforeEach(function () { | ||
this.raml = new Raml(); | ||
this.raml.type('books', { | ||
name: { type: 'string', required: true }, | ||
numberOfPages: { type: 'integer' }, | ||
author: { | ||
name: { type: 'string' }, | ||
email: { type: 'email' }, | ||
}, | ||
}); | ||
this.raml.methods('books', 'get', { | ||
description: 'Get information about all books', | ||
responses: { | ||
200: { 'application/json': [{ name: 'one', author: { name: 'Art' } }] }, | ||
404: { 'application/json': { code: '120', message: 'Books not found' } }, | ||
}, | ||
}); | ||
}); | ||
afterEach(function () { | ||
this.vm = null; | ||
}); | ||
it('type testing', function (done) { | ||
this.raml.type().should.eql({ | ||
books: { | ||
name: { type: 'string', required: true }, | ||
numberOfPages: { type: 'integer' }, | ||
author: { name: { type: 'string' }, email: { type: 'email' } }, | ||
}, | ||
}); | ||
this.raml.type('books').should.eql({ | ||
name: { type: 'string', required: true }, | ||
numberOfPages: { type: 'integer' }, | ||
author: { name: { type: 'string' }, email: { type: 'email' } }, | ||
}); | ||
done(); | ||
}); | ||
it('methods testing', function (done) { | ||
this.raml.methods().should.eql({ | ||
books: { | ||
get: { | ||
description: 'Get information about all books', | ||
responses: { | ||
200: { 'application/json': [{ name: 'one', author: { name: 'Art' } }] }, | ||
404: { 'application/json': { code: '120', message: 'Books not found' } }, | ||
}, | ||
}, | ||
}, | ||
}); | ||
this.raml.methods('books').should.eql({ | ||
get: { | ||
description: 'Get information about all books', | ||
responses: { | ||
200: { 'application/json': [{ name: 'one', author: { name: 'Art' } }] }, | ||
404: { 'application/json': { code: '120', message: 'Books not found' } }, | ||
}, | ||
}, | ||
}); | ||
done(); | ||
}); | ||
}); | ||
describe('errors', function () { | ||
@@ -207,3 +136,3 @@ | ||
afterEach(function () { | ||
this.vm = null; | ||
this.raml = null; | ||
}); | ||
@@ -214,4 +143,3 @@ | ||
(err instanceof Error).should.equal(true); | ||
String(err).should.eql('Error: ENOENT: ' + | ||
"no such file or directory, open 'template/noFile'"); | ||
String(err).should.match(/ENOENT: no such file/); | ||
done(); | ||
@@ -224,1 +152,2 @@ }); | ||
}); | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
38604
17
860
132
8
3
1