Skip to content

Commit a59288c

Browse files
committed
feat: @vue-hooks-plus/use-request image pull
1 parent 99e5756 commit a59288c

File tree

6 files changed

+82
-14
lines changed

6 files changed

+82
-14
lines changed

packages/use-request/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/use-request",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Vue use-request hooks library",
55
"files": [
66
"dist",

packages/use-request/src/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { InjectionKey } from 'vue';
2+
3+
import type { UseRequestOptions } from './types';
4+
5+
const USEREQUEST_GLOBAL_OPTIONS:Record<string,any> = {};
6+
7+
export const USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY: InjectionKey<string> = Symbol(
8+
'USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY',
9+
);
10+
11+
export const setGlobalOptions = (config: UseRequestOptions<unknown,any,[]>) => {
12+
Object.keys(config).forEach(key => {
13+
USEREQUEST_GLOBAL_OPTIONS[key] = config[key as keyof typeof config];
14+
});
15+
};
16+
17+
export const getGlobalOptions = () => {
18+
return USEREQUEST_GLOBAL_OPTIONS;
19+
};
20+
21+
export const clearGlobalOptions = () => {
22+
Object.keys(USEREQUEST_GLOBAL_OPTIONS).forEach(key => {
23+
delete USEREQUEST_GLOBAL_OPTIONS[key];
24+
});
25+
};
26+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { UseRequestOptions, UseRequestPlugin, UseRequestService } from './types'
2+
import useRequest from './useRequest'
3+
4+
function createUseRequest<
5+
TData,
6+
TParams extends unknown[] = unknown[],
7+
PluginsOptions extends UseRequestPlugin<TData, TParams>[] = UseRequestPlugin<TData, TParams>[]
8+
>(
9+
service: UseRequestService<TData, TParams>,
10+
options?: UseRequestOptions<
11+
TData,
12+
TParams,
13+
PluginsOptions extends (infer P)[]
14+
? P extends UseRequestPlugin<TData, TParams, infer R>
15+
? R
16+
: any
17+
: any
18+
>,
19+
plugins?: PluginsOptions,
20+
) {
21+
return useRequest<TData, TParams, PluginsOptions>(service, options, plugins)
22+
}
23+
24+
export default createUseRequest

packages/use-request/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import useRequest from "./useRequest";
2-
import { clearCache } from "./utils/cache";
1+
import useRequest from './useRequest'
2+
import useRequestProvider from './useRequestProvider'
3+
import createUseRequest from './createUseRequest'
4+
import { clearCache as clearUseRequestCache } from './utils/cache'
35

4-
export { clearCache };
6+
export { clearUseRequestCache, createUseRequest, useRequestProvider }
57

6-
export default useRequest;
8+
export default useRequest

packages/use-request/src/useRequestImplement.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
import { ref, reactive, toRefs, onMounted, onUnmounted, unref } from 'vue'
1+
import { ref, reactive, toRefs, onMounted, onUnmounted, unref, inject } from 'vue'
22

33
import Fetch from './Fetch'
4+
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config'
45
import { UseRequestOptions, UseRequestPlugin, useRequestResult, UseRequestService } from './types'
56

67
function useRequestImplement<TData, TParams extends any[]>(
78
service: UseRequestService<TData, TParams>,
89
options: UseRequestOptions<TData, TParams, any> = {},
910
plugins: UseRequestPlugin<TData, TParams>[] = [],
1011
) {
11-
// 读取配置
12-
const { initialData = undefined, manual = false, ready = true, ...rest } = options
12+
// global option
13+
const USEREQUEST_GLOBAL_OPTIONS = inject<Record<string, any>>(
14+
USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY,
15+
)
16+
// read option
17+
const { initialData = undefined, manual = false, ready = true, ...rest } = {
18+
...(USEREQUEST_GLOBAL_OPTIONS ?? {}),
19+
...(options ?? {}),
20+
} as Record<string, any>
1321

1422
const fetchOptions = {
1523
manual,
1624
ready,
1725
...rest,
1826
}
1927

20-
// 定义一个serviceRef
28+
// serviceRef store service
2129
const serviceRef = ref(service)
2230

23-
// 存储state的响应式对象
31+
// reactive
2432
const state = reactive<{
2533
data?: TData
2634
loading: boolean
@@ -45,7 +53,7 @@ function useRequestImplement<TData, TParams extends any[]>(
4553
}
4654

4755
const initState = plugins.map(p => p?.onInit?.(fetchOptions)).filter(Boolean)
48-
// fetch的实例化
56+
// Fetch Instance
4957
const fetchInstance = new Fetch<TData, TParams>(
5058
serviceRef,
5159
fetchOptions,
@@ -55,20 +63,20 @@ function useRequestImplement<TData, TParams extends any[]>(
5563

5664
fetchInstance.options = fetchOptions
5765

58-
// 运行插件
66+
// run plugins
5967
fetchInstance.pluginImpls = plugins.map(p => {
6068
return p(fetchInstance, fetchOptions)
6169
})
6270

63-
// manual控制是否自动发送请求
71+
// manual
6472
onMounted(() => {
6573
if (!manual) {
6674
const params = fetchInstance.state.params || options.defaultParams || []
6775
if (unref(ready)) fetchInstance.run(...(params as TParams))
6876
}
6977
})
7078

71-
// 组件卸载的时候取消请求
79+
// onUnmounted cancel request
7280
onUnmounted(() => {
7381
fetchInstance.cancel()
7482
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { provide } from 'vue';
2+
3+
import { USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY } from './config';
4+
import type { UseRequestOptions } from './types';
5+
6+
export default function useRequestProvider(config: UseRequestOptions<unknown,any,any>){
7+
provide<typeof config>(USEREQUEST_GLOBAL_OPTIONS_PROVIDE_KEY, config);
8+
}

0 commit comments

Comments
 (0)