Files
aranroig.com/frontend/app/composables/api.js
BinarySandia04 45404157b3
Some checks failed
Build and Deploy Nuxt / build (push) Failing after 2m1s
Backend integrated?
2026-03-17 19:48:05 +01:00

27 lines
733 B
JavaScript

// composables/useApi.js
import { ref } from 'vue'
export default function useApi() {
const config = useRuntimeConfig()
const baseUrl = config.public.apiBaseUrl
// Generic GET request
const get = async (endpoint) => {
const res = await fetch(`${baseUrl}${endpoint}`)
if (!res.ok) throw new Error(`API GET ${endpoint} failed`)
return res.json()
}
// Generic POST request
const post = async (endpoint, payload) => {
const res = await fetch(`${baseUrl}${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
if (!res.ok) throw new Error(`API POST ${endpoint} failed`)
return res.json()
}
return { get, post }
}