Skip to content

Commit 38c29bc

Browse files
authored
feat: close #20 #21, upgrade deps and support runtimeOptions (#22)
* feat: close #20 #21, upgrade deps and support runtimeOptions
1 parent c426bfd commit 38c29bc

31 files changed

+298
-180
lines changed

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ fooApi
8686
```js
8787
// 甚至可以更进一步和 tua-storage 配合使用
8888
import TuaStorage from 'tua-storage'
89+
import { getSyncFnMapByApis } from 'tua-api'
8990

90-
const storage = new TuaStorage({ ... })
91+
// 本地写好的各种接口配置
92+
import * as apis from '@/apis'
93+
94+
const tuaStorage = new TuaStorage({
95+
syncFnMap: getSyncFnMapByApis(apis),
96+
})
9197

9298
const fetchParam = {
9399
key: fooApi.bar.key,
@@ -103,7 +109,7 @@ const fetchParam = {
103109
// ...
104110
}
105111

106-
storage
112+
tuaStorage
107113
.load(fetchParam)
108114
.then(console.log)
109115
.catch(console.error)
@@ -155,7 +161,7 @@ export default {
155161
}
156162
```
157163

158-
[更多配置请点击这里查看](https://tuateam.github.io/tua-api/config/detail.html)
164+
[更多配置请点击这里查看](https://tuateam.github.io/tua-api/config/common.html)
159165

160166
### 配置导出
161167
最后来看一下 `apis/index.js` 该怎么写:
@@ -180,7 +186,8 @@ tuaApi
180186
// 链式调用
181187
.use(...)
182188

183-
export const somethingApi = tuaApi.getApi(require('./something').default)
189+
export const fakeGet = tuaApi.getApi(require('./fake-get').default)
190+
export const fakePost = tuaApi.getApi(require('./fake-post').default)
184191
```
185192

186193
小程序端建议使用 [@tua-mp/cli](https://tuateam.github.io/tua-mp/tua-mp-cli/) 一键生成 api。
@@ -190,15 +197,16 @@ $ tuamp add api <api-name>
190197
```
191198

192199
### 配置的构成
193-
`tua-api` 中配置分为三种
200+
`tua-api` 中配置分为四种
194201

195-
* 默认配置(调用 `new TuaApi({ ... })` 时传递的)
196-
* 公共配置(和 `pathList` 同级的配置)
197-
* 自身配置(`pathList` 数组中的对象上的配置)
202+
* [默认配置(调用 `new TuaApi({ ... })` 时传递的)](https://tuateam.github.io/tua-api/config/default.html)
203+
* [公共配置(和 `pathList` 同级的配置)](https://tuateam.github.io/tua-api/config/common.html)
204+
* [自身配置(`pathList` 数组中的对象上的配置)](https://tuateam.github.io/tua-api/config/self.html)
205+
* [运行配置(在实际调用接口时传递的配置)](https://tuateam.github.io/tua-api/config/runtime.html)
198206

199207
其中优先级自然是:
200208

201-
`自身配置 > 公共配置 > 默认配置`
209+
`默认配置 < 公共配置 < 自身配置 < 运行配置`
202210

203211
<p align="center">
204212
<a href="https://tuateam.github.io/tua-api/config/">👉更多配置点击这里👈</a>

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
],
1515
],
1616
},
17-
prod: {
17+
production: {
1818
presets: [
1919
[
2020
'@babel/preset-env',

commitlint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
// https://www.npmjs.com/package/@commitlint/config-conventional
33
extends: ['@commitlint/config-conventional'],
4-
"rules": {
4+
rules: {
55
},
66
}

docs/.vuepress/config.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1+
const { name } = require('../../package.json')
2+
3+
const description = '🏗 一款可配置的通用 api 请求函数生成工具'
4+
15
module.exports = {
2-
base: '/tua-api/',
6+
base: '/' + name + '/',
37
locales: {
4-
'/': {
5-
title: 'tua-api',
6-
description: '🏗 一款可配置的通用 api 请求函数生成工具'
7-
}
8+
'/': { title: name, description },
89
},
910
head: [
1011
['link', { rel: 'icon', href: `/logo.png` }],
1112
],
13+
evergreen: true,
1214
serviceWorker: true,
1315
themeConfig: {
1416
repo: 'tuateam/tua-api',
1517
docsDir: 'docs',
1618
editLinks: true,
1719
lastUpdated: '上次更新',
20+
sidebarDepth: 2,
1821
editLinkText: '在 GitHub 上编辑此页',
19-
lastUpdated: '上次更新',
2022
nav: [
2123
{
22-
text: '快速上手',
23-
link: '/quick-start/',
24+
text: '🌱指南',
25+
link: '/guide/',
2426
},
2527
{
26-
text: '配置',
28+
text: '⚙️配置',
2729
link: '/config/',
2830
},
2931
{
30-
text: '生态系统',
32+
text: '🔥生态系统',
3133
items: [
32-
{ text: '本地存储', link: 'https://tuateam.github.io/tua-storage/' },
33-
{ text: '小程序框架', link: 'https://tuateam.github.io/tua-mp/' },
34+
{ text: '📦本地存储', link: 'https://tuateam.github.io/tua-storage/' },
35+
{ text: '🖖小程序框架', link: 'https://tuateam.github.io/tua-mp/' },
3436
],
3537
},
3638
],
3739
sidebar: {
38-
'/quick-start/': [
40+
'/guide/': [
3941
{
40-
title: '快速上手',
42+
title: '🌱指南',
4143
collapsable: false,
4244
children: [
4345
'installation',
@@ -51,21 +53,22 @@ module.exports = {
5153
],
5254
'/config/': [
5355
{
54-
title: '配置',
56+
title: '⚙️配置',
5557
collapsable: false,
5658
children: [
5759
'',
5860
'default',
59-
'detail',
60-
'own',
61+
'common',
62+
'self',
63+
'runtime',
6164
],
6265
},
6366
],
6467
},
6568
serviceWorker: {
6669
updatePopup: {
67-
message: 'New content is available.',
68-
buttonText: 'Refresh',
70+
message: 'New content is available.',
71+
buttonText: 'Refresh',
6972
},
7073
},
7174
},

docs/README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
home: true
33
actionText: 快速上手 →
4-
actionLink: /quick-start/
4+
actionLink: /guide/
55
features:
66
- title: 支持多端
77
details: 支持 web 端、Node 端和小程序端
@@ -16,17 +16,24 @@ features:
1616
footer: MIT Licensed | Copyright © 2018-present StEve Young
1717
---
1818

19-
<h4 align="center">
20-
让我们优雅地调用 api~
21-
</h4>
19+
<h4 align="center">让我们优雅地调用 api~</h4>
2220

2321
<p align="center">
24-
<a href="https://github.com/feross/standard"><img :src="$withBase('/standard.svg')" alt="Standard - JavaScript Style"></a>
22+
<a href="https://github.com/feross/standard" target="_blank">
23+
<img :src="$withBase('/standard.svg')" alt="Standard - JavaScript Style">
24+
</a>
2525
</p>
2626

2727
<p align="center">
28-
<a href="https://circleci.com/gh/tuateam/tua-api/tree/master"><img src="https://img.shields.io/circleci/project/github/tuateam/tua-api/master.svg" alt="Build Status"></a>
29-
<a href="https://codecov.io/github/tuateam/tua-api?branch=master"><img src="https://img.shields.io/codecov/c/github/tuateam/tua-api/master.svg" alt="Coverage Status"></a>
30-
<a href="https://www.npmjs.com/package/tua-api"><img src="https://img.shields.io/npm/v/tua-api.svg" alt="Version"></a>
31-
<a href="https://www.npmjs.com/package/tua-api"><img src="https://img.shields.io/npm/l/tua-api.svg" alt="License"></a>
28+
<a href="https://circleci.com/gh/tuateam/tua-api/tree/master" target="_blank">
29+
<img src="https://img.shields.io/circleci/project/github/tuateam/tua-api/master.svg" alt="Build Status">
30+
</a>
31+
<a href="https://codecov.io/github/tuateam/tua-api?branch=master" target="_blank">
32+
<img src="https://img.shields.io/codecov/c/github/tuateam/tua-api/master.svg" alt="Coverage Status">
33+
</a>
34+
<a href="https://www.npmjs.com/package/tua-api" target="_blank">
35+
<img src="https://badgen.net/npm/dm/tua-api" alt="Downloads per month">
36+
<img src="https://img.shields.io/npm/v/tua-api.svg" alt="Version">
37+
<img src="https://img.shields.io/npm/l/tua-api.svg" alt="License">
38+
</a>
3239
</p>

docs/config/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# 配置说明
2-
`tua-api` 中配置分为三种
2+
`tua-api` 中配置分为四种
33

4-
* 默认配置(调用 `new TuaApi({ ... })` 时传递的)
5-
* 公共配置(和 `pathList` 同级的配置)
6-
* 自身配置(`pathList` 数组中的对象上的配置)
4+
* [默认配置(调用 `new TuaApi({ ... })` 时传递的)](./default.md)
5+
* [公共配置(和 `pathList` 同级的配置)](./common.md)
6+
* [自身配置(`pathList` 数组中的对象上的配置)](./self.md)
7+
* [运行配置(在实际调用接口时传递的配置)](./runtime.md)
78

89
其中优先级自然是:
910

10-
`默认配置 < 公共配置 < 自身配置`
11+
`默认配置 < 公共配置 < 自身配置 < 运行配置`

docs/config/detail.md renamed to docs/config/common.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 详细配置
1+
# 公共配置
22
详细地址指的是填写在 `src/apis/foobar.js` 中的一级配置。这部分的配置优先级比默认配置高,但低于各个接口的自身配置。
33

44
## type 请求类型
@@ -37,7 +37,7 @@ export default {
3737
}
3838
```
3939

40-
详情参阅 [快速上手 - mock 章节](../quick-start/mock.md)
40+
详情参阅 [mock 章节](../guide/mock.md)
4141

4242
## prefix 接口中间地址
4343
建议与文件同名,方便维护。
@@ -93,7 +93,7 @@ export default {
9393
}
9494
```
9595

96-
详情参阅:[中间件进阶](../quick-start/middleware.md)
96+
详情参阅:[中间件进阶](../guide/middleware.md)
9797

9898
## beforeFn 发起请求前钩子函数
9999
在请求发起前执行的函数(例如小程序可以通过返回 `header` 传递 `cookie`),因为是通过 `beforeFn().then(...)` 调用,所以注意要返回 Promise。

docs/config/runtime.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 运行配置
2+
运行配置指的是在接口实际调用时通过第二个参数传递的配置。这部分的配置优先级最高。
3+
4+
以下接口以导出为 `exampleApi` 为例。
5+
6+
```js
7+
exampleApi.foo(
8+
{ ... }, // 第一个参数传接口参数
9+
{ ... } // 第二个参数传接口配置
10+
)
11+
```
12+
13+
## callbackName 回调函数名称
14+
在通过 jsonp 发起请求时,为了使用缓存一般需要添加 callbackName,但是注意重复请求时会报错。
15+
16+
```js
17+
exampleApi.foo(
18+
{ ... },
19+
{ callbackName: `foo` }
20+
)
21+
```
22+
23+
## 其他参数
24+
公共配置一节中的所有参数(除了 `pathList` 外),以及自身配置一节中的所有参数均有效,且优先级最高。
25+
26+
* 详情参阅[公共配置](./common.md)
27+
* 详情参阅[自身配置](./self.md)

docs/config/own.md renamed to docs/config/self.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 自身配置
2-
自身配置指的是填写在 `pathList` 中的配置。这部分的配置优先级最高
2+
自身配置指的是填写在 `pathList` 中的配置。这部分的配置优先级比公共配置高,但低于各个接口的运行配置
33

44
以下接口以导出为 `exampleApi` 为例。
55

@@ -67,6 +67,6 @@ export default {
6767
有时某个接口正好不需要上一级中 `commonParams` 的参数。那么可以传递 `null` 覆盖上一级中的 `commonParams`
6868

6969
## 其他参数
70-
上一节中的所有参数(除了 `pathList` 外)均有效,且优先级最高
70+
上一节中的所有参数(除了 `pathList` 外)均有效。
7171

72-
详情参阅上一节 [详细配置](./detail.md)
72+
详情参阅上一节 [公共配置](./common.md)

docs/quick-start/README.md renamed to docs/guide/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const fetchParam = {
5353
// ...
5454
}
5555

56-
storage
56+
tuaStorage
5757
.load(fetchParam)
5858
.then(console.log)
5959
.catch(console.error)
@@ -62,15 +62,15 @@ storage
6262
## 怎么写 `api` 配置?
6363
拿以下 api 地址举例:
6464

65-
* https://example-base.com/foo/bar/something/create
66-
* https://example-base.com/foo/bar/something/modify
67-
* https://example-base.com/foo/bar/something/delete
65+
* `https://example-base.com/foo/bar/something/create`
66+
* `https://example-base.com/foo/bar/something/modify`
67+
* `https://example-base.com/foo/bar/something/delete`
6868

6969
### 地址结构划分
7070
以上地址,一般将其分为`3`部分:
7171

72-
* host: `https://example-base.com/`
73-
* prefix: `foo/bar/something`
72+
* host: `'https://example-base.com/'`
73+
* prefix: `'foo/bar/something'`
7474
* pathList: `[ 'create', 'modify', 'delete' ]`
7575

7676
### 文件结构
@@ -91,7 +91,7 @@ storage
9191

9292
export default {
9393
// 请求的公用服务器地址
94-
host: 'https://example-base.com//',
94+
host: 'https://example-base.com/',
9595

9696
// 请求的中间路径
9797
prefix: 'foo/bar/something',
@@ -105,7 +105,7 @@ export default {
105105
}
106106
```
107107

108-
[更多配置请点击这里查看](../config/detail.md)
108+
[更多配置请点击这里查看](../config/common.md)
109109

110110
### 配置导出
111111
最后来看一下 `apis/index.js` 该怎么写:
@@ -130,7 +130,8 @@ tuaApi
130130
// 链式调用
131131
.use(...)
132132

133-
export const somethingApi = tuaApi.getApi(require('./something').default)
133+
export const fakeGet = tuaApi.getApi(require('./fake-get').default)
134+
export const fakePost = tuaApi.getApi(require('./fake-post').default)
134135
```
135136

136137
::: tip

0 commit comments

Comments
 (0)