Skip to content

Commit 6fa0829

Browse files
committed
feat: 新增文档加载事件
1 parent 99b6c3a commit 6fa0829

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,9 @@ rpc模式的优点:
158158
根据Tag生成不同的分组,以类似 **client.user.getUsers()** 这种方式调用。仅在 `classMode=rpc` 场景下生效。
159159

160160
如果没有提供tags,则默认合并到`default`分组
161+
162+
### onDocumentLoaded
163+
164+
类型:`(docs: Document) => Document | void`
165+
166+
加载完openapi文档后的事件,允许直接对文档进行修改

src/bin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ spinner.add({
3131
spinner.add({
3232
title: '获取openapi文档',
3333
task: async (ctx) => {
34-
ctx.docs = await Promise.all(ctx.configs.map((config) => pathToOpenapi(config.path)));
34+
ctx.docs = await Promise.all(
35+
ctx.configs.map((config) => pathToOpenapi(config.path, config.onDocumentLoaded)),
36+
);
3537
await sleep();
3638
},
3739
});

src/define-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { OpenAPIV3 } from 'openapi-types';
2+
13
export interface OpenapiClientConfig {
24
/**
35
* openapi本地或者远程文件,支持格式:`yaml | json`
@@ -35,6 +37,10 @@ export interface OpenapiClientConfig {
3537
* 如果没有提供tags,则默认合并到`default`分组
3638
*/
3739
tagToGroup?: boolean;
40+
/**
41+
* 加载完openapi文档后的事件,允许直接对文档进行修改
42+
*/
43+
onDocumentLoaded?: (doc: OpenAPIV3.Document) => OpenAPIV3.Document | void;
3844
}
3945

4046
export const defineConfig = (options: OpenapiClientConfig | OpenapiClientConfig[]) => {

src/lib/path-to-openapi.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { readFile } from 'fs/promises';
22
import path from 'node:path';
33
import type { OpenAPIV3 } from 'openapi-types';
44
import YAML from 'yaml';
5+
import type { OpenapiClientConfig } from '../define-config';
56

6-
export const pathToOpenapi = async (uri: string): Promise<OpenAPIV3.Document> => {
7+
export const pathToOpenapi = async (
8+
uri: string,
9+
onLoaded?: OpenapiClientConfig['onDocumentLoaded'],
10+
): Promise<OpenAPIV3.Document> => {
711
let originContent: string;
812
if (uri.startsWith('http:') || uri.startsWith('https:')) {
913
const response = await fetch(uri, { method: 'get' });
@@ -12,9 +16,12 @@ export const pathToOpenapi = async (uri: string): Promise<OpenAPIV3.Document> =>
1216
originContent = await readFile(path.resolve(uri), 'utf8');
1317
}
1418

19+
let document: OpenAPIV3.Document;
1520
if (originContent.startsWith('{')) {
16-
return JSON.parse(originContent);
21+
document = JSON.parse(originContent);
1722
} else {
18-
return YAML.parse(originContent);
23+
document = YAML.parse(originContent);
1924
}
25+
26+
return onLoaded?.(document) || document;
2027
};

test/lib/path-to-openapi.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect, test, vitest } from 'vitest';
22
import { pathToOpenapi } from '../../src/lib/path-to-openapi';
33
import path from 'node:path';
44
import { readFileSync } from 'node:fs';
5+
import type { OpenAPIV3 } from 'openapi-types';
56

67
test('从本地获取json', async () => {
78
const result = await pathToOpenapi('./openapi/openapi.json');
@@ -32,6 +33,17 @@ test('从远程获取', async () => {
3233
globalThis.fetch = originalFetch;
3334
});
3435

36+
test('支持加载事件', async () => {
37+
let eventDocs: OpenAPIV3.Document | undefined;
38+
const result = await pathToOpenapi('./openapi/openapi.json', (docs) => {
39+
eventDocs = docs;
40+
});
41+
expect(result).toBe(eventDocs);
42+
expect(JSON.stringify(result)).toMatchFileSnapshot(
43+
path.resolve('openapi', 'openapi.json'),
44+
);
45+
});
46+
3547
test('解析失败则直接报错', async () => {
3648
await expect(() => pathToOpenapi('./cli.ts')).rejects.toThrowError();
3749
});

0 commit comments

Comments
 (0)