Skip to content

Commit 8b7753b

Browse files
committed
refactor: optimize types
1 parent 32952a0 commit 8b7753b

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/core/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export interface FunctionContext<R, P extends unknown[]> {
3434
mutate: Mutate<R>;
3535
}
3636

37+
export interface QueryResult<R, P extends unknown[]>
38+
extends State<R, P>,
39+
FunctionContext<R, P> {}
40+
3741
interface DebounceOptions {
3842
leading?: boolean;
3943
trailing?: boolean;

src/core/useQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import type {
66
GlobalOptions,
77
Options,
88
PluginImplementType,
9+
QueryResult,
910
Service,
1011
} from './types';
1112

1213
function useQuery<R, P extends unknown[]>(
1314
service: Service<R, P>,
1415
options: Options<R, P> = {},
1516
plugins: PluginImplementType<R, P>[] = [],
16-
) {
17+
): QueryResult<R, P> {
1718
const injectedGlobalOptions = inject<GlobalOptions>(
1819
GLOBAL_OPTIONS_PROVIDE_KEY,
1920
{},

src/usePagination.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import type { ComputedRef, Ref, WritableComputedRef } from 'vue-demi';
12
import { computed, inject, ref } from 'vue-demi';
23

34
import { getGlobalOptions, GLOBAL_OPTIONS_PROVIDE_KEY } from './core/config';
4-
import type { GlobalOptions, Options, Service } from './core/types';
5+
import type {
6+
GlobalOptions,
7+
Options,
8+
QueryResult,
9+
Service,
10+
} from './core/types';
511
import { get } from './core/utils';
612
import { merge } from './core/utils/lodash';
713
import useRequest from './useRequest';
@@ -19,10 +25,23 @@ export interface PaginationOptions<R, P extends unknown[]>
1925
extends Options<R, P>,
2026
PaginationExtendsOption {}
2127

28+
interface PaginationQueryResult<R, P extends unknown[]>
29+
extends QueryResult<R, P> {
30+
current: WritableComputedRef<number>;
31+
pageSize: WritableComputedRef<number>;
32+
total: ComputedRef<number>;
33+
totalPage: ComputedRef<number>;
34+
reloading: Ref<boolean>;
35+
reload: () => void;
36+
changeCurrent: (current: number) => void;
37+
changePageSize: (pageSize: number) => void;
38+
changePagination: (current: number, pageSize: number) => void;
39+
}
40+
2241
function usePagination<R, P extends unknown[] = any>(
2342
service: Service<R, P>,
2443
options?: PaginationOptions<R, P>,
25-
) {
44+
): PaginationQueryResult<R, P> {
2645
const defaultOptions = {
2746
pagination: {
2847
currentKey: 'current',

src/useRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import useReadyPlugin from './core/plugins/useReadyPlugin';
77
import useRefreshDepsPlugin from './core/plugins/useRefreshDepsPlugin';
88
import useRefreshOnWindowFocus from './core/plugins/useRefreshOnWindowFocus';
99
import useThrottlePlugin from './core/plugins/useThrottlePlugin';
10-
import type { Options, Service } from './core/types';
10+
import type { Options, QueryResult, Service } from './core/types';
1111
import useQuery from './core/useQuery';
1212

1313
function useRequest<R, P extends unknown[] = any>(
1414
service: Service<R, P>,
1515
options?: Options<R, P>,
16-
) {
16+
): QueryResult<R, P> {
1717
return useQuery<R, P>(service, options, [
1818
useLoadingDelayPlugin,
1919
useErrorRetryPlugin,

0 commit comments

Comments
 (0)