Files
aranroig.com/frontend/app/composables/useApi.ts
BinarySandia04 9b7751c571
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 42s
Fix?
2026-06-12 00:12:30 +02:00

31 lines
837 B
TypeScript

// composables/useApi.js
import { ref } from 'vue'
function normalizeUrl(url: string): string {
return url.replace(/\/+$/, '')
}
export default function useApi() {
const config = useRuntimeConfig()
const baseUrl = `${normalizeUrl(config.public.apiBaseUrl)}/api`
// 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 }
}