react-native-code-sync
Version:
React-native plugin for the code-sync functionality on live via OTA (Over-the-air)
67 lines (58 loc) • 2 kB
JavaScript
import axios from 'axios';
import { Utility } from '../utils'
// import OtaApiConstant from './OtaApiConstant.json';
import { getCodeSyncConfig } from '../config';
const config = getCodeSyncConfig();
const BASE_URL = config.baseUrl
const axiosClient = axios.create({
baseURL: BASE_URL,
timeout: 30000
});
export const OTAApi = async (
config = { method: 'GET', data: {}, url: '' },
customHeader = {},
multipart = false
) => {
Utility.log("ota configconfig==>", BASE_URL, JSON.stringify(config))
const onSuccess = response => {
Utility.log('ota Request Successful! oxygen', JSON.stringify(response.data));
if (response.data && response.data.status && response.data.status === 200) {
return Promise.resolve(response.data);
} else {
return Promise.reject(response.data);
}
};
// Error
const onError = async error => {
Utility.log('ota Request Failed: oxygen CommonManager', error.response);
if (error && error.response) {
return Promise.reject(error.response.data);
} else {
// Something else happened while setting up the request
// triggered the error
Utility.log('ota Error Message:', error.message);
const data = { message: 'Something went wrong, please try later' }
if (error.message) {
data.message = error.message
}
return Promise.reject(data);
}
};
// Append headers
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
if (multipart) {
headers['Content-Type'] = "multipart/form-data"
}
const completeheader = {
...headers,
...customHeader
}
Utility.log("ota headers-->", JSON.stringify(completeheader))
axiosClient.defaults.headers = completeheader;
return axiosClient(config)
.then(onSuccess)
.catch(onError);
}