Skip to content

Commit

Permalink
Merge pull request #5 from laoer536/master
Browse files Browse the repository at this point in the history
希望给axios增加返回类型定义,便于页面调取接口获取类型便利。
  • Loading branch information
nekobc1998923 authored Apr 19, 2022
2 parents fa10250 + abd7817 commit 65aedad
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/api/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import showCodeMessage from '@/api/code';
import { formatJsonToUrlParams, instanceObject } from '@/utils/format';

Expand Down Expand Up @@ -32,7 +32,7 @@ axiosInstance.interceptors.request.use(
axiosInstance.interceptors.response.use(
(response: AxiosResponse) => {
if (response.status === 200) {
return response;
return response.data;
}
ElMessage.info(JSON.stringify(response.status));
return response;
Expand All @@ -48,17 +48,28 @@ axiosInstance.interceptors.response.use(
},
);
const service = {
get: (url: string, data?: object) => axiosInstance.get(url, { params: data }),
post: (url: string, data?: object) => axiosInstance.post(url, data),
put: (url: string, data?: object) => axiosInstance.put(url, data),
delete: (url: string, data?: object) => axiosInstance.delete(url, data),
upload: (url: string, file: File) =>
get<T = any>(url: string, data?: object): Promise<T> {
return axiosInstance.get(url, { params: data });
},

post<T = any>(url: string, data?: object): Promise<T> {
return axiosInstance.post(url, data);
},

put<T = any>(url: string, data?: object): Promise<T> {
return axiosInstance.put(url, data);
},

delete<T = any>(url: string, data?: object): Promise<T> {
return axiosInstance.delete(url, data);
},

upload: (url: string, file: FormData | File) =>
axiosInstance.post(url, file, {
headers: { 'Content-Type': 'multipart/form-data' },
}),
download: (url: string, data: instanceObject) => {
const downloadUrl = `${BASE_PREFIX}/${url}?${formatJsonToUrlParams(data)}`;
window.location.href = downloadUrl;
window.location.href = `${BASE_PREFIX}/${url}?${formatJsonToUrlParams(data)}`;
},
};

Expand Down

0 comments on commit 65aedad

Please sign in to comment.