showdoc-auto-api
Advanced tools
+37
-11
@@ -11,5 +11,5 @@ 'use strict'; | ||
| constructor({ apiKey, apiToken, homePage = 'https://console.ksbao.com', prefix = 'API说明', createIndex = true }) { | ||
| constructor({ apiKey, apiToken, homePage = 'https://console.ksbao.com', prefix = 'API说明', createIndex = true, fileExt = '{js,java,go,py}' }) { | ||
| if (!apiKey || !apiToken) { | ||
| throw new Error('node-showdoc: invaild config. api_key, api_token and itemID required.'); | ||
| throw new Error('node-showdoc: invaild config. api_key, api_token.'); | ||
| } | ||
@@ -29,2 +29,3 @@ let url = homePage + '/server/index.php?s='; | ||
| createIndex, | ||
| fileExt, | ||
| }; | ||
@@ -35,12 +36,8 @@ } | ||
| const apis = []; | ||
| const check = {}; | ||
| for (const folder of folders) { | ||
| const jsFiles = glob.sync(path.join(folder, './**/*.js')); | ||
| const jsFiles = glob.sync(path.join(folder, './**/*.' + this.config.fileExt)); | ||
| for (const filePath of jsFiles) { | ||
| const apiInfos = this.readApi(filePath); | ||
| this.checkApis(apiInfos); | ||
| for (const ai of apiInfos) { | ||
| if (check[ai.path[0]]) { | ||
| throw new Error('接口路径重复:' + ai.path[0]); | ||
| } | ||
| check[ai.path[0]] = true; | ||
| apis.push(ai); | ||
@@ -53,3 +50,29 @@ } | ||
| checkApis(apis) { | ||
| const check = {}; | ||
| for (const api of apis) { | ||
| if (check[api.path[0]]) { | ||
| throw new Error('接口路径重复:' + api.path[0]); | ||
| } | ||
| check[api.path[0]] = true; | ||
| } | ||
| } | ||
| async startAsJson(json) { | ||
| const apis = []; | ||
| for (const filePath of Object.keys(json)) { | ||
| const content = json[filePath]; | ||
| const apiInfos = this.readApi(filePath, content); | ||
| this.checkApis(apiInfos); | ||
| for (const ai of apiInfos) { | ||
| apis.push(ai); | ||
| } | ||
| } | ||
| await this.buildDoc(apis); | ||
| } | ||
| async buildDoc(apis) { | ||
| if (apis.length <= 0) { | ||
| return; | ||
| } | ||
| const { prefix, createIndex } = this.config; | ||
@@ -228,5 +251,8 @@ const group = {}; | ||
| readApi(filePath) { | ||
| readApi(filePath, fileContent) { | ||
| const ret = []; | ||
| const rawComments = parse(fs.readFileSync(filePath, 'utf-8'), { spacing: 'preserve' }); | ||
| if (!fileContent) { | ||
| fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
| } | ||
| const rawComments = parse(fileContent, { spacing: 'preserve' }); | ||
| const tagHanlder = this.getTagHanlder(); | ||
@@ -250,3 +276,3 @@ const simpleTag = this.getSimpleTag(); | ||
| } | ||
| if(!apiInfo.desc.trim()){ | ||
| if (!apiInfo.desc.trim()) { | ||
| throw new Error(filePath + ':' + apiInfo.path[0] + ' 接口描述不能为空'); | ||
@@ -253,0 +279,0 @@ } |
+1
-1
| { | ||
| "name": "showdoc-auto-api", | ||
| "version": "1.0.1", | ||
| "version": "1.1.0", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
9387
6.94%291
8.99%