Skip to content

Commit 0be8013

Browse files
committed
feat: useFormatResult
1 parent 6902c73 commit 0be8013

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

packages/hooks/docs/.vitepress/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function getHooksSidebar() {
145145
text: 'State',
146146
items: [
147147
{ text: 'useBoolean', link: '/useBoolean/' },
148-
// { text: 'useUrlState', link: '/useUrlState/' },
148+
{ text: 'useFormatResult', link: '/useFormatResult/' },
149149
{ text: 'useDebounce', link: '/useDebounce/' },
150150
{ text: 'useThrottle', link: '/useThrottle/' },
151151
{ text: 'useToggle', link: '/useToggle/' },

packages/hooks/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import useEventListener from './useEventListener'
1313
import useEventEmitter from './useEventEmitter'
1414
import useFavicon from './useFavicon'
1515
import useFocusWithin from './useFocusWithin'
16+
import useFormatResult from './useFormatResult'
1617
import useHover from './useHover'
1718
import usePreview from './usePreview'
1819
import useInterval from './useInterval'
@@ -56,6 +57,7 @@ export {
5657
useEventEmitter,
5758
useFavicon,
5859
useFocusWithin,
60+
useFormatResult,
5961
useHover,
6062
useInterval,
6163
useInfiniteScroll,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div>name: {{ name }}</div>
3+
<div>age: {{ age }}</div>
4+
<div>double age: {{ formatAge }}</div>
5+
</template>
6+
7+
<script lang="ts" setup>
8+
import { ref } from 'vue'
9+
import { useFormatResult, useInterval } from 'vue-hooks-plus'
10+
11+
const age = ref(0)
12+
const name = useFormatResult({ name: 'vue-hooks-plus' }, data => {
13+
return data.name
14+
})
15+
16+
const formatAge = useFormatResult(age, data => {
17+
return data * 2
18+
})
19+
20+
useInterval(() => {
21+
age.value += 1
22+
}, 1000)
23+
</script>
24+
25+
<style scoped lang="less"></style>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
map:
3+
# 映射到docs的路径
4+
path: /useFormatResult
5+
---
6+
7+
# useFormatResult
8+
9+
格式化数据的 hook.
10+
11+
## 代码演示
12+
13+
### 基础用法
14+
15+
<demo src="./demo/demo.vue"
16+
language="vue"
17+
title="基本用法"
18+
desc="格式化数据">
19+
</demo>
20+
21+
22+
## API
23+
24+
```typescript
25+
const formatData = useFormatResult(data, callback)
26+
```
27+
28+
29+
30+
### Argument
31+
32+
| 参数 | 说明 | 类型 | 默认值 |
33+
| -------- | -------------- | ---------------------------------- | ------ |
34+
| data | 需要格式化的数据 | `TData` | - |
35+
| callback | 格式化函数 | `(data: TData) => FData` | - |
36+
37+
### Result
38+
39+
| 参数 | 说明 | 类型 |
40+
| ------------- | ------------------ | --------- |
41+
| formatData | 格式化后的数据 | `ComputedRef<FData>` \| `FData` |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isRef, computed, Ref, ComputedRef } from 'vue'
2+
3+
function useFormatResult<TData, FData>(
4+
data: TData | Ref<TData>,
5+
formatResultCallback: (data: TData) => FData,
6+
): ComputedRef<FData> {
7+
const formatResultData = computed(() =>
8+
isRef(data) ? formatResultCallback(data.value) : formatResultCallback(data),
9+
)
10+
return formatResultData
11+
}
12+
13+
export default useFormatResult

packages/hooks/src/useRequest/docs/basic/index.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ map:
1919
desc="默认发送获取请求">
2020
</demo>
2121

22+
### 格式化请求数据
2223

24+
由于 `useRequest` 需要保证良好的插件系统,format对于系统来说侵入性太大,这里格式化使用的的是 `useFormatResult`,在请求数据完成后将 data 传入 `useFormatResult` 进行格式化, `useFormatResult` 可以很好的支持 `typescript` 类型提示。
25+
<br />
26+
27+
<a href="/vue-hooks-plus/docs/useFormatResult/" >跳转至useFormatResult</a>
2328

2429
## API
2530

@@ -57,7 +62,7 @@ const {
5762
| error | service 抛出的异常 |`Ref<Error>` \| `undefined` |
5863
| loading | service 是否正在执行 | `Ref<boolean>` |
5964
| params | 当次执行的 service 的参数数组。比如你触发了 `run(1, 2, 3)`,则 params 等于 `[1, 2, 3]` | `Ref<TParams>` \| `[]` |
60-
| formatResult | 格式化请求结果 | `(response: TData) => any` | |
65+
| formatResult | 格式化请求结果,建议使用 `useFormatResult` | `(response: TData) => any` | |
6166
| run | <ul><li> 手动触发 service 执行,参数会传递给 service</li><li>异常自动处理,通过 `onError` 反馈</li></ul> | `(...params: TParams) => void` |
6267
| runAsync |`run` 用法一致,但返回的是 Promise,需要自行处理异常。 | `(...params: TParams) => Promise<TData>` |
6368
| refresh | 使用上一次的 params,重新调用 `run` | `() => void` |

0 commit comments

Comments
 (0)