-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate_docs.js
86 lines (76 loc) · 2.42 KB
/
generate_docs.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Generate a API docs proposal based on the webpage it is executed on
* @returns {string} - The API docs proposal
*/
function generateDocs(json, url, authed = null) {
json = json || JSON.parse(document.body.innerText);
url = url ? new URL(url) : new URL(location.href);
function buildDocs(json, extraCols, prefix) {
let text = "";
let todo = new Array(extraCols).fill(` \`TODO\` |`).join("");
for (let [key, value] of Object.entries(json)) {
let name = `${prefix}${key}${Array.isArray(value) ? "[]" : ""}`;
text += `> | \`${name}\` | ${Array.isArray(value) ? "array" : typeof value
} |${todo}\n`;
if (Array.isArray(value) && value.length > 0) {
if (value.every(v => v instanceof Object)) {
let combined = {};
for (let item of value) {
combined = Object.assign(combined, item);
}
text += buildDocs(combined, extraCols, name + ".");
} else {
text += buildDocs(value[0], extraCols, name + ".");
}
} else if (typeof value === "object" && value !== null) {
text += buildDocs(value, extraCols, name + ".");
}
}
return text;
}
let params = {};
for (let [key, value] of url.searchParams.entries()) {
params[key] = value;
}
let paramDocs = buildDocs(params, 2, "");
let responseDocs = "";
if (Array.isArray(json) && json.length > 0) {
responseDocs = `> | \`[]\` | array | \`TODO\` |\n${buildDocs(json[0], 1, "[]")}`;
} else {
responseDocs = buildDocs(json, 1, "");
}
let output = `# GET ${url.pathname}
## Rate limits
> *No known rate limit*
## Request
> **Authenticated**: ${authed === false ? "No" : authed === true ? "Yes" : `\`TODO\``}
>
> **Method**: \`GET\`
>
> **Host**: \`${url.host}\`
>
> **Path**: \`${url.pathname}\`
>
> **Query Parameters**:
> | **Name** | **Type** | **Required** | **Description** |
> |:---|:---|:---|:---|
${paramDocs}
## Response
> ### 200 OK
> | **Name** | **Type** | **Description** |
> |:---|:---|:---|
${responseDocs}
## Example
\`\`\`
GET ${url.href}
\`\`\`
\`\`\`json
${JSON.stringify(json, null, 4)}
\`\`\``;
return output;
}
if (typeof module === "object") {
module.exports = generateDocs;
} else {
console.log(generateDocs());
}