Last active
June 25, 2023 16:55
-
-
Save ambrizals/9b7deb0ff770e154bb2db6a93bf84183 to your computer and use it in GitHub Desktop.
Axios Helper for React Native
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
import axios from 'axios'; | |
const config = { | |
baseUrl: 'MY_URL', | |
headers: { | |
'Access-Control-Allow-Origin': '*', | |
'Content-Type': 'application/json', | |
}, | |
}; | |
const base = async param => { | |
const CancelToken = axios.CancelToken; | |
let source = CancelToken.source(); | |
setTimeout(() => { | |
source.cancel(); | |
}, 10000); | |
return await axios({ | |
method: param.method, | |
baseURL: config.baseUrl, | |
url: param.url, | |
headers: config.headers, | |
cancelToken: source.token, | |
data: param.data, | |
}) | |
.then(res => { | |
return Promise.resolve(res); | |
}) | |
.catch(err => { | |
if (err.response) { | |
return Promise.reject(err.response); | |
} else { | |
return Promise.reject('TIMEOUT'); | |
} | |
}); | |
}; | |
const request = async (method, url) => { | |
return await base({method, url}) | |
.then(res => Promise.resolve(res)) | |
.catch(err => Promise.reject(err)); | |
}; | |
const requestData = async (method, url, data) => { | |
return await base({method, url, data}) | |
.then(res => Promise.resolve(res)) | |
.catch(err => Promise.reject(err)); | |
}; | |
export default { | |
request, | |
requestData, | |
}; |
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
import axios from './Axios' | |
function request() { | |
return axios.request('POST', 'path/point').then(res => res).catch(err => err) | |
} | |
function requestData(data) { | |
return axios.requestData('POST', 'path/point', data).then(res => res).catch(err => err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use this in useEffect hook?