Skip to content

Commit 9b0f849

Browse files
committed
feat: useRequest middleware
1 parent 1089e62 commit 9b0f849

File tree

8 files changed

+187
-4
lines changed

8 files changed

+187
-4
lines changed

packages/hooks/docs/.vitepress/router.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ const useRequestRouter = [
154154
text: '滚动加载 & 分页加载',
155155
link: '/useRequest/scroll/',
156156
},
157+
{
158+
text: '中间件·Beta',
159+
link: '/useRequest/middleware/',
160+
},
157161
{
158162
text: '插件设计',
159163
link: '/useRequest/plugin/',
@@ -263,6 +267,10 @@ const useRequestRouterEN = [
263267
text: 'Scroll',
264268
link: '/en/useRequest/scroll/',
265269
},
270+
{
271+
text: 'Middleware·Beta',
272+
link: '/en/useRequest/middleware/',
273+
},
266274
{
267275
text: 'Plugins Design',
268276
link: '/en/useRequest/plugin/',
454 KB
Loading

packages/hooks/src/useRequest/docs/devtools/index.en-US.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ source:
66
show: false
77
---
88

9-
# DevTools `1.7.7-alpha.1`
9+
# DevTools
1010

1111
Wave your hands in the air and shout hooray because useRequest comes with dedicated DevTools! 😍
1212

@@ -16,7 +16,7 @@ The only thing you need to do is to install official [Vue Devtools](https://devt
1616

1717
useRequest will seemingly integrate with official devtools, adding custom inspector and timeline events. Devtools would be treeshaken from production bundles by default.
1818

19-
Currently in the `testing phase`, you can download `1.7.7-aLpha.1` and later versions to use.
19+
Currently in the `testing phase`, you can download `1.7.7` and later versions to use.
2020

2121
## Import the Devtools
2222

@@ -37,3 +37,5 @@ const { data, loading } = useRequest(() => getUsername({ desc: 'good' }), { devK
3737
Use `devKey` to create a unique identifier and enable it.
3838

3939
You're done! Open the browser Vue plugin to use it 🍺
40+
41+
![Alt](/plugin.png 'plugin devtool image')

packages/hooks/src/useRequest/docs/devtools/index.zh-CN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ source:
66
show: false
77
---
88

9-
# 开发者工具 `1.7.7-alpha.1`
9+
# 开发者工具
1010

1111
`useRequest` 提供了一个 `devKey` 参数,并且在 `dev` 模式下会开启 `devtools`
1212

@@ -18,7 +18,7 @@ source:
1818

1919
useRequest devtools 会与官方的开发工具整合,添加自定义的检查器和时间轴事件。Devtools would be treeshaken from production bundles by default.
2020

21-
目前处于`测试阶段`, 你可以下载 `1.7.7-alpha.1`及更高版本使用。
21+
目前处于`测试阶段`, 你可以下载 `1.7.7`及更高版本使用。
2222

2323
## 导入 Devtools
2424

@@ -39,3 +39,5 @@ const { data, loading } = useRequest(() => getUsername({ desc: 'good' }), { devK
3939
```
4040

4141
大功告成!打开浏览器 Vue 插件进行使用吧 🍺
42+
43+
![Alt](/plugin.png 'plugin devtool image')
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<template>
2+
<div>ready: {{ ready }}</div>
3+
<div>log: {{ logRef }}</div>
4+
<vhp-button style="margin-top: 16px;" @click="() => toggle()">changeReady</vhp-button>
5+
<div style="margin-top: 16px;">{{ data }}</div>
6+
</template>
7+
8+
<script lang="ts" setup>
9+
import { ref } from 'vue'
10+
import {
11+
RequestHook,
12+
UseRequestOptions,
13+
UseRequestPlugin,
14+
UseRequestService,
15+
} from '../../../types'
16+
import { useRequest, useToggle } from 'vue-hooks-plus'
17+
18+
const logRef = ref('')
19+
20+
function logger<TData, TParams extends any[]>(useRequestNext: RequestHook<TData, TParams>) {
21+
return (
22+
service: UseRequestService<TData, TParams>,
23+
options: UseRequestOptions<TData, TParams, any>,
24+
plugins: UseRequestPlugin<TData, TParams, any>[],
25+
) => {
26+
console.log('enter a')
27+
28+
const extendedService = (...args: TParams) => {
29+
console.log('request a')
30+
const data = service(...args)
31+
data.then(res => {
32+
logRef.value += `Response: ${JSON.stringify(res)}\n`
33+
})
34+
return data
35+
}
36+
const next = useRequestNext(extendedService, options, plugins)
37+
38+
console.log('exit a')
39+
40+
return next
41+
}
42+
}
43+
44+
function logger1<TData, TParams extends any[]>(useRequestNext: RequestHook<TData, TParams>) {
45+
return (
46+
service: UseRequestService<TData, TParams>,
47+
options: UseRequestOptions<TData, TParams, any>,
48+
plugins: UseRequestPlugin<TData, TParams, any>[],
49+
) => {
50+
console.log('enter b')
51+
52+
const extendedService = (...args: TParams) => {
53+
console.log('request b')
54+
return service(...args)
55+
}
56+
const next = useRequestNext(extendedService, options, plugins)
57+
58+
console.log('exit b')
59+
60+
return next
61+
}
62+
}
63+
64+
function getUsername(): Promise<string> {
65+
return new Promise(resolve => {
66+
setTimeout(() => {
67+
console.log('return')
68+
resolve(String(Date.now()))
69+
}, 1000)
70+
})
71+
}
72+
const [ready, { toggle }] = useToggle(false)
73+
74+
const { data } = useRequest(() => getUsername(), {
75+
ready,
76+
use: [logger, logger1],
77+
})
78+
</script>
79+
80+
<style scoped lang="less"></style>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useRequest/middleware/
5+
source:
6+
path: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/hooks/src/useRequest/useRequest.ts
7+
demoPath: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/hooks/src/useRequest/docs/middleware/demo/demo.vue
8+
---
9+
10+
# Middleware Beta
11+
12+
Middleware is a new beta feature, please upgrade to the latest version for use. It allows you to execute code before and after the useRequest hook.
13+
14+
## Usage
15+
16+
The middleware receives a useRequest hook and can execute logic before and after running it. If there are multiple middleware, each middleware is packaged with the next middleware. The last middleware in the list will receive the original hook useRequest
17+
18+
## Principle
19+
20+
```
21+
enter a
22+
enter b
23+
enter c
24+
useRequest()
25+
exit c
26+
exit b
27+
exit a
28+
29+
```
30+
31+
## A simple request log retention middleware
32+
33+
<demo src="./demo/demo.vue"
34+
language="vue"
35+
title=""
36+
desc=""> </demo>
37+
38+
## API
39+
40+
```typescript
41+
const { data } = useRequest(() => getUsername(), {
42+
ready,
43+
use: [middleware],
44+
})
45+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useRequest/middleware/
5+
source:
6+
path: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/hooks/src/useRequest/useRequest.ts
7+
demoPath: https://github.com/InhiblabCore/vue-hooks-plus/blob/master/packages/hooks/src/useRequest/docs/middleware/demo/demo.vue
8+
---
9+
10+
# 中间件 Beta
11+
12+
中间件是新增的一个 beta 功能,请升级最新版使用。它让你能够在 useRequest hook 之前和之后执行代码。
13+
14+
## 用法
15+
16+
中间件接收 useRequest hook,可以在运行它之前和之后执行逻辑。如果有多个中间件,则每个中间件包装下一个中间件。列表中的最后一个中间件将接收原始的 hook useRequest。
17+
18+
## 原理
19+
20+
```
21+
enter a
22+
enter b
23+
enter c
24+
useRequest()
25+
exit c
26+
exit b
27+
exit a
28+
29+
```
30+
31+
## 一个简单的请求日志保留中间件
32+
33+
<demo src="./demo/demo.vue"
34+
language="vue"
35+
title=""
36+
desc=""> </demo>
37+
38+
## API
39+
40+
```typescript
41+
const { data } = useRequest(() => getUsername(), {
42+
ready,
43+
use: [middleware],
44+
})
45+
```

packages/hooks/src/useRequest/docs/pluginDoc/demo/demo.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
}
2626
> = (fetchInstance, { formatter }) => {
2727
return {
28+
name: 'formatterPlugin',
2829
onSuccess: () => {
2930
fetchInstance.setFetchState(formatter?.(fetchInstance.state.data), 'data')
3031
},

0 commit comments

Comments
 (0)