Skip to content

Instantly share code, notes, and snippets.

@lobo-tuerto
Created May 27, 2018 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobo-tuerto/ab8c073754d8d39a93a5d587f58d9504 to your computer and use it in GitHub Desktop.
Save lobo-tuerto/ab8c073754d8d39a93a5d587f58d9504 to your computer and use it in GitHub Desktop.
Base service with Axios
import { userService } from '@/services'
# ...
created () {
this.isLoading = true
this.id = this.$route.params.id
userService
.one(this.id)
.then(({ data }) => {
this.isLoading = false
this.user = data.attributes
})
.catch(res => {
this.isLoading = false
throw res
})
},
import axios from 'axios'
import Vue from 'vue'
const baseService = axios.create({
baseURL: 'http://localhost:4000/api/',
headers: {
'Accept': 'application/json',
'Accept-Language': 'es',
'Content-Type': 'application/json'
},
withCredentials: true
})
baseService.interceptors.response.use(
function (response) {
// Return unwrapped response ---the "body" of it
return response.data
},
function (error) {
// TODO: Do proper error handling here (logout user, etc)
console.error(error)
}
)
export default baseService
import sessionService from './session-service'
import userService from './user-service'
export {
sessionService,
userService
}
import baseService from './base-service'
export default {
create (email, password) {
return baseService
.post('users/sign_in', { email, password })
},
delete () {
return baseService
.post('users/sign_out')
},
me () {
return baseService
.get('users/me')
}
}
import baseService from './base-service'
export default {
all () {
return baseService
.get('users')
},
one (id) {
return baseService
.get(`users/${id}`)
},
create (data) {
return baseService
.post('users', {user: data})
},
update (id, data) {
return baseService
.put(`users/${id}`, {user: data})
},
delete (id) {
return baseService
.delete(`users/${id}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment