Skip to content

Instantly share code, notes, and snippets.

@Ramarti
Created July 23, 2019 13:43
Show Gist options
  • Save Ramarti/b6668a2592c1acefa7aee793f058c030 to your computer and use it in GitHub Desktop.
Save Ramarti/b6668a2592c1acefa7aee793f058c030 to your computer and use it in GitHub Desktop.
import axios from 'axios'
import VueAxios from 'vue-axios'
import Vue from 'vue'
Vue.use(VueAxios, axios)
Vue.axios.defaults.baseURL = process.env.VUE_APP_BASE_URL
Vue.axios.defaults.headers.common['Content-Type'] = 'application/json'
Vue.axios.defaults.headers.post['Content-Type'] = 'application/json'
Vue.axios.defaults.headers.put['Content-Type'] = 'application/json'
Vue.axios.defaults.headers.patch['Content-Type'] = 'application/json'
/**
from vuex action
**/
placeOrder ({ commit }, order) {
return new Promise((resolve, reject) => {
Vue.axios.post(process.env.VUE_APP_BITY_BASE_URL + '/orders', order, {
withCredentials: true
}).then(response => {
// Will not have set cookie
console.log(response.headers)
commit('addOrderID', response.headers.location.split('/').pop())
resolve(response.data)
}).catch(error => {
reject(error)
})
})
}
/**
Alternative
**/
placeOrder ({ commit }, order) {
return new Promise((resolve, reject) => {
return fetch(process.env.VUE_APP_BITY_BASE_URL + '/orders', {
method: 'POST',
body: JSON.stringify(order),
mode: 'cors',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
if (!response.ok) {
return response.json().then(json => reject(
// eslint-disable-line prefer-promise-reject-errors
{
httpStatusCode: data.status,
...json
}))
}
const orderUrl = response.headers.get('Location') || ''
console.log(response.headers.get('Cookie'))
console.log(response.headers.get('Set-Cookie'))
console.log(response.headers.get('set-cookie'))
commit('addOrderID', orderUrl.location.split('/').pop())
resolve(response.data)
}).catch(error => {
reject(error)
})
})
}
/**
vue.config.js
Dev server config
**/
module.exports = {
configureWebpack: {
devtool: 'source-map'
},
/* ... */
devServer: {
// TODO update to "webpack-dev-server": "3.1.14" when vue-cli updates and remove disableHostCheck option
disableHostCheck: true,
inline: true,
hot: true,
historyApiFallback: true,
host: '0.0.0.0',
port: 3000,
filename: 'bundle.js',
headers: {
//'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*', // 'X-Requested-With, Accept,Content-Type, Origin, content-type, Authorization, set-cookie',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE'
},
proxy: {
'/api': {
target: 'http://backend:8000/',
secure: false,
changeOrigin: true
},
'/admin': {
target: 'http://backend:8000/',
secure: false,
changeOrigin: true
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment