-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiDocGenerator.js
executable file
·72 lines (58 loc) · 2.1 KB
/
ApiDocGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const handlebars = require('./external_libs/handlebars');
const utils = require('./utils');
const APIDocGenerator = function () {
this.generate = function (context, requests, options) {
const request = context.getCurrentRequest();
const headers = [];
const params = [];
const response = [];
console.log(request.getLastExchange().responseStatusCode)
const responseBody = JSON.parse(request.getLastExchange().responseBody);
if (request.getLastExchange().responseStatusCode && responseBody.data) {
extractKeyNamesAndTypes(responseBody.data, response);
}
extractKeyNamesAndTypes(request.headers, headers);
extractKeyNamesAndTypes(request.urlParameters, params);
extractKeyNamesAndTypes(request.jsonBody, params);
const view = {
headers: headers,
params: params,
response: response,
method: request.method,
request_name: request.name,
api_name: utils.toCamelCase(request.name),
request_description: request.description,
request_group: request.parent ? request.parent.name : `...`,
url: request.urlBase
};
handlebars.registerHelper('surroundWithCurlyBraces', function(text) {
var result = '{' + text + '}';
return new handlebars.SafeString(result);
});
const template = handlebars.compile(readFile('./ApiDocTemplate.hbs'));
return template(view);
};
};
function processor (key, type, result) {
result.push({
name: key,
type
});
}
const traverse = function(o, parent_key = null, result) {
for (const key in o) {
const parent = parent_key ? parent_key + "." : "";
processor(`${parent}${key}`, `${utils.getType(o[key])}`, result)
if (o[key] !== null && typeof(o[key])=="object") {
traverse(o[key], `${parent}${key}`, result);
}
}
}
const extractKeyNamesAndTypes = function (theObj, resultsconstiable) {
traverse(theObj, null, resultsconstiable)
};
APIDocGenerator.identifier = 'com.ygilany.PawExtensions.apiDocGenerator';
APIDocGenerator.title = 'ApiDoc Paw Generator';
APIDocGenerator.languageHighlighter = 'javascript'; // The Javascript highlighter doesn't highlight :(
APIDocGenerator.fileExtension = 'js';
registerCodeGenerator(APIDocGenerator);