Yield generated for 6e53886e-4e8d-41a6-ad7e-08d823883c45
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.4 KiB

import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import JwtService from "@/common/jwt.service";
import { API_URL } from "@/common/config";
const ApiService = {
init() {
Vue.use(VueAxios, axios);
Vue.axios.defaults.baseURL = API_URL;
},
setHeader() {
Vue.axios.defaults.headers.common[
"Authorization"
] = `Token ${JwtService.getToken()}`;
},
query(resource, params) {
return Vue.axios.get(resource, params).catch(error => {
throw new Error(`[RWV] ApiService ${error}`);
});
},
get(resource, slug = "") {
return Vue.axios.get(`${resource}/${slug}`).catch(error => {
throw new Error(`[RWV] ApiService ${error}`);
});
},
post(resource, params) {
return Vue.axios.post(`${resource}`, params);
},
update(resource, slug, params) {
return Vue.axios.put(`${resource}/${slug}`, params);
},
put(resource, params) {
return Vue.axios.put(`${resource}`, params);
},
delete(resource) {
return Vue.axios.delete(resource).catch(error => {
throw new Error(`[RWV] ApiService ${error}`);
});
}
};
export default ApiService;
export const TagsService = {
get() {
return ApiService.get("tags");
}
};
export const ArticlesService = {
query(type, params) {
return ApiService.query("articles" + (type === "feed" ? "/feed" : ""), {
params: params
});
},
get(slug) {
return ApiService.get("articles", slug);
},
create(params) {
delete params.author
return ApiService.post("articles", { article: params });
},
update(slug, params) {
delete params.author
return ApiService.update("articles", slug, { article: params });
},
destroy(slug) {
return ApiService.delete(`articles/${slug}`);
}
};
export const CommentsService = {
get(slug) {
if (typeof slug !== "string") {
throw new Error(
"[RWV] CommentsService.get() article slug required to fetch comments"
);
}
return ApiService.get("articles", `${slug}/comments`);
},
post(slug, payload) {
return ApiService.post(`articles/${slug}/comments`, {
comment: { body: payload }
});
},
destroy(slug, commentId) {
return ApiService.delete(`articles/${slug}/comments/${commentId}`);
}
};
export const FavoriteService = {
add(slug) {
return ApiService.post(`articles/${slug}/favorite`);
},
remove(slug) {
return ApiService.delete(`articles/${slug}/favorite`);
}
};