Spookie/assets/js/composables/useResource.js

35 lines
738 B
JavaScript
Raw Normal View History

2022-05-23 16:25:55 +02:00
import { ref } from 'vue'
import axios from 'axios'
export default function useResource(resource) {
2022-05-25 14:41:58 +02:00
2022-05-23 16:25:55 +02:00
console.log('useBlog')
2022-05-25 14:41:58 +02:00
axios.defaults.headers.common['app-id'] = '628cbf673800f869ed218152' // for all requests
const apiBaseUrl = 'https://628cc5c6a3fd714fd03914c2.mockapi.io/api/'
2022-05-23 16:25:55 +02:00
const items = ref([])
const item = ref(null)
const fetchAll = async () => {
await axios
2022-05-25 14:41:58 +02:00
.get(apiBaseUrl + resource)
2022-05-23 16:25:55 +02:00
.then((response) => {
2022-05-25 14:41:58 +02:00
console.log(response.data)
2022-05-23 16:25:55 +02:00
items.value = response.data
})
}
const fetchOne = async (id) => {
console.log('fetchOne', id)
await axios
.get(`${resource}/${id}`)
.then((response) => {
item.value = response.data
})
}
return {
items,
fetchAll,
item,
fetchOne
}
}