Skip to content

Commit 481ed9e

Browse files
committed
feat: support tags
1 parent 77b2328 commit 481ed9e

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "outdoc",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Auto-generate OpenAPI document for Node.js service from the local testing",
55
"main": "lib/index.js",
66
"bin": {

src/APICollector.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import type { API_URL } from './types/apiTypes';
77

88
export default class APICollector {
99
private items: Record<API_URL, OpenAPIV3_1.PathItemObject>;
10+
private pathCount: Record<string, number>;
1011

1112
constructor () {
1213
this.items = {};
14+
this.pathCount = {};
1315
}
1416

1517
/**
@@ -227,12 +229,24 @@ export default class APICollector {
227229
};
228230
}
229231

232+
private increasePathCountByUrl (url: string) {
233+
const paths = url.split("/").slice(1, 3);
234+
paths.forEach(path => {
235+
this.pathCount[path] = this.pathCount[path] || 0;
236+
this.pathCount[path]++;
237+
});
238+
}
239+
230240
private insertNewAPIItem (
231241
url: API_URL,
232242
method: OpenAPIV3_1.HttpMethods,
233243
methodContent: OpenAPIV3_1.OperationObject,
234244
statusCode: number
235245
): void {
246+
if (!this.items[url]) {
247+
this.increasePathCountByUrl(url);
248+
}
249+
236250
// If the url method doesnt exist
237251
if (!this.items[url] || !this.items[url][method]) {
238252
this.items[url] = this.items[url] || {};
@@ -273,8 +287,11 @@ export default class APICollector {
273287
const statusCode = serverResponse.statusCode;
274288
const url = this.extractURL(serverResponse);
275289
if (!url) throw new Error('No url found');
290+
276291
const method = this.extractMethod(serverResponse);
277-
const operationObj: OpenAPIV3_1.OperationObject = {};
292+
const operationObj: OpenAPIV3_1.OperationObject = {
293+
tags: undefined
294+
};
278295
const methodsHasReqBody = ['post', 'put'];
279296

280297
if (statusCode < 400) {
@@ -303,5 +320,9 @@ export default class APICollector {
303320
public getItems(): Record<API_URL, OpenAPIV3_1.PathItemObject> {
304321
return this.items;
305322
}
323+
324+
public getCountByPath(path: string): number {
325+
return this.pathCount[path] || 0;
326+
}
306327
}
307328

src/APIGenerator.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,42 @@ type GenDocOpts = {
1616
const SUPPORTED_FORMAT = ['.json', '.yaml'];
1717

1818
export default class APIGenerator {
19+
private static getTagByUrl (
20+
url: API_URL,
21+
apiCollector: APICollector
22+
): string | undefined {
23+
const paths = url.split("/").slice(1, 3);
24+
const totalURLAmount = Object.keys(apiCollector.getItems()).length;
25+
let tag;
26+
let max = 0;
27+
paths.forEach((path, index) => {
28+
const count = apiCollector.getCountByPath(path);
29+
if (index === 0) {
30+
if (count <= 1 || count >= totalURLAmount -1) {
31+
return;
32+
} else {
33+
tag = path.trim();
34+
max = Infinity;
35+
}
36+
} else {
37+
if (count > max && count > 1) {
38+
tag = path.trim();
39+
max = count;
40+
}
41+
}
42+
});
43+
return tag;
44+
}
45+
46+
private static addTagToPathItem(
47+
tag: string,
48+
pathItem: OpenAPIV3_1.PathItemObject
49+
): void {
50+
Object.keys(pathItem).forEach(key => {
51+
pathItem[key as OpenAPIV3_1.HttpMethods]!.tags = [tag]
52+
})
53+
}
54+
1955
public static async generate (
2056
apiCollector: APICollector,
2157
opts: GenDocOpts
@@ -25,6 +61,10 @@ export default class APIGenerator {
2561
.reduce((acc, cur) => {
2662
const key: API_URL = cur[0];
2763
const value: OpenAPIV3_1.PathItemObject = cur[1];
64+
const tag = APIGenerator.getTagByUrl(key, apiCollector);
65+
if (tag) {
66+
APIGenerator.addTagToPathItem(tag, value);
67+
}
2868
acc[key] = value;
2969
return acc;
3070
}, {} as Record<string, any>);

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export const PREFIX_RESPONSE_BODY_DATA = 'outdoc response body data -';
2-
export const PREFIX_SERVER_RESPONSE = 'outdoc server response -';
2+
export const PREFIX_SERVER_RESPONSE = 'outdoc server response -';

0 commit comments

Comments
 (0)