Api Doc
Api Doc
http://apidocjs.com
What does apiDoc ?
apiDoc
creates a Documentation HTML-Page
use API-Descriptions from your source code
For who ?
/**
* This is a comment.
*/
Where i find apiDoc ?
Special functions
apiDoc
support own created Templates
includes History
compare old and new API-Method Versions
allows frontend Developer to see what changed
support Inherit
reuse of Documentation parts
extendable
you can create your own Documentation Methods
Ok, let us begin...
Install apiDoc
[sudo] npm install -g apidoc
This are only small parts of apiDocs functions, for all visit http://apidocjs.com
There is also a complex example: http://apidocjs.com/#example-full
Create a new directory my_project.
Within that dir create a file example.js.
example.js
var currentUser = {
name: 'Mary'
};
function getUser() {
return { code: 200, data: currentUser };
}
function setName(name) {
if(name.length === 0) {
return { code: 404, message: 'NameEmptyError' };
}
currentUser.name = name;
return { code: 204 };
}
A basic doc
By default apiDoc search for all .jjs files in current directory and sub directories.
You can change the includeFilter, call apidoc -h for details.
We see the rudimentary Webpage, with Navigation left and Content right.
We have a group User and within that group an API-Method GET /user
Describe the API-Answer
_apidoc.js
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
* @apiVersion 0.1.0
*
* @apiSuccess {String} name The users name.
*
* @apiSuccessExample Example data on success:
*{
* name: 'Paul'
*}
*/
_apidoc.js will be our history file and contains old documentation blocks.
Now we change the documentation block in example.js
example.js
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
* @apiVersion 0.2.0
*
* @apiSuccess {String} name The users name.
* @apiSuccess {Number} age Calculated age from Birthday
*
* @apiSuccessExample Example data on success:
*{
* name: 'Paul',
* age: 27
*}
*/
function getUser() {
return { code: 200, data: currentUser };
}
After call apidoc and open doc/index.html you see now Version 0.2.0 with the
Field age and the changed example.
And now the magic...
Select from Dropdown 0.2.0 the Version 0.1.0.
You see now a comparison from the 2 Versions. Green marked content indicates the
Fields that are added to Version 0.2.0.
In the future, when you made more changes to an API-Method, simply copy and add
the complete documentation block to the history file _apidoc.js.
Leave the old blocks as they are.
Then you have a Documentation where everybody can see any change of your API.
PARAMS AND ERRORS
We take now a quick look at how to add Parameters, that an API-Method need and
how we return errors.
We add now a documentation to our second function setName.
example.js
/**
* @api {put} /user Change User
* @apiName PutUser
* @apiGroup User
* @apiVersion 0.1.0
*
* @apiParam {String} name New name of the user
*
* @apiError NameEmptyError The name was empty. Minimum of <code>1</code> character is required.
*/
function setName(name) {
if(name.length === 0) {
return { code: 404, message: 'NameEmptyError' };
}
currentUser.name = name;
return { code: 204 };
}
You will see now the new Change User entry in navigation and the content for the
API-Method.
Thank you and have a productive use !
For more Information visit http://apidocjs.com