added useAPI for backend communication

This commit is contained in:
Jakub Kropáček 2025-02-11 20:05:09 +01:00
parent e9403fea68
commit 62800c9776
3 changed files with 26 additions and 2 deletions

View file

@ -0,0 +1,19 @@
import type { UseFetchOptions } from "nuxt/app";
export function useAPI<T>(
endpoint: string,
options?: UseFetchOptions<T>,
) {
if (!endpoint.startsWith("/")) {
endpoint = `/${endpoint}`
}
const cfg = useRuntimeConfig()
const fullUrl = `${cfg.public.backendUrl}${endpoint}`
return useFetch(
fullUrl,
{...options}
)
}

View file

@ -8,6 +8,11 @@ export default defineNuxtConfig({
build: {
transpile: ['vuetify'],
},
runtimeConfig: {
public: {
backendUrl: process.env.BACKEND_URL ?? "http://localhost:8000"
},
},
modules: [
(_options, nuxt) => {
nuxt.hooks.hook('vite:extendConfig', (config) => {

View file

@ -1,3 +1,3 @@
<template>
Index file
</template>
Index file
</template>