Skip to content

Commit 0156fa1

Browse files
committed
refactor: 修改 createUseRequestQueryComponent 为 createUseRequestComponent,增强类型支持并简化 props 处理
1 parent cbe3459 commit 0156fa1

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed
Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
import { defineComponent, PropType, computed, SlotsType, unref, watch } from "vue";
1+
import { defineComponent, PropType, computed, SlotsType, unref } from "vue";
22
import useRequest from "../useRequest";
33
import type {
44
UseRequestService,
55
UseRequestOptions,
66
UseRequestPlugin,
77
} from "../types";
8-
import { isEqual } from "lodash-es";
98

109
// 泛型工厂
11-
function createUseRequestQueryComponent<TData = any, TParams extends unknown[] = any[]>() {
10+
function createUseRequestComponent<
11+
TServiceData = any,
12+
TParams extends unknown[] = any[],
13+
TFormatResult = TServiceData
14+
>() {
1215
// 先定义组件
1316
const Comp = defineComponent({
1417
name: "UseRequest",
1518
props: {
1619
service: {
17-
type: Function as PropType<UseRequestService<TData, TParams>>,
20+
type: Function as PropType<UseRequestService<TServiceData, TParams>>,
1821
required: true,
1922
},
20-
21-
initialData: {
22-
type: String,
23-
default: () => undefined,
24-
},
2523
ready: {
26-
// 支持 initialData
2724
type: Object as PropType<
28-
UseRequestOptions<TData, TParams>['ready']
25+
UseRequestOptions<TServiceData, TParams>["ready"]
2926
>,
3027
default: () => ({
3128
ready: false,
@@ -36,46 +33,37 @@ function createUseRequestQueryComponent<TData = any, TParams extends unknown[] =
3633
default: () => [],
3734
},
3835
plugins: {
39-
type: Array as PropType<UseRequestPlugin<TData, TParams>[]>,
36+
type: Array as PropType<UseRequestPlugin<TServiceData, TParams>[]>,
4037
default: () => [],
4138
},
39+
formatResult: {
40+
type: Function as PropType<(res: TServiceData) => TFormatResult>,
41+
default: undefined,
42+
},
4243
},
4344
slots: Object as SlotsType<{
44-
default: (props: Pick<ReturnType<typeof useRequest<TData, TParams>>, "data">) => any,
45+
default: (props: ReturnType<typeof useRequest<TFormatResult, TParams>>) => any,
4546
loading: () => any,
46-
error: (props: { error: ReturnType<typeof useRequest<TData, TParams>>["error"]['value'] }) => any,
47+
error: (props: { error: ReturnType<typeof useRequest<TFormatResult, TParams>>["error"]['value'] }) => any,
48+
target?: () => any
4749
}>,
4850
setup(props, { slots }) {
49-
5051
// 支持响应式 props
5152
const service = computed(() => unref(props.service));
5253
const plugins = computed(() => unref(props.plugins));
5354

54-
55-
// 让 props 转为响应式
56-
const ready = computed(() => props.ready);
57-
const refreshDeps = computed(() => props.refreshDeps);
58-
59-
6055
// useRequest 的返回类型会自动推断
61-
const result = useRequest(service.value, {
62-
// @ts-ignore
63-
initialData: props.initialData,
64-
// @ts-ignore
65-
ready,
66-
// @ts-ignore
67-
refreshDeps,
68-
}, plugins.value);
56+
const options = {
57+
ready: props.ready,
58+
refreshDeps: props.refreshDeps,
59+
};
60+
// @ts-expect-error: formatResult 可能不是 options 的一部分
61+
if (props.formatResult) options.formatResult = props.formatResult;
6962

70-
watch(
71-
refreshDeps,
72-
(newVal, oldVal) => {
73-
// 如果 refreshDeps 没有变化,则不刷新
74-
// 这里对比的是数组,所以需要使用 isEqual 来对比
75-
if (isEqual(newVal, oldVal)) return;
76-
result.refresh();
77-
},
78-
{ immediate: true }
63+
const result = useRequest(
64+
service.value,
65+
options,
66+
plugins.value
7967
);
8068

8169
return () => {
@@ -86,20 +74,16 @@ function createUseRequestQueryComponent<TData = any, TParams extends unknown[] =
8674
return slots.error({ error: result.error.value });
8775
}
8876
if (!result.loading.value && !result.error.value && slots.default) {
89-
90-
return slots.default({
91-
// @ts-ignore
92-
data: result.data.value
93-
});
77+
return slots.default(result as any);
9478
}
9579
return null;
9680
};
9781
},
9882
});
9983

10084
// 用类型断言给组件加上 slots 类型声明
101-
return Comp
85+
return Comp;
10286
}
10387

10488
// 导出泛型组件工厂
105-
export default createUseRequestQueryComponent;
89+
export default createUseRequestComponent;

0 commit comments

Comments
 (0)