Skip to content

Commit

Permalink
It works!!! Now I have to refactor and do some tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Cheung committed Jun 5, 2015
1 parent d850f3f commit 5b4c1eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{swagger-express}
=========

# Notes
* YAML does not like tabs. Do no use them in the Swagger JSDoc comments!!!

[Swagger](https://developers.helloreverb.com/swagger/) is a specification and complete framework
implementation for describing, producing, consuming, and visualizing RESTful web services.
View [demo](http://petstore.swagger.wordnik.com/).
Expand Down
11 changes: 4 additions & 7 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var express = require('express'),
routes = require('./routes'),
swagger = require('jsdoc-express-with-swagger');
swagger = require('..');

var app = express();

Expand All @@ -11,9 +11,9 @@ swagger.init(app, {
swaggerUiPath: '/api',
info: {
title: 'Hello World',
version: '1.0.0',
apis: ['./routes.js']
}
version: '1.0.0'
},
apis: ['./routes.js']
});

routes.setup(app);
Expand All @@ -23,7 +23,4 @@ var server = app.listen(3000, function () {
var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);

// FIXME: This is here for testing. Remove when finished.
console.dir(swagger.swaggerObject);
});
34 changes: 12 additions & 22 deletions example/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,22 @@ module.exports.setup = function (app) {

/**
* @swagger
* /:
* get:
* responses:
* 200:
* description: hello world
*/
app.get('/', rootHandler);

/**
* @swagger
* path: /login
* operations:
* - httpMethod: POST
* summary: Login with username and password
* notes: Returns a user based on username
* responseClass: User
* nickname: login
* consumes:
* - text/html
* parameters:
* - name: username
* description: Your username
* paramType: query
* required: true
* dataType: string
* - name: password
* description: Your password
* paramType: query
* required: true
* dataType: string
*/
* @swagger
* /login:
* post:
* responses:
* 200:
* description: login
*/
app.get('/login', loginHandler);
};

Expand Down
23 changes: 15 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports.init = function (app, options) {
throw new Error('\'apis\' is required.');
}

options.swaggerUiDir = options.swaggerUiDir || './swagger-ui';
options.swaggerUiDir = options.swaggerUiDir || path.join(__dirname, '/swagger-ui');
module.exports.swaggerObject.info = options.info;

// Parse the documentation in the APIs array.
Expand All @@ -64,8 +64,9 @@ module.exports.init = function (app, options) {
*/
function parseApiFile(file) {
var fileExtension = path.extname(file);

if (fileExtension === '.js') {
parseJsDoc(file);
parseJsFile(file);
}
else {
throw new Error('Unsupported extension \'' + fileExtension + '\'.');
Expand All @@ -76,15 +77,20 @@ function parseApiFile(file) {
* Parse the JSDoc comments from a JavaScript file.
* @param {string} file - File to parse
*/
function parseJsDoc(file) {
var jsSourceCode = fs.readFileSync(file, { encodeing: 'utf8' });
var jsDocComments = parseJsDocComments(jsSourceCode);
function parseJsFile(file) {
var sourceCode = fs.readFileSync(file, { encoding: 'utf8' });
var jsDocComments = parseJsDoc(sourceCode);
var swaggerJsDocComments = filterSwaggerTags(jsDocComments);

// Attach the findings to the Swagger object.
for (var i = 0; i < swaggerJsDocComments.length; i++) {
var pathObject = swaggerJsDocComments[i];
module.exports.swaggerObject.paths[pathObject.path] = pathObject.operations;
var propertyNames = Object.getOwnPropertyNames(pathObject);

for (var j = 0; j < propertyNames.length; j++) {
var propertyName = propertyNames[j];
module.exports.swaggerObject.paths[propertyName] = pathObject[propertyName];
}
}
}

Expand All @@ -93,7 +99,7 @@ function parseJsDoc(file) {
* @param {string} sourceCode - Source code to parse
* @returns {array}
*/
function parseJsDocComments(jsSourceCode) {
function parseJsDoc(jsSourceCode) {
var jsDocRegex = /\/\*\*([\s\S]*?)\*\//gm;
var fragments = jsSourceCode.match(jsDocRegex);
var jsDocs = [];
Expand All @@ -102,7 +108,6 @@ function parseJsDocComments(jsSourceCode) {
for (var i = 0; i < fragments.length; i++) {
var fragment = fragments[i];
var jsDoc = doctrine.parse(fragment, { unwrap: true });

jsDocs.push(jsDoc);
}
}
Expand All @@ -117,6 +122,7 @@ function parseJsDocComments(jsSourceCode) {
*/
function filterSwaggerTags(jsDocs) {
var swaggerJsDocs = [];

for (var i = 0; i < jsDocs.length; i++) {
var jsDoc = jsDocs[i];
for (var j = 0; j < jsDoc.tags.length; j++) {
Expand All @@ -126,5 +132,6 @@ function filterSwaggerTags(jsDocs) {
}
}
}

return swaggerJsDocs;
}

0 comments on commit 5b4c1eb

Please sign in to comment.