-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgen_doc.js
157 lines (138 loc) · 3.87 KB
/
gen_doc.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const async = require('async')
const fs = require('fs')
const join = require('path').join
const yaml = require('js-yaml')
const apiSchemaPath = join(__dirname, 'swagger.yml')
const apiSchema = yaml.safeLoad(fs.readFileSync(apiSchemaPath))
function pathToDocName(path) {
return `${path.replace(/^\//, '')}`
}
function pathToDocFileName(path) {
return `${path.replace(/^\//, '')}.md`
}
function describeAPI(methodSchema) {
return (methodSchema.decl.description || '').trim()
}
function describeRequestMethod(methodSchema) {
return [
'```',
`${methodSchema.method} {base_url}${methodSchema.path}`,
'```'
].join('\n')
}
function describeRequestParameters(methodSchema) {
let content = []
if (methodSchema.decl.authentication != false) {
content = content.concat([
'**需要登录**',
'',
])
}
const parameters = methodSchema.decl.parameters || []
if (parameters.length > 0) {
content = content.concat([
'| 参数名称 | 参数类型 | 参数是否必须? | 说明 | 样例 |',
'|:--------:|:--------:|:--------------:|------|------|',
])
parameters.forEach((p) => {
if (p.in == 'body') {
// init required
schema = p.schema
required = schema.required || []
Object.keys(schema.properties).forEach((key) => {
let type = '`string`'
if (schema.properties[key].type) {
type = `\`${schema.properties[key].type}\``
}
const line = [
`\`${key}\``,
type,
(required.indexOf(key) != -1) ? '是' : '否',
(schema.properties[key].description || '').replace('\n', ' ').trim(),
(schema.properties[key].example || '').replace('\n', ' ').trim()
].join(' | ')
content.push(`| ${line} |`)
})
} else {
let type = '`string`'
if (p.type) {
type = `\`${p.type}\``
}
const line = [
`\`${p.name}\``,
type,
p.required ? '是' : '否',
(p.description || '').replace('\n', ' ').trim(),
(p.example || '').replace('\n', ' ').trim(),
].join(' | ')
content.push(`| ${line} |`)
}
})
}
if (methodSchema.decl.parameters_example) {
content.push('')
content.push('```javascript')
content.push(methodSchema.decl.parameters_example.trim())
content.push('```')
}
return content.join('\n')
}
function describeResponse(methodSchema) {
const responses = methodSchema.decl.responses
let content = []
Object.keys(responses).forEach((statusCode) => {
if (statusCode === 'default') return
content.push(`### ${statusCode}`);
const examples = responses[statusCode].examples;
if (examples) {
Object.values(examples).forEach((example) => {
content.push('');
content.push('```javascript');
content.push(example.trim());
content.push('```');
});
}
})
if (responses.default) {
content = content.concat([
`### 错误响应`,
'',
'```javascript',
'{',
' "code": // error code,',
' "error": "unexpected error"',
'}',
'```',
])
}
return content.join('\n')
}
async.forEachOf(apiSchema.paths, (decl, path, callback) => {
const methodSchema = {
path,
method: Object.keys(decl)[0].toUpperCase(),
decl: Object.values(decl)[0],
}
const content = [
`# ${pathToDocName(path)}`,
'',
describeAPI(methodSchema),
'',
'## 请求方式',
'',
describeRequestMethod(methodSchema),
'',
'## 请求参数',
'',
describeRequestParameters(methodSchema),
'',
'## 响应',
'',
describeResponse(methodSchema),
'',
'<!-- generated by gen_doc.js -->'
].join('\n')
const file = join(__dirname, pathToDocFileName(path))
fs.writeFileSync(file, content.trim() + '\n')
console.log(`generated ${file}`)
})