christianmalek/vuex-rest-api

Global axios settings

ghenry opened this issue · 3 comments

Hi,
I've been following #34 and see there is https://github.com/christianmalek/vuex-rest-api/blob/v2/CHANGELOG.md#260-06022018 now, but could you add an example?

For instance, I want to do this globally (withCredentials: true), rather than per request:

  .get({
    action: "whoAmI",
    property: "whoami",
    path: "/whoami",
    requestConfig: {
      withCredentials: true,
    }
  })

Hi ghenry,

here is an (untested) example: You could append the axios instance on the vm and access its requestConfig if required.

import Vuex from "vuex"
import Vue from "vue"
import Vapi from "vuex-rest-api"
import Axios from "axios"

// create axios instance (maybe unnecessary)
const axios = Axios.create();

// create Vapi store
const posts = new Vapi({
  axios,
  baseURL: "https://jsonplaceholder.typicode.com",
    state: {
      posts: []
    }
  })
  // Step 3a
  .get({
    action: "getPost",
    property: "post",
    path: ({ id }) => `/posts/${id}`
  })
  .getStore()

const store = new Vuex.Store(posts)

Vue.use(Vuex)

new Vue(
  axios,
  store
  // other options
)

Then you could access this.axios in your components and get/set its properties.

I'll add an proper example to the docs to clarify this.