import axios from "axios";
import Cookies from "js-cookie";
export abstract class APIService {
protected baseURL: string;
protected headers: any = {};
constructor(baseURL: string) {
this.baseURL = baseURL;
}
setRefreshToken(token: string) {
Cookies.set("refreshToken", token, { expires: 30 });
getRefreshToken() {
return Cookies.get("refreshToken");
purgeRefreshToken() {
Cookies.remove("refreshToken", { path: "/" });
setAccessToken(token: string) {
Cookies.set("accessToken", token, { expires: 30 });
getAccessToken() {
return Cookies.get("accessToken");
purgeAccessToken() {
Cookies.remove("accessToken", { path: "/" });
getHeaders() {
return {
Authorization: `Bearer ${this.getAccessToken()}`,
};
get(url: string, config = {}): Promise<any> {
return axios({
method: "get",
url: this.baseURL + url,
headers: this.getAccessToken() ? this.getHeaders() : {},
...config,
});
post(url: string, data = {}, config = {}): Promise<any> {
method: "post",
data,
put(url: string, data = {}, config = {}): Promise<any> {
method: "put",
patch(url: string, data = {}, config = {}): Promise<any> {
method: "patch",
delete(url: string, data?: any, config = {}): Promise<any> {
method: "delete",
data: data,
request(config = {}) {
return axios(config);