Added base project
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import { inject, ref } from 'vue'
|
||||
|
||||
export const useAPIStore = defineStore('api', () => {
|
||||
const API_BASE_URL = inject('apiBaseURL')
|
||||
|
||||
const token = ref()
|
||||
const gameQueryParameters = ref({
|
||||
page: 1,
|
||||
filters: {
|
||||
type: '',
|
||||
status: '',
|
||||
sort_by: 'began_at',
|
||||
sort_direction: 'desc',
|
||||
},
|
||||
})
|
||||
|
||||
// AUTH
|
||||
const postLogin = async (credentials) => {
|
||||
const response = await axios.post(`${API_BASE_URL}/login`, credentials)
|
||||
token.value = response.data.token
|
||||
axios.defaults.headers.common['Authorization'] = `Bearer ${token.value}`
|
||||
}
|
||||
const postLogout = async () => {
|
||||
await axios.post(`${API_BASE_URL}/logout`)
|
||||
token.value = undefined
|
||||
delete axios.defaults.headers.common['Authorization']
|
||||
}
|
||||
|
||||
// Users
|
||||
const getAuthUser = () => {
|
||||
return axios.get(`${API_BASE_URL}/users/me`)
|
||||
}
|
||||
|
||||
//Games
|
||||
const getGames = (resetPagination = false) => {
|
||||
if (resetPagination) {
|
||||
gameQueryParameters.value.page = 1
|
||||
}
|
||||
|
||||
const queryParams = new URLSearchParams({
|
||||
page: gameQueryParameters.value.page,
|
||||
...(gameQueryParameters.value.filters.type && {
|
||||
type: gameQueryParameters.value.filters.type,
|
||||
}),
|
||||
...(gameQueryParameters.value.filters.status && {
|
||||
status: gameQueryParameters.value.filters.status,
|
||||
}),
|
||||
sort_by: gameQueryParameters.value.filters.sort_by,
|
||||
sort_direction: gameQueryParameters.value.filters.sort_direction,
|
||||
}).toString()
|
||||
return axios.get(`${API_BASE_URL}/games?${queryParams}`)
|
||||
}
|
||||
|
||||
return {
|
||||
postLogin,
|
||||
postLogout,
|
||||
getAuthUser,
|
||||
getGames,
|
||||
gameQueryParameters,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user