Skip to content

Commit

Permalink
fix(projects): fix flatRequest error type. fixed #376
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Apr 2, 2024
1 parent 9125cc9 commit 1ec5ea0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
9 changes: 5 additions & 4 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ export function createFlatRequest<ResponseData = any, State = Record<string, unk
) {
const { instance, opts, cancelRequest, cancelAllRequest } = createCommonRequest<ResponseData>(axiosConfig, options);

const flatRequest: FlatRequestInstance<State> = async function flatRequest<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
) {
const flatRequest: FlatRequestInstance<State, ResponseData> = async function flatRequest<
T = any,
R extends ResponseType = 'json'
>(config: CustomAxiosRequestConfig) {
try {
const response: AxiosResponse<ResponseData> = await instance(config);

Expand All @@ -163,7 +164,7 @@ export function createFlatRequest<ResponseData = any, State = Record<string, unk
} catch (error) {
return { data: null, error };
}
} as FlatRequestInstance<State>;
} as FlatRequestInstance<State, ResponseData>;

flatRequest.cancelRequest = cancelRequest;
flatRequest.cancelAllRequest = cancelAllRequest;
Expand Down
12 changes: 7 additions & 5 deletions packages/axios/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,17 @@ export type FlatResponseSuccessData<T = any> = {
error: null;
};

export type FlatResponseFailData<T = any> = {
export type FlatResponseFailData<ResponseData = any> = {
data: null;
error: AxiosError<T>;
error: AxiosError<ResponseData>;
};

export type FlatResponseData<T = any> = FlatResponseSuccessData<T> | FlatResponseFailData<T>;
export type FlatResponseData<T = any, ResponseData = any> =
| FlatResponseSuccessData<T>
| FlatResponseFailData<ResponseData>;

export interface FlatRequestInstance<S = Record<string, unknown>> extends RequestInstanceCommon<S> {
export interface FlatRequestInstance<S = Record<string, unknown>, ResponseData = any> extends RequestInstanceCommon<S> {
<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig<R>
): Promise<FlatResponseData<MappedType<R, T>>>;
): Promise<FlatResponseData<MappedType<R, T>, ResponseData>>;
}
20 changes: 10 additions & 10 deletions packages/hooks/src/use-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export type HookRequestInstanceResponseSuccessData<T = any> = {
error: Ref<null>;
};

export type HookRequestInstanceResponseFailData<T = any> = {
export type HookRequestInstanceResponseFailData<ResponseData = any> = {
data: Ref<null>;
error: Ref<AxiosError<T>>;
error: Ref<AxiosError<ResponseData>>;
};

export type HookRequestInstanceResponseData<T = any> = {
export type HookRequestInstanceResponseData<T = any, ResponseData = any> = {
loading: Ref<boolean>;
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<T>);
} & (HookRequestInstanceResponseSuccessData<T> | HookRequestInstanceResponseFailData<ResponseData>);

export interface HookRequestInstance {
export interface HookRequestInstance<ResponseData = any> {
<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
): HookRequestInstanceResponseData<MappedType<R, T>>;
): HookRequestInstanceResponseData<MappedType<R, T>, ResponseData>;
cancelRequest: (requestId: string) => void;
cancelAllRequest: () => void;
}
Expand All @@ -45,13 +45,13 @@ export default function createHookRequest<ResponseData = any>(
) {
const request = createFlatRequest<ResponseData>(axiosConfig, options);

const hookRequest: HookRequestInstance = function hookRequest<T = any, R extends ResponseType = 'json'>(
const hookRequest: HookRequestInstance<ResponseData> = function hookRequest<T = any, R extends ResponseType = 'json'>(
config: CustomAxiosRequestConfig
) {
const { loading, startLoading, endLoading } = useLoading();

const data = ref<MappedType<R, T> | null>(null);
const error = ref<AxiosError<MappedType<R, T>> | null>(null);
const data = ref<MappedType<R, T> | null>(null) as Ref<MappedType<R, T>>;
const error = ref<AxiosError<ResponseData> | null>(null) as Ref<AxiosError<ResponseData> | null>;

startLoading();

Expand All @@ -70,7 +70,7 @@ export default function createHookRequest<ResponseData = any>(
data,
error
};
} as HookRequestInstance;
} as HookRequestInstance<ResponseData>;

hookRequest.cancelRequest = request.cancelRequest;
hookRequest.cancelAllRequest = request.cancelAllRequest;
Expand Down

0 comments on commit 1ec5ea0

Please sign in to comment.