apidoc-summary
Advanced tools
Comparing version 0.0.1 to 0.0.2
101
index.js
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const defaultMethods = ['get', 'post', 'patch', 'delete']; | ||
let shownMethods; | ||
function Endpoint(name, group){ | ||
this.name = name; | ||
this.group = group; | ||
this.get = ''; | ||
this.post = ''; | ||
this.patch = ''; | ||
this.delete = ''; | ||
function Endpoint(endpointData){ | ||
this.name = endpointData.url; | ||
this.group = endpointData.group; | ||
this[endpointData.type] = endpointData.description; //ie, this.post = 'post to this endpoint for ...' | ||
} | ||
function parseEndpoints(endpointArray){ | ||
let groups = {}; | ||
endpointArray.forEach(e =>{ | ||
if(!groups[e.group]) groups[e.group] = {}; //make a group if it doesn't exist | ||
if(!groups[e.group][e.url]){ //make an endpoint if it doesn't exist | ||
groups[e.group][e.url] = new Endpoint(e); | ||
}else{ //if it already exists, then attach a different method to it | ||
groups[e.group][e.url][e.type] = e.description; //ie, this.delete = 'delete this model ...' | ||
} | ||
}); | ||
return groups; | ||
} | ||
function addGroupRows(groupTitle, columnCount){ | ||
let row = `<tr class="group"><td class="group-name">${groupTitle}</td>`; | ||
for(let i = 0; i < columnCount; i++){ | ||
row += '<td></td>'; | ||
} | ||
return row + '</tr>'; | ||
} | ||
function addMethodRows(endpoint, methods){ | ||
let row = `<tr><td>${endpoint.name}</td>`; | ||
methods.forEach(method =>{ | ||
row += `<td class="description">${endpoint[method] || ''}</td>`; | ||
}); | ||
return row + '</tr>'; | ||
} | ||
function addHeaders(methods){ | ||
let row = '<tr><td></td>'; | ||
methods.forEach(method =>{ | ||
row += `<th>${method}</td>`; | ||
}); | ||
return row + '</tr>'; | ||
} | ||
module.exports = function(options, callback){ | ||
@@ -19,30 +57,20 @@ if(!options.src) throw new Error('options.src is required by apidoc-summary'); | ||
const endpointArray = require(options.src); | ||
if(options.columns && options.columns.length > 0){ | ||
shownMethods = options.columns; | ||
}else{ | ||
shownMethods = defaultMethods; | ||
} | ||
let groups = {}; | ||
const headers = addHeaders(shownMethods); | ||
const groups = parseEndpoints(require(options.src)); | ||
let rows = ''; | ||
endpointArray.forEach(e =>{ | ||
if(e.type === 'info') return; | ||
if(!groups[e.group]) groups[e.group] = {}; | ||
Object.keys(groups).sort().forEach(groupKey =>{ | ||
rows += addGroupRows(groupKey, shownMethods.length); | ||
if(!groups[e.group][e.url]){ | ||
groups[e.group][e.url] = new Endpoint(e.url, e.group); | ||
} | ||
groups[e.group][e.url][e.type] = e.description; | ||
}); | ||
Object.keys(groups[groupKey]).sort().forEach(endpointKey => { | ||
let endpoint = groups[groupKey][endpointKey]; | ||
Object.keys(groups).sort().forEach(group =>{ | ||
rows += `<tr class="separator"><td class="group-name">${group}</td><td></td><td></td><td></td><td></td></tr>`; | ||
Object.keys(groups[group]).sort().forEach(key => { | ||
let endpoint = groups[group][key]; | ||
rows += ` | ||
<tr> | ||
<td>${endpoint.name}</td> | ||
<td class="description">${endpoint.get}</td> | ||
<td class="description">${endpoint.post}</td> | ||
<td class="description">${endpoint.patch}</td> | ||
<td class="description">${endpoint.delete}</td> | ||
</tr>`; | ||
rows += addMethodRows(endpoint, shownMethods); | ||
}); | ||
@@ -72,2 +100,3 @@ }); | ||
font-weight: lighter; | ||
text-transform: uppercase; | ||
} | ||
@@ -77,3 +106,3 @@ table td.description{ | ||
} | ||
table tr.separator{ | ||
table tr.group{ | ||
background-color: aliceblue; | ||
@@ -90,9 +119,3 @@ } | ||
<thead> | ||
<tr> | ||
<td></td> | ||
<th>GET</th> | ||
<th>POST</th> | ||
<th>PATCH</th> | ||
<th>DELETE</th> | ||
</tr> | ||
${headers} | ||
</thead> | ||
@@ -99,0 +122,0 @@ <tbody> |
{ | ||
"name": "apidoc-summary", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Generate a summary page from Apidoc api_data.json", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# apiDoc Summary | ||
Generate an HTML summary page from the api_data.json file created by [apiDoc](http://apidocjs.com/). The summary | ||
is a table with HTML methods as columns (GET, POST, PATCH, DELETE) and the grouped endpoints as the rows. The cells | ||
are the descriptions of the endpoints. | ||
is a table with HTML methods as columns (by default: GET, POST, PATCH, DELETE) and the grouped endpoints as the rows. | ||
Endpoint descriptions fill in the cells. | ||
@@ -10,5 +10,5 @@ ![apiDocSummary Example image](example.png "Example table") | ||
Call from gulp like so: | ||
Call from Gulp like so, making sure that the apiDoc task runs first: | ||
``` | ||
``` javascript | ||
@@ -19,8 +19,13 @@ const apidocSummary = require('apidoc-summary') | ||
gulp.task('apidoc-summary', function(cb){ | ||
gulp.task('apidoc-summary', ['apidoc'], function(cb){ | ||
apidocSummary({ | ||
src: __dirname + '/public/docs/api_data.json', | ||
dest: __dirname + '/public/docs/summary.html' | ||
src: __dirname + '/public/docs/api_data.json', //required | ||
dest: __dirname + '/public/docs/summary.html', //required | ||
columns: ['get', 'post', 'put', 'delete'] //optional | ||
}, cb); | ||
}) | ||
``` | ||
``` | ||
The options `src` and `dest` parameters are required. Optionally, a `columns` parameter can be passed with an array of the | ||
HTTP methods that should be output. At least one method should be passed. | ||
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
14
155
30
28282