fix: request type (#151)

This commit is contained in:
ClarkXia 2020-03-24 15:34:07 +08:00 committed by GitHub
parent 50981ba8fb
commit 719b22dcb5
2 changed files with 12 additions and 10 deletions

View File

@ -7,7 +7,8 @@ const Home = () => {
useEffect(() => {
fetchRepo()
request('/user').then(res => console.log('get:', res))
// 2. requse.get alias
request.get('/user').then(res => console.log('get:', res))

View File

@ -3,20 +3,21 @@ import * as utils from 'axios/lib/utils'
import axiosInstance from './axiosInstance'
export interface IRequestProps {
get?: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
delete?: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
head?: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
options?: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
post?: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
put?: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
patch?: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
get: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
delete: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
head: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
options: (url: string, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
post: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
put: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
patch: (url: string, data?: any, config?: AxiosRequestConfig) => Promise<AxiosResponse<any>>;
}
interface IRequest extends IRequestProps {
(options: AxiosRequestConfig): any;
(url: string, config?: AxiosRequestConfig): any;
}
const request: IRequest = async function (options: AxiosRequestConfig) {
const request = async function (options) {
try {
const response = await axiosInstance(options)
return response.data
@ -46,4 +47,4 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
};
});
export default request
export default request as IRequest