diff --git a/README.md b/README.md index c6b28d8..17e2dcb 100644 --- a/README.md +++ b/README.md @@ -158,3 +158,9 @@ rpc模式的优点: 根据Tag生成不同的分组,以类似 **client.user.getUsers()** 这种方式调用。仅在 `classMode=rpc` 场景下生效。 如果没有提供tags,则默认合并到`default`分组 + +### onDocumentLoaded + +类型:`(docs: Document) => Document | void` + +加载完openapi文档后的事件,允许直接对文档进行修改 diff --git a/src/bin.ts b/src/bin.ts index 5e0cf00..ddaa0f7 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -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(); }, }); diff --git a/src/define-config.ts b/src/define-config.ts index 62350e5..ddea1d5 100644 --- a/src/define-config.ts +++ b/src/define-config.ts @@ -1,3 +1,5 @@ +import type { OpenAPIV3 } from 'openapi-types'; + export interface OpenapiClientConfig { /** * openapi本地或者远程文件,支持格式:`yaml | json` @@ -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[]) => { diff --git a/src/lib/path-to-openapi.ts b/src/lib/path-to-openapi.ts index 82661de..d50afed 100644 --- a/src/lib/path-to-openapi.ts +++ b/src/lib/path-to-openapi.ts @@ -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 => { +export const pathToOpenapi = async ( + uri: string, + onLoaded?: OpenapiClientConfig['onDocumentLoaded'], +): Promise => { let originContent: string; if (uri.startsWith('http:') || uri.startsWith('https:')) { const response = await fetch(uri, { method: 'get' }); @@ -12,9 +16,12 @@ export const pathToOpenapi = async (uri: string): Promise => 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; }; diff --git a/test/lib/path-to-openapi.test.ts b/test/lib/path-to-openapi.test.ts index ec4cd1d..39ccd03 100644 --- a/test/lib/path-to-openapi.test.ts +++ b/test/lib/path-to-openapi.test.ts @@ -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'); @@ -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(); });