-
Notifications
You must be signed in to change notification settings - Fork 2
Code documentation
Code documentation appears to be not so clear into JS world. I've been reading several pages and blogs about this, and finally I got 2 main solutions, jsdoc-toolkit and ScriptDoc.
At the beginning I leaned for ScriptDoc, because people say is very similar to Javadoc and some days ago I developed a mobile app using Titanium. But, I found JSDoc more mature, even when the project appears to be forgotten (last commit on June 2010).
Finally, I selected JSDoc. Here is a list of people using it. The project documentation here and a list of tags here
From /lib/rest/common.js
/**
* @fileOverview Common JS for REST services (express-examples/sqlite)
*
* @author Rodolfo Campos <camposer at gmail dot com>
* @version 1.0
*/
/**
* HTTP codes
* @see For all HTTP codes refer to: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*/
var HTTP_CODE_OK = 200
, HTTP_CODE_UNAUTHORIZED = 401
, HTTP_CODE_FORBIDDEN = 403
, HTTP_CODE_NOT_FOUND = 404
, HTTP_CODE_METHOD_NOT_ALLOWED = 405
, HTTP_CODE_PRECONDITION_FAILED = 412;
/**
* JSON Content-Type HTTP Header
* @field
*/
var JSON_RESPONSE_HEADER = {'Content-Type': 'application/json; charset=utf-8'};
/**
* Writes HTTP code 200 and Content-Type for JSON in HTTP response headers
* @function
* @param {HttpResponse} res HTTP response
* @param {Number} code HTTP code to be written in header
*/
var ok = function(res) {
res.writeHead(HTTP_CODE_OK, this.JSON_RESPONSE_HEADER);
};
/**
* Writes HTTP code 4XX (error) and Content-Type for JSON in HTTP response headers
* @function
* @param {HttpResponse} res HTTP response
* @param {Number} code HTTP code to be written into header
*/
var nok = function(res, code) {
res.writeHead(code, JSON_RESPONSE_HEADER);
};
/**
* Executes validate method using params, if true, calls execute method, if not, returns HTTP code 412
* @param {HttpResponse} res HttpResponse object
* @param {Mixed|Object} params Can be value or object (JSON)
* @param {Function([params])} validate Function used for validation, if returns true everything is OK,
* if not something went wrong and call returns HTTP_CODE_PRECONDITION_FAILED(412)
* @param {Function(params, callback)|Function(callback)} execute Function executed if validate=true.
* Parameters (params) can be value (e.g. String) or Object (JSON). Callback should receive
* one parameter (result)
*
* @example
* common.call(
* res,
* params.name,
* function(name) {
* var valid = true;
*
* if (name == null)
* valid = false;
*
* return valid;
* },
* function(params, callback) {
* // ...
* // Get result
* callback(result);
* }
* );
*/
this.call = function(res, params, validate, execute) {
if (validate(params)) {
if (params != null) {
execute(params, function(result) { // TODO: Add extra parameter for error msg
if (result)
ok(res);
else
nok(res, HTTP_CODE_FORBIDDEN);
res.end(JSON.stringify(result));
});
} else {
execute(function(result) { // TODO: Add extra parameter for error msg
if (result)
ok(res);
else
nok(res, HTTP_CODE_FORBIDDEN);
res.end(JSON.stringify(result));
});
}
} else {
nok(res, HTTP_CODE_PRECONDITION_FAILED);
res.end();
}
};
For generating the HTML documentation corresponding to the example above (common.js), you just have to download jsdoc-tool from here, unzip it, and run the following command:
$ java -jar jsrun.jar app/run.js -a -t=templates/jsdoc -d=common common.js
Where:
- jsrun.jar: Is the generator
- app/run.js: Is the bootstrap script
- -a: For including all functions, even undocumented ones
- templates/jsdoc: Directory of the template to be used
- -d: Directory where output is going to put. By default out.
- common.js: File to be analyzed
For JsDoc inline documentation use:
$ java -jar jsrun.jar app/run.js --help
And for generating all project documentation just add -r option and the directory path. For optimal results you may want to remove, while generating documentation, node_modules folder.
$ java -jar jsrun.jar app/run.js -a -t=templates/jsdoc -d=sqlite -r ~/express-examples/sqlite