export default {
    actions: {
        crudRequest({getters, dispatch},  {endpoint, method, parameters}){
            return fetch(endpoint, {
                method: method,
                responseType: 'json',
                body: parameters ? JSON.stringify(parameters):null,
                headers: {
                    'content-type': 'application/json',
                    'Authorization': 'Bearer '+getters.getAccessToken
                }
            }).then(response => {

                if(response.status === 401 && window.location.href !== route('login')){
                    dispatch('userAuthentication', {access_token: '', redirect_url: '/'});
                }

                return response;

            })
        }
    }
}