From 62800c97766f5c8163883e7c28c72c91a2003d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Krop=C3=A1=C4=8Dek?= Date: Tue, 11 Feb 2025 20:05:09 +0100 Subject: [PATCH] added useAPI for backend communication --- services/frontend/composables/useAPI.ts | 19 +++++++++++++++++++ services/frontend/nuxt.config.ts | 5 +++++ services/frontend/pages/index.vue | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 services/frontend/composables/useAPI.ts diff --git a/services/frontend/composables/useAPI.ts b/services/frontend/composables/useAPI.ts new file mode 100644 index 0000000..aace9a4 --- /dev/null +++ b/services/frontend/composables/useAPI.ts @@ -0,0 +1,19 @@ +import type { UseFetchOptions } from "nuxt/app"; + +export function useAPI( + endpoint: string, + options?: UseFetchOptions, +) { + if (!endpoint.startsWith("/")) { + endpoint = `/${endpoint}` + } + + const cfg = useRuntimeConfig() + + const fullUrl = `${cfg.public.backendUrl}${endpoint}` + + return useFetch( + fullUrl, + {...options} + ) +} \ No newline at end of file diff --git a/services/frontend/nuxt.config.ts b/services/frontend/nuxt.config.ts index 9ea4264..39c2bde 100644 --- a/services/frontend/nuxt.config.ts +++ b/services/frontend/nuxt.config.ts @@ -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) => { diff --git a/services/frontend/pages/index.vue b/services/frontend/pages/index.vue index 522c197..c7f6aa0 100644 --- a/services/frontend/pages/index.vue +++ b/services/frontend/pages/index.vue @@ -1,3 +1,3 @@ \ No newline at end of file + Index file +