vue2_frontend/src/store/modules/todo.js
2022-09-10 19:50:25 +09:00

68 lines
1.7 KiB
JavaScript

import axios from 'axios'
import jwt from './jwt'
//참조: https://yamoo9.github.io/axios/guide/interceptors.html
const apiServer = axios.create({
baseURL: process.env.VUE_APP_BACKEND_HOST,
headers: { 'content-type': 'application/json' }
})
apiServer.interceptors.request.use(
(config) => jwt.getAuthorizationHeader(config),
(error) => {
// Do something with request error
Promise.reject(error)
}
)
apiServer.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded'
// count state 속성 추가
const state = {
total: 0,
rows: []
}
const getters = {
getTotal: function (state) {
return state.total
},
getRows: function (state) {
return state.rows
}
}
const mutations = {
setData: function (state, data) {
state.total = data.total
state.rows = data.rows
}
}
const actions = {
sendAccessToken: async function (context, accessToken) {
//주의 .get의 parameter는 { params: params} 처럼 보내야함
return await apiServer
.get('/todo/accessToken', { params: accessToken })
.then((response) => {
const { data } = response
context.commit('setData', data)
return response
})
.catch((error) => {
return error
})
},
setData: async function (context, params) {
console.log(params)
//주의 .get의 parameter는 { params: params} 처럼 보내야함
return await apiServer
.get('/todo', { params: params })
.then((response) => {
const { data } = response
context.commit('setData', data)
return response
})
.catch((error) => {
return error
})
}
}
export default { namespaced: true, state, getters, mutations, actions }