Skip to content

Instantly share code, notes, and snippets.

@csorgod
Created September 9, 2022 19:02
Show Gist options
  • Save csorgod/17bee94521f5724db1dd975dc6d1fc0b to your computer and use it in GitHub Desktop.
Save csorgod/17bee94521f5724db1dd975dc6d1fc0b to your computer and use it in GitHub Desktop.
Baseclient with axios
import axios from 'axios';
import Raven from 'raven-js';
const getClient = (baseUrl = null) => {
const opt = {
baseURL: baseUrl,
crossdomain: true
};
opt.headers = {
Authorization: `JWT ${process.env.MINORITY_API_TOKEN}`,
"Access-Control-Allow-Origin": "*"
};
const client = axios.create(opt);
client.interceptors.response.use(
_handleResponse,
_handleError,
);
const _handleResponse = ({ data }) => Promise.resolve(response);
const _handleError = (error) => Promise.reject(error);
return client;
};
class ApiClient {
constructor() {
var baseUrl = process.env.MINORITY_API ?? "https://url.com.br";
this.client = getClient(baseUrl);
}
get(url, conf = {}) {
return this.client.get(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
delete(url, conf = {}) {
return this.client.delete(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
head(url, conf = {}) {
return this.client.head(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
options(url, conf = {}) {
return this.client.options(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
post(url, data = {}, conf = {}) {
return this.client.post(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
put(url, data = {}, conf = {}) {
return this.client.put(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
patch(url, data = {}, conf = {}) {
return this.client.patch(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
}
export { ApiClient };
export default {
get(url, conf = {}) {
return getClient().get(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
delete(url, conf = {}) {
return getClient().delete(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
head(url, conf = {}) {
return getClient().head(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
options(url, conf = {}) {
return getClient().options(url, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
post(url, data = {}, conf = {}) {
return getClient().post(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
put(url, data = {}, conf = {}) {
return getClient().put(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
},
patch(url, data = {}, conf = {}) {
return getClient().patch(url, data, conf)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error));
}
};
import { ApiClient } from './baseClient';
let client = new ApiClient();
const UsersClient = {
findById(id) {
return client.get(`/users/${id}`);
}
}
export { UsersClient };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment