create-raml
Advanced tools
Comparing version 3.1.2 to 3.2.2
# create-raml changelog | ||
## [3.2.2](http://github.com/ivanoff/create-raml/tree/3.2.2) (2017-03-07) | ||
[Full Changelog](http://github.com/ivanoff/create-raml/compare/3.1.1...3.2.2) | ||
**What Was Done:** | ||
- guessAll implemented to make description quite pretty | ||
- describe options parameters | ||
## [3.1.1](http://github.com/ivanoff/create-raml/tree/3.1.1) (2017-03-07) | ||
@@ -4,0 +13,0 @@ [Full Changelog](http://github.com/ivanoff/create-raml/compare/3.0.1...3.1.1) |
@@ -29,5 +29,5 @@ # create-raml | ||
express: app, | ||
path: '/api.raml', | ||
storeResponses: true, | ||
guessAll: true, | ||
path: '/api.raml', // path to API RAML | ||
storeResponses: true, // store first response as example | ||
guessAll: true, // make description quite pretty | ||
title: 'Movies Database', | ||
@@ -60,2 +60,4 @@ baseUri: 'http://127.0.0.1:3000', | ||
app.get('/movies/:id/actors', function (req, res) { res.json({}); }); | ||
app.listen(3000, function () { | ||
@@ -135,2 +137,10 @@ console.log('Example app listening on port 3000!'); | ||
#### Get list of actors (not implemented) | ||
``` | ||
curl 127.0.0.1:3000/movies/1/actors | ||
``` | ||
`{}` | ||
### Get RAML | ||
@@ -152,7 +162,7 @@ | ||
get: | ||
description: get /api.raml | ||
description: List all /api.raml | ||
/movies: | ||
get: | ||
description: get /movies | ||
description: List all movies | ||
responses: | ||
@@ -174,3 +184,3 @@ 200: | ||
post: | ||
description: post /movies | ||
description: Insert a new record in to movie collection | ||
body: | ||
@@ -205,3 +215,3 @@ application/json: | ||
get: | ||
description: get /movies/:id | ||
description: Get movie with ID {id} | ||
responses: | ||
@@ -224,3 +234,3 @@ 200: | ||
delete: | ||
description: delete /movies/:id | ||
description: Delete movie with ID {id} | ||
responses: | ||
@@ -241,2 +251,12 @@ 200: | ||
} | ||
/actors: | ||
get: | ||
description: List of actors that movie {id} has | ||
responses: | ||
200: | ||
body: | ||
application/json: | ||
example: | | ||
{} | ||
``` | ||
@@ -243,0 +263,0 @@ |
46
index.js
@@ -44,3 +44,3 @@ /*! | ||
if (options.express && Object.keys(methodsData).length === 0) | ||
methodsData = parseExpressData(options.express); | ||
methodsData = parseExpressData(options); | ||
}; | ||
@@ -62,3 +62,4 @@ | ||
if (options.storeResponses && req.route && req.route.path !== apiPath) | ||
methodsData[req.route.path] = updateMethodsData(methodsData[req.route.path], req, res, c); | ||
methodsData[req.route.path] = | ||
updateMethodsData(methodsData[req.route.path], req, res, c, options); | ||
}; | ||
@@ -141,3 +142,4 @@ | ||
function parseExpressData(app) { | ||
function parseExpressData(options) { | ||
var app = options.express; | ||
if (undef(app) || undef(app._router)) return {}; | ||
@@ -155,3 +157,3 @@ var s = app._router.stack; | ||
result[r.path][method] = { | ||
description: method + ' ' + r.path, | ||
description: guessDescription( method, r.path, options.guessAll ), | ||
}; | ||
@@ -164,3 +166,3 @@ }); | ||
function updateMethodsData(methodsPath, req, res, chunk) { | ||
function updateMethodsData(methodsPath, req, res, chunk, options) { | ||
var r = req.route; | ||
@@ -171,3 +173,3 @@ | ||
methodsPath[r.stack[0].method] = { | ||
description: r.stack[0].method + ' ' + r.path, | ||
description: guessDescription( r.stack[0].method, r.path, options.guessAll ), | ||
}; | ||
@@ -202,1 +204,33 @@ | ||
} | ||
function guessDescription( method, path, guess ) { | ||
var defaultResult = method + ' ' + path; | ||
if(!guess) return defaultResult; | ||
var responseText = { | ||
get : 'List all {names}', | ||
getOne: 'Get {name} with ID {id}', | ||
getSub: 'List of {subnames} that {name} ID {id} owns', | ||
post : 'Insert a new record in to {name} collection', | ||
postSub: 'Insert a new record in to {subname} collection that {name} ID {id} has', | ||
put : 'Replace {name} with ID {id}', | ||
patch : 'Modify {name} with ID {id}', | ||
delete : 'Delete {name} with ID {id}', | ||
}; | ||
var result; | ||
var m = method.toLowerCase(); | ||
var r; | ||
if( r = path.match(/^\/?([^\/]+?)s?\/:([^/:-]+)\/(([^\/]+?)s?)$/) ){ | ||
m += 'Sub'; | ||
result = responseText[m].replace('{name}',r[1]).replace('{id}','{'+r[2]+'}'); | ||
result = result.replace('{subname}',r[4]).replace('{subnames}',r[3]); | ||
} | ||
if( r = path.match(/^\/?([^\/]+?)s?\/:([^/:-]+)$/) ){ | ||
if(m === 'get') m = 'getOne'; | ||
result = responseText[m].replace('{name}',r[1]).replace('{id}','{'+r[2]+'}'); | ||
} | ||
if( r = path.match(/^\/?(([^\/]+?)s?)$/) ){ | ||
result = responseText[m].replace('{name}',r[2]).replace('{names}',r[1]); | ||
} | ||
return result || defaultResult; | ||
} | ||
{ | ||
"name": "create-raml", | ||
"version": "3.1.2", | ||
"version": "3.2.2", | ||
"description": "Create RAML", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,5 +13,5 @@ | ||
### Create RAML from object | ||
### Create RAML from object or Express.js application | ||
v3.1.2 | ||
v3.2.2 | ||
@@ -23,7 +23,7 @@ | ||
## Usage: RAML from express | ||
## Create RAML based on Express.js | ||
### Extended example | ||
- [extended express API example](docs/express_movies_api.md) - movies database API example ( GET, POST, DELETE methods; RAM data storage ). Result example as html: [Movies Database API documentation](http://create-raml.simpleness.org/express_movies_api.html) | ||
- [extended Express API example](docs/express_movies_api.md) - movies database API example ( GET, POST, DELETE methods; RAM data storage ). Result example as html: [Movies Database API documentation](http://create-raml.simpleness.org/express_movies_api.html) | ||
@@ -75,3 +75,3 @@ ### Simple example | ||
## Usage: RAML from object | ||
## Create RAML from object | ||
@@ -105,3 +105,3 @@ ```javascript | ||
## Result | ||
### Result | ||
@@ -152,2 +152,19 @@ ``` | ||
## Options parameters | ||
```javascript | ||
var raml = new Raml(options); | ||
``` | ||
- **version** - version of RAML ( default: 1.0 ) | ||
- **express** - an Express application | ||
- **path** - path to get API RAML ( default: /api.raml ) | ||
- **storeResponses** - store first response as example ( default: false ) | ||
- **guessAll** - make description quite pretty ( default: false ) | ||
- **title** - title of API in document | ||
- **baseUri** - URI of API in document | ||
- **versionAPI** - version of API in document | ||
- **templateFileName** - path to template | ||
## Tests | ||
@@ -154,0 +171,0 @@ |
@@ -95,3 +95,3 @@ 'use strict'; | ||
describe('with httpMocks', function () { | ||
describe('with httpMocks no guessAll', function () { | ||
@@ -190,3 +190,90 @@ var app = { _router: { stack: [ | ||
}); | ||
}); | ||
describe('with httpMocks', function () { | ||
var app = { _router: { stack: [ | ||
{ route: { path: '/aaa', methods: { get: true } } }, | ||
{ route: { path: '/aaa', methods: { post: true } } }, | ||
{ route: { path: '/aaa/:id', methods: { get: true } } }, | ||
], }, }; | ||
app.use = function() {}; | ||
app.get = function() {}; | ||
var raml = new Raml({ express: app, storeResponses: true, guessAll: true }); | ||
var request = {}; | ||
var response = {}; | ||
beforeEach(function(done) { | ||
response = httpMocks.createResponse({ | ||
eventEmitter: require('events').EventEmitter | ||
}); | ||
done(); | ||
}); | ||
it('get /aaa/1/bbb', function (done) { | ||
request = httpMocks.createRequest({ | ||
method: 'GET', | ||
url: '/aaa/1/bbb', | ||
route: { | ||
path: '/aaa/:id/bbb', | ||
stack: [{ | ||
method: 'get', | ||
}] | ||
} | ||
}); | ||
response.on('end', function () { | ||
// console.log(response._getData()); | ||
response.status(200); | ||
response._headers['content-type'] = 'application/json'; | ||
response.end('{"name":"foo"}'); | ||
done(); | ||
}); | ||
var _this = this; | ||
raml.storeResponses(request, response, function next(error) { | ||
raml.express(request, response, function next(error) { | ||
}); | ||
}); | ||
}); | ||
it('post /aaa', 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(201); | ||
response._headers['content-type'] = 'application/json'; | ||
response.end('{"name":"foo"}'); | ||
done(); | ||
}); | ||
var _this = this; | ||
raml.storeResponses(request, response, function next(error) { | ||
raml.express(request, response, function next(error) { | ||
}); | ||
}); | ||
}); | ||
it('post /aaa again', function (done) { | ||
@@ -216,2 +303,39 @@ | ||
// 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); | ||
@@ -218,0 +342,0 @@ response._headers['content-type'] = 'text/other'; |
Sorry, the diff of this file is not supported yet
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
68737
991
200