Skip to content

Commit affd557

Browse files
committed
docs: 增加文档
1 parent afc1552 commit affd557

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# foca-openapi
2+
3+
使用openapi文档生成请求服务
4+
5+
[![npm peer typescript version](https://img.shields.io/npm/dependency-version/foca-openapi/peer/typescript?logo=typescript)](https://github.com/microsoft/TypeScript)
6+
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/foca-js/foca-openapi/test.yml?branch=master&label=test&logo=vitest)](https://github.com/foca-js/foca-openapi/actions)
7+
[![Codecov](https://img.shields.io/codecov/c/github/foca-js/foca-openapi?logo=codecov)](https://codecov.io/gh/foca-js/foca-openapi)
8+
[![npm](https://img.shields.io/npm/v/foca-openapi?logo=npm)](https://www.npmjs.com/package/foca-openapi)
9+
[![npm](https://img.shields.io/npm/dt/foca-openapi?logo=codeforces)](https://www.npmjs.com/package/foca-openapi)
10+
[![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/foca-openapi?label=bundle+size&cacheSeconds=3600&logo=esbuild)](https://bundlephobia.com/package/foca-openapi@latest)
11+
[![License](https://img.shields.io/github/license/foca-js/foca-openapi?logo=open-source-initiative)](https://github.com/foca-js/foca-openapi/blob/master/LICENSE)
12+
13+
# 安装
14+
15+
```bash
16+
pnpm add foca-openapi
17+
```
18+
19+
# 使用
20+
21+
## 1. 创建配置文件
22+
23+
在项目根目录下创建一个名为 `openapi.config.ts` 的文件
24+
25+
```typescript
26+
import { defineConfig } from 'foca-openapi';
27+
28+
export default defineConfig({
29+
// 可以是本地路径,也可以是远程地址。格式为json或者yaml
30+
path: 'http://domain.com/openapi.json',
31+
// 只包含指定的路由,支持字符串或者正则表达式
32+
// includeUriPrefix: ['/admin'],
33+
// 只包含指定的标签
34+
// includeTag: ['admin', 'public'],
35+
});
36+
```
37+
38+
## 2. 执行指令
39+
40+
指令的作用是把openapi文档转换为前端服务,代码会自动合并到库文件中
41+
42+
```bash
43+
npx foca-openapi
44+
```
45+
46+
## 3. 创建服务
47+
48+
使用合适的请求适配器创建好服务后,就可以导出给各个模块使用了
49+
50+
```typescript
51+
// ./src/services/http.ts
52+
import { OpenapiClient } from 'foca-openapi';
53+
import { fetchAdapter } from 'foca-openapi/adapters/fetch';
54+
55+
const adapter = fetchAdapter({ baseURL: 'http://api.com' });
56+
export const client = new OpenapiClient(adapter);
57+
```
58+
59+
# 适配器
60+
61+
引入形式
62+
`import { xxAdapter } from 'foca-openapi/adapters/xx';`
63+
64+
当前已内置多个适配器,可满足大部分需求。如果无法满足项目需求则可以自行创建适配器(或者提issue)
65+
66+
- axios
67+
- fetch
68+
- taro
69+
70+
# 多配置场景
71+
72+
如果一个项目需要融合多个openapi文档,则可以用数组的形式配置
73+
74+
```typescript
75+
import { defineConfig } from 'foca-openapi';
76+
77+
export default defineConfig([
78+
{
79+
path: 'http://domain.com/openapi_1.json',
80+
// 项目名称,必须是唯一的值
81+
projectName: 'foo',
82+
},
83+
{
84+
path: 'http://domain.com/openapi_2.json',
85+
// 项目名称,必须是唯一的值
86+
projectName: 'bar',
87+
},
88+
]);
89+
```
90+
91+
执行指令后就会生成两个类
92+
93+
```typescript
94+
import { OpenapiClientFoo, OpenapiClientBar } from 'foca-openapi';
95+
96+
export const fooClient = new OpenapiClientFoo(adapter1);
97+
export const barClient = new OpenapiClientBar(adapter2);
98+
```

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262
"engines": {
6363
"node": "^18.19 || ^20.6 || >=22"
6464
},
65+
"peerDependencies": {
66+
"typescript": ">=5.4"
67+
},
68+
"peerDependenciesMeta": {
69+
"typescript": {
70+
"optional": true
71+
}
72+
},
6573
"dependencies": {
6674
"listr2": "^8.2.3",
6775
"lodash-es": "^4.17.21",

0 commit comments

Comments
 (0)