Skip to content

Commit

Permalink
feat: 新增文档加载事件
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Jul 20, 2024
1 parent c7b45e2 commit fea1c80
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ rpc模式的优点:
根据Tag生成不同的分组,以类似 **client.user.getUsers()** 这种方式调用。仅在 `classMode=rpc` 场景下生效。

如果没有提供tags,则默认合并到`default`分组

### onDocumentLoaded

类型:`(docs: Document) => Document | void`

加载完openapi文档后的事件,允许直接对文档进行修改
4 changes: 3 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ spinner.add({
spinner.add({
title: '获取openapi文档',
task: async (ctx) => {
ctx.docs = await Promise.all(ctx.configs.map((config) => pathToOpenapi(config.path)));
ctx.docs = await Promise.all(
ctx.configs.map((config) => pathToOpenapi(config.path, config.onDocumentLoaded)),
);
await sleep();
},
});
Expand Down
6 changes: 6 additions & 0 deletions src/define-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { OpenAPIV3 } from 'openapi-types';

export interface OpenapiClientConfig {
/**
* openapi本地或者远程文件,支持格式:`yaml | json`
Expand Down Expand Up @@ -35,6 +37,10 @@ export interface OpenapiClientConfig {
* 如果没有提供tags,则默认合并到`default`分组
*/
tagToGroup?: boolean;
/**
* 加载完openapi文档后的事件,允许直接对文档进行修改
*/
onDocumentLoaded?: (doc: OpenAPIV3.Document) => OpenAPIV3.Document | void;
}

export const defineConfig = (options: OpenapiClientConfig | OpenapiClientConfig[]) => {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/path-to-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { readFile } from 'fs/promises';
import path from 'node:path';
import type { OpenAPIV3 } from 'openapi-types';
import YAML from 'yaml';
import type { OpenapiClientConfig } from '../define-config';

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

let document: OpenAPIV3.Document;
if (originContent.startsWith('{')) {
return JSON.parse(originContent);
document = JSON.parse(originContent);
} else {
return YAML.parse(originContent);
document = YAML.parse(originContent);
}

return onLoaded?.(document) || document;
};
12 changes: 12 additions & 0 deletions test/lib/path-to-openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test, vitest } from 'vitest';
import { pathToOpenapi } from '../../src/lib/path-to-openapi';
import path from 'node:path';
import { readFileSync } from 'node:fs';
import type { OpenAPIV3 } from 'openapi-types';

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

test('支持加载事件', async () => {
let eventDocs: OpenAPIV3.Document | undefined;
const result = await pathToOpenapi('./openapi/openapi.json', (docs) => {
eventDocs = docs;
});
expect(result).toBe(eventDocs);
expect(JSON.stringify(result)).toMatchFileSnapshot(
path.resolve('openapi', 'openapi.json'),
);
});

test('解析失败则直接报错', async () => {
await expect(() => pathToOpenapi('./cli.ts')).rejects.toThrowError();
});

0 comments on commit fea1c80

Please sign in to comment.