… use API-Descriptions from your source code For who ? apiDoc can be used with any programming language, that allow block documentation: /** * This is a comment. */
distributed over NPM and github Visit: http://apidocjs.com 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
install Node.js first: http://nodejs.org Node.js includes „npm“, a package manager for node. Install apiDoc [sudo] npm install -g apidoc We install apiDoc global, so you can access it from everywhere.
ignore routing, validation or database operations. 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
documentation block /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User */ function getUser() { return { code: 200, data: currentUser }; } This 3 parameters are required! @api describes the path of the Method that clients call. @apiName is a unique Name, you can name as you want, for simplicity i prefer „Type of Method“ + „Name of Method“ e.g. {get} and „/user“ => GetUser. @apiGroup is the name of the group to which this method belongs. The group will be used for Navigation.
directory and sub directories. You can change the includeFilter, call „apidoc -h“ for details. apiDoc created a new HTML-Page in directory „doc“. From command line run within „my_project“ directory: apidoc
* @api {get} /user Request User information * @apiName GetUser * @apiGroup User * * @apiSuccess {String} name The users name. */ function getUser() { return { code: 200, data: currentUser }; } apidoc Run apiDoc again, it will overwrite the „doc“ Directory
(Format “major.minor.patch”, see http://semver.org for details) and we add an example code, that shows what will be return in case of success /** * @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' * } */ function getUser() { return { code: 200, data: currentUser }; }
work an the API backend and some on Frontend Client, it is important to know what changed in a new Release. Especially during developing an Application, many things can change.
only the documentation Block from „getUser“ in that file: _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.
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.
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 }; }