Created
July 26, 2021 11:53
-
-
Save okedialf/a3c342cb9a40ba265d24357a67dabab0 to your computer and use it in GitHub Desktop.
Singleton Api Axios
This file contains hidden or 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 {AsyncStorage} from 'react-native'; | |
// import Echo from 'laravel-echo'; | |
import io from 'socket.io-client'; | |
import AsyncStorage from '@react-native-community/async-storage'; | |
import RNRestart from 'react-native-restart'; | |
import UserManager from '../user/UserManager'; | |
// import _ from 'lodash'; | |
const Config = require('../../../env.json'); | |
const axios = require('axios'); | |
export default class ApiManager { | |
static myInstance = null; | |
_userID = ''; | |
token = null; | |
msisdn = null; | |
auth_token = null; | |
socket = null; | |
client_id = null; | |
static getInstance() { | |
if (ApiManager.myInstance == null) { | |
ApiManager.myInstance = new ApiManager(); | |
ApiManager.myInstance.initAxios(); | |
// ApiManager.myInstance.initEcho(); | |
} | |
return this.myInstance; | |
} | |
getUserID() { | |
return this._userID; | |
} | |
setUserID(id) { | |
this._userID = id; | |
} | |
setHeader(token, msisdn) { | |
this.token = token; | |
this.msisdn = msisdn; | |
} | |
getMyStringValue = async () => { | |
try { | |
return await AsyncStorage.getItem('@tayari'); | |
} catch (e) { | |
// read error | |
} | |
// console.log('Done. =========='); | |
}; | |
clearToken = () => { | |
try { | |
AsyncStorage.removeItem('@tayari'); | |
return true; | |
} catch (e) { | |
return false; | |
} | |
}; | |
initAxios() { | |
this.getMyStringValue().then((res) => { | |
this.auth_token = res; | |
this.initEcho(); | |
}); | |
axios.interceptors.request.use((req) => { | |
// if (this.token) req.headers.authorization = this.token; | |
if (this.token) req.headers['Auth-Token'] = this.token; | |
if (this.msisdn) req.headers['MSISDN'] = this.msisdn; | |
if (this.auth_token) { | |
req.headers.authorization = 'Bearer ' + this.auth_token; | |
} | |
return req; | |
}); | |
axios.interceptors.response.use( | |
(res) => { | |
return res; | |
}, | |
(err) => { | |
if (err.response.status === 401) { | |
console.log(' restart ........................... '); | |
this.clearToken(); | |
RNRestart.Restart(); | |
} | |
//throw err; | |
return Promise.reject(err); | |
}, | |
); | |
} | |
initEcho() { | |
// let auth_token = this.auth_token; | |
console.log(this.auth_token); | |
this.socket = io(Config.ioHost, { | |
transports: ['websocket'], | |
auth: { | |
token: this.auth_token, | |
}, | |
}); | |
this.socket.on('connect', () => { | |
console.log('connect -------'); | |
console.log(this.socket.id); // x8WIv7-mJelg7on_ALbx | |
}); | |
this.socket.on('disconnect', () => { | |
console.log('disconnect ' + this.socket.id); // undefined | |
}); | |
this.socket.on('connect_error', (err) => { | |
console.log(err.message); // prints the message associated with the error | |
}); | |
this.socket.on('reconnect_attempt', () => { | |
this.socket.io.opts.transports = ['polling', 'websocket']; | |
}); | |
// this.socket.subscribe("order.create", { | |
// Authorization: "d41d8cd98f00b204e9800998ecf8427e" | |
// }); | |
this.socket.on('authed', (arg) => { | |
// console.log(arg.details[0].user_id); | |
try { | |
this.client_id = arg.details[0].user_id; | |
} catch (e) {} | |
console.log('authed ' + this.client_id); | |
this.socket.emit('subscribe', 'room1'); | |
// console.log(this.socket); | |
}); | |
this.socket.on('data', () => { | |
console.log('data ---------------------'); | |
}); | |
const listener = (eventName, ...args) => { | |
console.log('any listener ---------------------'); | |
console.log(eventName, args); | |
}; | |
this.socket.on('topUp', (topUp) => { | |
console.log('topUp'); | |
console.log(topUp); | |
// console.log(d); | |
UserManager.getInstance().topUp = topUp; | |
UserManager.getInstance().accountInfo(); | |
}); | |
this.socket.onAny(listener); | |
} | |
setAuthToken(cookiePhone) { | |
this.auth_token = cookiePhone; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment