feat(projects): 添加请求适配器的请求示例

This commit is contained in:
Soybean
2022-04-04 19:13:15 +08:00
parent 6bed9ead38
commit bed4292ed3
8 changed files with 63 additions and 38 deletions

View File

@ -1 +1,10 @@
export {};
export function adapterOfDataWithAdapter(res: Service.RequestResult<ApiDemo.DataWithAdapter>): Demo.DataWithAdapter {
const { dataId, dataName } = res.data!;
const result: Demo.DataWithAdapter = {
id: dataId,
name: dataName
};
return result;
}

View File

@ -1,6 +1,9 @@
import { request } from '../request';
import { adapterOfServiceResult } from '@/utils';
import { mockRequest } from '../request';
import { adapterOfDataWithAdapter } from '../adapter';
/** 测试请求代理 */
export function fetchTestProxy() {
return request.get('/test');
/** 带有适配器的请求(将请求结果进行数据处理) */
export async function fetchDataWithAdapter() {
const res = await mockRequest.post<ApiDemo.DataWithAdapter>('/apiDemoWithAdapter');
return adapterOfServiceResult(adapterOfDataWithAdapter, res);
}

View File

@ -29,7 +29,7 @@ export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: S
* - data: 请求的body的data
* - axiosConfig: axios配置
*/
async function asyncRequest<T = any>(param: RequestParam): Promise<Service.RequestResult<T>> {
async function asyncRequest<T>(param: RequestParam): Promise<Service.RequestResult<T>> {
const { url } = param;
const method = param.method || 'get';
const { instance } = customInstance;
@ -49,7 +49,7 @@ export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: S
* @param url - 请求地址
* @param config - axios配置
*/
function get<T = any>(url: string, config?: AxiosRequestConfig) {
function get<T>(url: string, config?: AxiosRequestConfig) {
return asyncRequest<T>({ url, method: 'get', axiosConfig: config });
}
@ -59,7 +59,7 @@ export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: S
* @param data - 请求的body的data
* @param config - axios配置
*/
function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
function post<T>(url: string, data?: any, config?: AxiosRequestConfig) {
return asyncRequest<T>({ url, method: 'post', data, axiosConfig: config });
}
/**
@ -68,7 +68,7 @@ export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: S
* @param data - 请求的body的data
* @param config - axios配置
*/
function put<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
function put<T>(url: string, data?: any, config?: AxiosRequestConfig) {
return asyncRequest<T>({ url, method: 'put', data, axiosConfig: config });
}
@ -77,7 +77,7 @@ export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: S
* @param url - 请求地址
* @param config - axios配置
*/
function handleDelete<T = any>(url: string, config: AxiosRequestConfig) {
function handleDelete<T>(url: string, config: AxiosRequestConfig) {
return asyncRequest<T>({ url, method: 'delete', axiosConfig: config });
}
@ -112,7 +112,7 @@ export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig
* - data: 请求的body的data
* - axiosConfig: axios配置
*/
function useRequest<T = any>(param: RequestParam): RequestResultHook<T> {
function useRequest<T>(param: RequestParam): RequestResultHook<T> {
const { loading, startLoading, endLoading } = useLoading();
const { bool: network, setBool: setNetwork } = useBoolean(window.navigator.onLine);
@ -147,7 +147,7 @@ export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig
* @param url - 请求地址
* @param config - axios配置
*/
function get<T = any>(url: string, config?: AxiosRequestConfig) {
function get<T>(url: string, config?: AxiosRequestConfig) {
return useRequest<T>({ url, method: 'get', axiosConfig: config });
}
@ -157,7 +157,7 @@ export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig
* @param data - 请求的body的data
* @param config - axios配置
*/
function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
function post<T>(url: string, data?: any, config?: AxiosRequestConfig) {
return useRequest<T>({ url, method: 'post', data, axiosConfig: config });
}
/**
@ -166,7 +166,7 @@ export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig
* @param data - 请求的body的data
* @param config - axios配置
*/
function put<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
function put<T>(url: string, data?: any, config?: AxiosRequestConfig) {
return useRequest<T>({ url, method: 'put', data, axiosConfig: config });
}
@ -175,7 +175,7 @@ export function createHookRequest(axiosConfig: AxiosRequestConfig, backendConfig
* @param url - 请求地址
* @param config - axios配置
*/
function handleDelete<T = any>(url: string, config: AxiosRequestConfig) {
function handleDelete<T>(url: string, config: AxiosRequestConfig) {
return useRequest<T>({ url, method: 'delete', axiosConfig: config });
}

View File

@ -21,3 +21,10 @@ declare namespace ApiRoute {
home: AuthRoute.RouteKey;
}
}
declare namespace ApiDemo {
interface DataWithAdapter {
dataId: string;
dataName: string;
}
}

View File

@ -21,3 +21,10 @@ declare namespace Auth {
userRole: RoleType;
}
}
declare namespace Demo {
interface DataWithAdapter {
id: string;
name: string;
}
}

View File

@ -23,6 +23,7 @@ type Adapter<T = any> = (...args: Service.RequestResult[]) => T;
*/
export function adapterOfServiceResult<T extends Adapter>(adapter: T, ...args: TypeUtil.GetFunArgs<T>) {
let result: Service.RequestResult | undefined;
const hasError = args.some(item => {
const flag = Boolean(item.error);
if (flag) {
@ -41,5 +42,5 @@ export function adapterOfServiceResult<T extends Adapter>(adapter: T, ...args: T
};
}
return result as TypeUtil.GetFunReturn<T>;
return result as Service.RequestResult<TypeUtil.GetFunReturn<T>>;
}