-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
68 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,50 @@ | ||
import axios, { AxiosRequestConfig } from 'axios' | ||
import axios, { AxiosRequestConfig, ResponseType, AxiosInstance } from 'axios' | ||
import Toaster from 'helpers/toaster' | ||
import { SERVER } from 'constants/server' | ||
|
||
const myAxios = ({ | ||
url, | ||
method = 'get', | ||
params, | ||
data, | ||
...others | ||
}: AxiosRequestConfig) => { | ||
return axios({ | ||
const TIMEOUT = 40000 | ||
const MIME_TYPE: IDictionary<ResponseType> = { | ||
JSON: 'json' | ||
} | ||
|
||
const createInstance = () => { | ||
const instance = axios.create({ | ||
baseURL: SERVER, | ||
url, | ||
method, | ||
params, | ||
data, | ||
withCredentials: true, | ||
...others | ||
}).then(result => { | ||
// console.log('axios origin result => ', result) | ||
return result.data | ||
}).catch(error => { | ||
// console.log('axios origin error => ', error) | ||
throw error | ||
timeout: TIMEOUT, | ||
responseType: MIME_TYPE.JSON | ||
}) | ||
|
||
instance.interceptors.response.use(handleResponse, handleError) | ||
|
||
return instance | ||
} | ||
|
||
const handleResponse = (response: any) => { | ||
return response.data | ||
} | ||
|
||
const handleError = (error: any) => { | ||
const { response, message } = error | ||
return Promise.reject(response ? new Error(response.data.message || message) : error) | ||
} | ||
|
||
const toastError = (error: any) => { | ||
const { response, message } = error | ||
|
||
Toaster.show({ | ||
message: response?.data?.message || message | ||
}) | ||
|
||
return Promise.reject(error) | ||
} | ||
|
||
interface Instance extends AxiosInstance { | ||
(config: AxiosRequestConfig): Promise<any> | ||
} | ||
export const requestWithoutErrorToast: Instance = createInstance() | ||
|
||
const request: Instance = createInstance() | ||
request.interceptors.response.use(undefined, toastError) | ||
|
||
export default myAxios | ||
export default request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Toaster, Position } from '@blueprintjs/core' | ||
|
||
const AppToaster = Toaster.create({ | ||
className: 'mo-toaster', | ||
position: Position.TOP_RIGHT | ||
}) | ||
|
||
export default AppToaster |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters