Skip to content

Commit eda4900

Browse files
committed
type: use-request Readonly field & notes
1 parent 490ca18 commit eda4900

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
</template>
55

66
<script lang="ts" setup>
7-
import { useRequest } from 'vue-hooks-plus'
7+
import { useRequest } from 'vue-hooks-plus'
88
9-
function getUsername(params: { desc: string }): Promise<string> {
10-
return new Promise(resolve => {
11-
setTimeout(() => {
12-
resolve(`vue-hooks-plus ${params.desc}`)
13-
}, 1000)
14-
})
15-
}
9+
function getUsername(params: { desc: string }): Promise<string> {
10+
return new Promise(resolve => {
11+
setTimeout(() => {
12+
resolve(`vue-hooks-plus ${params.desc}`)
13+
}, 1000)
14+
})
15+
}
1616
17-
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
17+
const { data, loading } = useRequest(() => getUsername({ desc: 'good' }))
1818
</script>

packages/hooks/src/useRequest/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export interface UseRequestPluginReturn<TData, TParams extends unknown[]> {
2020
params: TParams,
2121
) =>
2222
| ({
23-
stopNow?: boolean
24-
returnNow?: boolean
25-
} & Partial<UseRequestFetchState<TData, TParams>>)
23+
stopNow?: boolean
24+
returnNow?: boolean
25+
} & Partial<UseRequestFetchState<TData, TParams>>)
2626
| void
2727

2828
onRequest?: (
@@ -246,22 +246,22 @@ export interface useRequestResult<TData, TParams extends unknown[]> {
246246
/**
247247
* Is the service being executed.
248248
*/
249-
loading: Ref<boolean>
249+
loading: Readonly<Ref<boolean>>
250250

251251
/**
252252
* Data returned by service.
253253
*/
254-
data: Ref<TData | undefined>
254+
data: Readonly<Ref<TData | undefined>>
255255

256256
/**
257257
* Exception thrown by service.
258258
*/
259-
error: Ref<Error | undefined>
259+
error: Readonly<Ref<Error | undefined>>
260260

261261
/**
262262
* params An array of parameters for the service being executed. For example, you triggered `run(1, 2, 3)`, then params is equal to `[1, 2, 3]`.
263263
*/
264-
params: Ref<TParams | []>
264+
params: Readonly<Ref<TParams | []>>
265265

266266
/**
267267
* Ignore the current promise response.

packages/hooks/src/useRequest/useRequestImplement.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ function isUseRequestFetchState<TData, TParams extends any[]>(
1717
return keys.filter(i => ['data', 'loading', 'params', 'error'].includes(i)).length === 4
1818
}
1919

20-
// function isUseRequestFetchStateKey<TData, TParams extends any[]>(
21-
// field: string,
22-
// state: unknown,
23-
// ): state is UseRequestFetchState<TData, TParams>[keyof UseRequestFetchState<TData, TParams>] {
24-
// return Boolean(['data', 'loading', 'params', 'error'].find(i => i === field))
25-
// }
26-
2720
function useRequestImplement<TData, TParams extends any[]>(
2821
service: UseRequestService<TData, TParams>,
2922
options: UseRequestOptions<TData, TParams, any> = {},
@@ -59,9 +52,6 @@ function useRequestImplement<TData, TParams extends any[]>(
5952

6053
const setState = (currentState: unknown, field?: keyof typeof state) => {
6154
if (field) {
62-
// if (isUseRequestFetchStateKey<UnwrapRef<TData>, UnwrapRef<TParams>>(field, currentState)) {
63-
// state[field] = currentState as any
64-
// }
6555
state[field] = currentState as any
6656
} else {
6757
if (isUseRequestFetchState<UnwrapRef<TData>, UnwrapRef<TParams>>(currentState)) {
@@ -91,18 +81,10 @@ function useRequestImplement<TData, TParams extends any[]>(
9181

9282
const readyComputed = computed(() => isRef(ready) ? ready.value : ready)
9383

94-
// const isMount = ref(false)
95-
9684
watchEffect(() => {
9785
if (!manual) {
9886
const params = fetchInstance.state.params || options.defaultParams || []
99-
// if (readyComputed.value && !isMount.value) {
100-
// fetchInstance.run(...(params as TParams))
101-
// // 模拟首次mount
102-
// isMount.value = true
103-
104-
// }
105-
// 自动收集依赖
87+
// auto collect
10688
if (readyComputed.value && fetchInstance.options.refreshDeps === true && !!serviceRef.value) {
10789
fetchInstance.run(...(params as TParams))
10890
}

packages/use-request-plugins/src/useFetchingPlugin/useFetching.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,29 @@ import { UseRequestPlugin } from '@vue-hooks-plus/use-request/dist/types/types'
22
import { useFetchingGlobalStore } from './store'
33

44
export interface FetchingPluginType {
5+
/**
6+
* useRequest params
7+
* @param params request params
8+
* @type (params: any[]) => string
9+
* @returns string
10+
*/
511
fetchingKey?: (params: any[]) => string
12+
13+
/**
14+
*
15+
* @param current current fetch data
16+
* @param record all fetch data
17+
* @type (current: any, record: any) => void
18+
* @returns void
19+
*/
620
onFetching?: (current: any, record: any) => void
21+
22+
/**
23+
*
24+
* @param _isFetching all fetching loading status
25+
* @type (_isFetching: boolean) => void
26+
* @returns void
27+
*/
728
isFetching?: (_isFetching: boolean) => void
829
}
930

0 commit comments

Comments
 (0)