Skip to content

Commit b88c906

Browse files
authored
feat: ✨ use-request-plugins lib
feat: use-request-plugins lib
2 parents 28a1358 + 74e8ac0 commit b88c906

File tree

20 files changed

+525
-3
lines changed

20 files changed

+525
-3
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ jobs:
2929
run: |
3030
cd packages/use-worker
3131
pnpm build
32+
- name: Build use-request-image
33+
run: |
34+
cd packages/use-request
35+
pnpm build
36+
- name: Build use-request-plugins
37+
run: |
38+
cd packages/use-request-plugins
39+
pnpm build
3240
- name: Docs-Build
3341
run: |
3442
cd packages/hooks

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"@vue-hooks-plus/use-immer":"workspace:^1.0.0",
3939
"@vue-hooks-plus/use-worker":"workspace:^1.0.0",
4040
"@vue-hooks-plus/use-request":"workspace:^1.0.0",
41+
"@vue-hooks-plus/use-request-plugins":"workspace:^1.0.0",
42+
"@vue-hooks-plus/types":"workspace:^1.0.0",
4143
"@vue/test-utils": "^2.1.0",
4244
"@vitest/coverage-c8":"^0.25.7",
4345
"eslint": "^8.20.0",
@@ -70,6 +72,7 @@
7072
"vue": "^3.2.25",
7173
"vue-tsc": "1.0.9",
7274
"vue-typical": "^2.1.0",
75+
"pinia":"^2.0.34",
7376
"three":"0.147.0",
7477
"vue-demi": "^0.13.11"
7578
},

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ const useRequestRouter = [
162162
},
163163
]
164164

165+
const useRequestPlugins = [
166+
{
167+
text: 'useRequest 外置插件',
168+
items: [
169+
{
170+
text: '全局请求状态管理',
171+
link: '/useRequest/plugins/fetchsing/',
172+
},
173+
],
174+
},
175+
]
176+
177+
const useRequestPluginsEN = [
178+
{
179+
text: 'UseRequest External Plugins',
180+
items: [
181+
{
182+
text: 'Global Fetching',
183+
link: '/en/useRequest/plugins/fetchsing/',
184+
},
185+
],
186+
},
187+
]
188+
165189
const useRequestRouterEN = [
166190
{
167191
text: 'useRequest',
@@ -240,6 +264,11 @@ const getUseRequestRouter = (langPrefix = '/') => {
240264
else return useRequestRouterEN
241265
}
242266

267+
const getUseRequestPlugins = (langPrefix = '/') => {
268+
if (langPrefix === '/') return useRequestPlugins
269+
else return useRequestPluginsEN
270+
}
271+
243272
export function getRouterConfig(langPrefix = '/') {
244273
return [
245274
{
@@ -260,6 +289,7 @@ export function getRouterConfig(langPrefix = '/') {
260289
],
261290
},
262291
...getUseRequestRouter(langPrefix),
292+
...getUseRequestPlugins(langPrefix),
263293
...Router.map(item => ({
264294
text: item.text,
265295
items: item.items?.map(i => ({

packages/hooks/docs/.vitepress/theme/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import VhpButton from '../components/button'
55
import '@vue-hooks-plus/vitepress-demo-block/dist/style.css'
66
import './var.less'
77

8+
import { createPinia } from 'pinia'
9+
10+
const store = createPinia()
11+
812
export default {
913
...DefaultTheme,
1014
enhanceApp({ app }: { app: App<Element> }) {
15+
app.use(store)
1116
app.component('demo', DemoBlock)
1217
app.component('vhp-button', VhpButton)
1318
},
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<template>
2+
<demo1 />
3+
<demo2 />
4+
<demo3 />
5+
</template>
6+
7+
<script lang="ts" setup>
8+
import { useRequest } from 'vue-hooks-plus'
9+
import { defineComponent, h } from 'vue'
10+
import { useFetchingPlugin } from '@vue-hooks-plus/use-request-plugins';
11+
import Demo3 from './demo3.vue'
12+
13+
const demo1 = defineComponent({
14+
setup: () => {
15+
function getUsername(params: { desc: string }): Promise<string> {
16+
return new Promise(resolve => {
17+
setTimeout(() => {
18+
resolve(`vue-hooks-plus ${params.desc}`)
19+
}, 1000)
20+
})
21+
}
22+
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }), {
23+
fetchingKey: () => {
24+
return 'one'
25+
},
26+
onFetching: (current: any, record: any) => {
27+
console.log(current);
28+
console.log(record);
29+
},
30+
isFetching: (v: any) => {
31+
console.log(v);
32+
}
33+
}, [useFetchingPlugin])
34+
return () => {
35+
return h('div', {}, [
36+
h('h3', 'demo1'),
37+
h('div', null, loading.value ? 'loading' : data.value),
38+
])
39+
}
40+
},
41+
})
42+
43+
const DemoGlobal = defineComponent({
44+
setup: () => {
45+
function getUsername(params: { desc: string }): Promise<string> {
46+
return new Promise(resolve => {
47+
setTimeout(() => {
48+
resolve(`vue-hooks-plus ${params.desc}`)
49+
}, 2000)
50+
})
51+
}
52+
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }), {
53+
// manual: true,
54+
fetchingKey: () => {
55+
return 'two'
56+
},
57+
onFetching: (current: any, record: any) => {
58+
console.log(current);
59+
console.log(record);
60+
}
61+
}, [useFetchingPlugin])
62+
63+
// run()
64+
return () => {
65+
return h('div', {}, [
66+
h('h3', 'demo2'),
67+
h('div', null, loading.value ? 'loading' : data.value),
68+
])
69+
}
70+
},
71+
})
72+
73+
const demo2 = defineComponent({
74+
setup: () => {
75+
return () => {
76+
return h(DemoGlobal)
77+
}
78+
},
79+
})
80+
</script>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<template>
2+
<h3>demo3</h3>
3+
<div>{{ loading ? "loading..." : data }}</div>
4+
<br>
5+
<div>{{ isFetching ? "complete!" : "all loading..." }}</div>
6+
</template>
7+
8+
<script lang="ts" setup>
9+
import { useRequest } from 'vue-hooks-plus'
10+
import { useFetchingPlugin } from '@vue-hooks-plus/use-request-plugins';
11+
import { ref } from 'vue';
12+
function getUsername(params: { desc: string }): Promise<string> {
13+
return new Promise(resolve => {
14+
setTimeout(() => {
15+
resolve(`vue-hooks-plus ${params.desc}`)
16+
}, 3000)
17+
})
18+
}
19+
20+
const isFetching = ref(false)
21+
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }), {
22+
fetchingKey: () => {
23+
return 'three'
24+
},
25+
onFetching: (current: any, record: any) => {
26+
console.log(current);
27+
console.log(record);
28+
},
29+
isFetching: (v: boolean) => {
30+
isFetching.value = v
31+
}
32+
}, [useFetchingPlugin])
33+
</script>
34+
35+
<style scoped lang="less"></style>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useRequest/plugins/fetchsing
5+
source:
6+
show: false
7+
---
8+
9+
# UseRequest Fetching Plugin
10+
11+
A plugin for global request state management based on `pinia` implementation. The `useRequest Fetching plugin` will internally create a state management instance of pinia to collect request information.
12+
13+
## Feature
14+
15+
- Act as the intermediate state for all requests, where users can operate on the collected request results.
16+
- Whether all requests have been completed and automatically collected to determine whether all requests have been completed.
17+
- Non intrusive, all configurations are injected by plugins, and are non-invasive to the current function.
18+
19+
## Install
20+
21+
```bash
22+
23+
# It is necessary to ensure that the application contains pinia and has been used by Vue instances.
24+
25+
1. npm i pinia
26+
27+
2. npm i @vue-hooks-plus/use-request-plugins
28+
29+
```
30+
31+
## Demo
32+
33+
<demo src="./demo/demo.vue"
34+
language="vue"
35+
title=""
36+
desc="Multiple components, displaying complete when all requests are completed"> </demo>
37+
38+
## API
39+
40+
```typescript
41+
import { useRequest } from 'vue-hooks-plus'
42+
import { useFetchingPlugin } from '@vue-hooks-plus/use-request-plugins'
43+
44+
useRequest(
45+
service,
46+
{
47+
fetchingKey: (params: any[]) => string
48+
onFetching: (current:any,record:Record<string,any>) => void,
49+
isFetching: (_isFetching: boolean) => void,
50+
},
51+
[useFetchingPlugin],
52+
)
53+
```
54+
55+
## Options
56+
57+
| Property | Description | Type | Default |
58+
| --- | --- | --- | --- |
59+
| fetchingKey | The identification key of the status needs to be collected, and if it exists, it will be collected by the status |
60+
| `(params: any[]) => string` | - |
61+
| onFetching | Intermediate state function callback, the first parameter `current` is the current state of itself, and the second parameter `record` is all states | `(current:any,record:Record<string,any>) => void` | - |
62+
| isFetching | Official built-in function to determine whether all request statuses have been completed | `(_isFetching: boolean) => void` | - |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useRequest/plugins/fetchsing
5+
source:
6+
show: false
7+
---
8+
9+
# useRequest Fetching 插件
10+
11+
基于 `pinia` 实现的全局请求状态管理的插件。useRequest Fetching 插件会在内部创建一个 `pinia`的状态管理实例,收集请求的信息。
12+
13+
## 功能
14+
15+
- 充当所有请求的状态中间态,用户可以在中间态中对收集的请求结果进行操作。
16+
- 是否所有请求都完成,自动收集判断所有请求是否完成。
17+
- 无侵入性,所有配置均由插件注入,对当前函数无侵入性。
18+
19+
## 安装
20+
21+
```bash
22+
23+
# 需要保证应用含有pinia,并且被Vue实例 use。
24+
25+
1. npm i pinia
26+
27+
2. npm i @vue-hooks-plus/use-request-plugins
28+
29+
```
30+
31+
## 示例
32+
33+
<demo src="./demo/demo.vue"
34+
language="vue"
35+
title=""
36+
desc="多个组件,当所有请求完成后显示 complete"> </demo>
37+
38+
## API
39+
40+
```typescript
41+
import { useRequest } from 'vue-hooks-plus'
42+
import { useFetchingPlugin } from '@vue-hooks-plus/use-request-plugins'
43+
44+
useRequest(
45+
service,
46+
{
47+
fetchingKey: (params: any[]) => string
48+
onFetching: (current:any,record:Record<string,any>) => void,
49+
isFetching: (_isFetching: boolean) => void,
50+
},
51+
[useFetchingPlugin],
52+
)
53+
```
54+
55+
## Options
56+
57+
| Property | Description | Type | Default |
58+
| --- | --- | --- | --- |
59+
| fetchingKey | 需要收集状态的标识 key,存在即会被状态收集 | `(params: any[]) => string` | - |
60+
| onFetching | 中间态函数回调,第一个参数`current`是当前自身的状态,第二个参数`record`是所有的状态 | `(current:any,record:Record<string,any>) => void` | - |
61+
| isFetching | 官方自带的功能,判断所有请求状态是否请求完成 | `(_isFetching: boolean) => void` | - |

packages/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ export declare function usePrevious<T>(state: Ref<T> | ComputedRef<T>, shouldUpd
749749

750750
declare type UsePreviousShouldUpdateFunc<T> = (prev: T | undefined, next: T) => boolean;
751751

752-
export declare function useRequest<TData, TParams extends unknown[] = unknown[], PluginsOptions extends UseRequestPlugin<TData, TParams>[] = UseRequestPlugin<TData, TParams>[]>(service: UseRequestService<TData, TParams>, options?: UseRequestOptions<TData, TParams, PluginsOptions extends (infer P)[] ? P extends UseRequestPlugin<TData, TParams, infer R> ? R : any : any>, plugins?: PluginsOptions): useRequestResult<TData, TParams>;
752+
export declare function useRequest<TData, TParams extends unknown[] = unknown[], PluginsOptions extends UseRequestPlugin<TData, TParams>[] = UseRequestPlugin<TData, TParams>[]>(service: UseRequestService<TData, TParams>, options?: UseRequestOptions<TData, TParams, PluginsOptions extends (infer P)[] ? P extends UseRequestPlugin<TData, TParams, infer R> ? R : never : never>, plugins?: PluginsOptions): useRequestResult<TData, TParams>;
753753

754754
declare interface UseRequestBasicOptions<TData, TParams extends unknown[]> {
755755
/**
@@ -913,7 +913,7 @@ declare type UseRequestOptions<TData, TParams extends unknown[], TPlugin> = {
913913
[K in keyof TPlugin]: TPlugin[K];
914914
};
915915

916-
declare interface UseRequestPlugin<TData, TParams extends unknown[] = unknown[], TPlugin = any> {
916+
export declare interface UseRequestPlugin<TData, TParams extends unknown[] = unknown[], TPlugin = any> {
917917
(fetchInstance: Fetch<TData, TParams>, options: UseRequestOptions<TData, TParams, TPlugin>): UseRequestPluginReturn<TData, TParams>;
918918
onInit?: (options: UseRequestOptions<TData, TParams, TPlugin>) => Partial<UseRequestFetchState<TData, TParams>>;
919919
}

packages/types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vue-hooks-plus/types",
3-
"version": "1.6.0-alpha.8",
3+
"version": "1.6.0",
44
"description": "",
55
"files": [
66
"index.d.ts",

0 commit comments

Comments
 (0)