mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-10-16 06:33:43 +08:00
refactor(projects): 文件夹位置规范
This commit is contained in:
66
src/service/request/request.ts
Normal file
66
src/service/request/request.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import type { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios';
|
||||
import type { RequestServiceError, CustomSuccessRequestResult, CustomFailRequestResult } from '@/interface';
|
||||
import CustomAxiosInstance from './instance';
|
||||
|
||||
/**
|
||||
* 封装各个请求方法及结果处理的类
|
||||
* @author Soybean<honghuangdc@gmail.com>
|
||||
*/
|
||||
export class Request {
|
||||
instance: AxiosInstance;
|
||||
|
||||
constructor(instance: AxiosInstance) {
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
static successHandler<ResponseData>(response: AxiosResponse) {
|
||||
const successResult: CustomSuccessRequestResult<ResponseData> = {
|
||||
data: response as unknown as ResponseData,
|
||||
error: null
|
||||
};
|
||||
|
||||
return successResult;
|
||||
}
|
||||
|
||||
static failHandler(error: RequestServiceError) {
|
||||
const failResult: CustomFailRequestResult = {
|
||||
data: null,
|
||||
error
|
||||
};
|
||||
|
||||
return failResult;
|
||||
}
|
||||
|
||||
get<ResponseData>(url: string, config?: AxiosRequestConfig) {
|
||||
return this.instance
|
||||
.get(url, config)
|
||||
.then(res => Request.successHandler<ResponseData>(res))
|
||||
.catch(Request.failHandler);
|
||||
}
|
||||
|
||||
post<ResponseData>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return this.instance
|
||||
.post(url, data, config)
|
||||
.then(res => Request.successHandler<ResponseData>(res))
|
||||
.catch(Request.failHandler);
|
||||
}
|
||||
|
||||
put<ResponseData>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return this.instance
|
||||
.put(url, data, config)
|
||||
.then(res => Request.successHandler<ResponseData>(res))
|
||||
.catch(Request.failHandler);
|
||||
}
|
||||
|
||||
delete<ResponseData>(url: string, config?: AxiosRequestConfig) {
|
||||
return this.instance
|
||||
.delete(url, config)
|
||||
.then(res => Request.successHandler<ResponseData>(res))
|
||||
.catch(Request.failHandler);
|
||||
}
|
||||
}
|
||||
|
||||
export function createRequest(axiosConfig: AxiosRequestConfig) {
|
||||
const customInstance = new CustomAxiosInstance(axiosConfig);
|
||||
return new Request(customInstance.instance);
|
||||
}
|
Reference in New Issue
Block a user