Skip to content

Commit 9216d2b

Browse files
Merge pull request #144 from openapi-ui/main
docs: enrich readme
2 parents 6cfdf1e + f419429 commit 9216d2b

File tree

5 files changed

+173
-41
lines changed

5 files changed

+173
-41
lines changed

.changeset/proud-melons-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
docs: enrich readme files

.changeset/sharp-numbers-film.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
docs: update readme

README-en_US.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
English | <a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README.md">简体中文</a>
66

7-
Generate TS interfaces, request client, request mock service, enum, type field label, JSON Schemas based on [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) specification
7+
based on [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) specification Generate
88

9-
文档:[use docs](https://github.com/openapi-ui/openapi-ts-request/issues/100)
9+
- TS interfaces
10+
- request client
11+
- request mock service
12+
- enum and enum translation
13+
- type field label
14+
- JSON Schemas
15+
16+
docs:[use docs](https://github.com/openapi-ui/openapi-ts-request/issues/100)
1017

1118
## Features
1219

@@ -38,6 +45,8 @@ create `openapi-ts-request.config.ts` file in the project root directory
3845
import type { GenerateServiceProps } from 'openapi-ts-request';
3946

4047
export default {
48+
// schemaPath: './openapi.json', // local openapi file
49+
// serversPath: './src/apis', // interface storage path
4150
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
4251
} as GenerateServiceProps;
4352
```
@@ -61,12 +70,47 @@ export default [
6170

6271
add the command in `script` of `package.json`: `"openapi": "openapi-ts",`
6372

64-
generate result:
73+
run:
6574

6675
```bash
6776
npm run openapi
6877
```
6978

79+
run:
80+
81+
```bash
82+
src/apis/index.ts #interface entry file
83+
src/apis/types.ts #type definition file
84+
src/apis/app #app interface
85+
```
86+
87+
```typescript
88+
// src/apis/pet.ts
89+
90+
/* eslint-disable */
91+
// @ts-ignore
92+
import request from 'axios';
93+
94+
import * as API from './types';
95+
96+
/** Update an existing pet PUT /pet */
97+
export async function updatePet(
98+
body: API.Pet,
99+
options?: { [key: string]: unknown }
100+
) {
101+
return request<unknown>(`/pet`, {
102+
method: 'PUT',
103+
headers: {
104+
'Content-Type': 'application/json',
105+
},
106+
data: body,
107+
...(options || {}),
108+
});
109+
}
110+
111+
// ... more interfaces
112+
```
113+
70114
### JS
71115

72116
create a new `openapi-ts-request.config.js` file in any directory `xxx/xxx`
@@ -82,7 +126,7 @@ generateService({
82126

83127
add the command in `script` of `package.json`: `"openapi": "node xxx/xxx/openapi-ts-request.config.js"`
84128

85-
generate result:
129+
run:
86130

87131
```bash
88132
npm run openapi
@@ -103,7 +147,7 @@ generateService({
103147

104148
add the command in `script` of `package.json`: `"openapi": "ts-node xxx/xxx/openapi-ts-request.config.ts",`
105149

106-
generate result:
150+
run:
107151

108152
```bash
109153
npm run openapi
@@ -160,7 +204,7 @@ $ openapi --help
160204
-h, --help display help for command
161205
```
162206

163-
generate result:
207+
run:
164208

165209
```bash
166210
openapi -i ./spec.json -o ./apis
@@ -171,7 +215,7 @@ openapi -i ./spec.json -o ./apis
171215
| props | required | type | default | remark |
172216
| --- | --- | --- | --- | --- |
173217
| schemaPath | yes | string | - | Swagger2/OpenAPI3 URL |
174-
| serversPath | no | string | './src/apis' | the folder path for the generated results |
218+
| serversPath | no | string | './src/apis' | the folder path for the run results |
175219
| requestLibPath | no | string | 'axios' | custom request lib path, for example: '@/request', 'node-fetch' |
176220
| enableLogging | no | boolean | false | open the log |
177221
| priorityRule | no | string | 'include' | priority rule, include/exclude/both |

README.md

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
<a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README-en_US.md">English</a> | 简体中文
66

7-
根据 [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 TS 类型, 客户端请求函数, 模拟请求响应服务, 枚举, 类型字段翻译, JSON Schemas
7+
根据 [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成:
8+
9+
- TS 类型
10+
- 客户端请求函数
11+
- 模拟请求响应服务
12+
- 枚举和枚举翻译
13+
- 类型字段翻译
14+
- JSON Schemas
815

916
文档:[使用手册](https://github.com/openapi-ui/openapi-ts-request/issues/100)
1017

@@ -38,6 +45,8 @@ pnpm i openapi-ts-request -D
3845
import type { GenerateServiceProps } from 'openapi-ts-request';
3946

4047
export default {
48+
// schemaPath: './openapi.json', // 本地openapi文件
49+
// serversPath: './src/apis', // 接口存放路径
4150
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
4251
} as GenerateServiceProps;
4352
```
@@ -61,12 +70,47 @@ export default [
6170

6271
`package.json``script` 中添加命令: `"openapi": "openapi-ts",`
6372

64-
生成结果
73+
运行
6574

6675
```bash
6776
npm run openapi
6877
```
6978

79+
生成的接口:
80+
81+
```bash
82+
src/apis/index.ts #接口入口文件
83+
src/apis/types.ts #类型定义文件
84+
src/apis/pet.ts #接口文件
85+
```
86+
87+
```typescript
88+
// src/apis/pet.ts
89+
90+
/* eslint-disable */
91+
// @ts-ignore
92+
import request from 'axios';
93+
94+
import * as API from './types';
95+
96+
/** Update an existing pet PUT /pet */
97+
export async function updatePet(
98+
body: API.Pet,
99+
options?: { [key: string]: unknown }
100+
) {
101+
return request<unknown>(`/pet`, {
102+
method: 'PUT',
103+
headers: {
104+
'Content-Type': 'application/json',
105+
},
106+
data: body,
107+
...(options || {}),
108+
});
109+
}
110+
111+
// ... 其他接口
112+
```
113+
70114
### JS
71115

72116
任意目录 `xxx/xxx` 新建 `openapi-ts-request.config.js`
@@ -82,7 +126,7 @@ generateService({
82126

83127
`package.json``script` 中添加命令: `"openapi": "node xxx/xxx/openapi-ts-request.config.js"`
84128

85-
生成结果
129+
运行
86130

87131
```bash
88132
npm run openapi
@@ -103,7 +147,7 @@ generateService({
103147

104148
`package.json``script` 中添加命令: `"openapi": "ts-node xxx/xxx/openapi-ts-request.config.ts",`
105149

106-
生成结果
150+
运行
107151

108152
```bash
109153
npm run openapi
@@ -160,7 +204,7 @@ $ openapi --help
160204
-h, --help display help for command
161205
```
162206

163-
生成结果
207+
运行
164208

165209
```bash
166210
openapi --i ./spec.json --o ./apis
@@ -171,7 +215,7 @@ openapi --i ./spec.json --o ./apis
171215
| 属性 | 必填 | 类型 | 默认值 | 说明 |
172216
| --- | --- | --- | --- | --- |
173217
| schemaPath || string | - | Swagger2/OpenAPI3 地址 |
174-
| serversPath || string | './src/apis' | 生成结果的文件夹路径 |
218+
| serversPath || string | './src/apis' | 运行结果文件夹路径 |
175219
| requestLibPath || string | 'axios' | 自定义请求方法路径,例如:'@/request'、'node-fetch' |
176220
| enableLogging || boolean | false | 是否开启日志 |
177221
| priorityRule || string | 'include' | 模式规则,可选include/exclude/both |

0 commit comments

Comments
 (0)