create-raml
Advanced tools
Comparing version 2.0.1 to 3.0.1
# create-raml changelog | ||
## [3.0.1](http://github.com/ivanoff/create-raml/tree/3.0.1) (2017-03-05) | ||
[Full Changelog](http://github.com/ivanoff/create-raml/compare/2.0.1...3.0.1) | ||
**What Was Done:** | ||
- add express support | ||
## [2.0.1](http://github.com/ivanoff/create-raml/tree/2.0.1) (2017-03-01) | ||
@@ -8,6 +16,6 @@ [Full Changelog](http://github.com/ivanoff/create-raml/compare/1.1.1...2.0.1) | ||
- 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) | ||
@@ -14,0 +22,0 @@ [Full Changelog](http://github.com/ivanoff/create-raml/compare/1.0.1...1.1.1) |
79
index.js
@@ -17,2 +17,3 @@ /*! | ||
var ramlFile = (options.version === 0.8) ? 'raml_0_8.nunjucks' : 'raml_1_0.nunjucks'; | ||
var apiPath = options.path || '/api.raml'; | ||
if (undef(options.templateFileName)) | ||
@@ -27,3 +28,3 @@ options.templateFileName = path.resolve(__dirname, 'templates', ramlFile); | ||
: typesData[name] = obj; | ||
} | ||
}; | ||
@@ -41,37 +42,28 @@ exp.methods = function (name, method, obj) { | ||
} | ||
} | ||
}; | ||
exp.express = function (req, res) { | ||
exp.checkMethodsData = function () { | ||
if (options.express && Object.keys(methodsData).length === 0) | ||
methodsData = parseExpressData(options.express); | ||
}; | ||
exp.express = function (req, res) { | ||
this.checkMethodsData(); | ||
this.generate(function (err, data) { | ||
res.send(data); | ||
}); | ||
} | ||
}; | ||
exp.storeResponses = function (req, res, next) { | ||
this.checkMethodsData(); | ||
var resEnd = res.end; | ||
res.end = function (chunk) { | ||
res.end = function (c) { | ||
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, | ||
} | ||
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!' | ||
}; | ||
} | ||
if (options.storeResponses && req.route && req.route.path !== apiPath) | ||
methodsData[req.route.path] = updateMethodsData(methodsData[req.route.path], req, res, c); | ||
}; | ||
next(); | ||
} | ||
}; | ||
@@ -93,6 +85,9 @@ exp.generate = function (cb) { | ||
}); | ||
}; | ||
if (options.express) { | ||
options.express.use(exp.storeResponses.bind(exp)); | ||
options.express.get(apiPath, exp.express.bind(exp)); | ||
} | ||
if(options.express) options.express.use(exp.storeResponses.bind(exp)) | ||
return exp; | ||
@@ -169,1 +164,37 @@ | ||
function updateMethodsData(methodsPath, req, res, chunk) { | ||
var r = req.route; | ||
if (undef(methodsPath)) methodsPath = {}; | ||
if (undef(methodsPath[r.stack[0].method])) | ||
methodsPath[r.stack[0].method] = { | ||
description: r.stack[0].method + ' ' + r.path, | ||
}; | ||
var m = methodsPath[r.stack[0].method]; | ||
if (req.headers['content-type']) { | ||
var reqCType = req.headers['content-type'].match(/([^;]*(application|text)[^;]*)/); | ||
if (reqCType && reqCType[1]) { | ||
if (undef(m.body)) m.body = {}; | ||
if (undef(m.body[reqCType[1]])) | ||
m.body[reqCType[1]] = { | ||
example: req.body, | ||
}; | ||
} | ||
} | ||
if (res._headers['content-type']) { | ||
var resCType = res._headers['content-type'].match(/([^;]*(application|text)[^;]*)/); | ||
if (resCType && resCType[1]) { | ||
var result = chunk.toString(); | ||
if (resCType[1] === 'application/json') result = JSON.parse(result); | ||
if (undef(m.responses)) m.responses = {}; | ||
if (undef(m.responses[res.statusCode])) m.responses[res.statusCode] = {}; | ||
if (undef(m.responses[res.statusCode][resCType[1]])) | ||
m.responses[res.statusCode][resCType[1]] = result; | ||
} | ||
} | ||
return methodsPath; | ||
} |
{ | ||
"name": "create-raml", | ||
"version": "2.0.1", | ||
"version": "3.0.1", | ||
"description": "Create RAML", | ||
@@ -28,2 +28,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"body-parser": "^1.17.0", | ||
"chai": "^3.3.0", | ||
@@ -35,3 +36,2 @@ "coveralls": "^2.11.15", | ||
"node-mocks-http": "^1.6.1", | ||
"raml2html": "^5.0.0", | ||
"raml2obj": "^5.0.0" | ||
@@ -38,0 +38,0 @@ }, |
@@ -15,3 +15,3 @@ | ||
v2.0.1 | ||
v3.0.1 | ||
@@ -23,6 +23,53 @@ | ||
## Usage | ||
## Usage: RAML from express | ||
- [extended express API example](docs/express_movies_api.md) | ||
```javascript | ||
var express = require('express'); | ||
var Raml = require('create-raml'); | ||
var app = express(); | ||
var raml = new Raml({ express: app }); | ||
app.get('/movies', function (req, res) { res.send('List of all movies'); }); | ||
app.post('/movies', function (req, res) { res.send('Add new movie'); }); | ||
app.get('/movies/:id', function (req, res) { res.send('Get movie by id'); }); | ||
app.delete('/movies/:id', function (req, res) { res.send('Delete movie by id'); }); | ||
app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); | ||
``` | ||
```curl 127.0.0.1:3000/api.raml``` | ||
### Result | ||
``` | ||
#%RAML 1.0 | ||
title: | ||
version: | ||
types: | ||
/api.raml: | ||
get: | ||
description: get /api.raml | ||
/movies: | ||
get: | ||
description: get /movies | ||
post: | ||
description: post /movies | ||
/{id}: | ||
get: | ||
description: get /movies/:id | ||
delete: | ||
description: delete /movies/:id | ||
``` | ||
## Usage: RAML from object | ||
```javascript | ||
var Raml = require('create-raml'); | ||
var raml = new Raml({ | ||
@@ -29,0 +76,0 @@ title: 'Testing', |
@@ -22,5 +22,6 @@ 'use strict'; | ||
app.use = function() {}; | ||
app.get = function() {}; | ||
beforeEach(function () { | ||
this.raml = new Raml({ express: app }); | ||
this.raml = new Raml({ express: app, storeResponses: true }); | ||
}); | ||
@@ -46,3 +47,4 @@ | ||
if(app) app.use = function() {}; | ||
this.raml = new Raml({ express: app }); | ||
if(app) app.get = function() {}; | ||
this.raml = new Raml({ express: app, storeResponses: true }); | ||
}); | ||
@@ -103,4 +105,5 @@ | ||
app.use = function() {}; | ||
app.get = function() {}; | ||
var raml = new Raml({ express: app }); | ||
var raml = new Raml({ express: app, storeResponses: true }); | ||
var request = {}; | ||
@@ -124,2 +127,8 @@ var response = {}; | ||
}, | ||
headers: { | ||
'content-type' : 'application/json', | ||
}, | ||
body: { | ||
name: 'foo' | ||
}, | ||
route: { | ||
@@ -135,3 +144,5 @@ path: '/aaa', | ||
// console.log(response._getData()); | ||
response.status(400); | ||
response.status(201); | ||
response._headers['content-type'] = 'application/json'; | ||
response.end('{"name":"foo"}'); | ||
done(); | ||
@@ -155,2 +166,8 @@ }); | ||
}, | ||
headers: { | ||
'content-type' : 'application/json', | ||
}, | ||
body: { | ||
name: 'foo' | ||
}, | ||
route: { | ||
@@ -166,3 +183,79 @@ path: '/aaa', | ||
// console.log(response._getData()); | ||
response.status(201); | ||
response._headers['content-type'] = 'application/json'; | ||
response.end('{"error":"conflict"}'); | ||
done(); | ||
}); | ||
var _this = this; | ||
raml.storeResponses(request, response, function next(error) { | ||
raml.express(request, response, function next(error) { | ||
}); | ||
}); | ||
}); | ||
it('post /aaa again', function (done) { | ||
request = httpMocks.createRequest({ | ||
method: 'POST', | ||
url: '/aaa', | ||
query: { | ||
name: 'foo' | ||
}, | ||
headers: { | ||
'content-type' : 'application/json', | ||
}, | ||
body: { | ||
name: 'foo' | ||
}, | ||
route: { | ||
path: '/aaa', | ||
stack: [{ | ||
method: 'post', | ||
}] | ||
} | ||
}); | ||
response.on('end', function () { | ||
// console.log(response._getData()); | ||
response.status(409); | ||
response._headers['content-type'] = 'text/other'; | ||
response.end('{"error":"conflict"}'); | ||
done(); | ||
}); | ||
var _this = this; | ||
raml.storeResponses(request, response, function next(error) { | ||
raml.express(request, response, function next(error) { | ||
}); | ||
}); | ||
}); | ||
it('post /aaa unknown content type', function (done) { | ||
request = httpMocks.createRequest({ | ||
method: 'POST', | ||
url: '/aaa', | ||
query: { | ||
name: 'foo' | ||
}, | ||
headers: { | ||
'content-type' : 'unknown', | ||
}, | ||
body: { | ||
name: 'foo' | ||
}, | ||
route: { | ||
path: '/aaa', | ||
stack: [{ | ||
method: 'post', | ||
}] | ||
} | ||
}); | ||
response.on('end', function () { | ||
// console.log(response._getData()); | ||
response.status(400); | ||
response._headers['content-type'] = 'unknown'; | ||
response.end('{"name":"foo"}'); | ||
done(); | ||
@@ -169,0 +262,0 @@ }); |
Sorry, the diff of this file is not supported yet
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
43281
179
2
0
851