Skip to content

Commit 3f89463

Browse files
feat: support parse swagger.yml/openapi.yml
1 parent d5d289f commit 3f89463

File tree

8 files changed

+798
-11
lines changed

8 files changed

+798
-11
lines changed

.changeset/heavy-rivers-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': minor
3+
---
4+
5+
feat: support parse swagger.yml/openapi.yml

README-en_US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Generate TS interfaces, request client, request mock service, enum, type field l
1313
- support work with npx, CLI, Nodejs
1414
- support custom request function, Fetch、Axios、[UniApp-request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR client available
1515
- support filter generate result by tags
16-
- support JSON specification
16+
- support JSON/YAML specification
1717

1818
## Usage
1919

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- 支持通过 npx、CLI、Nodejs 的方式使用
1414
- 支持自定义请求工具函数, 支持 Fetch、Axios、[UniApp-Request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR 客户端
1515
- 支持通过 tags 过滤生成结果
16-
- 支持 JSON 定义文件
16+
- 支持 JSON/YAML 定义文件
1717

1818
## 使用
1919

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"cosmiconfig": "^9.0.0",
4141
"cosmiconfig-typescript-loader": "^5.0.0",
4242
"glob": "^10.4.2",
43+
"js-yaml": "^4.1.0",
4344
"lodash": "^4.17.21",
4445
"memoizee": "^0.4.17",
4546
"mockjs": "^1.1.0",
@@ -56,6 +57,7 @@
5657
"@changesets/cli": "^2.27.6",
5758
"@commitlint/cli": "^19.2.1",
5859
"@commitlint/config-conventional": "^19.2.2",
60+
"@types/js-yaml": "^4.0.9",
5961
"@types/lodash": "^4.17.5",
6062
"@types/memoizee": "^0.4.11",
6163
"@types/mockjs": "^1.0.10",

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import axios from 'axios';
22
import http from 'http';
33
import https from 'https';
4-
import { OpenAPI, OpenAPIV2 } from 'openapi-types';
4+
import * as yaml from 'js-yaml';
5+
import { isObject } from 'lodash';
6+
import { readFileSync } from 'node:fs';
7+
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
58
import converter from 'swagger2openapi';
69

710
import log from './log';
@@ -44,17 +47,23 @@ async function getSchema(schemaPath: string) {
4447
delete require.cache[schemaPath];
4548
}
4649

47-
const schema = (await require(schemaPath)) as OpenAPI.Document;
50+
let schema: string | OpenAPI.Document = '';
51+
52+
try {
53+
schema = (await require(schemaPath)) as OpenAPI.Document;
54+
} catch {
55+
try {
56+
schema = readFileSync(schemaPath, 'utf8');
57+
} catch (error) {
58+
console.error('Error reading schema file:', error);
59+
}
60+
}
4861

4962
return schema;
5063
}
5164

5265
function converterSwaggerToOpenApi(swagger: OpenAPI.Document) {
53-
if (!(swagger as OpenAPIV2.Document).swagger) {
54-
return swagger;
55-
}
56-
57-
return new Promise((resolve, reject) => {
66+
return new Promise<OpenAPIV3.Document>((resolve, reject) => {
5867
const convertOptions = {
5968
patch: true,
6069
warnOnly: true,
@@ -83,10 +92,46 @@ export const getOpenAPIConfig = async (schemaPath: string) => {
8392
const schema = await getSchema(schemaPath);
8493

8594
if (!schema) {
86-
return null;
95+
return;
8796
}
8897

89-
const openAPI = await converterSwaggerToOpenApi(schema);
98+
const openAPI = await parseSwaggerOrOpenapi(schema);
9099

91100
return openAPI;
92101
};
102+
103+
export async function parseSwaggerOrOpenapi(
104+
content: string | OpenAPI.Document
105+
) {
106+
let openapi = {} as OpenAPI.Document;
107+
108+
if (isObject(content)) {
109+
openapi = content;
110+
111+
// if is swagger2.0 json, covert swagger2.0 to openapi3.0
112+
if ((openapi as OpenAPIV2.Document).swagger) {
113+
openapi = await converterSwaggerToOpenApi(openapi);
114+
}
115+
} else {
116+
if (isJSONString(content)) {
117+
openapi = JSON.parse(content) as OpenAPI.Document;
118+
} else {
119+
openapi = yaml.load(content) as OpenAPI.Document;
120+
}
121+
122+
if ((openapi as OpenAPIV2.Document).swagger) {
123+
openapi = await converterSwaggerToOpenApi(openapi);
124+
}
125+
}
126+
127+
return openapi;
128+
}
129+
130+
function isJSONString(str: string) {
131+
try {
132+
JSON.parse(str);
133+
return true;
134+
} catch (error) {
135+
return false;
136+
}
137+
}

0 commit comments

Comments
 (0)