Skip to content

Instantly share code, notes, and snippets.

@marwinious
Created December 4, 2019 16:12
Show Gist options
  • Select an option

  • Save marwinious/143ef8f5dfa964b07445f83ca858fc81 to your computer and use it in GitHub Desktop.

Select an option

Save marwinious/143ef8f5dfa964b07445f83ca858fc81 to your computer and use it in GitHub Desktop.
Vue.js: HTTP store pattern module w/axios
import Vue from 'vue'
import axios from 'axios'
export default {
state: {
paths: {
dev: 'http://localhost/bitbucket/',
prod: ''
},
endpoints: {
someEndpoint: 'endpoints/some_endpoint.php'
}
},
fetchEndpointData: function(endpoint, options) {
const vm = this;
let env = vm.detectEnv();
let endpointURL = vm.state.paths[env] + vm.state.endpoints[endpoint];
// CHECK FOR OPTIONS
if(options) {
let queryString = Object.keys(options).map(key => key + '=' + options[key]).join('&');
endpointURL += '?' + queryString;
}
return axios.get(endpointURL);
},
detectEnv: function() {
let env = 'dev';
switch(document.domain) {
case 'localhost':
env = 'dev';
break;
default:
env = 'prod';
break;
}
return env;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment