Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Container, ApiAbort } from 'apicase'
- // I separated it for better understanding
- import services from './services'
- // Pass authorization token to every service
- const headers = () => ({
- token: localStorage.getItem('token')
- })
- // Made our API accessible only for authorized users
- const hooks = {
- before: {
- name: 'Token validation',
- handler (ctx, next) {
- return ctx.headers.token
- ? next()
- : next(new ApiAbort('You are not authorized', { type: 'missedToken' }))
- }
- },
- missedToken () {
- location.href = '/login'
- }
- }
- // Global mixins for our specific API
- const mixins = {
- success () {
- return this.ok && this.result
- ? this.result.success
- : false
- },
- data () {
- return this.success
- ? this.result.data
- : null
- },
- }
- export default new Container ({
- base: '/api/v1',
- hooks,
- mixins,
- headers,
- services
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement