Skip to content

Commit

Permalink
perf(request): 优化 request类型及使用示例以更好的提示返回值
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Nov 30, 2022
1 parent cd4d985 commit 3c23293
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/api/routeMenu.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import request from '@/utils/request';
import { request, RequestOptions } from '@/utils/request';
import { RouteRecordRaw } from 'vue-router';

const enum Api {
MENU = '/api/menu/list',
}

export function menuApi(returnAxios = true) {
return request<RouteRecordRaw[], [], typeof returnAxios>(
export function menuApi<T extends boolean = true>(
options: RequestOptions<RouteRecordRaw[], []> = {},
returnAxios: T = true as T,
) {
return request(
() => ({
url: Api.MENU,
method: 'get',
}),
{},
options,
returnAxios,
);
}
6 changes: 3 additions & 3 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export function request<R, P extends unknown[] = []>(
axiosConfig: (...args: P) => AxiosRequestConfig | Promise<AxiosRequestConfig>,
options?: RequestOptions<R, P>,
): ReturnType<typeof useRequest<R, P>>;
export function request<R, P extends unknown[] = [], T extends boolean | undefined = boolean | undefined>(
export function request<R, P extends unknown[] = [], T extends boolean = boolean>(
axiosConfig: (...args: P) => AxiosRequestConfig | Promise<AxiosRequestConfig>,
options: RequestOptions<R, P>,
returnAxios?: T,
): T extends boolean ? (...args: P) => Promise<R> : ReturnType<typeof useRequest<R, P>>;
returnAxios: T,
): T extends true ? (...args: P) => Promise<R> : ReturnType<typeof useRequest<R, P>>;
/**
* 请求函数
* @param axiosConfig axios的配置项
Expand Down

0 comments on commit 3c23293

Please sign in to comment.