Skip to content

Commit

Permalink
fix: onSuccess type is incorrect
Browse files Browse the repository at this point in the history
link #31
  • Loading branch information
John60676 committed Mar 22, 2021
1 parent 0a31504 commit 407ed11
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,23 @@ export type BaseOptions<R, P extends unknown[]> = GlobalOptions & {
refreshDeps?: WatchSource<any>[];
cacheKey?: string;
queryKey?: (...args: P) => string;
onSuccess?: (data: R, params: P) => void;
onSuccess: (data: R, params: P) => void;
onError?: (error: Error, params: P) => void;
};

const FRPlaceholderType = Symbol('FR');
export type FRPlaceholderType = typeof FRPlaceholderType;

// temporary fix: https://github.com/AttoJS/vue-request/issues/31
// When `formatResult` and `onSuccess` are used at the same time
// the type of the parameter `data` of `onSuccess` is temporarily set to `any`
export type FormatOptions<R, P extends unknown[], FR> = {
formatResult: (data: R) => FR;
} & BaseOptions<FR, P>;
onSuccess?: (
data: FR extends FRPlaceholderType ? any : FR,
params: P,
) => void;
} & Omit<BaseOptions<FR, P>, 'onSuccess'>;

export type MixinOptions<R, P extends unknown[], FR> =
| BaseOptions<R, P>
Expand Down
3 changes: 2 additions & 1 deletion src/useLoadMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { computed, inject, ref, Ref, watchEffect } from 'vue';
import {
BaseOptions,
FormatOptions,
FRPlaceholderType,
getGlobalOptions,
GlobalOptions,
GLOBAL_OPTIONS_PROVIDE_KEY,
Expand Down Expand Up @@ -59,7 +60,7 @@ function useLoadMore<
function useLoadMore<
R,
P extends unknown[] = any,
FR = any,
FR = FRPlaceholderType,
LR extends unknown[] = any[]
>(
service: LoadMoreService<R, P, LR>,
Expand Down
3 changes: 2 additions & 1 deletion src/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { computed, inject, Ref } from 'vue';
import {
BaseOptions,
FormatOptions,
FRPlaceholderType,
getGlobalOptions,
GlobalOptions,
GLOBAL_OPTIONS_PROVIDE_KEY,
Expand Down Expand Up @@ -47,7 +48,7 @@ export type PaginationMixinOptions<R, P extends unknown[], FR> =
function usePagination<R, P extends unknown[] = any>(
service: IService<R, P>,
): PaginationResult<R, P>;
function usePagination<R, P extends unknown[] = any, FR = any>(
function usePagination<R, P extends unknown[] = any, FR = FRPlaceholderType>(
service: IService<R, P>,
options: PaginationFormatOptions<R, P, FR>,
): PaginationResult<FR, P>;
Expand Down
9 changes: 7 additions & 2 deletions src/useRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BaseOptions, FormatOptions, MixinOptions } from './core/config';
import {
BaseOptions,
FormatOptions,
FRPlaceholderType,
MixinOptions,
} from './core/config';
import useAsyncQuery, { BaseResult } from './core/useAsyncQuery';
import generateService from './core/utils/generateService';
import { IService } from './core/utils/types';
Expand All @@ -10,7 +15,7 @@ export interface RequestResult<R, P extends unknown[]>
function useRequest<R, P extends unknown[] = any>(
service: IService<R, P>,
): RequestResult<R, P>;
function useRequest<R, P extends unknown[] = any, FR = any>(
function useRequest<R, P extends unknown[] = any, FR = FRPlaceholderType>(
service: IService<R, P>,
options: FormatOptions<R, P, FR>,
): RequestResult<FR, P>;
Expand Down

0 comments on commit 407ed11

Please sign in to comment.