Skip to content

Commit 0b9705a

Browse files
authored
Merge pull request #176 from InhiblabCore/dev
feat: ✨ useRequest add Middleware
2 parents 064fe31 + 9b0f849 commit 0b9705a

32 files changed

+786
-121
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
"@types/node": "^17.0.21",
3030
"@types/qs": "^6.9.7",
3131
"@vitejs/plugin-vue": "^2.3.1",
32+
"@vue/compiler-core": "^3.2.37",
3233
"@vue-hooks-plus/vite-plugin-gen-temp": "^2.6.6",
3334
"@vue-hooks-plus/md-demo-plugins": "^1.0.0",
3435
"@vue-hooks-plus/vitepress": "^1.2.4",
35-
"@vue-hooks-plus/vitepress-demo-block": "^1.3.1",
36+
"@vue-hooks-plus/vitepress-demo-block": "workspace:^1.0.0",
3637
"@vue-hooks-plus/use-immer":"workspace:^1.0.0",
3738
"@vue-hooks-plus/use-worker":"workspace:^1.0.0",
3839
"@vue-hooks-plus/use-request":"workspace:^1.0.0",

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/',

packages/hooks/docs/.vitepress/theme/home/Home-EN.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import Icon from './Icon.vue'
55
// import Waves from '../Waves/index.vue'
66
import Waves from '../Wave.vue'
7+
8+
// @ts-ignore
9+
const mode = import.meta.env.MODE
710
</script>
811

912
<template>
@@ -18,7 +21,11 @@
1821
</h1>
1922
<p class="description"> </p>
2023
<p class="actions">
21-
<a class="get-started" href="/docs/hooks/en/guide/" style="position: relative; z-index: 9;">
24+
<a
25+
class="get-started"
26+
:href="mode === 'development' ? '/en/guide/' : '/docs/hooks/en/guide/'"
27+
style="position: relative; z-index: 9;"
28+
>
2229
Get Started
2330
<svg
2431
class="icon"

packages/hooks/docs/.vitepress/theme/home/Home.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// import Waves from '../Waves/index.vue'
77
import Waves from '../Wave.vue'
88
// import Badge from './Badge.vue'
9+
10+
// @ts-ignore
11+
const mode = import.meta.env.MODE
912
</script>
1013

1114
<template>
@@ -20,7 +23,7 @@
2023
</h1>
2124
<p class="description"> </p>
2225
<p class="actions" style="position: relative; z-index: 9;">
23-
<a class="get-started" href="/docs/hooks/guide/">
26+
<a class="get-started" :href="mode === 'development' ? '/guide/' : '/docs/hooks/guide/'">
2427
快速开始
2528
<svg
2629
class="icon"
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+
```

0 commit comments

Comments
 (0)