Advertisement
Guest User

services/blogs.js

a guest
Nov 12th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from "axios"
  2. const baseUrl = "/api/blogs"
  3.  
  4. let token = null
  5.  
  6. const getConfig = () => ({
  7.   headers: { Authorization: token }
  8. })
  9.  
  10. const setToken = newToken => {
  11.   token = `bearer ${newToken}`
  12. }
  13.  
  14. const destroyToken = () => {
  15.   token = null
  16. }
  17.  
  18. const getAll = () => {
  19.   const request = axios.get(baseUrl)
  20.   return request.then(response => response.data)
  21. }
  22.  
  23. const create = async blog => {
  24.   const response = await axios.post(baseUrl, blog, getConfig())
  25.   return response.data
  26. }
  27.  
  28. const update = async (id, newObject) => {
  29.   const config = {
  30.     headers: { Authorization: token }
  31.   }
  32.  
  33.   const response = await axios.put(`${baseUrl}/${id}`, newObject, config)
  34.   return response.data
  35. }
  36.  
  37. const remove = async id => {
  38.   const config = {
  39.     headers: { Authorization: token }
  40.   }
  41.  
  42.   const response = await axios.delete(`${baseUrl}/${id}`, config)
  43.   return response.data
  44. }
  45.  
  46. const addComment = async (id, comment) => {
  47.   const config = {
  48.     headers: { Authorization: token }
  49.   }
  50.   const response = await axios.post(
  51.     `${baseUrl}/${id}/comments`,
  52.     comment,
  53.     config
  54.   )
  55.   return response.data
  56. }
  57.  
  58. export default {
  59.   getAll,
  60.   create,
  61.   update,
  62.   remove,
  63.   setToken,
  64.   destroyToken,
  65.   addComment
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement