import axios from 'axios' ; import get from 'lodash.get' ; param headerToStateMapping export function createAxiosPlugin ( headerToStateMapping ) { if ( ! headerToStateMapping ) { throw new Error ( 'Mapping of Axios headers to Vuex state path is required' ) ; } const axiosPlugin = ( store ) => { store . subscribe ( ( _mutation , state ) => { Object . keys ( headerToStateMapping ) . forEach ( ( header ) => { let value ; const mapping = headerToStateMapping [ header ] ; if ( typeof mapping === 'string' ) { value = get ( state , mapping ) ; } else { value = mapping . computed ( get ( state , mapping . path ) ) ; } if ( value ) { axios . defaults . headers . common [ header ] = value ; } else { delete axios . defaults . headers . common [ header ] ; } } ) ; } ) ; } ; return axiosPlugin ; }