Skip to content

Commit 407ed11

Browse files
committed
fix: onSuccess type is incorrect
link #31
1 parent 0a31504 commit 407ed11

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/core/config.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,23 @@ export type BaseOptions<R, P extends unknown[]> = GlobalOptions & {
5454
refreshDeps?: WatchSource<any>[];
5555
cacheKey?: string;
5656
queryKey?: (...args: P) => string;
57-
onSuccess?: (data: R, params: P) => void;
57+
onSuccess: (data: R, params: P) => void;
5858
onError?: (error: Error, params: P) => void;
5959
};
6060

61+
const FRPlaceholderType = Symbol('FR');
62+
export type FRPlaceholderType = typeof FRPlaceholderType;
63+
64+
// temporary fix: https://github.com/AttoJS/vue-request/issues/31
65+
// When `formatResult` and `onSuccess` are used at the same time
66+
// the type of the parameter `data` of `onSuccess` is temporarily set to `any`
6167
export type FormatOptions<R, P extends unknown[], FR> = {
6268
formatResult: (data: R) => FR;
63-
} & BaseOptions<FR, P>;
69+
onSuccess?: (
70+
data: FR extends FRPlaceholderType ? any : FR,
71+
params: P,
72+
) => void;
73+
} & Omit<BaseOptions<FR, P>, 'onSuccess'>;
6474

6575
export type MixinOptions<R, P extends unknown[], FR> =
6676
| BaseOptions<R, P>

src/useLoadMore.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { computed, inject, ref, Ref, watchEffect } from 'vue';
22
import {
33
BaseOptions,
44
FormatOptions,
5+
FRPlaceholderType,
56
getGlobalOptions,
67
GlobalOptions,
78
GLOBAL_OPTIONS_PROVIDE_KEY,
@@ -59,7 +60,7 @@ function useLoadMore<
5960
function useLoadMore<
6061
R,
6162
P extends unknown[] = any,
62-
FR = any,
63+
FR = FRPlaceholderType,
6364
LR extends unknown[] = any[]
6465
>(
6566
service: LoadMoreService<R, P, LR>,

src/usePagination.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { computed, inject, Ref } from 'vue';
22
import {
33
BaseOptions,
44
FormatOptions,
5+
FRPlaceholderType,
56
getGlobalOptions,
67
GlobalOptions,
78
GLOBAL_OPTIONS_PROVIDE_KEY,
@@ -47,7 +48,7 @@ export type PaginationMixinOptions<R, P extends unknown[], FR> =
4748
function usePagination<R, P extends unknown[] = any>(
4849
service: IService<R, P>,
4950
): PaginationResult<R, P>;
50-
function usePagination<R, P extends unknown[] = any, FR = any>(
51+
function usePagination<R, P extends unknown[] = any, FR = FRPlaceholderType>(
5152
service: IService<R, P>,
5253
options: PaginationFormatOptions<R, P, FR>,
5354
): PaginationResult<FR, P>;

src/useRequest.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { BaseOptions, FormatOptions, MixinOptions } from './core/config';
1+
import {
2+
BaseOptions,
3+
FormatOptions,
4+
FRPlaceholderType,
5+
MixinOptions,
6+
} from './core/config';
27
import useAsyncQuery, { BaseResult } from './core/useAsyncQuery';
38
import generateService from './core/utils/generateService';
49
import { IService } from './core/utils/types';
@@ -10,7 +15,7 @@ export interface RequestResult<R, P extends unknown[]>
1015
function useRequest<R, P extends unknown[] = any>(
1116
service: IService<R, P>,
1217
): RequestResult<R, P>;
13-
function useRequest<R, P extends unknown[] = any, FR = any>(
18+
function useRequest<R, P extends unknown[] = any, FR = FRPlaceholderType>(
1419
service: IService<R, P>,
1520
options: FormatOptions<R, P, FR>,
1621
): RequestResult<FR, P>;

0 commit comments

Comments
 (0)