Skip to content

Instantly share code, notes, and snippets.

@odeke
Created July 26, 2021 12:26
Show Gist options
  • Select an option

  • Save odeke/c5b69878dfda61edf796a8d6c896aad3 to your computer and use it in GitHub Desktop.

Select an option

Save odeke/c5b69878dfda61edf796a8d6c896aad3 to your computer and use it in GitHub Desktop.
import Config from "./Config";
import axios from 'axios';
export default class ApiManager {
static myInstance = null;
// _token = "";
/**
* @returns {ApiManager}
*/
static getInstance() {
if (ApiManager.myInstance == null) {
ApiManager.myInstance = new ApiManager();
ApiManager.myInstance.initAxios();
}
return this.myInstance;
}
initAxios(){
this.myInstance = axios.create({
baseURL: Config.server_url,
});
}
setToken(token){
this.myInstance.defaults.headers.common['Authorization'] = `Bearer ${token}`;
}
getCall(url) {
return this.myInstance.get(url)
}
postCall(url, body) {
return this.myInstance.post(url, body)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment